10
10
* This has not yet undergone a rigorous security audit.
11
11
*/
12
12
13
- #include <keys/encrypted-type.h>
14
- #include <keys/user-type.h>
15
13
#include <linux/scatterlist.h>
16
14
#include <linux/ratelimit.h>
17
15
#include <linux/fscrypto.h>
18
16
19
- static u32 size_round_up (size_t size , size_t blksize )
20
- {
21
- return ((size + blksize - 1 ) / blksize ) * blksize ;
22
- }
23
-
24
17
/**
25
- * dir_crypt_complete() -
18
+ * fname_crypt_complete() - completion callback for filename crypto
19
+ * @req: The asynchronous cipher request context
20
+ * @res: The result of the cipher operation
26
21
*/
27
- static void dir_crypt_complete (struct crypto_async_request * req , int res )
22
+ static void fname_crypt_complete (struct crypto_async_request * req , int res )
28
23
{
29
24
struct fscrypt_completion_result * ecr = req -> data ;
30
25
@@ -35,11 +30,11 @@ static void dir_crypt_complete(struct crypto_async_request *req, int res)
35
30
}
36
31
37
32
/**
38
- * fname_encrypt() -
33
+ * fname_encrypt() - encrypt a filename
39
34
*
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.
35
+ * The caller must have allocated sufficient memory for the @oname string.
36
+ *
37
+ * Return: 0 on success, -errno on failure
43
38
*/
44
39
static int fname_encrypt (struct inode * inode ,
45
40
const struct qstr * iname , struct fscrypt_str * oname )
@@ -60,10 +55,9 @@ static int fname_encrypt(struct inode *inode,
60
55
if (iname -> len <= 0 || iname -> len > lim )
61
56
return - EIO ;
62
57
63
- ciphertext_len = (iname -> len < FS_CRYPTO_BLOCK_SIZE ) ?
64
- FS_CRYPTO_BLOCK_SIZE : iname -> len ;
65
- ciphertext_len = size_round_up (ciphertext_len , padding );
66
- ciphertext_len = (ciphertext_len > lim ) ? lim : ciphertext_len ;
58
+ ciphertext_len = max (iname -> len , (u32 )FS_CRYPTO_BLOCK_SIZE );
59
+ ciphertext_len = round_up (ciphertext_len , padding );
60
+ ciphertext_len = min (ciphertext_len , lim );
67
61
68
62
if (ciphertext_len <= sizeof (buf )) {
69
63
workbuf = buf ;
@@ -84,7 +78,7 @@ static int fname_encrypt(struct inode *inode,
84
78
}
85
79
skcipher_request_set_callback (req ,
86
80
CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP ,
87
- dir_crypt_complete , & ecr );
81
+ fname_crypt_complete , & ecr );
88
82
89
83
/* Copy the input */
90
84
memcpy (workbuf , iname -> name , iname -> len );
@@ -105,20 +99,22 @@ static int fname_encrypt(struct inode *inode,
105
99
}
106
100
kfree (alloc_buf );
107
101
skcipher_request_free (req );
108
- if (res < 0 )
102
+ if (res < 0 ) {
109
103
printk_ratelimited (KERN_ERR
110
104
"%s: Error (error code %d)\n" , __func__ , res );
105
+ return res ;
106
+ }
111
107
112
108
oname -> len = ciphertext_len ;
113
- return res ;
109
+ return 0 ;
114
110
}
115
111
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.
112
+ /**
113
+ * fname_decrypt() - decrypt a filename
114
+ *
115
+ * The caller must have allocated sufficient memory for the @oname string .
116
+ *
117
+ * Return: 0 on success, -errno on failure
122
118
*/
123
119
static int fname_decrypt (struct inode * inode ,
124
120
const struct fscrypt_str * iname ,
@@ -146,7 +142,7 @@ static int fname_decrypt(struct inode *inode,
146
142
}
147
143
skcipher_request_set_callback (req ,
148
144
CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP ,
149
- dir_crypt_complete , & ecr );
145
+ fname_crypt_complete , & ecr );
150
146
151
147
/* Initialize IV */
152
148
memset (iv , 0 , FS_CRYPTO_BLOCK_SIZE );
@@ -168,7 +164,7 @@ static int fname_decrypt(struct inode *inode,
168
164
}
169
165
170
166
oname -> len = strnlen (oname -> name , iname -> len );
171
- return oname -> len ;
167
+ return 0 ;
172
168
}
173
169
174
170
static const char * lookup_table =
@@ -231,9 +227,8 @@ u32 fscrypt_fname_encrypted_size(struct inode *inode, u32 ilen)
231
227
232
228
if (ci )
233
229
padding = 4 << (ci -> ci_flags & FS_POLICY_FLAGS_PAD_MASK );
234
- if (ilen < FS_CRYPTO_BLOCK_SIZE )
235
- ilen = FS_CRYPTO_BLOCK_SIZE ;
236
- return size_round_up (ilen , padding );
230
+ ilen = max (ilen , (u32 )FS_CRYPTO_BLOCK_SIZE );
231
+ return round_up (ilen , padding );
237
232
}
238
233
EXPORT_SYMBOL (fscrypt_fname_encrypted_size );
239
234
@@ -279,6 +274,10 @@ EXPORT_SYMBOL(fscrypt_fname_free_buffer);
279
274
/**
280
275
* fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
281
276
* space
277
+ *
278
+ * The caller must have allocated sufficient memory for the @oname string.
279
+ *
280
+ * Return: 0 on success, -errno on failure
282
281
*/
283
282
int fscrypt_fname_disk_to_usr (struct inode * inode ,
284
283
u32 hash , u32 minor_hash ,
@@ -287,13 +286,12 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
287
286
{
288
287
const struct qstr qname = FSTR_TO_QSTR (iname );
289
288
char buf [24 ];
290
- int ret ;
291
289
292
290
if (fscrypt_is_dot_dotdot (& qname )) {
293
291
oname -> name [0 ] = '.' ;
294
292
oname -> name [iname -> len - 1 ] = '.' ;
295
293
oname -> len = iname -> len ;
296
- return oname -> len ;
294
+ return 0 ;
297
295
}
298
296
299
297
if (iname -> len < FS_CRYPTO_BLOCK_SIZE )
@@ -303,9 +301,9 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
303
301
return fname_decrypt (inode , iname , oname );
304
302
305
303
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 ;
304
+ oname -> len = digest_encode (iname -> name , iname -> len ,
305
+ oname -> name ) ;
306
+ return 0 ;
309
307
}
310
308
if (hash ) {
311
309
memcpy (buf , & hash , 4 );
@@ -315,15 +313,18 @@ int fscrypt_fname_disk_to_usr(struct inode *inode,
315
313
}
316
314
memcpy (buf + 8 , iname -> name + iname -> len - 16 , 16 );
317
315
oname -> name [0 ] = '_' ;
318
- ret = digest_encode (buf , 24 , oname -> name + 1 );
319
- oname -> len = ret + 1 ;
320
- return ret + 1 ;
316
+ oname -> len = 1 + digest_encode (buf , 24 , oname -> name + 1 );
317
+ return 0 ;
321
318
}
322
319
EXPORT_SYMBOL (fscrypt_fname_disk_to_usr );
323
320
324
321
/**
325
322
* fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
326
323
* space
324
+ *
325
+ * The caller must have allocated sufficient memory for the @oname string.
326
+ *
327
+ * Return: 0 on success, -errno on failure
327
328
*/
328
329
int fscrypt_fname_usr_to_disk (struct inode * inode ,
329
330
const struct qstr * iname ,
@@ -333,7 +334,7 @@ int fscrypt_fname_usr_to_disk(struct inode *inode,
333
334
oname -> name [0 ] = '.' ;
334
335
oname -> name [iname -> len - 1 ] = '.' ;
335
336
oname -> len = iname -> len ;
336
- return oname -> len ;
337
+ return 0 ;
337
338
}
338
339
if (inode -> i_crypt_info )
339
340
return fname_encrypt (inode , iname , oname );
@@ -367,10 +368,10 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
367
368
if (dir -> i_crypt_info ) {
368
369
ret = fscrypt_fname_alloc_buffer (dir , iname -> len ,
369
370
& fname -> crypto_buf );
370
- if (ret < 0 )
371
+ if (ret )
371
372
return ret ;
372
373
ret = fname_encrypt (dir , iname , & fname -> crypto_buf );
373
- if (ret < 0 )
374
+ if (ret )
374
375
goto errout ;
375
376
fname -> disk_name .name = fname -> crypto_buf .name ;
376
377
fname -> disk_name .len = fname -> crypto_buf .len ;
0 commit comments