diff options
author | Tyler Hicks <tyhicks@linux.vnet.ibm.com> | 2011-05-24 05:56:23 -0400 |
---|---|---|
committer | Tyler Hicks <tyhicks@linux.vnet.ibm.com> | 2011-05-29 15:24:24 -0400 |
commit | 778aeb42a708d2a57e491d2cbb5a1e74f61270b9 (patch) | |
tree | 6651d8a2356b9075e6978285cbcfa7aab16e9b7a /fs | |
parent | 7a86617e553f47761b10f57de472d7262562b7de (diff) |
eCryptfs: Cleanup and optimize ecryptfs_lookup_interpose()
ecryptfs_lookup_interpose() has turned into spaghetti code over the
years. This is an effort to clean it up.
- Shorten overly descriptive variable names such as ecryptfs_dentry
- Simplify gotos and error paths
- Create helper function for reading plaintext i_size from metadata
It also includes an optimization when reading i_size from the metadata.
A complete page-sized kmem_cache_alloc() was being done to read in 16
bytes of metadata. The buffer for that is now statically declared.
Signed-off-by: Tyler Hicks <tyhicks@linux.vnet.ibm.com>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/ecryptfs/crypto.c | 45 | ||||
-rw-r--r-- | fs/ecryptfs/ecryptfs_kernel.h | 7 | ||||
-rw-r--r-- | fs/ecryptfs/inode.c | 141 |
3 files changed, 88 insertions, 105 deletions
diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c index 162f9baf9eb5..66d8e6748a46 100644 --- a/fs/ecryptfs/crypto.c +++ b/fs/ecryptfs/crypto.c | |||
@@ -1201,24 +1201,19 @@ int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code) | |||
1201 | return rc; | 1201 | return rc; |
1202 | } | 1202 | } |
1203 | 1203 | ||
1204 | int ecryptfs_read_and_validate_header_region(char *data, | 1204 | int ecryptfs_read_and_validate_header_region(struct inode *inode) |
1205 | struct inode *ecryptfs_inode) | ||
1206 | { | 1205 | { |
1207 | struct ecryptfs_crypt_stat *crypt_stat = | 1206 | u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; |
1208 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); | 1207 | u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; |
1209 | int rc; | 1208 | int rc; |
1210 | 1209 | ||
1211 | if (crypt_stat->extent_size == 0) | 1210 | rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES, |
1212 | crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; | 1211 | inode); |
1213 | rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size, | 1212 | if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) |
1214 | ecryptfs_inode); | 1213 | return rc >= 0 ? -EINVAL : rc; |
1215 | if (rc < 0) { | 1214 | rc = ecryptfs_validate_marker(marker); |
1216 | printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n", | 1215 | if (!rc) |
1217 | __func__, rc); | 1216 | ecryptfs_i_size_init(file_size, inode); |
1218 | goto out; | ||
1219 | } | ||
1220 | rc = ecryptfs_validate_marker(data + ECRYPTFS_FILE_SIZE_BYTES); | ||
1221 | out: | ||
1222 | return rc; | 1217 | return rc; |
1223 | } | 1218 | } |
1224 | 1219 | ||
@@ -1562,19 +1557,21 @@ out: | |||
1562 | return rc; | 1557 | return rc; |
1563 | } | 1558 | } |
1564 | 1559 | ||
1565 | int ecryptfs_read_and_validate_xattr_region(char *page_virt, | 1560 | int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry, |
1566 | struct inode *inode) | 1561 | struct inode *inode) |
1567 | { | 1562 | { |
1563 | u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; | ||
1564 | u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; | ||
1568 | int rc; | 1565 | int rc; |
1569 | 1566 | ||
1570 | rc = ecryptfs_read_xattr_region(page_virt, inode); | 1567 | rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), |
1571 | if (rc) | 1568 | ECRYPTFS_XATTR_NAME, file_size, |
1572 | goto out; | 1569 | ECRYPTFS_SIZE_AND_MARKER_BYTES); |
1573 | rc = ecryptfs_validate_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES); | 1570 | if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) |
1574 | if (rc) | 1571 | return rc >= 0 ? -EINVAL : rc; |
1575 | printk(KERN_WARNING "Valid data found in [%s] xattr, but " | 1572 | rc = ecryptfs_validate_marker(marker); |
1576 | "the marker is invalid\n", ECRYPTFS_XATTR_NAME); | 1573 | if (!rc) |
1577 | out: | 1574 | ecryptfs_i_size_init(file_size, inode); |
1578 | return rc; | 1575 | return rc; |
1579 | } | 1576 | } |
1580 | 1577 | ||
diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h index 72aa24a4c71e..8297ddaca7c4 100644 --- a/fs/ecryptfs/ecryptfs_kernel.h +++ b/fs/ecryptfs/ecryptfs_kernel.h | |||
@@ -200,6 +200,8 @@ ecryptfs_get_key_payload_data(struct key *key) | |||
200 | #define MAGIC_ECRYPTFS_MARKER 0x3c81b7f5 | 200 | #define MAGIC_ECRYPTFS_MARKER 0x3c81b7f5 |
201 | #define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES 8 /* 4*2 */ | 201 | #define MAGIC_ECRYPTFS_MARKER_SIZE_BYTES 8 /* 4*2 */ |
202 | #define ECRYPTFS_FILE_SIZE_BYTES (sizeof(u64)) | 202 | #define ECRYPTFS_FILE_SIZE_BYTES (sizeof(u64)) |
203 | #define ECRYPTFS_SIZE_AND_MARKER_BYTES (ECRYPTFS_FILE_SIZE_BYTES \ | ||
204 | + MAGIC_ECRYPTFS_MARKER_SIZE_BYTES) | ||
203 | #define ECRYPTFS_DEFAULT_CIPHER "aes" | 205 | #define ECRYPTFS_DEFAULT_CIPHER "aes" |
204 | #define ECRYPTFS_DEFAULT_KEY_BYTES 16 | 206 | #define ECRYPTFS_DEFAULT_KEY_BYTES 16 |
205 | #define ECRYPTFS_DEFAULT_HASH "md5" | 207 | #define ECRYPTFS_DEFAULT_HASH "md5" |
@@ -659,9 +661,8 @@ int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry); | |||
659 | void ecryptfs_write_crypt_stat_flags(char *page_virt, | 661 | void ecryptfs_write_crypt_stat_flags(char *page_virt, |
660 | struct ecryptfs_crypt_stat *crypt_stat, | 662 | struct ecryptfs_crypt_stat *crypt_stat, |
661 | size_t *written); | 663 | size_t *written); |
662 | int ecryptfs_read_and_validate_header_region(char *data, | 664 | int ecryptfs_read_and_validate_header_region(struct inode *inode); |
663 | struct inode *ecryptfs_inode); | 665 | int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry, |
664 | int ecryptfs_read_and_validate_xattr_region(char *page_virt, | ||
665 | struct inode *inode); | 666 | struct inode *inode); |
666 | u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes); | 667 | u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes); |
667 | int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code); | 668 | int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code); |
diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c index f0ad965d7d51..7349ade17de6 100644 --- a/fs/ecryptfs/inode.c +++ b/fs/ecryptfs/inode.c | |||
@@ -307,105 +307,90 @@ out: | |||
307 | return rc; | 307 | return rc; |
308 | } | 308 | } |
309 | 309 | ||
310 | static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode) | ||
311 | { | ||
312 | struct ecryptfs_crypt_stat *crypt_stat; | ||
313 | int rc; | ||
314 | |||
315 | rc = ecryptfs_get_lower_file(dentry, inode); | ||
316 | if (rc) { | ||
317 | printk(KERN_ERR "%s: Error attempting to initialize " | ||
318 | "the lower file for the dentry with name " | ||
319 | "[%s]; rc = [%d]\n", __func__, | ||
320 | dentry->d_name.name, rc); | ||
321 | return rc; | ||
322 | } | ||
323 | |||
324 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; | ||
325 | /* TODO: lock for crypt_stat comparison */ | ||
326 | if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) | ||
327 | ecryptfs_set_default_sizes(crypt_stat); | ||
328 | |||
329 | rc = ecryptfs_read_and_validate_header_region(inode); | ||
330 | ecryptfs_put_lower_file(inode); | ||
331 | if (rc) { | ||
332 | rc = ecryptfs_read_and_validate_xattr_region(dentry, inode); | ||
333 | if (!rc) | ||
334 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; | ||
335 | } | ||
336 | |||
337 | /* Must return 0 to allow non-eCryptfs files to be looked up, too */ | ||
338 | return 0; | ||
339 | } | ||
340 | |||
310 | /** | 341 | /** |
311 | * ecryptfs_lookup_interpose - Dentry interposition for a lookup | 342 | * ecryptfs_lookup_interpose - Dentry interposition for a lookup |
312 | */ | 343 | */ |
313 | static int ecryptfs_lookup_interpose(struct dentry *ecryptfs_dentry, | 344 | static int ecryptfs_lookup_interpose(struct dentry *dentry, |
314 | struct dentry *lower_dentry, | 345 | struct dentry *lower_dentry, |
315 | struct inode *ecryptfs_dir_inode) | 346 | struct inode *dir_inode) |
316 | { | 347 | { |
317 | struct dentry *lower_dir_dentry; | 348 | struct inode *inode, *lower_inode = lower_dentry->d_inode; |
349 | struct ecryptfs_dentry_info *dentry_info; | ||
318 | struct vfsmount *lower_mnt; | 350 | struct vfsmount *lower_mnt; |
319 | struct inode *inode, *lower_inode; | 351 | int rc = 0; |
320 | struct ecryptfs_crypt_stat *crypt_stat; | 352 | |
321 | char *page_virt = NULL; | 353 | lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent)); |
322 | int put_lower = 0, rc = 0; | 354 | fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode); |
323 | |||
324 | lower_dir_dentry = lower_dentry->d_parent; | ||
325 | lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt( | ||
326 | ecryptfs_dentry->d_parent)); | ||
327 | lower_inode = lower_dentry->d_inode; | ||
328 | fsstack_copy_attr_atime(ecryptfs_dir_inode, lower_dir_dentry->d_inode); | ||
329 | BUG_ON(!lower_dentry->d_count); | 355 | BUG_ON(!lower_dentry->d_count); |
330 | ecryptfs_set_dentry_private(ecryptfs_dentry, | 356 | |
331 | kmem_cache_alloc(ecryptfs_dentry_info_cache, | 357 | dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL); |
332 | GFP_KERNEL)); | 358 | ecryptfs_set_dentry_private(dentry, dentry_info); |
333 | if (!ecryptfs_dentry_to_private(ecryptfs_dentry)) { | 359 | if (!dentry_info) { |
334 | rc = -ENOMEM; | ||
335 | printk(KERN_ERR "%s: Out of memory whilst attempting " | 360 | printk(KERN_ERR "%s: Out of memory whilst attempting " |
336 | "to allocate ecryptfs_dentry_info struct\n", | 361 | "to allocate ecryptfs_dentry_info struct\n", |
337 | __func__); | 362 | __func__); |
338 | goto out_put; | 363 | dput(lower_dentry); |
364 | mntput(lower_mnt); | ||
365 | d_drop(dentry); | ||
366 | return -ENOMEM; | ||
339 | } | 367 | } |
340 | ecryptfs_set_dentry_lower(ecryptfs_dentry, lower_dentry); | 368 | ecryptfs_set_dentry_lower(dentry, lower_dentry); |
341 | ecryptfs_set_dentry_lower_mnt(ecryptfs_dentry, lower_mnt); | 369 | ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt); |
370 | |||
342 | if (!lower_dentry->d_inode) { | 371 | if (!lower_dentry->d_inode) { |
343 | /* We want to add because we couldn't find in lower */ | 372 | /* We want to add because we couldn't find in lower */ |
344 | d_add(ecryptfs_dentry, NULL); | 373 | d_add(dentry, NULL); |
345 | goto out; | 374 | return 0; |
346 | } | 375 | } |
347 | inode = __ecryptfs_get_inode(lower_inode, ecryptfs_dir_inode->i_sb); | 376 | inode = __ecryptfs_get_inode(lower_inode, dir_inode->i_sb); |
348 | if (IS_ERR(inode)) { | 377 | if (IS_ERR(inode)) { |
349 | rc = PTR_ERR(inode); | 378 | printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n", |
350 | printk(KERN_ERR "%s: Error interposing; rc = [%d]\n", | 379 | __func__, PTR_ERR(inode)); |
351 | __func__, rc); | 380 | return PTR_ERR(inode); |
352 | goto out; | ||
353 | } | ||
354 | if (!S_ISREG(inode->i_mode)) { | ||
355 | if (inode->i_state & I_NEW) | ||
356 | unlock_new_inode(inode); | ||
357 | d_add(ecryptfs_dentry, inode); | ||
358 | goto out; | ||
359 | } | ||
360 | /* Released in this function */ | ||
361 | page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, GFP_USER); | ||
362 | if (!page_virt) { | ||
363 | printk(KERN_ERR "%s: Cannot kmem_cache_zalloc() a page\n", | ||
364 | __func__); | ||
365 | rc = -ENOMEM; | ||
366 | make_bad_inode(inode); | ||
367 | goto out; | ||
368 | } | ||
369 | rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode); | ||
370 | if (rc) { | ||
371 | printk(KERN_ERR "%s: Error attempting to initialize " | ||
372 | "the lower file for the dentry with name " | ||
373 | "[%s]; rc = [%d]\n", __func__, | ||
374 | ecryptfs_dentry->d_name.name, rc); | ||
375 | make_bad_inode(inode); | ||
376 | goto out_free_kmem; | ||
377 | } | 381 | } |
378 | put_lower = 1; | 382 | if (S_ISREG(inode->i_mode)) { |
379 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; | 383 | rc = ecryptfs_i_size_read(dentry, inode); |
380 | /* TODO: lock for crypt_stat comparison */ | ||
381 | if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) | ||
382 | ecryptfs_set_default_sizes(crypt_stat); | ||
383 | rc = ecryptfs_read_and_validate_header_region(page_virt, inode); | ||
384 | if (rc) { | ||
385 | memset(page_virt, 0, PAGE_CACHE_SIZE); | ||
386 | rc = ecryptfs_read_and_validate_xattr_region(page_virt, | ||
387 | inode); | ||
388 | if (rc) { | 384 | if (rc) { |
389 | rc = 0; | 385 | make_bad_inode(inode); |
390 | goto unlock_inode; | 386 | return rc; |
391 | } | 387 | } |
392 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; | ||
393 | } | 388 | } |
394 | ecryptfs_i_size_init(page_virt, inode); | 389 | |
395 | unlock_inode: | ||
396 | if (inode->i_state & I_NEW) | 390 | if (inode->i_state & I_NEW) |
397 | unlock_new_inode(inode); | 391 | unlock_new_inode(inode); |
398 | d_add(ecryptfs_dentry, inode); | 392 | d_add(dentry, inode); |
399 | out_free_kmem: | 393 | |
400 | kmem_cache_free(ecryptfs_header_cache_2, page_virt); | ||
401 | goto out; | ||
402 | out_put: | ||
403 | dput(lower_dentry); | ||
404 | mntput(lower_mnt); | ||
405 | d_drop(ecryptfs_dentry); | ||
406 | out: | ||
407 | if (put_lower) | ||
408 | ecryptfs_put_lower_file(inode); | ||
409 | return rc; | 394 | return rc; |
410 | } | 395 | } |
411 | 396 | ||