ANDROID: sdcardfs: Switch strcasecmp for internal call
[firefly-linux-kernel-4.4.55.git] / fs / sdcardfs / sdcardfs.h
1 /*
2  * fs/sdcardfs/sdcardfs.h
3  *
4  * The sdcardfs v2.0
5  *   This file system replaces the sdcard daemon on Android
6  *   On version 2.0, some of the daemon functions have been ported
7  *   to support the multi-user concepts of Android 4.4
8  *
9  * Copyright (c) 2013 Samsung Electronics Co. Ltd
10  *   Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
11  *               Sunghwan Yun, Sungjong Seo
12  *
13  * This program has been developed as a stackable file system based on
14  * the WrapFS which written by
15  *
16  * Copyright (c) 1998-2011 Erez Zadok
17  * Copyright (c) 2009     Shrikar Archak
18  * Copyright (c) 2003-2011 Stony Brook University
19  * Copyright (c) 2003-2011 The Research Foundation of SUNY
20  *
21  * This file is dual licensed.  It may be redistributed and/or modified
22  * under the terms of the Apache 2.0 License OR version 2 of the GNU
23  * General Public License.
24  */
25
26 #ifndef _SDCARDFS_H_
27 #define _SDCARDFS_H_
28
29 #include <linux/dcache.h>
30 #include <linux/file.h>
31 #include <linux/fs.h>
32 #include <linux/mm.h>
33 #include <linux/mount.h>
34 #include <linux/namei.h>
35 #include <linux/seq_file.h>
36 #include <linux/statfs.h>
37 #include <linux/fs_stack.h>
38 #include <linux/magic.h>
39 #include <linux/uaccess.h>
40 #include <linux/slab.h>
41 #include <linux/sched.h>
42 #include <linux/types.h>
43 #include <linux/security.h>
44 #include <linux/string.h>
45 #include <linux/list.h>
46 #include "multiuser.h"
47
48 /* the file system name */
49 #define SDCARDFS_NAME "sdcardfs"
50
51 /* sdcardfs root inode number */
52 #define SDCARDFS_ROOT_INO     1
53
54 /* useful for tracking code reachability */
55 #define UDBG printk(KERN_DEFAULT "DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
56
57 #define SDCARDFS_DIRENT_SIZE 256
58
59 /* temporary static uid settings for development */
60 #define AID_ROOT             0  /* uid for accessing /mnt/sdcard & extSdcard */
61 #define AID_MEDIA_RW      1023  /* internal media storage write access */
62
63 #define AID_SDCARD_RW     1015  /* external storage write access */
64 #define AID_SDCARD_R      1028  /* external storage read access */
65 #define AID_SDCARD_PICS   1033  /* external storage photos access */
66 #define AID_SDCARD_AV     1034  /* external storage audio/video access */
67 #define AID_SDCARD_ALL    1035  /* access all users external storage */
68 #define AID_MEDIA_OBB     1059  /* obb files */
69
70 #define AID_SDCARD_IMAGE  1057
71
72 #define AID_PACKAGE_INFO  1027
73
74
75 /*
76  * Permissions are handled by our permission function.
77  * We don't want anyone who happens to look at our inode value to prematurely
78  * block access, so store more permissive values. These are probably never
79  * used.
80  */
81 #define fixup_tmp_permissions(x)        \
82         do {                                            \
83                 (x)->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(x)->d_uid);    \
84                 (x)->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW);   \
85                 (x)->i_mode = ((x)->i_mode & S_IFMT) | 0775;\
86         } while (0)
87
88 /* OVERRIDE_CRED() and REVERT_CRED()
89  *      OVERRID_CRED()
90  *              backup original task->cred
91  *              and modifies task->cred->fsuid/fsgid to specified value.
92  *      REVERT_CRED()
93  *              restore original task->cred->fsuid/fsgid.
94  * These two macro should be used in pair, and OVERRIDE_CRED() should be
95  * placed at the beginning of a function, right after variable declaration.
96  */
97 #define OVERRIDE_CRED(sdcardfs_sbi, saved_cred, info)           \
98         saved_cred = override_fsids(sdcardfs_sbi, info);        \
99         if (!saved_cred) { return -ENOMEM; }
100
101 #define OVERRIDE_CRED_PTR(sdcardfs_sbi, saved_cred, info)       \
102         saved_cred = override_fsids(sdcardfs_sbi, info);        \
103         if (!saved_cred) { return ERR_PTR(-ENOMEM); }
104
105 #define REVERT_CRED(saved_cred) revert_fsids(saved_cred)
106
107 #define DEBUG_CRED()            \
108         printk("KAKJAGI: %s:%d fsuid %d fsgid %d\n",    \
109                 __FUNCTION__, __LINE__,                 \
110                 (int)current->cred->fsuid,              \
111                 (int)current->cred->fsgid);
112
113 /* Android 5.0 support */
114
115 /* Permission mode for a specific node. Controls how file permissions
116  * are derived for children nodes. */
117 typedef enum {
118     /* Nothing special; this node should just inherit from its parent. */
119     PERM_INHERIT,
120     /* This node is one level above a normal root; used for legacy layouts
121      * which use the first level to represent user_id. */
122     PERM_PRE_ROOT,
123     /* This node is "/" */
124     PERM_ROOT,
125     /* This node is "/Android" */
126     PERM_ANDROID,
127     /* This node is "/Android/data" */
128     PERM_ANDROID_DATA,
129     /* This node is "/Android/obb" */
130     PERM_ANDROID_OBB,
131     /* This node is "/Android/media" */
132     PERM_ANDROID_MEDIA,
133     /* This node is "/Android/[data|media|obb]/[package]" */
134     PERM_ANDROID_PACKAGE,
135     /* This node is "/Android/[data|media|obb]/[package]/cache" */
136     PERM_ANDROID_PACKAGE_CACHE,
137 } perm_t;
138
139 struct sdcardfs_sb_info;
140 struct sdcardfs_mount_options;
141 struct sdcardfs_inode_info;
142
143 /* Do not directly use this function. Use OVERRIDE_CRED() instead. */
144 const struct cred * override_fsids(struct sdcardfs_sb_info* sbi, struct sdcardfs_inode_info *info);
145 /* Do not directly use this function, use REVERT_CRED() instead. */
146 void revert_fsids(const struct cred * old_cred);
147
148 /* operations vectors defined in specific files */
149 extern const struct file_operations sdcardfs_main_fops;
150 extern const struct file_operations sdcardfs_dir_fops;
151 extern const struct inode_operations sdcardfs_main_iops;
152 extern const struct inode_operations sdcardfs_dir_iops;
153 extern const struct inode_operations sdcardfs_symlink_iops;
154 extern const struct super_operations sdcardfs_sops;
155 extern const struct dentry_operations sdcardfs_ci_dops;
156 extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
157 extern const struct vm_operations_struct sdcardfs_vm_ops;
158
159 extern int sdcardfs_init_inode_cache(void);
160 extern void sdcardfs_destroy_inode_cache(void);
161 extern int sdcardfs_init_dentry_cache(void);
162 extern void sdcardfs_destroy_dentry_cache(void);
163 extern int new_dentry_private_data(struct dentry *dentry);
164 extern void free_dentry_private_data(struct dentry *dentry);
165 extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
166                                 unsigned int flags);
167 extern struct inode *sdcardfs_iget(struct super_block *sb,
168                                  struct inode *lower_inode, userid_t id);
169 extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
170                             struct path *lower_path, userid_t id);
171
172 /* file private data */
173 struct sdcardfs_file_info {
174         struct file *lower_file;
175         const struct vm_operations_struct *lower_vm_ops;
176 };
177
178 /* sdcardfs inode data in memory */
179 struct sdcardfs_inode_info {
180         struct inode *lower_inode;
181         /* state derived based on current position in hierachy */
182         perm_t perm;
183         userid_t userid;
184         uid_t d_uid;
185         bool under_android;
186         bool under_cache;
187         bool under_obb;
188         /* top folder for ownership */
189         struct inode *top;
190
191         struct inode vfs_inode;
192 };
193
194
195 /* sdcardfs dentry data in memory */
196 struct sdcardfs_dentry_info {
197         spinlock_t lock;        /* protects lower_path */
198         struct path lower_path;
199         struct path orig_path;
200 };
201
202 struct sdcardfs_mount_options {
203         uid_t fs_low_uid;
204         gid_t fs_low_gid;
205         userid_t fs_user_id;
206         bool multiuser;
207         unsigned int reserved_mb;
208 };
209
210 struct sdcardfs_vfsmount_options {
211         gid_t gid;
212         mode_t mask;
213 };
214
215 extern int parse_options_remount(struct super_block *sb, char *options, int silent,
216                 struct sdcardfs_vfsmount_options *vfsopts);
217
218 /* sdcardfs super-block data in memory */
219 struct sdcardfs_sb_info {
220         struct super_block *sb;
221         struct super_block *lower_sb;
222         /* derived perm policy : some of options have been added
223          * to sdcardfs_mount_options (Android 4.4 support) */
224         struct sdcardfs_mount_options options;
225         spinlock_t lock;        /* protects obbpath */
226         char *obbpath_s;
227         struct path obbpath;
228         void *pkgl_id;
229         struct list_head list;
230 };
231
232 /*
233  * inode to private data
234  *
235  * Since we use containers and the struct inode is _inside_ the
236  * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
237  * inode pointer), return a valid non-NULL pointer.
238  */
239 static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
240 {
241         return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
242 }
243
244 /* dentry to private data */
245 #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
246
247 /* superblock to private data */
248 #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
249
250 /* file to private Data */
251 #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
252
253 /* file to lower file */
254 static inline struct file *sdcardfs_lower_file(const struct file *f)
255 {
256         return SDCARDFS_F(f)->lower_file;
257 }
258
259 static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
260 {
261         SDCARDFS_F(f)->lower_file = val;
262 }
263
264 /* inode to lower inode. */
265 static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
266 {
267         return SDCARDFS_I(i)->lower_inode;
268 }
269
270 static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
271 {
272         SDCARDFS_I(i)->lower_inode = val;
273 }
274
275 /* superblock to lower superblock */
276 static inline struct super_block *sdcardfs_lower_super(
277         const struct super_block *sb)
278 {
279         return SDCARDFS_SB(sb)->lower_sb;
280 }
281
282 static inline void sdcardfs_set_lower_super(struct super_block *sb,
283                                           struct super_block *val)
284 {
285         SDCARDFS_SB(sb)->lower_sb = val;
286 }
287
288 /* path based (dentry/mnt) macros */
289 static inline void pathcpy(struct path *dst, const struct path *src)
290 {
291         dst->dentry = src->dentry;
292         dst->mnt = src->mnt;
293 }
294
295 /* sdcardfs_get_pname functions calls path_get()
296  * therefore, the caller must call "proper" path_put functions
297  */
298 #define SDCARDFS_DENT_FUNC(pname) \
299 static inline void sdcardfs_get_##pname(const struct dentry *dent, \
300                                         struct path *pname) \
301 { \
302         spin_lock(&SDCARDFS_D(dent)->lock); \
303         pathcpy(pname, &SDCARDFS_D(dent)->pname); \
304         path_get(pname); \
305         spin_unlock(&SDCARDFS_D(dent)->lock); \
306         return; \
307 } \
308 static inline void sdcardfs_put_##pname(const struct dentry *dent, \
309                                         struct path *pname) \
310 { \
311         path_put(pname); \
312         return; \
313 } \
314 static inline void sdcardfs_set_##pname(const struct dentry *dent, \
315                                         struct path *pname) \
316 { \
317         spin_lock(&SDCARDFS_D(dent)->lock); \
318         pathcpy(&SDCARDFS_D(dent)->pname, pname); \
319         spin_unlock(&SDCARDFS_D(dent)->lock); \
320         return; \
321 } \
322 static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
323 { \
324         spin_lock(&SDCARDFS_D(dent)->lock); \
325         SDCARDFS_D(dent)->pname.dentry = NULL; \
326         SDCARDFS_D(dent)->pname.mnt = NULL; \
327         spin_unlock(&SDCARDFS_D(dent)->lock); \
328         return; \
329 } \
330 static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
331 { \
332         struct path pname; \
333         spin_lock(&SDCARDFS_D(dent)->lock); \
334         if(SDCARDFS_D(dent)->pname.dentry) { \
335                 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
336                 SDCARDFS_D(dent)->pname.dentry = NULL; \
337                 SDCARDFS_D(dent)->pname.mnt = NULL; \
338                 spin_unlock(&SDCARDFS_D(dent)->lock); \
339                 path_put(&pname); \
340         } else \
341                 spin_unlock(&SDCARDFS_D(dent)->lock); \
342         return; \
343 }
344
345 SDCARDFS_DENT_FUNC(lower_path)
346 SDCARDFS_DENT_FUNC(orig_path)
347
348 static inline bool sbinfo_has_sdcard_magic(struct sdcardfs_sb_info *sbinfo)
349 {
350   return sbinfo && sbinfo->sb && sbinfo->sb->s_magic == SDCARDFS_SUPER_MAGIC;
351 }
352
353 /* grab a refererence if we aren't linking to ourself */
354 static inline void set_top(struct sdcardfs_inode_info *info, struct inode *top)
355 {
356         struct inode *old_top = NULL;
357         BUG_ON(IS_ERR_OR_NULL(top));
358         if (info->top && info->top != &info->vfs_inode) {
359                 old_top = info->top;
360         }
361         if (top != &info->vfs_inode)
362                 igrab(top);
363         info->top = top;
364         iput(old_top);
365 }
366
367 static inline struct inode *grab_top(struct sdcardfs_inode_info *info)
368 {
369         struct inode *top = info->top;
370         if (top) {
371                 return igrab(top);
372         } else {
373                 return NULL;
374         }
375 }
376
377 static inline void release_top(struct sdcardfs_inode_info *info)
378 {
379         iput(info->top);
380 }
381
382 static inline int get_gid(struct vfsmount *mnt, struct sdcardfs_inode_info *info) {
383         struct sdcardfs_vfsmount_options *opts = mnt->data;
384
385         if (opts->gid == AID_SDCARD_RW) {
386                 /* As an optimization, certain trusted system components only run
387                  * as owner but operate across all users. Since we're now handing
388                  * out the sdcard_rw GID only to trusted apps, we're okay relaxing
389                  * the user boundary enforcement for the default view. The UIDs
390                  * assigned to app directories are still multiuser aware. */
391                 return AID_SDCARD_RW;
392         } else {
393                 return multiuser_get_uid(info->userid, opts->gid);
394         }
395 }
396 static inline int get_mode(struct vfsmount *mnt, struct sdcardfs_inode_info *info) {
397         int owner_mode;
398         int filtered_mode;
399         struct sdcardfs_vfsmount_options *opts = mnt->data;
400         int visible_mode = 0775 & ~opts->mask;
401
402
403         if (info->perm == PERM_PRE_ROOT) {
404                 /* Top of multi-user view should always be visible to ensure
405                 * secondary users can traverse inside. */
406                 visible_mode = 0711;
407         } else if (info->under_android) {
408                 /* Block "other" access to Android directories, since only apps
409                 * belonging to a specific user should be in there; we still
410                 * leave +x open for the default view. */
411                 if (opts->gid == AID_SDCARD_RW) {
412                         visible_mode = visible_mode & ~0006;
413                 } else {
414                         visible_mode = visible_mode & ~0007;
415                 }
416         }
417         owner_mode = info->lower_inode->i_mode & 0700;
418         filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
419         return filtered_mode;
420 }
421
422 static inline int has_graft_path(const struct dentry *dent)
423 {
424         int ret = 0;
425
426         spin_lock(&SDCARDFS_D(dent)->lock);
427         if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
428                 ret = 1;
429         spin_unlock(&SDCARDFS_D(dent)->lock);
430
431         return ret;
432 }
433
434 static inline void sdcardfs_get_real_lower(const struct dentry *dent,
435                                                 struct path *real_lower)
436 {
437         /* in case of a local obb dentry
438          * the orig_path should be returned
439          */
440         if(has_graft_path(dent))
441                 sdcardfs_get_orig_path(dent, real_lower);
442         else
443                 sdcardfs_get_lower_path(dent, real_lower);
444 }
445
446 static inline void sdcardfs_put_real_lower(const struct dentry *dent,
447                                                 struct path *real_lower)
448 {
449         if(has_graft_path(dent))
450                 sdcardfs_put_orig_path(dent, real_lower);
451         else
452                 sdcardfs_put_lower_path(dent, real_lower);
453 }
454
455 extern struct mutex sdcardfs_super_list_lock;
456 extern struct list_head sdcardfs_super_list;
457
458 /* for packagelist.c */
459 extern appid_t get_appid(const char *app_name);
460 extern appid_t get_ext_gid(const char *app_name);
461 extern appid_t is_excluded(const char *app_name, userid_t userid);
462 extern int check_caller_access_to_name(struct inode *parent_node, const struct qstr* name);
463 extern int open_flags_to_access_mode(int open_flags);
464 extern int packagelist_init(void);
465 extern void packagelist_exit(void);
466
467 /* for derived_perm.c */
468 #define BY_NAME         (1 << 0)
469 #define BY_USERID       (1 << 1)
470 struct limit_search {
471         unsigned int flags;
472         const char *name;
473         size_t length;
474         userid_t userid;
475 };
476
477 extern void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
478                         uid_t uid, bool under_android, struct inode *top);
479 extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
480 extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name);
481 extern void drop_recursive(struct dentry *parent);
482 extern void fixup_top_recursive(struct dentry *parent);
483 extern void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit);
484
485 extern void update_derived_permission_lock(struct dentry *dentry);
486 void fixup_lower_ownership(struct dentry* dentry, const char *name);
487 extern int need_graft_path(struct dentry *dentry);
488 extern int is_base_obbpath(struct dentry *dentry);
489 extern int is_obbpath_invalid(struct dentry *dentry);
490 extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
491
492 /* locking helpers */
493 static inline struct dentry *lock_parent(struct dentry *dentry)
494 {
495         struct dentry *dir = dget_parent(dentry);
496         mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_PARENT);
497         return dir;
498 }
499
500 static inline void unlock_dir(struct dentry *dir)
501 {
502         mutex_unlock(&d_inode(dir)->i_mutex);
503         dput(dir);
504 }
505
506 static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
507 {
508         int err;
509         struct dentry *dent;
510         struct iattr attrs;
511         struct path parent;
512
513         dent = kern_path_locked(path_s, &parent);
514         if (IS_ERR(dent)) {
515                 err = PTR_ERR(dent);
516                 if (err == -EEXIST)
517                         err = 0;
518                 goto out_unlock;
519         }
520
521         err = vfs_mkdir2(parent.mnt, d_inode(parent.dentry), dent, mode);
522         if (err) {
523                 if (err == -EEXIST)
524                         err = 0;
525                 goto out_dput;
526         }
527
528         attrs.ia_uid = make_kuid(&init_user_ns, uid);
529         attrs.ia_gid = make_kgid(&init_user_ns, gid);
530         attrs.ia_valid = ATTR_UID | ATTR_GID;
531         mutex_lock(&d_inode(dent)->i_mutex);
532         notify_change2(parent.mnt, dent, &attrs, NULL);
533         mutex_unlock(&d_inode(dent)->i_mutex);
534
535 out_dput:
536         dput(dent);
537
538 out_unlock:
539         /* parent dentry locked by lookup_create */
540         mutex_unlock(&d_inode(parent.dentry)->i_mutex);
541         path_put(&parent);
542         return err;
543 }
544
545 /*
546  * Return 1, if a disk has enough free space, otherwise 0.
547  * We assume that any files can not be overwritten.
548  */
549 static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
550 {
551         int err;
552         struct path lower_path;
553         struct kstatfs statfs;
554         u64 avail;
555         struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
556
557         if (sbi->options.reserved_mb) {
558                 /* Get fs stat of lower filesystem. */
559                 sdcardfs_get_lower_path(dentry, &lower_path);
560                 err = vfs_statfs(&lower_path, &statfs);
561                 sdcardfs_put_lower_path(dentry, &lower_path);
562
563                 if (unlikely(err))
564                         return 0;
565
566                 /* Invalid statfs informations. */
567                 if (unlikely(statfs.f_bsize == 0))
568                         return 0;
569
570                 /* if you are checking directory, set size to f_bsize. */
571                 if (unlikely(dir))
572                         size = statfs.f_bsize;
573
574                 /* available size */
575                 avail = statfs.f_bavail * statfs.f_bsize;
576
577                 /* not enough space */
578                 if ((u64)size > avail)
579                         return 0;
580
581                 /* enough space */
582                 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
583                         return 1;
584
585                 return 0;
586         } else
587                 return 1;
588 }
589
590 /*
591  * Copies attrs and maintains sdcardfs managed attrs
592  * Since our permission check handles all special permissions, set those to be open
593  */
594 static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src)
595 {
596         dest->i_mode = (src->i_mode  & S_IFMT) | S_IRWXU | S_IRWXG |
597                         S_IROTH | S_IXOTH; /* 0775 */
598         dest->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(dest)->d_uid);
599         dest->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW);
600         dest->i_rdev = src->i_rdev;
601         dest->i_atime = src->i_atime;
602         dest->i_mtime = src->i_mtime;
603         dest->i_ctime = src->i_ctime;
604         dest->i_blkbits = src->i_blkbits;
605         dest->i_flags = src->i_flags;
606         set_nlink(dest, src->i_nlink);
607 }
608
609 static inline bool str_case_eq(const char *s1, const char *s2)
610 {
611         return !strcasecmp(s1, s2);
612 }
613
614 static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2)
615 {
616         return q1->len == q2->len && str_case_eq(q1->name, q2->name);
617 }
618
619 #define QSTR_LITERAL(string) QSTR_INIT(string, sizeof(string)-1)
620
621 #endif  /* not _SDCARDFS_H_ */