• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * fs/sdcardfs/inode.c
3  *
4  * Copyright (c) 2013 Samsung Electronics Co. Ltd
5  *   Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6  *               Sunghwan Yun, Sungjong Seo
7  *
8  * This program has been developed as a stackable file system based on
9  * the WrapFS which written by
10  *
11  * Copyright (c) 1998-2011 Erez Zadok
12  * Copyright (c) 2009     Shrikar Archak
13  * Copyright (c) 2003-2011 Stony Brook University
14  * Copyright (c) 2003-2011 The Research Foundation of SUNY
15  *
16  * This file is dual licensed.  It may be redistributed and/or modified
17  * under the terms of the Apache 2.0 License OR version 2 of the GNU
18  * General Public License.
19  */
20 
21 #include "sdcardfs.h"
22 #include <linux/fs_struct.h>
23 #include <linux/ratelimit.h>
24 
25 /* Do not directly use this function. Use OVERRIDE_CRED() instead. */
override_fsids(struct sdcardfs_sb_info * sbi,struct sdcardfs_inode_data * data)26 const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
27 		struct sdcardfs_inode_data *data)
28 {
29 	struct cred *cred;
30 	const struct cred *old_cred;
31 	uid_t uid;
32 
33 	cred = prepare_creds();
34 	if (!cred)
35 		return NULL;
36 
37 	if (sbi->options.gid_derivation) {
38 		if (data->under_obb)
39 			uid = AID_MEDIA_OBB;
40 		else
41 			uid = multiuser_get_uid(data->userid, sbi->options.fs_low_uid);
42 	} else {
43 		uid = sbi->options.fs_low_uid;
44 	}
45 	cred->fsuid = make_kuid(&init_user_ns, uid);
46 	cred->fsgid = make_kgid(&init_user_ns, sbi->options.fs_low_gid);
47 
48 	old_cred = override_creds(cred);
49 
50 	return old_cred;
51 }
52 
53 /* Do not directly use this function, use REVERT_CRED() instead. */
revert_fsids(const struct cred * old_cred)54 void revert_fsids(const struct cred *old_cred)
55 {
56 	const struct cred *cur_cred;
57 
58 	cur_cred = current->cred;
59 	revert_creds(old_cred);
60 	put_cred(cur_cred);
61 }
62 
sdcardfs_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)63 static int sdcardfs_create(struct inode *dir, struct dentry *dentry,
64 			 umode_t mode, bool want_excl)
65 {
66 	int err;
67 	struct dentry *lower_dentry;
68 	struct vfsmount *lower_dentry_mnt;
69 	struct dentry *lower_parent_dentry = NULL;
70 	struct path lower_path;
71 	const struct cred *saved_cred = NULL;
72 	struct fs_struct *saved_fs;
73 	struct fs_struct *copied_fs;
74 
75 	if (!check_caller_access_to_name(dir, &dentry->d_name)) {
76 		err = -EACCES;
77 		goto out_eacces;
78 	}
79 
80 	/* save current_cred and override it */
81 	OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
82 
83 	sdcardfs_get_lower_path(dentry, &lower_path);
84 	lower_dentry = lower_path.dentry;
85 	lower_dentry_mnt = lower_path.mnt;
86 	lower_parent_dentry = lock_parent(lower_dentry);
87 
88 	/* set last 16bytes of mode field to 0664 */
89 	mode = (mode & S_IFMT) | 00664;
90 
91 	/* temporarily change umask for lower fs write */
92 	saved_fs = current->fs;
93 	copied_fs = copy_fs_struct(current->fs);
94 	if (!copied_fs) {
95 		err = -ENOMEM;
96 		goto out_unlock;
97 	}
98 	current->fs = copied_fs;
99 	current->fs->umask = 0;
100 	err = vfs_create2(lower_dentry_mnt, d_inode(lower_parent_dentry), lower_dentry, mode, want_excl);
101 	if (err)
102 		goto out;
103 
104 	err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path,
105 			SDCARDFS_I(dir)->data->userid);
106 	if (err)
107 		goto out;
108 	fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
109 	fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
110 	fixup_lower_ownership(dentry, dentry->d_name.name);
111 
112 out:
113 	current->fs = saved_fs;
114 	free_fs_struct(copied_fs);
115 out_unlock:
116 	unlock_dir(lower_parent_dentry);
117 	sdcardfs_put_lower_path(dentry, &lower_path);
118 	REVERT_CRED(saved_cred);
119 out_eacces:
120 	return err;
121 }
122 
123 #if 0
124 static int sdcardfs_link(struct dentry *old_dentry, struct inode *dir,
125 		       struct dentry *new_dentry)
126 {
127 	struct dentry *lower_old_dentry;
128 	struct dentry *lower_new_dentry;
129 	struct dentry *lower_dir_dentry;
130 	u64 file_size_save;
131 	int err;
132 	struct path lower_old_path, lower_new_path;
133 
134 	OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
135 
136 	file_size_save = i_size_read(d_inode(old_dentry));
137 	sdcardfs_get_lower_path(old_dentry, &lower_old_path);
138 	sdcardfs_get_lower_path(new_dentry, &lower_new_path);
139 	lower_old_dentry = lower_old_path.dentry;
140 	lower_new_dentry = lower_new_path.dentry;
141 	lower_dir_dentry = lock_parent(lower_new_dentry);
142 
143 	err = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
144 		       lower_new_dentry, NULL);
145 	if (err || !d_inode(lower_new_dentry))
146 		goto out;
147 
148 	err = sdcardfs_interpose(new_dentry, dir->i_sb, &lower_new_path);
149 	if (err)
150 		goto out;
151 	fsstack_copy_attr_times(dir, d_inode(lower_new_dentry));
152 	fsstack_copy_inode_size(dir, d_inode(lower_new_dentry));
153 	set_nlink(d_inode(old_dentry),
154 		  sdcardfs_lower_inode(d_inode(old_dentry))->i_nlink);
155 	i_size_write(d_inode(new_dentry), file_size_save);
156 out:
157 	unlock_dir(lower_dir_dentry);
158 	sdcardfs_put_lower_path(old_dentry, &lower_old_path);
159 	sdcardfs_put_lower_path(new_dentry, &lower_new_path);
160 	REVERT_CRED();
161 	return err;
162 }
163 #endif
164 
sdcardfs_unlink(struct inode * dir,struct dentry * dentry)165 static int sdcardfs_unlink(struct inode *dir, struct dentry *dentry)
166 {
167 	int err;
168 	struct dentry *lower_dentry;
169 	struct vfsmount *lower_mnt;
170 	struct inode *lower_dir_inode = sdcardfs_lower_inode(dir);
171 	struct dentry *lower_dir_dentry;
172 	struct path lower_path;
173 	const struct cred *saved_cred = NULL;
174 
175 	if (!check_caller_access_to_name(dir, &dentry->d_name)) {
176 		err = -EACCES;
177 		goto out_eacces;
178 	}
179 
180 	/* save current_cred and override it */
181 	OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
182 
183 	sdcardfs_get_lower_path(dentry, &lower_path);
184 	lower_dentry = lower_path.dentry;
185 	lower_mnt = lower_path.mnt;
186 	dget(lower_dentry);
187 	lower_dir_dentry = lock_parent(lower_dentry);
188 
189 	err = vfs_unlink2(lower_mnt, lower_dir_inode, lower_dentry, NULL);
190 
191 	/*
192 	 * Note: unlinking on top of NFS can cause silly-renamed files.
193 	 * Trying to delete such files results in EBUSY from NFS
194 	 * below.  Silly-renamed files will get deleted by NFS later on, so
195 	 * we just need to detect them here and treat such EBUSY errors as
196 	 * if the upper file was successfully deleted.
197 	 */
198 	if (err == -EBUSY && lower_dentry->d_flags & DCACHE_NFSFS_RENAMED)
199 		err = 0;
200 	if (err)
201 		goto out;
202 	fsstack_copy_attr_times(dir, lower_dir_inode);
203 	fsstack_copy_inode_size(dir, lower_dir_inode);
204 	set_nlink(d_inode(dentry),
205 		  sdcardfs_lower_inode(d_inode(dentry))->i_nlink);
206 	d_inode(dentry)->i_ctime = dir->i_ctime;
207 	d_drop(dentry); /* this is needed, else LTP fails (VFS won't do it) */
208 out:
209 	unlock_dir(lower_dir_dentry);
210 	dput(lower_dentry);
211 	sdcardfs_put_lower_path(dentry, &lower_path);
212 	REVERT_CRED(saved_cred);
213 out_eacces:
214 	return err;
215 }
216 
217 #if 0
218 static int sdcardfs_symlink(struct inode *dir, struct dentry *dentry,
219 			  const char *symname)
220 {
221 	int err;
222 	struct dentry *lower_dentry;
223 	struct dentry *lower_parent_dentry = NULL;
224 	struct path lower_path;
225 
226 	OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
227 
228 	sdcardfs_get_lower_path(dentry, &lower_path);
229 	lower_dentry = lower_path.dentry;
230 	lower_parent_dentry = lock_parent(lower_dentry);
231 
232 	err = vfs_symlink(d_inode(lower_parent_dentry), lower_dentry, symname);
233 	if (err)
234 		goto out;
235 	err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path);
236 	if (err)
237 		goto out;
238 	fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
239 	fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
240 
241 out:
242 	unlock_dir(lower_parent_dentry);
243 	sdcardfs_put_lower_path(dentry, &lower_path);
244 	REVERT_CRED();
245 	return err;
246 }
247 #endif
248 
touch(char * abs_path,mode_t mode)249 static int touch(char *abs_path, mode_t mode)
250 {
251 	struct file *filp = filp_open(abs_path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, mode);
252 
253 	if (IS_ERR(filp)) {
254 		if (PTR_ERR(filp) == -EEXIST) {
255 			return 0;
256 		} else {
257 			pr_err("sdcardfs: failed to open(%s): %ld\n",
258 						abs_path, PTR_ERR(filp));
259 			return PTR_ERR(filp);
260 		}
261 	}
262 	filp_close(filp, current->files);
263 	return 0;
264 }
265 
sdcardfs_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)266 static int sdcardfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
267 {
268 	int err;
269 	int make_nomedia_in_obb = 0;
270 	struct dentry *lower_dentry;
271 	struct vfsmount *lower_mnt;
272 	struct dentry *lower_parent_dentry = NULL;
273 	struct path lower_path;
274 	struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
275 	const struct cred *saved_cred = NULL;
276 	struct sdcardfs_inode_data *pd = SDCARDFS_I(dir)->data;
277 	int touch_err = 0;
278 	struct fs_struct *saved_fs;
279 	struct fs_struct *copied_fs;
280 	struct qstr q_obb = QSTR_LITERAL("obb");
281 	struct qstr q_data = QSTR_LITERAL("data");
282 
283 	if (!check_caller_access_to_name(dir, &dentry->d_name)) {
284 		err = -EACCES;
285 		goto out_eacces;
286 	}
287 
288 	/* save current_cred and override it */
289 	OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
290 
291 	/* check disk space */
292 	if (!check_min_free_space(dentry, 0, 1)) {
293 		pr_err("sdcardfs: No minimum free space.\n");
294 		err = -ENOSPC;
295 		goto out_revert;
296 	}
297 
298 	/* the lower_dentry is negative here */
299 	sdcardfs_get_lower_path(dentry, &lower_path);
300 	lower_dentry = lower_path.dentry;
301 	lower_mnt = lower_path.mnt;
302 	lower_parent_dentry = lock_parent(lower_dentry);
303 
304 	/* set last 16bytes of mode field to 0775 */
305 	mode = (mode & S_IFMT) | 00775;
306 
307 	/* temporarily change umask for lower fs write */
308 	saved_fs = current->fs;
309 	copied_fs = copy_fs_struct(current->fs);
310 	if (!copied_fs) {
311 		err = -ENOMEM;
312 		unlock_dir(lower_parent_dentry);
313 		goto out_unlock;
314 	}
315 	current->fs = copied_fs;
316 	current->fs->umask = 0;
317 	err = vfs_mkdir2(lower_mnt, d_inode(lower_parent_dentry), lower_dentry, mode);
318 
319 	if (err) {
320 		unlock_dir(lower_parent_dentry);
321 		goto out;
322 	}
323 
324 	/* if it is a local obb dentry, setup it with the base obbpath */
325 	if (need_graft_path(dentry)) {
326 
327 		err = setup_obb_dentry(dentry, &lower_path);
328 		if (err) {
329 			/* if the sbi->obbpath is not available, the lower_path won't be
330 			 * changed by setup_obb_dentry() but the lower path is saved to
331 			 * its orig_path. this dentry will be revalidated later.
332 			 * but now, the lower_path should be NULL
333 			 */
334 			sdcardfs_put_reset_lower_path(dentry);
335 
336 			/* the newly created lower path which saved to its orig_path or
337 			 * the lower_path is the base obbpath.
338 			 * therefore, an additional path_get is required
339 			 */
340 			path_get(&lower_path);
341 		} else
342 			make_nomedia_in_obb = 1;
343 	}
344 
345 	err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path, pd->userid);
346 	if (err) {
347 		unlock_dir(lower_parent_dentry);
348 		goto out;
349 	}
350 
351 	fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
352 	fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
353 	/* update number of links on parent directory */
354 	set_nlink(dir, sdcardfs_lower_inode(dir)->i_nlink);
355 	fixup_lower_ownership(dentry, dentry->d_name.name);
356 	unlock_dir(lower_parent_dentry);
357 	if ((!sbi->options.multiuser) && (qstr_case_eq(&dentry->d_name, &q_obb))
358 		&& (pd->perm == PERM_ANDROID) && (pd->userid == 0))
359 		make_nomedia_in_obb = 1;
360 
361 	/* When creating /Android/data and /Android/obb, mark them as .nomedia */
362 	if (make_nomedia_in_obb ||
363 		((pd->perm == PERM_ANDROID)
364 				&& (qstr_case_eq(&dentry->d_name, &q_data)))) {
365 		REVERT_CRED(saved_cred);
366 		OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(d_inode(dentry)));
367 		set_fs_pwd(current->fs, &lower_path);
368 		touch_err = touch(".nomedia", 0664);
369 		if (touch_err) {
370 			pr_err("sdcardfs: failed to create .nomedia in %s: %d\n",
371 							lower_path.dentry->d_name.name, touch_err);
372 			goto out;
373 		}
374 	}
375 out:
376 	current->fs = saved_fs;
377 	free_fs_struct(copied_fs);
378 out_unlock:
379 	sdcardfs_put_lower_path(dentry, &lower_path);
380 out_revert:
381 	REVERT_CRED(saved_cred);
382 out_eacces:
383 	return err;
384 }
385 
sdcardfs_rmdir(struct inode * dir,struct dentry * dentry)386 static int sdcardfs_rmdir(struct inode *dir, struct dentry *dentry)
387 {
388 	struct dentry *lower_dentry;
389 	struct dentry *lower_dir_dentry;
390 	struct vfsmount *lower_mnt;
391 	int err;
392 	struct path lower_path;
393 	const struct cred *saved_cred = NULL;
394 
395 	if (!check_caller_access_to_name(dir, &dentry->d_name)) {
396 		err = -EACCES;
397 		goto out_eacces;
398 	}
399 
400 	/* save current_cred and override it */
401 	OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
402 
403 	/* sdcardfs_get_real_lower(): in case of remove an user's obb dentry
404 	 * the dentry on the original path should be deleted.
405 	 */
406 	sdcardfs_get_real_lower(dentry, &lower_path);
407 
408 	lower_dentry = lower_path.dentry;
409 	lower_mnt = lower_path.mnt;
410 	lower_dir_dentry = lock_parent(lower_dentry);
411 
412 	err = vfs_rmdir2(lower_mnt, d_inode(lower_dir_dentry), lower_dentry);
413 	if (err)
414 		goto out;
415 
416 	d_drop(dentry);	/* drop our dentry on success (why not VFS's job?) */
417 	if (d_inode(dentry))
418 		clear_nlink(d_inode(dentry));
419 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
420 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
421 	set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
422 
423 out:
424 	unlock_dir(lower_dir_dentry);
425 	sdcardfs_put_real_lower(dentry, &lower_path);
426 	REVERT_CRED(saved_cred);
427 out_eacces:
428 	return err;
429 }
430 
431 #if 0
432 static int sdcardfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
433 			dev_t dev)
434 {
435 	int err;
436 	struct dentry *lower_dentry;
437 	struct dentry *lower_parent_dentry = NULL;
438 	struct path lower_path;
439 
440 	OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
441 
442 	sdcardfs_get_lower_path(dentry, &lower_path);
443 	lower_dentry = lower_path.dentry;
444 	lower_parent_dentry = lock_parent(lower_dentry);
445 
446 	err = vfs_mknod(d_inode(lower_parent_dentry), lower_dentry, mode, dev);
447 	if (err)
448 		goto out;
449 
450 	err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path);
451 	if (err)
452 		goto out;
453 	fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
454 	fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
455 
456 out:
457 	unlock_dir(lower_parent_dentry);
458 	sdcardfs_put_lower_path(dentry, &lower_path);
459 	REVERT_CRED();
460 	return err;
461 }
462 #endif
463 
464 /*
465  * The locking rules in sdcardfs_rename are complex.  We could use a simpler
466  * superblock-level name-space lock for renames and copy-ups.
467  */
sdcardfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)468 static int sdcardfs_rename(struct inode *old_dir, struct dentry *old_dentry,
469 			 struct inode *new_dir, struct dentry *new_dentry,
470 			 unsigned int flags)
471 {
472 	int err = 0;
473 	struct dentry *lower_old_dentry = NULL;
474 	struct dentry *lower_new_dentry = NULL;
475 	struct dentry *lower_old_dir_dentry = NULL;
476 	struct dentry *lower_new_dir_dentry = NULL;
477 	struct vfsmount *lower_mnt = NULL;
478 	struct dentry *trap = NULL;
479 	struct path lower_old_path, lower_new_path;
480 	const struct cred *saved_cred = NULL;
481 
482 	if (flags)
483 		return -EINVAL;
484 
485 	if (!check_caller_access_to_name(old_dir, &old_dentry->d_name) ||
486 		!check_caller_access_to_name(new_dir, &new_dentry->d_name)) {
487 		err = -EACCES;
488 		goto out_eacces;
489 	}
490 
491 	/* save current_cred and override it */
492 	OVERRIDE_CRED(SDCARDFS_SB(old_dir->i_sb), saved_cred, SDCARDFS_I(new_dir));
493 
494 	sdcardfs_get_real_lower(old_dentry, &lower_old_path);
495 	sdcardfs_get_lower_path(new_dentry, &lower_new_path);
496 	lower_old_dentry = lower_old_path.dentry;
497 	lower_new_dentry = lower_new_path.dentry;
498 	lower_mnt = lower_old_path.mnt;
499 	lower_old_dir_dentry = dget_parent(lower_old_dentry);
500 	lower_new_dir_dentry = dget_parent(lower_new_dentry);
501 
502 	trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
503 	/* source should not be ancestor of target */
504 	if (trap == lower_old_dentry) {
505 		err = -EINVAL;
506 		goto out;
507 	}
508 	/* target should not be ancestor of source */
509 	if (trap == lower_new_dentry) {
510 		err = -ENOTEMPTY;
511 		goto out;
512 	}
513 
514 	err = vfs_rename2(lower_mnt,
515 			 d_inode(lower_old_dir_dentry), lower_old_dentry,
516 			 d_inode(lower_new_dir_dentry), lower_new_dentry,
517 			 NULL, 0);
518 	if (err)
519 		goto out;
520 
521 	/* Copy attrs from lower dir, but i_uid/i_gid */
522 	sdcardfs_copy_and_fix_attrs(new_dir, d_inode(lower_new_dir_dentry));
523 	fsstack_copy_inode_size(new_dir, d_inode(lower_new_dir_dentry));
524 
525 	if (new_dir != old_dir) {
526 		sdcardfs_copy_and_fix_attrs(old_dir, d_inode(lower_old_dir_dentry));
527 		fsstack_copy_inode_size(old_dir, d_inode(lower_old_dir_dentry));
528 	}
529 	get_derived_permission_new(new_dentry->d_parent, old_dentry, &new_dentry->d_name);
530 	fixup_tmp_permissions(d_inode(old_dentry));
531 	fixup_lower_ownership(old_dentry, new_dentry->d_name.name);
532 	d_invalidate(old_dentry); /* Can't fixup ownership recursively :( */
533 out:
534 	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
535 	dput(lower_old_dir_dentry);
536 	dput(lower_new_dir_dentry);
537 	sdcardfs_put_real_lower(old_dentry, &lower_old_path);
538 	sdcardfs_put_lower_path(new_dentry, &lower_new_path);
539 	REVERT_CRED(saved_cred);
540 out_eacces:
541 	return err;
542 }
543 
544 #if 0
545 static int sdcardfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
546 {
547 	int err;
548 	struct dentry *lower_dentry;
549 	struct path lower_path;
550 	/* XXX readlink does not requires overriding credential */
551 
552 	sdcardfs_get_lower_path(dentry, &lower_path);
553 	lower_dentry = lower_path.dentry;
554 	if (!d_inode(lower_dentry)->i_op ||
555 	    !d_inode(lower_dentry)->i_op->readlink) {
556 		err = -EINVAL;
557 		goto out;
558 	}
559 
560 	err = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
561 						    buf, bufsiz);
562 	if (err < 0)
563 		goto out;
564 	fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry));
565 
566 out:
567 	sdcardfs_put_lower_path(dentry, &lower_path);
568 	return err;
569 }
570 #endif
571 
572 #if 0
573 static const char *sdcardfs_follow_link(struct dentry *dentry, void **cookie)
574 {
575 	char *buf;
576 	int len = PAGE_SIZE, err;
577 	mm_segment_t old_fs;
578 
579 	/* This is freed by the put_link method assuming a successful call. */
580 	buf = kmalloc(len, GFP_KERNEL);
581 	if (!buf) {
582 		buf = ERR_PTR(-ENOMEM);
583 		return buf;
584 	}
585 
586 	/* read the symlink, and then we will follow it */
587 	old_fs = get_fs();
588 	set_fs(KERNEL_DS);
589 	err = sdcardfs_readlink(dentry, buf, len);
590 	set_fs(old_fs);
591 	if (err < 0) {
592 		kfree(buf);
593 		buf = ERR_PTR(err);
594 	} else {
595 		buf[err] = '\0';
596 	}
597 	return *cookie = buf;
598 }
599 #endif
600 
sdcardfs_permission_wrn(struct inode * inode,int mask)601 static int sdcardfs_permission_wrn(struct inode *inode, int mask)
602 {
603 	WARN_RATELIMIT(1, "sdcardfs does not support permission. Use permission2.\n");
604 	return -EINVAL;
605 }
606 
copy_attrs(struct inode * dest,const struct inode * src)607 void copy_attrs(struct inode *dest, const struct inode *src)
608 {
609 	dest->i_mode = src->i_mode;
610 	dest->i_uid = src->i_uid;
611 	dest->i_gid = src->i_gid;
612 	dest->i_rdev = src->i_rdev;
613 	dest->i_atime = src->i_atime;
614 	dest->i_mtime = src->i_mtime;
615 	dest->i_ctime = src->i_ctime;
616 	dest->i_blkbits = src->i_blkbits;
617 	dest->i_flags = src->i_flags;
618 #ifdef CONFIG_FS_POSIX_ACL
619 	dest->i_acl = src->i_acl;
620 #endif
621 #ifdef CONFIG_SECURITY
622 	dest->i_security = src->i_security;
623 #endif
624 }
625 
sdcardfs_permission(struct vfsmount * mnt,struct inode * inode,int mask)626 static int sdcardfs_permission(struct vfsmount *mnt, struct inode *inode, int mask)
627 {
628 	int err;
629 	struct inode tmp;
630 	struct sdcardfs_inode_data *top = top_data_get(SDCARDFS_I(inode));
631 
632 	if (IS_ERR(mnt))
633 		return PTR_ERR(mnt);
634 	if (!top)
635 		return -EINVAL;
636 
637 	/*
638 	 * Permission check on sdcardfs inode.
639 	 * Calling process should have AID_SDCARD_RW permission
640 	 * Since generic_permission only needs i_mode, i_uid,
641 	 * i_gid, and i_sb, we can create a fake inode to pass
642 	 * this information down in.
643 	 *
644 	 * The underlying code may attempt to take locks in some
645 	 * cases for features we're not using, but if that changes,
646 	 * locks must be dealt with to avoid undefined behavior.
647 	 */
648 	copy_attrs(&tmp, inode);
649 	tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
650 	tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, inode->i_sb, top));
651 	tmp.i_mode = (inode->i_mode & S_IFMT)
652 			| get_mode(mnt, SDCARDFS_I(inode), top);
653 	data_put(top);
654 	tmp.i_sb = inode->i_sb;
655 	if (IS_POSIXACL(inode))
656 		pr_warn("%s: This may be undefined behavior...\n", __func__);
657 	err = generic_permission(&tmp, mask);
658 	/* XXX
659 	 * Original sdcardfs code calls inode_permission(lower_inode,.. )
660 	 * for checking inode permission. But doing such things here seems
661 	 * duplicated work, because the functions called after this func,
662 	 * such as vfs_create, vfs_unlink, vfs_rename, and etc,
663 	 * does exactly same thing, i.e., they calls inode_permission().
664 	 * So we just let they do the things.
665 	 * If there are any security hole, just uncomment following if block.
666 	 */
667 #if 0
668 	if (!err) {
669 		/*
670 		 * Permission check on lower_inode(=EXT4).
671 		 * we check it with AID_MEDIA_RW permission
672 		 */
673 		struct inode *lower_inode;
674 
675 		OVERRIDE_CRED(SDCARDFS_SB(inode->sb));
676 
677 		lower_inode = sdcardfs_lower_inode(inode);
678 		err = inode_permission(lower_inode, mask);
679 
680 		REVERT_CRED();
681 	}
682 #endif
683 	return err;
684 
685 }
686 
sdcardfs_setattr_wrn(struct dentry * dentry,struct iattr * ia)687 static int sdcardfs_setattr_wrn(struct dentry *dentry, struct iattr *ia)
688 {
689 	WARN_RATELIMIT(1, "sdcardfs does not support setattr. User setattr2.\n");
690 	return -EINVAL;
691 }
692 
sdcardfs_setattr(struct vfsmount * mnt,struct dentry * dentry,struct iattr * ia)693 static int sdcardfs_setattr(struct vfsmount *mnt, struct dentry *dentry, struct iattr *ia)
694 {
695 	int err;
696 	struct dentry *lower_dentry;
697 	struct vfsmount *lower_mnt;
698 	struct inode *inode;
699 	struct inode *lower_inode;
700 	struct path lower_path;
701 	struct iattr lower_ia;
702 	struct dentry *parent;
703 	struct inode tmp;
704 	struct dentry tmp_d;
705 	struct sdcardfs_inode_data *top;
706 
707 	const struct cred *saved_cred = NULL;
708 
709 	inode = d_inode(dentry);
710 	top = top_data_get(SDCARDFS_I(inode));
711 
712 	if (!top)
713 		return -EINVAL;
714 
715 	/*
716 	 * Permission check on sdcardfs inode.
717 	 * Calling process should have AID_SDCARD_RW permission
718 	 * Since generic_permission only needs i_mode, i_uid,
719 	 * i_gid, and i_sb, we can create a fake inode to pass
720 	 * this information down in.
721 	 *
722 	 * The underlying code may attempt to take locks in some
723 	 * cases for features we're not using, but if that changes,
724 	 * locks must be dealt with to avoid undefined behavior.
725 	 *
726 	 */
727 	copy_attrs(&tmp, inode);
728 	tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
729 	tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, dentry->d_sb, top));
730 	tmp.i_mode = (inode->i_mode & S_IFMT)
731 			| get_mode(mnt, SDCARDFS_I(inode), top);
732 	tmp.i_size = i_size_read(inode);
733 	data_put(top);
734 	tmp.i_sb = inode->i_sb;
735 	tmp_d.d_inode = &tmp;
736 
737 	/*
738 	 * Check if user has permission to change dentry.  We don't check if
739 	 * this user can change the lower inode: that should happen when
740 	 * calling notify_change on the lower inode.
741 	 */
742 	/* prepare our own lower struct iattr (with the lower file) */
743 	memcpy(&lower_ia, ia, sizeof(lower_ia));
744 	/* Allow touch updating timestamps. A previous permission check ensures
745 	 * we have write access. Changes to mode, owner, and group are ignored
746 	 */
747 	ia->ia_valid |= ATTR_FORCE;
748 	err = setattr_prepare(&tmp_d, ia);
749 
750 	if (!err) {
751 		/* check the Android group ID */
752 		parent = dget_parent(dentry);
753 		if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name))
754 			err = -EACCES;
755 		dput(parent);
756 	}
757 
758 	if (err)
759 		goto out_err;
760 
761 	/* save current_cred and override it */
762 	OVERRIDE_CRED(SDCARDFS_SB(dentry->d_sb), saved_cred, SDCARDFS_I(inode));
763 
764 	sdcardfs_get_lower_path(dentry, &lower_path);
765 	lower_dentry = lower_path.dentry;
766 	lower_mnt = lower_path.mnt;
767 	lower_inode = sdcardfs_lower_inode(inode);
768 
769 	if (ia->ia_valid & ATTR_FILE)
770 		lower_ia.ia_file = sdcardfs_lower_file(ia->ia_file);
771 
772 	lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
773 
774 	/*
775 	 * If shrinking, first truncate upper level to cancel writing dirty
776 	 * pages beyond the new eof; and also if its' maxbytes is more
777 	 * limiting (fail with -EFBIG before making any change to the lower
778 	 * level).  There is no need to vmtruncate the upper level
779 	 * afterwards in the other cases: we fsstack_copy_inode_size from
780 	 * the lower level.
781 	 */
782 	if (ia->ia_valid & ATTR_SIZE) {
783 		err = inode_newsize_ok(&tmp, ia->ia_size);
784 		if (err) {
785 			goto out;
786 		}
787 		truncate_setsize(inode, ia->ia_size);
788 	}
789 
790 	/*
791 	 * mode change is for clearing setuid/setgid bits. Allow lower fs
792 	 * to interpret this in its own way.
793 	 */
794 	if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
795 		lower_ia.ia_valid &= ~ATTR_MODE;
796 
797 	/* notify the (possibly copied-up) lower inode */
798 	/*
799 	 * Note: we use d_inode(lower_dentry), because lower_inode may be
800 	 * unlinked (no inode->i_sb and i_ino==0.  This happens if someone
801 	 * tries to open(), unlink(), then ftruncate() a file.
802 	 */
803 	inode_lock(d_inode(lower_dentry));
804 	err = notify_change2(lower_mnt, lower_dentry, &lower_ia, /* note: lower_ia */
805 			NULL);
806 	inode_unlock(d_inode(lower_dentry));
807 	if (err)
808 		goto out;
809 
810 	/* get attributes from the lower inode and update derived permissions */
811 	sdcardfs_copy_and_fix_attrs(inode, lower_inode);
812 
813 	/*
814 	 * Not running fsstack_copy_inode_size(inode, lower_inode), because
815 	 * VFS should update our inode size, and notify_change on
816 	 * lower_inode should update its size.
817 	 */
818 
819 out:
820 	sdcardfs_put_lower_path(dentry, &lower_path);
821 	REVERT_CRED(saved_cred);
822 out_err:
823 	return err;
824 }
825 
sdcardfs_fillattr(struct vfsmount * mnt,struct inode * inode,struct kstat * lower_stat,struct kstat * stat)826 static int sdcardfs_fillattr(struct vfsmount *mnt, struct inode *inode,
827 				struct kstat *lower_stat, struct kstat *stat)
828 {
829 	struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
830 	struct sdcardfs_inode_data *top = top_data_get(info);
831 	struct super_block *sb = inode->i_sb;
832 
833 	if (!top)
834 		return -EINVAL;
835 
836 	stat->dev = inode->i_sb->s_dev;
837 	stat->ino = inode->i_ino;
838 	stat->mode = (inode->i_mode  & S_IFMT) | get_mode(mnt, info, top);
839 	stat->nlink = inode->i_nlink;
840 	stat->uid = make_kuid(&init_user_ns, top->d_uid);
841 	stat->gid = make_kgid(&init_user_ns, get_gid(mnt, sb, top));
842 	stat->rdev = inode->i_rdev;
843 	stat->size = lower_stat->size;
844 	stat->atime = lower_stat->atime;
845 	stat->mtime = lower_stat->mtime;
846 	stat->ctime = lower_stat->ctime;
847 	stat->blksize = lower_stat->blksize;
848 	stat->blocks = lower_stat->blocks;
849 	data_put(top);
850 	return 0;
851 }
852 
sdcardfs_getattr(struct vfsmount * mnt,struct dentry * dentry,struct kstat * stat)853 static int sdcardfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
854 		 struct kstat *stat)
855 {
856 	struct kstat lower_stat;
857 	struct path lower_path;
858 	struct dentry *parent;
859 	int err;
860 
861 	parent = dget_parent(dentry);
862 	if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
863 		dput(parent);
864 		return -EACCES;
865 	}
866 	dput(parent);
867 
868 	sdcardfs_get_lower_path(dentry, &lower_path);
869 	err = vfs_getattr(&lower_path, &lower_stat);
870 	if (err)
871 		goto out;
872 	sdcardfs_copy_and_fix_attrs(d_inode(dentry),
873 			      d_inode(lower_path.dentry));
874 	err = sdcardfs_fillattr(mnt, d_inode(dentry), &lower_stat, stat);
875 out:
876 	sdcardfs_put_lower_path(dentry, &lower_path);
877 	return err;
878 }
879 
880 const struct inode_operations sdcardfs_symlink_iops = {
881 	.permission2	= sdcardfs_permission,
882 	.setattr2	= sdcardfs_setattr,
883 	/* XXX Following operations are implemented,
884 	 *     but FUSE(sdcard) or FAT does not support them
885 	 *     These methods are *NOT* perfectly tested.
886 	.readlink	= sdcardfs_readlink,
887 	.follow_link	= sdcardfs_follow_link,
888 	.put_link	= kfree_put_link,
889 	 */
890 };
891 
892 const struct inode_operations sdcardfs_dir_iops = {
893 	.create		= sdcardfs_create,
894 	.lookup		= sdcardfs_lookup,
895 	.permission	= sdcardfs_permission_wrn,
896 	.permission2	= sdcardfs_permission,
897 	.unlink		= sdcardfs_unlink,
898 	.mkdir		= sdcardfs_mkdir,
899 	.rmdir		= sdcardfs_rmdir,
900 	.rename		= sdcardfs_rename,
901 	.setattr	= sdcardfs_setattr_wrn,
902 	.setattr2	= sdcardfs_setattr,
903 	.getattr	= sdcardfs_getattr,
904 	/* XXX Following operations are implemented,
905 	 *     but FUSE(sdcard) or FAT does not support them
906 	 *     These methods are *NOT* perfectly tested.
907 	.symlink	= sdcardfs_symlink,
908 	.link		= sdcardfs_link,
909 	.mknod		= sdcardfs_mknod,
910 	 */
911 };
912 
913 const struct inode_operations sdcardfs_main_iops = {
914 	.permission	= sdcardfs_permission_wrn,
915 	.permission2	= sdcardfs_permission,
916 	.setattr	= sdcardfs_setattr_wrn,
917 	.setattr2	= sdcardfs_setattr,
918 	.getattr	= sdcardfs_getattr,
919 };
920