Skip to content

Commit ef1eb3a

Browse files
ebiggerstytso
authored andcommitted
fscrypto: make filename crypto functions return 0 on success
Several filename crypto functions: fname_decrypt(), fscrypt_fname_disk_to_usr(), and fscrypt_fname_usr_to_disk(), returned the output length on success or -errno on failure. However, the output length was redundant with the value written to 'oname->len'. It is also potentially error-prone to make callers have to check for '< 0' instead of '!= 0'. Therefore, make these functions return 0 instead of a length, and make the callers who cared about the return value being a length use 'oname->len' instead. For consistency also make other callers check for a nonzero result rather than a negative result. This change also fixes the inconsistency of fname_encrypt() actually already returning 0 on success, not a length like the other filename crypto functions and as documented in its function comment. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Andreas Dilger <adilger@dilger.ca> Acked-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 53fd755 commit ef1eb3a

6 files changed

Lines changed: 47 additions & 39 deletions

File tree

‎fs/crypto/fname.c‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ static void fname_crypt_complete(struct crypto_async_request *req, int res)
3535
}
3636

3737
/**
38-
* fname_encrypt() -
38+
* fname_encrypt() - encrypt a filename
3939
*
40-
* This function encrypts the input filename, and returns the length of the
41-
* ciphertext. Errors are returned as negative numbers. We trust the caller to
42-
* allocate sufficient memory to oname string.
40+
* The caller must have allocated sufficient memory for the @oname string.
41+
*
42+
* Return: 0 on success, -errno on failure
4343
*/
4444
static int fname_encrypt(struct inode *inode,
4545
const struct qstr *iname, struct fscrypt_str *oname)
@@ -105,20 +105,22 @@ static int fname_encrypt(struct inode *inode,
105105
}
106106
kfree(alloc_buf);
107107
skcipher_request_free(req);
108-
if (res < 0)
108+
if (res < 0) {
109109
printk_ratelimited(KERN_ERR
110110
"%s: Error (error code %d)\n", __func__, res);
111+
return res;
112+
}
111113

112114
oname->len = ciphertext_len;
113-
return res;
115+
return 0;
114116
}
115117

116-
/*
117-
* fname_decrypt()
118-
* This function decrypts the input filename, and returns
119-
* the length of the plaintext.
120-
* Errors are returned as negative numbers.
121-
* We trust the caller to allocate sufficient memory to oname string.
118+
/**
119+
* fname_decrypt() - decrypt a filename
120+
*
121+
* The caller must have allocated sufficient memory for the @oname string.
122+
*
123+
* Return: 0 on success, -errno on failure
122124
*/
123125
static int fname_decrypt(struct inode *inode,
124126
const struct fscrypt_str *iname,
@@ -168,7 +170,7 @@ static int fname_decrypt(struct inode *inode,
168170
}
169171

170172
oname->len = strnlen(oname->name, iname->len);
171-
return oname->len;
173+
return 0;
172174
}
173175

174176
static const char *lookup_table =
@@ -279,6 +281,10 @@ EXPORT_SYMBOL(fscrypt_fname_free_buffer);
279281
/**
280282
* fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
281283
* space
284+
*
285+
* The caller must have allocated sufficient memory for the @oname string.
286+
*
287+
* Return: 0 on success, -errno on failure
282288
*/
283289
int fscrypt_fname_disk_to_usr(struct inode *inode,
284290
u32 hash, u32 minor_hash,
@@ -287,13 +293,12 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
287293
{
288294
const struct qstr qname = FSTR_TO_QSTR(iname);
289295
char buf[24];
290-
int ret;
291296

292297
if (fscrypt_is_dot_dotdot(&qname)) {
293298
oname->name[0] = '.';
294299
oname->name[iname->len - 1] = '.';
295300
oname->len = iname->len;
296-
return oname->len;
301+
return 0;
297302
}
298303

299304
if (iname->len < FS_CRYPTO_BLOCK_SIZE)
@@ -303,9 +308,9 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
303308
return fname_decrypt(inode, iname, oname);
304309

305310
if (iname->len <= FS_FNAME_CRYPTO_DIGEST_SIZE) {
306-
ret = digest_encode(iname->name, iname->len, oname->name);
307-
oname->len = ret;
308-
return ret;
311+
oname->len = digest_encode(iname->name, iname->len,
312+
oname->name);
313+
return 0;
309314
}
310315
if (hash) {
311316
memcpy(buf, &hash, 4);
@@ -315,15 +320,18 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
315320
}
316321
memcpy(buf + 8, iname->name + iname->len - 16, 16);
317322
oname->name[0] = '_';
318-
ret = digest_encode(buf, 24, oname->name + 1);
319-
oname->len = ret + 1;
320-
return ret + 1;
323+
oname->len = 1 + digest_encode(buf, 24, oname->name + 1);
324+
return 0;
321325
}
322326
EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
323327

324328
/**
325329
* fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
326330
* space
331+
*
332+
* The caller must have allocated sufficient memory for the @oname string.
333+
*
334+
* Return: 0 on success, -errno on failure
327335
*/
328336
int fscrypt_fname_usr_to_disk(struct inode *inode,
329337
const struct qstr *iname,
@@ -333,7 +341,7 @@ int fscrypt_fname_usr_to_disk(struct inode *inode,
333341
oname->name[0] = '.';
334342
oname->name[iname->len - 1] = '.';
335343
oname->len = iname->len;
336-
return oname->len;
344+
return 0;
337345
}
338346
if (inode->i_crypt_info)
339347
return fname_encrypt(inode, iname, oname);
@@ -367,10 +375,10 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
367375
if (dir->i_crypt_info) {
368376
ret = fscrypt_fname_alloc_buffer(dir, iname->len,
369377
&fname->crypto_buf);
370-
if (ret < 0)
378+
if (ret)
371379
return ret;
372380
ret = fname_encrypt(dir, iname, &fname->crypto_buf);
373-
if (ret < 0)
381+
if (ret)
374382
goto errout;
375383
fname->disk_name.name = fname->crypto_buf.name;
376384
fname->disk_name.len = fname->crypto_buf.len;

‎fs/ext4/dir.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,12 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
260260
/* Directory is encrypted */
261261
err = fscrypt_fname_disk_to_usr(inode,
262262
0, 0, &de_name, &fstr);
263+
de_name = fstr;
263264
fstr.len = save_len;
264-
if (err < 0)
265+
if (err)
265266
goto errout;
266267
if (!dir_emit(ctx,
267-
fstr.name, err,
268+
de_name.name, de_name.len,
268269
le32_to_cpu(de->inode),
269270
get_dtype(sb, de->file_type)))
270271
goto done;

‎fs/ext4/namei.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,15 +639,15 @@ static struct stats dx_show_leaf(struct inode *dir,
639639
res = fscrypt_fname_alloc_buffer(
640640
dir, len,
641641
&fname_crypto_str);
642-
if (res < 0)
642+
if (res)
643643
printk(KERN_WARNING "Error "
644644
"allocating crypto "
645645
"buffer--skipping "
646646
"crypto\n");
647647
res = fscrypt_fname_disk_to_usr(dir,
648648
0, 0, &de_name,
649649
&fname_crypto_str);
650-
if (res < 0) {
650+
if (res) {
651651
printk(KERN_WARNING "Error "
652652
"converting filename "
653653
"from disk to usr"
@@ -1011,7 +1011,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
10111011
err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
10121012
hinfo->minor_hash, &de_name,
10131013
&fname_crypto_str);
1014-
if (err < 0) {
1014+
if (err) {
10151015
count = err;
10161016
goto errout;
10171017
}
@@ -3144,7 +3144,7 @@ static int ext4_symlink(struct inode *dir,
31443144
istr.name = (const unsigned char *) symname;
31453145
istr.len = len;
31463146
err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
3147-
if (err < 0)
3147+
if (err)
31483148
goto err_drop_inode;
31493149
sd->len = cpu_to_le16(ostr.len);
31503150
disk_link.name = (char *) sd;

‎fs/ext4/symlink.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ static const char *ext4_encrypted_get_link(struct dentry *dentry,
6868
paddr = pstr.name;
6969

7070
res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
71-
if (res < 0)
71+
if (res)
7272
goto errout;
7373

7474
/* Null-terminate the name */
75-
if (res <= pstr.len)
76-
paddr[res] = '\0';
75+
paddr[pstr.len] = '\0';
7776
if (cpage)
7877
put_page(cpage);
7978
set_delayed_call(done, kfree_link, paddr);

‎fs/f2fs/dir.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,19 +786,19 @@ bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
786786

787787
if (f2fs_encrypted_inode(d->inode)) {
788788
int save_len = fstr->len;
789-
int ret;
789+
int err;
790790

791791
de_name.name = f2fs_kmalloc(de_name.len, GFP_NOFS);
792792
if (!de_name.name)
793793
return false;
794794

795795
memcpy(de_name.name, d->filename[bit_pos], de_name.len);
796796

797-
ret = fscrypt_fname_disk_to_usr(d->inode,
797+
err = fscrypt_fname_disk_to_usr(d->inode,
798798
(u32)de->hash_code, 0,
799799
&de_name, fstr);
800800
kfree(de_name.name);
801-
if (ret < 0)
801+
if (err)
802802
return true;
803803

804804
de_name = *fstr;

‎fs/f2fs/namei.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
449449
ostr.name = sd->encrypted_path;
450450
ostr.len = disk_link.len;
451451
err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
452-
if (err < 0)
452+
if (err)
453453
goto err_out;
454454

455455
sd->len = cpu_to_le16(ostr.len);
@@ -1048,7 +1048,7 @@ static const char *f2fs_encrypted_get_link(struct dentry *dentry,
10481048
goto errout;
10491049

10501050
res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
1051-
if (res < 0)
1051+
if (res)
10521052
goto errout;
10531053

10541054
/* this is broken symlink case */
@@ -1060,7 +1060,7 @@ static const char *f2fs_encrypted_get_link(struct dentry *dentry,
10601060
paddr = pstr.name;
10611061

10621062
/* Null-terminate the name */
1063-
paddr[res] = '\0';
1063+
paddr[pstr.len] = '\0';
10641064

10651065
put_page(cpage);
10661066
set_delayed_call(done, kfree_link, paddr);

0 commit comments

Comments
 (0)