fscrypt: fix up fscrypt_fname_encrypted_size() for internal use
authorEric Biggers <ebiggers@google.com>
Fri, 12 Jan 2018 04:30:08 +0000 (23:30 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 12 Jan 2018 04:30:08 +0000 (23:30 -0500)
Filesystems don't need fscrypt_fname_encrypted_size() anymore, so
unexport it and move it to fscrypt_private.h.

We also never calculate the encrypted size of a filename without having
the fscrypt_info present since it is needed to know the amount of
NUL-padding which is determined by the encryption policy, and also we
will always truncate the NUL-padding to the maximum filename length.
Therefore, also make fscrypt_fname_encrypted_size() assume that the
fscrypt_info is present, and make it truncate the returned length to the
specified max_len.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/crypto/fname.c
fs/crypto/fscrypt_private.h
fs/crypto/hooks.c
include/linux/fscrypt_notsupp.h
include/linux/fscrypt_supp.h

index 44ddd094b7c56784b2a18045956da6408bdb9292..e33f3d3c5ade8ce6e647e6b5021f7e51f00a96c1 100644 (file)
@@ -191,17 +191,20 @@ static int digest_decode(const char *src, int len, char *dst)
        return cp - dst;
 }
 
-u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)
+bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
+                                 u32 max_len, u32 *encrypted_len_ret)
 {
-       int padding = 32;
-       struct fscrypt_info *ci = inode->i_crypt_info;
-
-       if (ci)
-               padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
-       ilen = max(ilen, (u32)FS_CRYPTO_BLOCK_SIZE);
-       return round_up(ilen, padding);
+       int padding = 4 << (inode->i_crypt_info->ci_flags &
+                           FS_POLICY_FLAGS_PAD_MASK);
+       u32 encrypted_len;
+
+       if (orig_len > max_len)
+               return false;
+       encrypted_len = max(orig_len, (u32)FS_CRYPTO_BLOCK_SIZE);
+       encrypted_len = round_up(encrypted_len, padding);
+       *encrypted_len_ret = min(encrypted_len, max_len);
+       return true;
 }
-EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
 
 /**
  * fscrypt_fname_alloc_buffer - allocate a buffer for presented filenames
@@ -342,14 +345,10 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
                return ret;
 
        if (dir->i_crypt_info) {
-               unsigned int max_len = dir->i_sb->s_cop->max_namelen(dir);
-
-               if (iname->len > max_len)
+               if (!fscrypt_fname_encrypted_size(dir, iname->len,
+                                                 dir->i_sb->s_cop->max_namelen(dir),
+                                                 &fname->crypto_buf.len))
                        return -ENAMETOOLONG;
-
-               fname->crypto_buf.len =
-                       min(fscrypt_fname_encrypted_size(dir, iname->len),
-                           max_len);
                fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
                                                 GFP_NOFS);
                if (!fname->crypto_buf.name)
index 0539175872009735224a82d40c9f787707a6f46c..ad6722bae8b7499b8128224cef436898043afc3d 100644 (file)
@@ -110,6 +110,9 @@ extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx,
 /* fname.c */
 extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
                         u8 *out, unsigned int olen);
+extern bool fscrypt_fname_encrypted_size(const struct inode *inode,
+                                        u32 orig_len, u32 max_len,
+                                        u32 *encrypted_len_ret);
 
 /* keyinfo.c */
 extern void __exit fscrypt_essiv_cleanup(void);
index 5bf38d94c5d239590224c4acc9a450c41a71f4b8..28f9f059571d27c4a7214ad5f95bc858ffe4e980 100644 (file)
@@ -143,12 +143,12 @@ int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
         * counting it (even though it is meaningless for ciphertext) is simpler
         * for now since filesystems will assume it is there and subtract it.
         */
-       if (sizeof(struct fscrypt_symlink_data) + len > max_len)
+       if (!fscrypt_fname_encrypted_size(dir, len,
+                                         max_len - sizeof(struct fscrypt_symlink_data),
+                                         &disk_link->len))
                return -ENAMETOOLONG;
-       disk_link->len = min_t(unsigned int,
-                              sizeof(struct fscrypt_symlink_data) +
-                                       fscrypt_fname_encrypted_size(dir, len),
-                              max_len);
+       disk_link->len += sizeof(struct fscrypt_symlink_data);
+
        disk_link->name = NULL;
        return 0;
 }
index c9592e307df54986f4aca68eaab921ce91c37ee4..342eb97e047677938a5e8acdd046dda50e3e44fb 100644 (file)
@@ -131,14 +131,6 @@ static inline void fscrypt_free_filename(struct fscrypt_name *fname)
        return;
 }
 
-static inline u32 fscrypt_fname_encrypted_size(const struct inode *inode,
-                                              u32 ilen)
-{
-       /* never happens */
-       WARN_ON(1);
-       return 0;
-}
-
 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
                                             u32 max_encrypted_len,
                                             struct fscrypt_str *crypto_str)
index e00191deb0d6474518430027b95095c851eda792..2dd5767c77b0c3e1f3c7cac0cc7c018466e1175a 100644 (file)
@@ -107,7 +107,6 @@ static inline void fscrypt_free_filename(struct fscrypt_name *fname)
        kfree(fname->crypto_buf.name);
 }
 
-extern u32 fscrypt_fname_encrypted_size(const struct inode *, u32);
 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
                                struct fscrypt_str *);
 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);