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