diff options
Diffstat (limited to 'fs/ecryptfs/inode.c')
-rw-r--r-- | fs/ecryptfs/inode.c | 281 |
1 files changed, 173 insertions, 108 deletions
diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c index 94ab3c06317a..7349ade17de6 100644 --- a/fs/ecryptfs/inode.c +++ b/fs/ecryptfs/inode.c | |||
@@ -51,6 +51,97 @@ static void unlock_dir(struct dentry *dir) | |||
51 | dput(dir); | 51 | dput(dir); |
52 | } | 52 | } |
53 | 53 | ||
54 | static int ecryptfs_inode_test(struct inode *inode, void *lower_inode) | ||
55 | { | ||
56 | if (ecryptfs_inode_to_lower(inode) == (struct inode *)lower_inode) | ||
57 | return 1; | ||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | static int ecryptfs_inode_set(struct inode *inode, void *opaque) | ||
62 | { | ||
63 | struct inode *lower_inode = opaque; | ||
64 | |||
65 | ecryptfs_set_inode_lower(inode, lower_inode); | ||
66 | fsstack_copy_attr_all(inode, lower_inode); | ||
67 | /* i_size will be overwritten for encrypted regular files */ | ||
68 | fsstack_copy_inode_size(inode, lower_inode); | ||
69 | inode->i_ino = lower_inode->i_ino; | ||
70 | inode->i_version++; | ||
71 | inode->i_mapping->a_ops = &ecryptfs_aops; | ||
72 | |||
73 | if (S_ISLNK(inode->i_mode)) | ||
74 | inode->i_op = &ecryptfs_symlink_iops; | ||
75 | else if (S_ISDIR(inode->i_mode)) | ||
76 | inode->i_op = &ecryptfs_dir_iops; | ||
77 | else | ||
78 | inode->i_op = &ecryptfs_main_iops; | ||
79 | |||
80 | if (S_ISDIR(inode->i_mode)) | ||
81 | inode->i_fop = &ecryptfs_dir_fops; | ||
82 | else if (special_file(inode->i_mode)) | ||
83 | init_special_inode(inode, inode->i_mode, inode->i_rdev); | ||
84 | else | ||
85 | inode->i_fop = &ecryptfs_main_fops; | ||
86 | |||
87 | return 0; | ||
88 | } | ||
89 | |||
90 | static struct inode *__ecryptfs_get_inode(struct inode *lower_inode, | ||
91 | struct super_block *sb) | ||
92 | { | ||
93 | struct inode *inode; | ||
94 | |||
95 | if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) | ||
96 | return ERR_PTR(-EXDEV); | ||
97 | if (!igrab(lower_inode)) | ||
98 | return ERR_PTR(-ESTALE); | ||
99 | inode = iget5_locked(sb, (unsigned long)lower_inode, | ||
100 | ecryptfs_inode_test, ecryptfs_inode_set, | ||
101 | lower_inode); | ||
102 | if (!inode) { | ||
103 | iput(lower_inode); | ||
104 | return ERR_PTR(-EACCES); | ||
105 | } | ||
106 | if (!(inode->i_state & I_NEW)) | ||
107 | iput(lower_inode); | ||
108 | |||
109 | return inode; | ||
110 | } | ||
111 | |||
112 | struct inode *ecryptfs_get_inode(struct inode *lower_inode, | ||
113 | struct super_block *sb) | ||
114 | { | ||
115 | struct inode *inode = __ecryptfs_get_inode(lower_inode, sb); | ||
116 | |||
117 | if (!IS_ERR(inode) && (inode->i_state & I_NEW)) | ||
118 | unlock_new_inode(inode); | ||
119 | |||
120 | return inode; | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * ecryptfs_interpose | ||
125 | * @lower_dentry: Existing dentry in the lower filesystem | ||
126 | * @dentry: ecryptfs' dentry | ||
127 | * @sb: ecryptfs's super_block | ||
128 | * | ||
129 | * Interposes upper and lower dentries. | ||
130 | * | ||
131 | * Returns zero on success; non-zero otherwise | ||
132 | */ | ||
133 | static int ecryptfs_interpose(struct dentry *lower_dentry, | ||
134 | struct dentry *dentry, struct super_block *sb) | ||
135 | { | ||
136 | struct inode *inode = ecryptfs_get_inode(lower_dentry->d_inode, sb); | ||
137 | |||
138 | if (IS_ERR(inode)) | ||
139 | return PTR_ERR(inode); | ||
140 | d_instantiate(dentry, inode); | ||
141 | |||
142 | return 0; | ||
143 | } | ||
144 | |||
54 | /** | 145 | /** |
55 | * ecryptfs_create_underlying_file | 146 | * ecryptfs_create_underlying_file |
56 | * @lower_dir_inode: inode of the parent in the lower fs of the new file | 147 | * @lower_dir_inode: inode of the parent in the lower fs of the new file |
@@ -129,7 +220,7 @@ ecryptfs_do_create(struct inode *directory_inode, | |||
129 | goto out_lock; | 220 | goto out_lock; |
130 | } | 221 | } |
131 | rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry, | 222 | rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry, |
132 | directory_inode->i_sb, 0); | 223 | directory_inode->i_sb); |
133 | if (rc) { | 224 | if (rc) { |
134 | ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n"); | 225 | ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n"); |
135 | goto out_lock; | 226 | goto out_lock; |
@@ -168,7 +259,8 @@ static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry) | |||
168 | "context; rc = [%d]\n", rc); | 259 | "context; rc = [%d]\n", rc); |
169 | goto out; | 260 | goto out; |
170 | } | 261 | } |
171 | rc = ecryptfs_get_lower_file(ecryptfs_dentry); | 262 | rc = ecryptfs_get_lower_file(ecryptfs_dentry, |
263 | ecryptfs_dentry->d_inode); | ||
172 | if (rc) { | 264 | if (rc) { |
173 | printk(KERN_ERR "%s: Error attempting to initialize " | 265 | printk(KERN_ERR "%s: Error attempting to initialize " |
174 | "the lower file for the dentry with name " | 266 | "the lower file for the dentry with name " |
@@ -215,102 +307,90 @@ out: | |||
215 | return rc; | 307 | return rc; |
216 | } | 308 | } |
217 | 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 | |||
218 | /** | 341 | /** |
219 | * ecryptfs_lookup_and_interpose_lower - Perform a lookup | 342 | * ecryptfs_lookup_interpose - Dentry interposition for a lookup |
220 | */ | 343 | */ |
221 | int ecryptfs_lookup_and_interpose_lower(struct dentry *ecryptfs_dentry, | 344 | static int ecryptfs_lookup_interpose(struct dentry *dentry, |
222 | struct dentry *lower_dentry, | 345 | struct dentry *lower_dentry, |
223 | struct inode *ecryptfs_dir_inode) | 346 | struct inode *dir_inode) |
224 | { | 347 | { |
225 | struct dentry *lower_dir_dentry; | 348 | struct inode *inode, *lower_inode = lower_dentry->d_inode; |
349 | struct ecryptfs_dentry_info *dentry_info; | ||
226 | struct vfsmount *lower_mnt; | 350 | struct vfsmount *lower_mnt; |
227 | struct inode *lower_inode; | 351 | int rc = 0; |
228 | struct ecryptfs_crypt_stat *crypt_stat; | 352 | |
229 | char *page_virt = NULL; | 353 | lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent)); |
230 | int put_lower = 0, rc = 0; | 354 | fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode); |
231 | |||
232 | lower_dir_dentry = lower_dentry->d_parent; | ||
233 | lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt( | ||
234 | ecryptfs_dentry->d_parent)); | ||
235 | lower_inode = lower_dentry->d_inode; | ||
236 | fsstack_copy_attr_atime(ecryptfs_dir_inode, lower_dir_dentry->d_inode); | ||
237 | BUG_ON(!lower_dentry->d_count); | 355 | BUG_ON(!lower_dentry->d_count); |
238 | ecryptfs_set_dentry_private(ecryptfs_dentry, | 356 | |
239 | kmem_cache_alloc(ecryptfs_dentry_info_cache, | 357 | dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL); |
240 | GFP_KERNEL)); | 358 | ecryptfs_set_dentry_private(dentry, dentry_info); |
241 | if (!ecryptfs_dentry_to_private(ecryptfs_dentry)) { | 359 | if (!dentry_info) { |
242 | rc = -ENOMEM; | ||
243 | printk(KERN_ERR "%s: Out of memory whilst attempting " | 360 | printk(KERN_ERR "%s: Out of memory whilst attempting " |
244 | "to allocate ecryptfs_dentry_info struct\n", | 361 | "to allocate ecryptfs_dentry_info struct\n", |
245 | __func__); | 362 | __func__); |
246 | goto out_put; | 363 | dput(lower_dentry); |
364 | mntput(lower_mnt); | ||
365 | d_drop(dentry); | ||
366 | return -ENOMEM; | ||
247 | } | 367 | } |
248 | ecryptfs_set_dentry_lower(ecryptfs_dentry, lower_dentry); | 368 | ecryptfs_set_dentry_lower(dentry, lower_dentry); |
249 | ecryptfs_set_dentry_lower_mnt(ecryptfs_dentry, lower_mnt); | 369 | ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt); |
370 | |||
250 | if (!lower_dentry->d_inode) { | 371 | if (!lower_dentry->d_inode) { |
251 | /* We want to add because we couldn't find in lower */ | 372 | /* We want to add because we couldn't find in lower */ |
252 | d_add(ecryptfs_dentry, NULL); | 373 | d_add(dentry, NULL); |
253 | goto out; | 374 | return 0; |
254 | } | 375 | } |
255 | rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry, | 376 | inode = __ecryptfs_get_inode(lower_inode, dir_inode->i_sb); |
256 | ecryptfs_dir_inode->i_sb, | 377 | if (IS_ERR(inode)) { |
257 | ECRYPTFS_INTERPOSE_FLAG_D_ADD); | 378 | printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n", |
258 | if (rc) { | 379 | __func__, PTR_ERR(inode)); |
259 | printk(KERN_ERR "%s: Error interposing; rc = [%d]\n", | 380 | return PTR_ERR(inode); |
260 | __func__, rc); | ||
261 | goto out; | ||
262 | } | 381 | } |
263 | if (S_ISDIR(lower_inode->i_mode)) | 382 | if (S_ISREG(inode->i_mode)) { |
264 | goto out; | 383 | rc = ecryptfs_i_size_read(dentry, inode); |
265 | if (S_ISLNK(lower_inode->i_mode)) | ||
266 | goto out; | ||
267 | if (special_file(lower_inode->i_mode)) | ||
268 | goto out; | ||
269 | /* Released in this function */ | ||
270 | page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, GFP_USER); | ||
271 | if (!page_virt) { | ||
272 | printk(KERN_ERR "%s: Cannot kmem_cache_zalloc() a page\n", | ||
273 | __func__); | ||
274 | rc = -ENOMEM; | ||
275 | goto out; | ||
276 | } | ||
277 | rc = ecryptfs_get_lower_file(ecryptfs_dentry); | ||
278 | if (rc) { | ||
279 | printk(KERN_ERR "%s: Error attempting to initialize " | ||
280 | "the lower file for the dentry with name " | ||
281 | "[%s]; rc = [%d]\n", __func__, | ||
282 | ecryptfs_dentry->d_name.name, rc); | ||
283 | goto out_free_kmem; | ||
284 | } | ||
285 | put_lower = 1; | ||
286 | crypt_stat = &ecryptfs_inode_to_private( | ||
287 | ecryptfs_dentry->d_inode)->crypt_stat; | ||
288 | /* TODO: lock for crypt_stat comparison */ | ||
289 | if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) | ||
290 | ecryptfs_set_default_sizes(crypt_stat); | ||
291 | rc = ecryptfs_read_and_validate_header_region(page_virt, | ||
292 | ecryptfs_dentry->d_inode); | ||
293 | if (rc) { | ||
294 | memset(page_virt, 0, PAGE_CACHE_SIZE); | ||
295 | rc = ecryptfs_read_and_validate_xattr_region(page_virt, | ||
296 | ecryptfs_dentry); | ||
297 | if (rc) { | 384 | if (rc) { |
298 | rc = 0; | 385 | make_bad_inode(inode); |
299 | goto out_free_kmem; | 386 | return rc; |
300 | } | 387 | } |
301 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; | ||
302 | } | 388 | } |
303 | ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode); | 389 | |
304 | out_free_kmem: | 390 | if (inode->i_state & I_NEW) |
305 | kmem_cache_free(ecryptfs_header_cache_2, page_virt); | 391 | unlock_new_inode(inode); |
306 | goto out; | 392 | d_add(dentry, inode); |
307 | out_put: | 393 | |
308 | dput(lower_dentry); | ||
309 | mntput(lower_mnt); | ||
310 | d_drop(ecryptfs_dentry); | ||
311 | out: | ||
312 | if (put_lower) | ||
313 | ecryptfs_put_lower_file(ecryptfs_dentry->d_inode); | ||
314 | return rc; | 394 | return rc; |
315 | } | 395 | } |
316 | 396 | ||
@@ -353,12 +433,12 @@ static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode, | |||
353 | goto out_d_drop; | 433 | goto out_d_drop; |
354 | } | 434 | } |
355 | if (lower_dentry->d_inode) | 435 | if (lower_dentry->d_inode) |
356 | goto lookup_and_interpose; | 436 | goto interpose; |
357 | mount_crypt_stat = &ecryptfs_superblock_to_private( | 437 | mount_crypt_stat = &ecryptfs_superblock_to_private( |
358 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | 438 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
359 | if (!(mount_crypt_stat | 439 | if (!(mount_crypt_stat |
360 | && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) | 440 | && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) |
361 | goto lookup_and_interpose; | 441 | goto interpose; |
362 | dput(lower_dentry); | 442 | dput(lower_dentry); |
363 | rc = ecryptfs_encrypt_and_encode_filename( | 443 | rc = ecryptfs_encrypt_and_encode_filename( |
364 | &encrypted_and_encoded_name, &encrypted_and_encoded_name_size, | 444 | &encrypted_and_encoded_name, &encrypted_and_encoded_name_size, |
@@ -381,9 +461,9 @@ static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode, | |||
381 | encrypted_and_encoded_name); | 461 | encrypted_and_encoded_name); |
382 | goto out_d_drop; | 462 | goto out_d_drop; |
383 | } | 463 | } |
384 | lookup_and_interpose: | 464 | interpose: |
385 | rc = ecryptfs_lookup_and_interpose_lower(ecryptfs_dentry, lower_dentry, | 465 | rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry, |
386 | ecryptfs_dir_inode); | 466 | ecryptfs_dir_inode); |
387 | goto out; | 467 | goto out; |
388 | out_d_drop: | 468 | out_d_drop: |
389 | d_drop(ecryptfs_dentry); | 469 | d_drop(ecryptfs_dentry); |
@@ -411,7 +491,7 @@ static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir, | |||
411 | lower_new_dentry); | 491 | lower_new_dentry); |
412 | if (rc || !lower_new_dentry->d_inode) | 492 | if (rc || !lower_new_dentry->d_inode) |
413 | goto out_lock; | 493 | goto out_lock; |
414 | rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0); | 494 | rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb); |
415 | if (rc) | 495 | if (rc) |
416 | goto out_lock; | 496 | goto out_lock; |
417 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); | 497 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
@@ -478,7 +558,7 @@ static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry, | |||
478 | kfree(encoded_symname); | 558 | kfree(encoded_symname); |
479 | if (rc || !lower_dentry->d_inode) | 559 | if (rc || !lower_dentry->d_inode) |
480 | goto out_lock; | 560 | goto out_lock; |
481 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); | 561 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); |
482 | if (rc) | 562 | if (rc) |
483 | goto out_lock; | 563 | goto out_lock; |
484 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); | 564 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
@@ -502,7 +582,7 @@ static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |||
502 | rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode); | 582 | rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode); |
503 | if (rc || !lower_dentry->d_inode) | 583 | if (rc || !lower_dentry->d_inode) |
504 | goto out; | 584 | goto out; |
505 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); | 585 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); |
506 | if (rc) | 586 | if (rc) |
507 | goto out; | 587 | goto out; |
508 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); | 588 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
@@ -550,7 +630,7 @@ ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) | |||
550 | rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev); | 630 | rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev); |
551 | if (rc || !lower_dentry->d_inode) | 631 | if (rc || !lower_dentry->d_inode) |
552 | goto out; | 632 | goto out; |
553 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0); | 633 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); |
554 | if (rc) | 634 | if (rc) |
555 | goto out; | 635 | goto out; |
556 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); | 636 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
@@ -750,7 +830,7 @@ static int truncate_upper(struct dentry *dentry, struct iattr *ia, | |||
750 | lower_ia->ia_valid &= ~ATTR_SIZE; | 830 | lower_ia->ia_valid &= ~ATTR_SIZE; |
751 | return 0; | 831 | return 0; |
752 | } | 832 | } |
753 | rc = ecryptfs_get_lower_file(dentry); | 833 | rc = ecryptfs_get_lower_file(dentry, inode); |
754 | if (rc) | 834 | if (rc) |
755 | return rc; | 835 | return rc; |
756 | crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; | 836 | crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; |
@@ -906,7 +986,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia) | |||
906 | 986 | ||
907 | mount_crypt_stat = &ecryptfs_superblock_to_private( | 987 | mount_crypt_stat = &ecryptfs_superblock_to_private( |
908 | dentry->d_sb)->mount_crypt_stat; | 988 | dentry->d_sb)->mount_crypt_stat; |
909 | rc = ecryptfs_get_lower_file(dentry); | 989 | rc = ecryptfs_get_lower_file(dentry, inode); |
910 | if (rc) { | 990 | if (rc) { |
911 | mutex_unlock(&crypt_stat->cs_mutex); | 991 | mutex_unlock(&crypt_stat->cs_mutex); |
912 | goto out; | 992 | goto out; |
@@ -1079,21 +1159,6 @@ out: | |||
1079 | return rc; | 1159 | return rc; |
1080 | } | 1160 | } |
1081 | 1161 | ||
1082 | int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode) | ||
1083 | { | ||
1084 | if ((ecryptfs_inode_to_lower(inode) | ||
1085 | == (struct inode *)candidate_lower_inode)) | ||
1086 | return 1; | ||
1087 | else | ||
1088 | return 0; | ||
1089 | } | ||
1090 | |||
1091 | int ecryptfs_inode_set(struct inode *inode, void *lower_inode) | ||
1092 | { | ||
1093 | ecryptfs_init_inode(inode, (struct inode *)lower_inode); | ||
1094 | return 0; | ||
1095 | } | ||
1096 | |||
1097 | const struct inode_operations ecryptfs_symlink_iops = { | 1162 | const struct inode_operations ecryptfs_symlink_iops = { |
1098 | .readlink = ecryptfs_readlink, | 1163 | .readlink = ecryptfs_readlink, |
1099 | .follow_link = ecryptfs_follow_link, | 1164 | .follow_link = ecryptfs_follow_link, |