1 fscrypto: make filename crypto functions return 0 on success
3 From: Eric Biggers <ebiggers@google.com>
5 Several filename crypto functions: fname_decrypt(),
6 fscrypt_fname_disk_to_usr(), and fscrypt_fname_usr_to_disk(), returned
7 the output length on success or -errno on failure. However, the output
8 length was redundant with the value written to 'oname->len'. It is also
9 potentially error-prone to make callers have to check for '< 0' instead
12 Therefore, make these functions return 0 instead of a length, and make
13 the callers who cared about the return value being a length use
14 'oname->len' instead. For consistency also make other callers check for
15 a nonzero result rather than a negative result.
17 This change also fixes the inconsistency of fname_encrypt() actually
18 already returning 0 on success, not a length like the other filename
19 crypto functions and as documented in its function comment.
21 Signed-off-by: Eric Biggers <ebiggers@google.com>
22 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
23 Reviewed-by: Andreas Dilger <adilger@dilger.ca>
24 Acked-by: Jaegeuk Kim <jaegeuk@kernel.org>
26 fs/crypto/fname.c | 56 ++++++++++++++++++++++++++++++++------------------------
27 fs/ext4/dir.c | 5 +++--
28 fs/ext4/namei.c | 8 ++++----
29 fs/ext4/symlink.c | 5 ++---
30 fs/f2fs/dir.c | 6 +++---
31 fs/f2fs/namei.c | 6 +++---
32 6 files changed, 47 insertions(+), 39 deletions(-)
34 diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c
35 index 6bbc3b1..90697c7 100644
36 --- a/fs/crypto/fname.c
37 +++ b/fs/crypto/fname.c
38 @@ -35,11 +35,11 @@ static void fname_crypt_complete(struct crypto_async_request *req, int res)
43 + * fname_encrypt() - encrypt a filename
45 - * This function encrypts the input filename, and returns the length of the
46 - * ciphertext. Errors are returned as negative numbers. We trust the caller to
47 - * allocate sufficient memory to oname string.
48 + * The caller must have allocated sufficient memory for the @oname string.
50 + * Return: 0 on success, -errno on failure
52 static int fname_encrypt(struct inode *inode,
53 const struct qstr *iname, struct fscrypt_str *oname)
54 @@ -105,20 +105,22 @@ static int fname_encrypt(struct inode *inode,
57 skcipher_request_free(req);
60 printk_ratelimited(KERN_ERR
61 "%s: Error (error code %d)\n", __func__, res);
65 oname->len = ciphertext_len;
72 - * This function decrypts the input filename, and returns
73 - * the length of the plaintext.
74 - * Errors are returned as negative numbers.
75 - * We trust the caller to allocate sufficient memory to oname string.
77 + * fname_decrypt() - decrypt a filename
79 + * The caller must have allocated sufficient memory for the @oname string.
81 + * Return: 0 on success, -errno on failure
83 static int fname_decrypt(struct inode *inode,
84 const struct fscrypt_str *iname,
85 @@ -168,7 +170,7 @@ static int fname_decrypt(struct inode *inode,
88 oname->len = strnlen(oname->name, iname->len);
93 static const char *lookup_table =
94 @@ -279,6 +281,10 @@ EXPORT_SYMBOL(fscrypt_fname_free_buffer);
96 * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
99 + * The caller must have allocated sufficient memory for the @oname string.
101 + * Return: 0 on success, -errno on failure
103 int fscrypt_fname_disk_to_usr(struct inode *inode,
104 u32 hash, u32 minor_hash,
105 @@ -287,13 +293,12 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
107 const struct qstr qname = FSTR_TO_QSTR(iname);
111 if (fscrypt_is_dot_dotdot(&qname)) {
112 oname->name[0] = '.';
113 oname->name[iname->len - 1] = '.';
114 oname->len = iname->len;
119 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
120 @@ -303,9 +308,9 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
121 return fname_decrypt(inode, iname, oname);
123 if (iname->len <= FS_FNAME_CRYPTO_DIGEST_SIZE) {
124 - ret = digest_encode(iname->name, iname->len, oname->name);
127 + oname->len = digest_encode(iname->name, iname->len,
132 memcpy(buf, &hash, 4);
133 @@ -315,15 +320,18 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
135 memcpy(buf + 8, iname->name + iname->len - 16, 16);
136 oname->name[0] = '_';
137 - ret = digest_encode(buf, 24, oname->name + 1);
138 - oname->len = ret + 1;
140 + oname->len = 1 + digest_encode(buf, 24, oname->name + 1);
143 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
146 * fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
149 + * The caller must have allocated sufficient memory for the @oname string.
151 + * Return: 0 on success, -errno on failure
153 int fscrypt_fname_usr_to_disk(struct inode *inode,
154 const struct qstr *iname,
155 @@ -333,7 +341,7 @@ int fscrypt_fname_usr_to_disk(struct inode *inode,
156 oname->name[0] = '.';
157 oname->name[iname->len - 1] = '.';
158 oname->len = iname->len;
162 if (inode->i_crypt_info)
163 return fname_encrypt(inode, iname, oname);
164 @@ -367,10 +375,10 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
165 if (dir->i_crypt_info) {
166 ret = fscrypt_fname_alloc_buffer(dir, iname->len,
171 ret = fname_encrypt(dir, iname, &fname->crypto_buf);
175 fname->disk_name.name = fname->crypto_buf.name;
176 fname->disk_name.len = fname->crypto_buf.len;
177 diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
178 index 67415e0..4d4b910 100644
181 @@ -260,11 +260,12 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
182 /* Directory is encrypted */
183 err = fscrypt_fname_disk_to_usr(inode,
184 0, 0, &de_name, &fstr);
192 + de_name.name, de_name.len,
193 le32_to_cpu(de->inode),
194 get_dtype(sb, de->file_type)))
196 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
197 index 34c0142..2243ae2 100644
198 --- a/fs/ext4/namei.c
199 +++ b/fs/ext4/namei.c
200 @@ -639,7 +639,7 @@ static struct stats dx_show_leaf(struct inode *dir,
201 res = fscrypt_fname_alloc_buffer(
206 printk(KERN_WARNING "Error "
209 @@ -647,7 +647,7 @@ static struct stats dx_show_leaf(struct inode *dir,
210 res = fscrypt_fname_disk_to_usr(dir,
215 printk(KERN_WARNING "Error "
216 "converting filename "
218 @@ -1011,7 +1011,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
219 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
220 hinfo->minor_hash, &de_name,
227 @@ -3144,7 +3144,7 @@ static int ext4_symlink(struct inode *dir,
228 istr.name = (const unsigned char *) symname;
230 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
234 sd->len = cpu_to_le16(ostr.len);
235 disk_link.name = (char *) sd;
236 diff --git a/fs/ext4/symlink.c b/fs/ext4/symlink.c
237 index 04a7850..0a26cbd 100644
238 --- a/fs/ext4/symlink.c
239 +++ b/fs/ext4/symlink.c
240 @@ -68,12 +68,11 @@ static const char *ext4_encrypted_get_link(struct dentry *dentry,
243 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
248 /* Null-terminate the name */
249 - if (res <= pstr.len)
251 + paddr[pstr.len] = '\0';
254 set_delayed_call(done, kfree_link, paddr);
255 diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
256 index 9054aea..8716943 100644
259 @@ -786,7 +786,7 @@ bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
261 if (f2fs_encrypted_inode(d->inode)) {
262 int save_len = fstr->len;
266 de_name.name = f2fs_kmalloc(de_name.len, GFP_NOFS);
268 @@ -794,11 +794,11 @@ bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
270 memcpy(de_name.name, d->filename[bit_pos], de_name.len);
272 - ret = fscrypt_fname_disk_to_usr(d->inode,
273 + err = fscrypt_fname_disk_to_usr(d->inode,
274 (u32)de->hash_code, 0,
282 diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
283 index 73fa356..afd5633 100644
284 --- a/fs/f2fs/namei.c
285 +++ b/fs/f2fs/namei.c
286 @@ -449,7 +449,7 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
287 ostr.name = sd->encrypted_path;
288 ostr.len = disk_link.len;
289 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
294 sd->len = cpu_to_le16(ostr.len);
295 @@ -1048,7 +1048,7 @@ static const char *f2fs_encrypted_get_link(struct dentry *dentry,
298 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
303 /* this is broken symlink case */
304 @@ -1060,7 +1060,7 @@ static const char *f2fs_encrypted_get_link(struct dentry *dentry,
307 /* Null-terminate the name */
309 + paddr[pstr.len] = '\0';
312 set_delayed_call(done, kfree_link, paddr);