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