Commit eeacfdc6 authored by Eric Biggers's avatar Eric Biggers

fscrypt: clean up some BUG_ON()s in block encryption/decryption

Replace some BUG_ON()s with WARN_ON_ONCE() and returning an error code,
and move the check for len divisible by FS_CRYPTO_BLOCK_SIZE into
fscrypt_crypt_block() so that it's done for both encryption and
decryption, not just encryption.
Reviewed-by: default avatarChandan Rajendra <chandan@linux.ibm.com>
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
parent f47fcbb2
...@@ -162,7 +162,10 @@ int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw, ...@@ -162,7 +162,10 @@ int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
struct crypto_skcipher *tfm = ci->ci_ctfm; struct crypto_skcipher *tfm = ci->ci_ctfm;
int res = 0; int res = 0;
BUG_ON(len == 0); if (WARN_ON_ONCE(len <= 0))
return -EINVAL;
if (WARN_ON_ONCE(len % FS_CRYPTO_BLOCK_SIZE != 0))
return -EINVAL;
fscrypt_generate_iv(&iv, lblk_num, ci); fscrypt_generate_iv(&iv, lblk_num, ci);
...@@ -225,8 +228,6 @@ struct page *fscrypt_encrypt_page(const struct inode *inode, ...@@ -225,8 +228,6 @@ struct page *fscrypt_encrypt_page(const struct inode *inode,
struct page *ciphertext_page = page; struct page *ciphertext_page = page;
int err; int err;
BUG_ON(len % FS_CRYPTO_BLOCK_SIZE != 0);
if (inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES) { if (inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES) {
/* with inplace-encryption we just encrypt the page */ /* with inplace-encryption we just encrypt the page */
err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page, err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page,
...@@ -238,7 +239,8 @@ struct page *fscrypt_encrypt_page(const struct inode *inode, ...@@ -238,7 +239,8 @@ struct page *fscrypt_encrypt_page(const struct inode *inode,
return ciphertext_page; return ciphertext_page;
} }
BUG_ON(!PageLocked(page)); if (WARN_ON_ONCE(!PageLocked(page)))
return ERR_PTR(-EINVAL);
/* The encryption operation will require a bounce page. */ /* The encryption operation will require a bounce page. */
ciphertext_page = fscrypt_alloc_bounce_page(gfp_flags); ciphertext_page = fscrypt_alloc_bounce_page(gfp_flags);
...@@ -275,8 +277,9 @@ EXPORT_SYMBOL(fscrypt_encrypt_page); ...@@ -275,8 +277,9 @@ EXPORT_SYMBOL(fscrypt_encrypt_page);
int fscrypt_decrypt_page(const struct inode *inode, struct page *page, int fscrypt_decrypt_page(const struct inode *inode, struct page *page,
unsigned int len, unsigned int offs, u64 lblk_num) unsigned int len, unsigned int offs, u64 lblk_num)
{ {
if (!(inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES)) if (WARN_ON_ONCE(!PageLocked(page) &&
BUG_ON(!PageLocked(page)); !(inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES)))
return -EINVAL;
return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page, return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page,
len, offs, GFP_NOFS); len, offs, GFP_NOFS);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment