• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * eCryptfs: Linux filesystem encryption layer
4  *
5  * Copyright (C) 1997-2004 Erez Zadok
6  * Copyright (C) 2001-2004 Stony Brook University
7  * Copyright (C) 2004-2007 International Business Machines Corp.
8  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9  *              Michael C. Thompsion <mcthomps@us.ibm.com>
10  */
11 
12 #include <linux/file.h>
13 #include <linux/vmalloc.h>
14 #include <linux/pagemap.h>
15 #include <linux/dcache.h>
16 #include <linux/namei.h>
17 #include <linux/mount.h>
18 #include <linux/fs_stack.h>
19 #include <linux/slab.h>
20 #include <linux/xattr.h>
21 #include <linux/fileattr.h>
22 #include <asm/unaligned.h>
23 #include "ecryptfs_kernel.h"
24 
lock_parent(struct dentry * dentry,struct dentry ** lower_dentry,struct inode ** lower_dir)25 static int lock_parent(struct dentry *dentry,
26 		       struct dentry **lower_dentry,
27 		       struct inode **lower_dir)
28 {
29 	struct dentry *lower_dir_dentry;
30 
31 	lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
32 	*lower_dir = d_inode(lower_dir_dentry);
33 	*lower_dentry = ecryptfs_dentry_to_lower(dentry);
34 
35 	inode_lock_nested(*lower_dir, I_MUTEX_PARENT);
36 	return (*lower_dentry)->d_parent == lower_dir_dentry ? 0 : -EINVAL;
37 }
38 
ecryptfs_inode_test(struct inode * inode,void * lower_inode)39 static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
40 {
41 	return ecryptfs_inode_to_lower(inode) == lower_inode;
42 }
43 
ecryptfs_inode_set(struct inode * inode,void * opaque)44 static int ecryptfs_inode_set(struct inode *inode, void *opaque)
45 {
46 	struct inode *lower_inode = opaque;
47 
48 	ecryptfs_set_inode_lower(inode, lower_inode);
49 	fsstack_copy_attr_all(inode, lower_inode);
50 	/* i_size will be overwritten for encrypted regular files */
51 	fsstack_copy_inode_size(inode, lower_inode);
52 	inode->i_ino = lower_inode->i_ino;
53 	inode->i_mapping->a_ops = &ecryptfs_aops;
54 
55 	if (S_ISLNK(inode->i_mode))
56 		inode->i_op = &ecryptfs_symlink_iops;
57 	else if (S_ISDIR(inode->i_mode))
58 		inode->i_op = &ecryptfs_dir_iops;
59 	else
60 		inode->i_op = &ecryptfs_main_iops;
61 
62 	if (S_ISDIR(inode->i_mode))
63 		inode->i_fop = &ecryptfs_dir_fops;
64 	else if (special_file(inode->i_mode))
65 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
66 	else
67 		inode->i_fop = &ecryptfs_main_fops;
68 
69 	return 0;
70 }
71 
__ecryptfs_get_inode(struct inode * lower_inode,struct super_block * sb)72 static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
73 					  struct super_block *sb)
74 {
75 	struct inode *inode;
76 
77 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
78 		return ERR_PTR(-EXDEV);
79 
80 	/* Reject dealing with casefold directories. */
81 	if (IS_CASEFOLDED(lower_inode)) {
82 		pr_err_ratelimited("%s: Can't handle casefolded directory.\n",
83 				   __func__);
84 		return ERR_PTR(-EREMOTE);
85 	}
86 
87 	if (!igrab(lower_inode))
88 		return ERR_PTR(-ESTALE);
89 	inode = iget5_locked(sb, (unsigned long)lower_inode,
90 			     ecryptfs_inode_test, ecryptfs_inode_set,
91 			     lower_inode);
92 	if (!inode) {
93 		iput(lower_inode);
94 		return ERR_PTR(-EACCES);
95 	}
96 	if (!(inode->i_state & I_NEW))
97 		iput(lower_inode);
98 
99 	return inode;
100 }
101 
ecryptfs_get_inode(struct inode * lower_inode,struct super_block * sb)102 struct inode *ecryptfs_get_inode(struct inode *lower_inode,
103 				 struct super_block *sb)
104 {
105 	struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
106 
107 	if (!IS_ERR(inode) && (inode->i_state & I_NEW))
108 		unlock_new_inode(inode);
109 
110 	return inode;
111 }
112 
113 /**
114  * ecryptfs_interpose
115  * @lower_dentry: Existing dentry in the lower filesystem
116  * @dentry: ecryptfs' dentry
117  * @sb: ecryptfs's super_block
118  *
119  * Interposes upper and lower dentries.
120  *
121  * Returns zero on success; non-zero otherwise
122  */
ecryptfs_interpose(struct dentry * lower_dentry,struct dentry * dentry,struct super_block * sb)123 static int ecryptfs_interpose(struct dentry *lower_dentry,
124 			      struct dentry *dentry, struct super_block *sb)
125 {
126 	struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
127 
128 	if (IS_ERR(inode))
129 		return PTR_ERR(inode);
130 	d_instantiate(dentry, inode);
131 
132 	return 0;
133 }
134 
ecryptfs_do_unlink(struct inode * dir,struct dentry * dentry,struct inode * inode)135 static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
136 			      struct inode *inode)
137 {
138 	struct dentry *lower_dentry;
139 	struct inode *lower_dir;
140 	int rc;
141 
142 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
143 	dget(lower_dentry);	// don't even try to make the lower negative
144 	if (!rc) {
145 		if (d_unhashed(lower_dentry))
146 			rc = -EINVAL;
147 		else
148 			rc = vfs_unlink(&init_user_ns, lower_dir, lower_dentry,
149 					NULL);
150 	}
151 	if (rc) {
152 		printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
153 		goto out_unlock;
154 	}
155 	fsstack_copy_attr_times(dir, lower_dir);
156 	set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
157 	inode->i_ctime = dir->i_ctime;
158 out_unlock:
159 	dput(lower_dentry);
160 	inode_unlock(lower_dir);
161 	if (!rc)
162 		d_drop(dentry);
163 	return rc;
164 }
165 
166 /**
167  * ecryptfs_do_create
168  * @directory_inode: inode of the new file's dentry's parent in ecryptfs
169  * @ecryptfs_dentry: New file's dentry in ecryptfs
170  * @mode: The mode of the new file
171  *
172  * Creates the underlying file and the eCryptfs inode which will link to
173  * it. It will also update the eCryptfs directory inode to mimic the
174  * stat of the lower directory inode.
175  *
176  * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
177  */
178 static struct inode *
ecryptfs_do_create(struct inode * directory_inode,struct dentry * ecryptfs_dentry,umode_t mode)179 ecryptfs_do_create(struct inode *directory_inode,
180 		   struct dentry *ecryptfs_dentry, umode_t mode)
181 {
182 	int rc;
183 	struct dentry *lower_dentry;
184 	struct inode *lower_dir;
185 	struct inode *inode;
186 
187 	rc = lock_parent(ecryptfs_dentry, &lower_dentry, &lower_dir);
188 	if (!rc)
189 		rc = vfs_create(&init_user_ns, lower_dir,
190 				lower_dentry, mode, true);
191 	if (rc) {
192 		printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
193 		       "rc = [%d]\n", __func__, rc);
194 		inode = ERR_PTR(rc);
195 		goto out_lock;
196 	}
197 	inode = __ecryptfs_get_inode(d_inode(lower_dentry),
198 				     directory_inode->i_sb);
199 	if (IS_ERR(inode)) {
200 		vfs_unlink(&init_user_ns, lower_dir, lower_dentry, NULL);
201 		goto out_lock;
202 	}
203 	fsstack_copy_attr_times(directory_inode, lower_dir);
204 	fsstack_copy_inode_size(directory_inode, lower_dir);
205 out_lock:
206 	inode_unlock(lower_dir);
207 	return inode;
208 }
209 
210 /*
211  * ecryptfs_initialize_file
212  *
213  * Cause the file to be changed from a basic empty file to an ecryptfs
214  * file with a header and first data page.
215  *
216  * Returns zero on success
217  */
ecryptfs_initialize_file(struct dentry * ecryptfs_dentry,struct inode * ecryptfs_inode)218 int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
219 			     struct inode *ecryptfs_inode)
220 {
221 	struct ecryptfs_crypt_stat *crypt_stat =
222 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
223 	int rc = 0;
224 
225 	if (S_ISDIR(ecryptfs_inode->i_mode)) {
226 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
227 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
228 		goto out;
229 	}
230 	ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
231 	rc = ecryptfs_new_file_context(ecryptfs_inode);
232 	if (rc) {
233 		ecryptfs_printk(KERN_ERR, "Error creating new file "
234 				"context; rc = [%d]\n", rc);
235 		goto out;
236 	}
237 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
238 	if (rc) {
239 		printk(KERN_ERR "%s: Error attempting to initialize "
240 			"the lower file for the dentry with name "
241 			"[%pd]; rc = [%d]\n", __func__,
242 			ecryptfs_dentry, rc);
243 		goto out;
244 	}
245 	rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
246 	if (rc)
247 		printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
248 	ecryptfs_put_lower_file(ecryptfs_inode);
249 out:
250 	return rc;
251 }
252 
253 /*
254  * ecryptfs_create
255  * @mode: The mode of the new file.
256  *
257  * Creates a new file.
258  *
259  * Returns zero on success; non-zero on error condition
260  */
261 static int
ecryptfs_create(struct user_namespace * mnt_userns,struct inode * directory_inode,struct dentry * ecryptfs_dentry,umode_t mode,bool excl)262 ecryptfs_create(struct user_namespace *mnt_userns,
263 		struct inode *directory_inode, struct dentry *ecryptfs_dentry,
264 		umode_t mode, bool excl)
265 {
266 	struct inode *ecryptfs_inode;
267 	int rc;
268 
269 	ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
270 					    mode);
271 	if (IS_ERR(ecryptfs_inode)) {
272 		ecryptfs_printk(KERN_WARNING, "Failed to create file in"
273 				"lower filesystem\n");
274 		rc = PTR_ERR(ecryptfs_inode);
275 		goto out;
276 	}
277 	/* At this point, a file exists on "disk"; we need to make sure
278 	 * that this on disk file is prepared to be an ecryptfs file */
279 	rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
280 	if (rc) {
281 		ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
282 				   ecryptfs_inode);
283 		iget_failed(ecryptfs_inode);
284 		goto out;
285 	}
286 	d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
287 out:
288 	return rc;
289 }
290 
ecryptfs_i_size_read(struct dentry * dentry,struct inode * inode)291 static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
292 {
293 	struct ecryptfs_crypt_stat *crypt_stat;
294 	int rc;
295 
296 	rc = ecryptfs_get_lower_file(dentry, inode);
297 	if (rc) {
298 		printk(KERN_ERR "%s: Error attempting to initialize "
299 			"the lower file for the dentry with name "
300 			"[%pd]; rc = [%d]\n", __func__,
301 			dentry, rc);
302 		return rc;
303 	}
304 
305 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
306 	/* TODO: lock for crypt_stat comparison */
307 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
308 		ecryptfs_set_default_sizes(crypt_stat);
309 
310 	rc = ecryptfs_read_and_validate_header_region(inode);
311 	ecryptfs_put_lower_file(inode);
312 	if (rc) {
313 		rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
314 		if (!rc)
315 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
316 	}
317 
318 	/* Must return 0 to allow non-eCryptfs files to be looked up, too */
319 	return 0;
320 }
321 
322 /*
323  * ecryptfs_lookup_interpose - Dentry interposition for a lookup
324  */
ecryptfs_lookup_interpose(struct dentry * dentry,struct dentry * lower_dentry)325 static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
326 				     struct dentry *lower_dentry)
327 {
328 	const struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
329 	struct inode *inode, *lower_inode;
330 	struct ecryptfs_dentry_info *dentry_info;
331 	int rc = 0;
332 
333 	dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
334 	if (!dentry_info) {
335 		dput(lower_dentry);
336 		return ERR_PTR(-ENOMEM);
337 	}
338 
339 	fsstack_copy_attr_atime(d_inode(dentry->d_parent),
340 				d_inode(path->dentry));
341 	BUG_ON(!d_count(lower_dentry));
342 
343 	ecryptfs_set_dentry_private(dentry, dentry_info);
344 	dentry_info->lower_path.mnt = mntget(path->mnt);
345 	dentry_info->lower_path.dentry = lower_dentry;
346 
347 	/*
348 	 * negative dentry can go positive under us here - its parent is not
349 	 * locked.  That's OK and that could happen just as we return from
350 	 * ecryptfs_lookup() anyway.  Just need to be careful and fetch
351 	 * ->d_inode only once - it's not stable here.
352 	 */
353 	lower_inode = READ_ONCE(lower_dentry->d_inode);
354 
355 	if (!lower_inode) {
356 		/* We want to add because we couldn't find in lower */
357 		d_add(dentry, NULL);
358 		return NULL;
359 	}
360 	inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
361 	if (IS_ERR(inode)) {
362 		printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
363 		       __func__, PTR_ERR(inode));
364 		return ERR_CAST(inode);
365 	}
366 	if (S_ISREG(inode->i_mode)) {
367 		rc = ecryptfs_i_size_read(dentry, inode);
368 		if (rc) {
369 			make_bad_inode(inode);
370 			return ERR_PTR(rc);
371 		}
372 	}
373 
374 	if (inode->i_state & I_NEW)
375 		unlock_new_inode(inode);
376 	return d_splice_alias(inode, dentry);
377 }
378 
379 /**
380  * ecryptfs_lookup
381  * @ecryptfs_dir_inode: The eCryptfs directory inode
382  * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
383  * @flags: lookup flags
384  *
385  * Find a file on disk. If the file does not exist, then we'll add it to the
386  * dentry cache and continue on to read it from the disk.
387  */
ecryptfs_lookup(struct inode * ecryptfs_dir_inode,struct dentry * ecryptfs_dentry,unsigned int flags)388 static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
389 				      struct dentry *ecryptfs_dentry,
390 				      unsigned int flags)
391 {
392 	char *encrypted_and_encoded_name = NULL;
393 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
394 	struct dentry *lower_dir_dentry, *lower_dentry;
395 	const char *name = ecryptfs_dentry->d_name.name;
396 	size_t len = ecryptfs_dentry->d_name.len;
397 	struct dentry *res;
398 	int rc = 0;
399 
400 	lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
401 
402 	mount_crypt_stat = &ecryptfs_superblock_to_private(
403 				ecryptfs_dentry->d_sb)->mount_crypt_stat;
404 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
405 		rc = ecryptfs_encrypt_and_encode_filename(
406 			&encrypted_and_encoded_name, &len,
407 			mount_crypt_stat, name, len);
408 		if (rc) {
409 			printk(KERN_ERR "%s: Error attempting to encrypt and encode "
410 			       "filename; rc = [%d]\n", __func__, rc);
411 			return ERR_PTR(rc);
412 		}
413 		name = encrypted_and_encoded_name;
414 	}
415 
416 	lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
417 	if (IS_ERR(lower_dentry)) {
418 		ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
419 				"[%ld] on lower_dentry = [%s]\n", __func__,
420 				PTR_ERR(lower_dentry),
421 				name);
422 		res = ERR_CAST(lower_dentry);
423 	} else {
424 		res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
425 	}
426 	kfree(encrypted_and_encoded_name);
427 	return res;
428 }
429 
ecryptfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)430 static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
431 			 struct dentry *new_dentry)
432 {
433 	struct dentry *lower_old_dentry;
434 	struct dentry *lower_new_dentry;
435 	struct inode *lower_dir;
436 	u64 file_size_save;
437 	int rc;
438 
439 	file_size_save = i_size_read(d_inode(old_dentry));
440 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
441 	rc = lock_parent(new_dentry, &lower_new_dentry, &lower_dir);
442 	if (!rc)
443 		rc = vfs_link(lower_old_dentry, &init_user_ns, lower_dir,
444 			      lower_new_dentry, NULL);
445 	if (rc || d_really_is_negative(lower_new_dentry))
446 		goto out_lock;
447 	rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
448 	if (rc)
449 		goto out_lock;
450 	fsstack_copy_attr_times(dir, lower_dir);
451 	fsstack_copy_inode_size(dir, lower_dir);
452 	set_nlink(d_inode(old_dentry),
453 		  ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
454 	i_size_write(d_inode(new_dentry), file_size_save);
455 out_lock:
456 	inode_unlock(lower_dir);
457 	return rc;
458 }
459 
ecryptfs_unlink(struct inode * dir,struct dentry * dentry)460 static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
461 {
462 	return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
463 }
464 
ecryptfs_symlink(struct user_namespace * mnt_userns,struct inode * dir,struct dentry * dentry,const char * symname)465 static int ecryptfs_symlink(struct user_namespace *mnt_userns,
466 			    struct inode *dir, struct dentry *dentry,
467 			    const char *symname)
468 {
469 	int rc;
470 	struct dentry *lower_dentry;
471 	struct inode *lower_dir;
472 	char *encoded_symname;
473 	size_t encoded_symlen;
474 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
475 
476 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
477 	if (rc)
478 		goto out_lock;
479 	mount_crypt_stat = &ecryptfs_superblock_to_private(
480 		dir->i_sb)->mount_crypt_stat;
481 	rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
482 						  &encoded_symlen,
483 						  mount_crypt_stat, symname,
484 						  strlen(symname));
485 	if (rc)
486 		goto out_lock;
487 	rc = vfs_symlink(&init_user_ns, lower_dir, lower_dentry,
488 			 encoded_symname);
489 	kfree(encoded_symname);
490 	if (rc || d_really_is_negative(lower_dentry))
491 		goto out_lock;
492 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
493 	if (rc)
494 		goto out_lock;
495 	fsstack_copy_attr_times(dir, lower_dir);
496 	fsstack_copy_inode_size(dir, lower_dir);
497 out_lock:
498 	inode_unlock(lower_dir);
499 	if (d_really_is_negative(dentry))
500 		d_drop(dentry);
501 	return rc;
502 }
503 
ecryptfs_mkdir(struct user_namespace * mnt_userns,struct inode * dir,struct dentry * dentry,umode_t mode)504 static int ecryptfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
505 			  struct dentry *dentry, umode_t mode)
506 {
507 	int rc;
508 	struct dentry *lower_dentry;
509 	struct inode *lower_dir;
510 
511 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
512 	if (!rc)
513 		rc = vfs_mkdir(&init_user_ns, lower_dir,
514 			       lower_dentry, mode);
515 	if (rc || d_really_is_negative(lower_dentry))
516 		goto out;
517 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
518 	if (rc)
519 		goto out;
520 	fsstack_copy_attr_times(dir, lower_dir);
521 	fsstack_copy_inode_size(dir, lower_dir);
522 	set_nlink(dir, lower_dir->i_nlink);
523 out:
524 	inode_unlock(lower_dir);
525 	if (d_really_is_negative(dentry))
526 		d_drop(dentry);
527 	return rc;
528 }
529 
ecryptfs_rmdir(struct inode * dir,struct dentry * dentry)530 static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
531 {
532 	struct dentry *lower_dentry;
533 	struct inode *lower_dir;
534 	int rc;
535 
536 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
537 	dget(lower_dentry);	// don't even try to make the lower negative
538 	if (!rc) {
539 		if (d_unhashed(lower_dentry))
540 			rc = -EINVAL;
541 		else
542 			rc = vfs_rmdir(&init_user_ns, lower_dir, lower_dentry);
543 	}
544 	if (!rc) {
545 		clear_nlink(d_inode(dentry));
546 		fsstack_copy_attr_times(dir, lower_dir);
547 		set_nlink(dir, lower_dir->i_nlink);
548 	}
549 	dput(lower_dentry);
550 	inode_unlock(lower_dir);
551 	if (!rc)
552 		d_drop(dentry);
553 	return rc;
554 }
555 
556 static int
ecryptfs_mknod(struct user_namespace * mnt_userns,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)557 ecryptfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
558 	       struct dentry *dentry, umode_t mode, dev_t dev)
559 {
560 	int rc;
561 	struct dentry *lower_dentry;
562 	struct inode *lower_dir;
563 
564 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
565 	if (!rc)
566 		rc = vfs_mknod(&init_user_ns, lower_dir,
567 			       lower_dentry, mode, dev);
568 	if (rc || d_really_is_negative(lower_dentry))
569 		goto out;
570 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
571 	if (rc)
572 		goto out;
573 	fsstack_copy_attr_times(dir, lower_dir);
574 	fsstack_copy_inode_size(dir, lower_dir);
575 out:
576 	inode_unlock(lower_dir);
577 	if (d_really_is_negative(dentry))
578 		d_drop(dentry);
579 	return rc;
580 }
581 
582 static int
ecryptfs_rename(struct user_namespace * mnt_userns,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)583 ecryptfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
584 		struct dentry *old_dentry, struct inode *new_dir,
585 		struct dentry *new_dentry, unsigned int flags)
586 {
587 	int rc;
588 	struct dentry *lower_old_dentry;
589 	struct dentry *lower_new_dentry;
590 	struct dentry *lower_old_dir_dentry;
591 	struct dentry *lower_new_dir_dentry;
592 	struct dentry *trap;
593 	struct inode *target_inode;
594 	struct renamedata rd = {};
595 
596 	if (flags)
597 		return -EINVAL;
598 
599 	lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent);
600 	lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent);
601 
602 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
603 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
604 
605 	target_inode = d_inode(new_dentry);
606 
607 	trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
608 	dget(lower_new_dentry);
609 	rc = -EINVAL;
610 	if (lower_old_dentry->d_parent != lower_old_dir_dentry)
611 		goto out_lock;
612 	if (lower_new_dentry->d_parent != lower_new_dir_dentry)
613 		goto out_lock;
614 	if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry))
615 		goto out_lock;
616 	/* source should not be ancestor of target */
617 	if (trap == lower_old_dentry)
618 		goto out_lock;
619 	/* target should not be ancestor of source */
620 	if (trap == lower_new_dentry) {
621 		rc = -ENOTEMPTY;
622 		goto out_lock;
623 	}
624 
625 	rd.old_mnt_userns	= &init_user_ns;
626 	rd.old_dir		= d_inode(lower_old_dir_dentry);
627 	rd.old_dentry		= lower_old_dentry;
628 	rd.new_mnt_userns	= &init_user_ns;
629 	rd.new_dir		= d_inode(lower_new_dir_dentry);
630 	rd.new_dentry		= lower_new_dentry;
631 	rc = vfs_rename(&rd);
632 	if (rc)
633 		goto out_lock;
634 	if (target_inode)
635 		fsstack_copy_attr_all(target_inode,
636 				      ecryptfs_inode_to_lower(target_inode));
637 	fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
638 	if (new_dir != old_dir)
639 		fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
640 out_lock:
641 	dput(lower_new_dentry);
642 	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
643 	return rc;
644 }
645 
ecryptfs_readlink_lower(struct dentry * dentry,size_t * bufsiz)646 static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
647 {
648 	DEFINE_DELAYED_CALL(done);
649 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
650 	const char *link;
651 	char *buf;
652 	int rc;
653 
654 	link = vfs_get_link(lower_dentry, &done);
655 	if (IS_ERR(link))
656 		return ERR_CAST(link);
657 
658 	rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
659 						  link, strlen(link));
660 	do_delayed_call(&done);
661 	if (rc)
662 		return ERR_PTR(rc);
663 
664 	return buf;
665 }
666 
ecryptfs_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)667 static const char *ecryptfs_get_link(struct dentry *dentry,
668 				     struct inode *inode,
669 				     struct delayed_call *done)
670 {
671 	size_t len;
672 	char *buf;
673 
674 	if (!dentry)
675 		return ERR_PTR(-ECHILD);
676 
677 	buf = ecryptfs_readlink_lower(dentry, &len);
678 	if (IS_ERR(buf))
679 		return buf;
680 	fsstack_copy_attr_atime(d_inode(dentry),
681 				d_inode(ecryptfs_dentry_to_lower(dentry)));
682 	buf[len] = '\0';
683 	set_delayed_call(done, kfree_link, buf);
684 	return buf;
685 }
686 
687 /**
688  * upper_size_to_lower_size
689  * @crypt_stat: Crypt_stat associated with file
690  * @upper_size: Size of the upper file
691  *
692  * Calculate the required size of the lower file based on the
693  * specified size of the upper file. This calculation is based on the
694  * number of headers in the underlying file and the extent size.
695  *
696  * Returns Calculated size of the lower file.
697  */
698 static loff_t
upper_size_to_lower_size(struct ecryptfs_crypt_stat * crypt_stat,loff_t upper_size)699 upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
700 			 loff_t upper_size)
701 {
702 	loff_t lower_size;
703 
704 	lower_size = ecryptfs_lower_header_size(crypt_stat);
705 	if (upper_size != 0) {
706 		loff_t num_extents;
707 
708 		num_extents = upper_size >> crypt_stat->extent_shift;
709 		if (upper_size & ~crypt_stat->extent_mask)
710 			num_extents++;
711 		lower_size += (num_extents * crypt_stat->extent_size);
712 	}
713 	return lower_size;
714 }
715 
716 /**
717  * truncate_upper
718  * @dentry: The ecryptfs layer dentry
719  * @ia: Address of the ecryptfs inode's attributes
720  * @lower_ia: Address of the lower inode's attributes
721  *
722  * Function to handle truncations modifying the size of the file. Note
723  * that the file sizes are interpolated. When expanding, we are simply
724  * writing strings of 0's out. When truncating, we truncate the upper
725  * inode and update the lower_ia according to the page index
726  * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
727  * the caller must use lower_ia in a call to notify_change() to perform
728  * the truncation of the lower inode.
729  *
730  * Returns zero on success; non-zero otherwise
731  */
truncate_upper(struct dentry * dentry,struct iattr * ia,struct iattr * lower_ia)732 static int truncate_upper(struct dentry *dentry, struct iattr *ia,
733 			  struct iattr *lower_ia)
734 {
735 	int rc = 0;
736 	struct inode *inode = d_inode(dentry);
737 	struct ecryptfs_crypt_stat *crypt_stat;
738 	loff_t i_size = i_size_read(inode);
739 	loff_t lower_size_before_truncate;
740 	loff_t lower_size_after_truncate;
741 
742 	if (unlikely((ia->ia_size == i_size))) {
743 		lower_ia->ia_valid &= ~ATTR_SIZE;
744 		return 0;
745 	}
746 	rc = ecryptfs_get_lower_file(dentry, inode);
747 	if (rc)
748 		return rc;
749 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
750 	/* Switch on growing or shrinking file */
751 	if (ia->ia_size > i_size) {
752 		char zero[] = { 0x00 };
753 
754 		lower_ia->ia_valid &= ~ATTR_SIZE;
755 		/* Write a single 0 at the last position of the file;
756 		 * this triggers code that will fill in 0's throughout
757 		 * the intermediate portion of the previous end of the
758 		 * file and the new and of the file */
759 		rc = ecryptfs_write(inode, zero,
760 				    (ia->ia_size - 1), 1);
761 	} else { /* ia->ia_size < i_size_read(inode) */
762 		/* We're chopping off all the pages down to the page
763 		 * in which ia->ia_size is located. Fill in the end of
764 		 * that page from (ia->ia_size & ~PAGE_MASK) to
765 		 * PAGE_SIZE with zeros. */
766 		size_t num_zeros = (PAGE_SIZE
767 				    - (ia->ia_size & ~PAGE_MASK));
768 
769 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
770 			truncate_setsize(inode, ia->ia_size);
771 			lower_ia->ia_size = ia->ia_size;
772 			lower_ia->ia_valid |= ATTR_SIZE;
773 			goto out;
774 		}
775 		if (num_zeros) {
776 			char *zeros_virt;
777 
778 			zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
779 			if (!zeros_virt) {
780 				rc = -ENOMEM;
781 				goto out;
782 			}
783 			rc = ecryptfs_write(inode, zeros_virt,
784 					    ia->ia_size, num_zeros);
785 			kfree(zeros_virt);
786 			if (rc) {
787 				printk(KERN_ERR "Error attempting to zero out "
788 				       "the remainder of the end page on "
789 				       "reducing truncate; rc = [%d]\n", rc);
790 				goto out;
791 			}
792 		}
793 		truncate_setsize(inode, ia->ia_size);
794 		rc = ecryptfs_write_inode_size_to_metadata(inode);
795 		if (rc) {
796 			printk(KERN_ERR	"Problem with "
797 			       "ecryptfs_write_inode_size_to_metadata; "
798 			       "rc = [%d]\n", rc);
799 			goto out;
800 		}
801 		/* We are reducing the size of the ecryptfs file, and need to
802 		 * know if we need to reduce the size of the lower file. */
803 		lower_size_before_truncate =
804 		    upper_size_to_lower_size(crypt_stat, i_size);
805 		lower_size_after_truncate =
806 		    upper_size_to_lower_size(crypt_stat, ia->ia_size);
807 		if (lower_size_after_truncate < lower_size_before_truncate) {
808 			lower_ia->ia_size = lower_size_after_truncate;
809 			lower_ia->ia_valid |= ATTR_SIZE;
810 		} else
811 			lower_ia->ia_valid &= ~ATTR_SIZE;
812 	}
813 out:
814 	ecryptfs_put_lower_file(inode);
815 	return rc;
816 }
817 
ecryptfs_inode_newsize_ok(struct inode * inode,loff_t offset)818 static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
819 {
820 	struct ecryptfs_crypt_stat *crypt_stat;
821 	loff_t lower_oldsize, lower_newsize;
822 
823 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
824 	lower_oldsize = upper_size_to_lower_size(crypt_stat,
825 						 i_size_read(inode));
826 	lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
827 	if (lower_newsize > lower_oldsize) {
828 		/*
829 		 * The eCryptfs inode and the new *lower* size are mixed here
830 		 * because we may not have the lower i_mutex held and/or it may
831 		 * not be appropriate to call inode_newsize_ok() with inodes
832 		 * from other filesystems.
833 		 */
834 		return inode_newsize_ok(inode, lower_newsize);
835 	}
836 
837 	return 0;
838 }
839 
840 /**
841  * ecryptfs_truncate
842  * @dentry: The ecryptfs layer dentry
843  * @new_length: The length to expand the file to
844  *
845  * Simple function that handles the truncation of an eCryptfs inode and
846  * its corresponding lower inode.
847  *
848  * Returns zero on success; non-zero otherwise
849  */
ecryptfs_truncate(struct dentry * dentry,loff_t new_length)850 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
851 {
852 	struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
853 	struct iattr lower_ia = { .ia_valid = 0 };
854 	int rc;
855 
856 	rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
857 	if (rc)
858 		return rc;
859 
860 	rc = truncate_upper(dentry, &ia, &lower_ia);
861 	if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
862 		struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
863 
864 		inode_lock(d_inode(lower_dentry));
865 		rc = notify_change(&init_user_ns, lower_dentry,
866 				   &lower_ia, NULL);
867 		inode_unlock(d_inode(lower_dentry));
868 	}
869 	return rc;
870 }
871 
872 static int
ecryptfs_permission(struct user_namespace * mnt_userns,struct inode * inode,int mask)873 ecryptfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
874 		    int mask)
875 {
876 	return inode_permission(&init_user_ns,
877 				ecryptfs_inode_to_lower(inode), mask);
878 }
879 
880 /**
881  * ecryptfs_setattr
882  * @mnt_userns: user namespace of the target mount
883  * @dentry: dentry handle to the inode to modify
884  * @ia: Structure with flags of what to change and values
885  *
886  * Updates the metadata of an inode. If the update is to the size
887  * i.e. truncation, then ecryptfs_truncate will handle the size modification
888  * of both the ecryptfs inode and the lower inode.
889  *
890  * All other metadata changes will be passed right to the lower filesystem,
891  * and we will just update our inode to look like the lower.
892  */
ecryptfs_setattr(struct user_namespace * mnt_userns,struct dentry * dentry,struct iattr * ia)893 static int ecryptfs_setattr(struct user_namespace *mnt_userns,
894 			    struct dentry *dentry, struct iattr *ia)
895 {
896 	int rc = 0;
897 	struct dentry *lower_dentry;
898 	struct iattr lower_ia;
899 	struct inode *inode;
900 	struct inode *lower_inode;
901 	struct ecryptfs_crypt_stat *crypt_stat;
902 
903 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
904 	if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
905 		rc = ecryptfs_init_crypt_stat(crypt_stat);
906 		if (rc)
907 			return rc;
908 	}
909 	inode = d_inode(dentry);
910 	lower_inode = ecryptfs_inode_to_lower(inode);
911 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
912 	mutex_lock(&crypt_stat->cs_mutex);
913 	if (d_is_dir(dentry))
914 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
915 	else if (d_is_reg(dentry)
916 		 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
917 		     || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
918 		struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
919 
920 		mount_crypt_stat = &ecryptfs_superblock_to_private(
921 			dentry->d_sb)->mount_crypt_stat;
922 		rc = ecryptfs_get_lower_file(dentry, inode);
923 		if (rc) {
924 			mutex_unlock(&crypt_stat->cs_mutex);
925 			goto out;
926 		}
927 		rc = ecryptfs_read_metadata(dentry);
928 		ecryptfs_put_lower_file(inode);
929 		if (rc) {
930 			if (!(mount_crypt_stat->flags
931 			      & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
932 				rc = -EIO;
933 				printk(KERN_WARNING "Either the lower file "
934 				       "is not in a valid eCryptfs format, "
935 				       "or the key could not be retrieved. "
936 				       "Plaintext passthrough mode is not "
937 				       "enabled; returning -EIO\n");
938 				mutex_unlock(&crypt_stat->cs_mutex);
939 				goto out;
940 			}
941 			rc = 0;
942 			crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
943 					       | ECRYPTFS_ENCRYPTED);
944 		}
945 	}
946 	mutex_unlock(&crypt_stat->cs_mutex);
947 
948 	rc = setattr_prepare(&init_user_ns, dentry, ia);
949 	if (rc)
950 		goto out;
951 	if (ia->ia_valid & ATTR_SIZE) {
952 		rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
953 		if (rc)
954 			goto out;
955 	}
956 
957 	memcpy(&lower_ia, ia, sizeof(lower_ia));
958 	if (ia->ia_valid & ATTR_FILE)
959 		lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
960 	if (ia->ia_valid & ATTR_SIZE) {
961 		rc = truncate_upper(dentry, ia, &lower_ia);
962 		if (rc < 0)
963 			goto out;
964 	}
965 
966 	/*
967 	 * mode change is for clearing setuid/setgid bits. Allow lower fs
968 	 * to interpret this in its own way.
969 	 */
970 	if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
971 		lower_ia.ia_valid &= ~ATTR_MODE;
972 
973 	inode_lock(d_inode(lower_dentry));
974 	rc = notify_change(&init_user_ns, lower_dentry, &lower_ia, NULL);
975 	inode_unlock(d_inode(lower_dentry));
976 out:
977 	fsstack_copy_attr_all(inode, lower_inode);
978 	return rc;
979 }
980 
ecryptfs_getattr_link(struct user_namespace * mnt_userns,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)981 static int ecryptfs_getattr_link(struct user_namespace *mnt_userns,
982 				 const struct path *path, struct kstat *stat,
983 				 u32 request_mask, unsigned int flags)
984 {
985 	struct dentry *dentry = path->dentry;
986 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
987 	int rc = 0;
988 
989 	mount_crypt_stat = &ecryptfs_superblock_to_private(
990 						dentry->d_sb)->mount_crypt_stat;
991 	generic_fillattr(&init_user_ns, d_inode(dentry), stat);
992 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
993 		char *target;
994 		size_t targetsiz;
995 
996 		target = ecryptfs_readlink_lower(dentry, &targetsiz);
997 		if (!IS_ERR(target)) {
998 			kfree(target);
999 			stat->size = targetsiz;
1000 		} else {
1001 			rc = PTR_ERR(target);
1002 		}
1003 	}
1004 	return rc;
1005 }
1006 
ecryptfs_getattr(struct user_namespace * mnt_userns,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)1007 static int ecryptfs_getattr(struct user_namespace *mnt_userns,
1008 			    const struct path *path, struct kstat *stat,
1009 			    u32 request_mask, unsigned int flags)
1010 {
1011 	struct dentry *dentry = path->dentry;
1012 	struct kstat lower_stat;
1013 	int rc;
1014 
1015 	rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat,
1016 			 request_mask, flags);
1017 	if (!rc) {
1018 		fsstack_copy_attr_all(d_inode(dentry),
1019 				      ecryptfs_inode_to_lower(d_inode(dentry)));
1020 		generic_fillattr(&init_user_ns, d_inode(dentry), stat);
1021 		stat->blocks = lower_stat.blocks;
1022 	}
1023 	return rc;
1024 }
1025 
1026 int
ecryptfs_setxattr(struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)1027 ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
1028 		  const char *name, const void *value,
1029 		  size_t size, int flags)
1030 {
1031 	int rc;
1032 	struct dentry *lower_dentry;
1033 	struct inode *lower_inode;
1034 
1035 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
1036 	lower_inode = d_inode(lower_dentry);
1037 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1038 		rc = -EOPNOTSUPP;
1039 		goto out;
1040 	}
1041 	inode_lock(lower_inode);
1042 	rc = __vfs_setxattr_locked(&init_user_ns, lower_dentry, name, value, size, flags, NULL);
1043 	inode_unlock(lower_inode);
1044 	if (!rc && inode)
1045 		fsstack_copy_attr_all(inode, lower_inode);
1046 out:
1047 	return rc;
1048 }
1049 
1050 ssize_t
ecryptfs_getxattr_lower(struct dentry * lower_dentry,struct inode * lower_inode,const char * name,void * value,size_t size)1051 ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
1052 			const char *name, void *value, size_t size)
1053 {
1054 	int rc;
1055 
1056 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1057 		rc = -EOPNOTSUPP;
1058 		goto out;
1059 	}
1060 	inode_lock(lower_inode);
1061 	rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
1062 	inode_unlock(lower_inode);
1063 out:
1064 	return rc;
1065 }
1066 
1067 static ssize_t
ecryptfs_getxattr(struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)1068 ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
1069 		  const char *name, void *value, size_t size)
1070 {
1071 	return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1072 				       ecryptfs_inode_to_lower(inode),
1073 				       name, value, size);
1074 }
1075 
1076 static ssize_t
ecryptfs_listxattr(struct dentry * dentry,char * list,size_t size)1077 ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1078 {
1079 	int rc = 0;
1080 	struct dentry *lower_dentry;
1081 
1082 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
1083 	if (!d_inode(lower_dentry)->i_op->listxattr) {
1084 		rc = -EOPNOTSUPP;
1085 		goto out;
1086 	}
1087 	inode_lock(d_inode(lower_dentry));
1088 	rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
1089 	inode_unlock(d_inode(lower_dentry));
1090 out:
1091 	return rc;
1092 }
1093 
ecryptfs_removexattr(struct dentry * dentry,struct inode * inode,const char * name)1094 static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
1095 				const char *name)
1096 {
1097 	int rc;
1098 	struct dentry *lower_dentry;
1099 	struct inode *lower_inode;
1100 
1101 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
1102 	lower_inode = ecryptfs_inode_to_lower(inode);
1103 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1104 		rc = -EOPNOTSUPP;
1105 		goto out;
1106 	}
1107 	inode_lock(lower_inode);
1108 	rc = __vfs_removexattr(&init_user_ns, lower_dentry, name);
1109 	inode_unlock(lower_inode);
1110 out:
1111 	return rc;
1112 }
1113 
ecryptfs_fileattr_get(struct dentry * dentry,struct fileattr * fa)1114 static int ecryptfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
1115 {
1116 	return vfs_fileattr_get(ecryptfs_dentry_to_lower(dentry), fa);
1117 }
1118 
ecryptfs_fileattr_set(struct user_namespace * mnt_userns,struct dentry * dentry,struct fileattr * fa)1119 static int ecryptfs_fileattr_set(struct user_namespace *mnt_userns,
1120 				 struct dentry *dentry, struct fileattr *fa)
1121 {
1122 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
1123 	int rc;
1124 
1125 	rc = vfs_fileattr_set(&init_user_ns, lower_dentry, fa);
1126 	fsstack_copy_attr_all(d_inode(dentry), d_inode(lower_dentry));
1127 
1128 	return rc;
1129 }
1130 
1131 const struct inode_operations ecryptfs_symlink_iops = {
1132 	.get_link = ecryptfs_get_link,
1133 	.permission = ecryptfs_permission,
1134 	.setattr = ecryptfs_setattr,
1135 	.getattr = ecryptfs_getattr_link,
1136 	.listxattr = ecryptfs_listxattr,
1137 };
1138 
1139 const struct inode_operations ecryptfs_dir_iops = {
1140 	.create = ecryptfs_create,
1141 	.lookup = ecryptfs_lookup,
1142 	.link = ecryptfs_link,
1143 	.unlink = ecryptfs_unlink,
1144 	.symlink = ecryptfs_symlink,
1145 	.mkdir = ecryptfs_mkdir,
1146 	.rmdir = ecryptfs_rmdir,
1147 	.mknod = ecryptfs_mknod,
1148 	.rename = ecryptfs_rename,
1149 	.permission = ecryptfs_permission,
1150 	.setattr = ecryptfs_setattr,
1151 	.listxattr = ecryptfs_listxattr,
1152 	.fileattr_get = ecryptfs_fileattr_get,
1153 	.fileattr_set = ecryptfs_fileattr_set,
1154 };
1155 
1156 const struct inode_operations ecryptfs_main_iops = {
1157 	.permission = ecryptfs_permission,
1158 	.setattr = ecryptfs_setattr,
1159 	.getattr = ecryptfs_getattr,
1160 	.listxattr = ecryptfs_listxattr,
1161 	.fileattr_get = ecryptfs_fileattr_get,
1162 	.fileattr_set = ecryptfs_fileattr_set,
1163 };
1164 
ecryptfs_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * buffer,size_t size)1165 static int ecryptfs_xattr_get(const struct xattr_handler *handler,
1166 			      struct dentry *dentry, struct inode *inode,
1167 			      const char *name, void *buffer, size_t size)
1168 {
1169 	return ecryptfs_getxattr(dentry, inode, name, buffer, size);
1170 }
1171 
ecryptfs_xattr_set(const struct xattr_handler * handler,struct user_namespace * mnt_userns,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)1172 static int ecryptfs_xattr_set(const struct xattr_handler *handler,
1173 			      struct user_namespace *mnt_userns,
1174 			      struct dentry *dentry, struct inode *inode,
1175 			      const char *name, const void *value, size_t size,
1176 			      int flags)
1177 {
1178 	if (value)
1179 		return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
1180 	else {
1181 		BUG_ON(flags != XATTR_REPLACE);
1182 		return ecryptfs_removexattr(dentry, inode, name);
1183 	}
1184 }
1185 
1186 static const struct xattr_handler ecryptfs_xattr_handler = {
1187 	.prefix = "",  /* match anything */
1188 	.get = ecryptfs_xattr_get,
1189 	.set = ecryptfs_xattr_set,
1190 };
1191 
1192 const struct xattr_handler *ecryptfs_xattr_handlers[] = {
1193 	&ecryptfs_xattr_handler,
1194 	NULL
1195 };
1196