blob: 401445e9121dd03961020964c4e263ffa15fa252 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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/kref.h>
34#include <linux/mm.h>
35#include <linux/mount.h>
36#include <linux/namei.h>
37#include <linux/seq_file.h>
38#include <linux/statfs.h>
39#include <linux/fs_stack.h>
40#include <linux/magic.h>
41#include <linux/uaccess.h>
42#include <linux/slab.h>
43#include <linux/sched.h>
44#include <linux/types.h>
45#include <linux/security.h>
46#include <linux/string.h>
47#include <linux/list.h>
48#include <linux/iversion.h>
49#include "multiuser.h"
50
51/* the file system name */
52#define SDCARDFS_NAME "sdcardfs"
53
54/* sdcardfs root inode number */
55#define SDCARDFS_ROOT_INO 1
56
57/* useful for tracking code reachability */
58#define UDBG pr_default("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
59
60#define SDCARDFS_DIRENT_SIZE 256
61
62/* temporary static uid settings for development */
63#define AID_ROOT 0 /* uid for accessing /mnt/sdcard & extSdcard */
64#define AID_MEDIA_RW 1023 /* internal media storage write access */
65
66#define AID_SDCARD_RW 1015 /* external storage write access */
67#define AID_SDCARD_R 1028 /* external storage read access */
68#define AID_SDCARD_PICS 1033 /* external storage photos access */
69#define AID_SDCARD_AV 1034 /* external storage audio/video access */
70#define AID_SDCARD_ALL 1035 /* access all users external storage */
71#define AID_MEDIA_OBB 1059 /* obb files */
72
73#define AID_SDCARD_IMAGE 1057
74
75#define AID_PACKAGE_INFO 1027
76
77
78/*
79 * Permissions are handled by our permission function.
80 * We don't want anyone who happens to look at our inode value to prematurely
81 * block access, so store more permissive values. These are probably never
82 * used.
83 */
84#define fixup_tmp_permissions(x) \
85 do { \
86 (x)->i_uid = make_kuid(&init_user_ns, \
87 SDCARDFS_I(x)->data->d_uid); \
88 (x)->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW); \
89 (x)->i_mode = ((x)->i_mode & S_IFMT) | 0775;\
90 } while (0)
91
92/* Android 5.0 support */
93
94/* Permission mode for a specific node. Controls how file permissions
95 * are derived for children nodes.
96 */
97typedef enum {
98 /* Nothing special; this node should just inherit from its parent. */
99 PERM_INHERIT,
100 /* This node is one level above a normal root; used for legacy layouts
101 * which use the first level to represent user_id.
102 */
103 PERM_PRE_ROOT,
104 /* This node is "/" */
105 PERM_ROOT,
106 /* This node is "/Android" */
107 PERM_ANDROID,
108 /* This node is "/Android/data" */
109 PERM_ANDROID_DATA,
110 /* This node is "/Android/obb" */
111 PERM_ANDROID_OBB,
112 /* This node is "/Android/media" */
113 PERM_ANDROID_MEDIA,
114 /* This node is "/Android/[data|media|obb]/[package]" */
115 PERM_ANDROID_PACKAGE,
116 /* This node is "/Android/[data|media|obb]/[package]/cache" */
117 PERM_ANDROID_PACKAGE_CACHE,
118} perm_t;
119
120struct sdcardfs_sb_info;
121struct sdcardfs_mount_options;
122struct sdcardfs_inode_info;
123struct sdcardfs_inode_data;
124
125/* Do not directly use this function. Use OVERRIDE_CRED() instead. */
126const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
127 struct sdcardfs_inode_data *data);
128/* Do not directly use this function, use REVERT_CRED() instead. */
129void revert_fsids(const struct cred *old_cred);
130
131/* operations vectors defined in specific files */
132extern const struct file_operations sdcardfs_main_fops;
133extern const struct file_operations sdcardfs_dir_fops;
134extern const struct inode_operations sdcardfs_main_iops;
135extern const struct inode_operations sdcardfs_dir_iops;
136extern const struct inode_operations sdcardfs_symlink_iops;
137extern const struct super_operations sdcardfs_sops;
138extern const struct dentry_operations sdcardfs_ci_dops;
139extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
140extern const struct vm_operations_struct sdcardfs_vm_ops;
141
142extern int sdcardfs_init_inode_cache(void);
143extern void sdcardfs_destroy_inode_cache(void);
144extern int sdcardfs_init_dentry_cache(void);
145extern void sdcardfs_destroy_dentry_cache(void);
146extern int new_dentry_private_data(struct dentry *dentry);
147extern void free_dentry_private_data(struct dentry *dentry);
148extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
149 unsigned int flags);
150extern struct inode *sdcardfs_iget(struct super_block *sb,
151 struct inode *lower_inode, userid_t id);
152extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
153 struct path *lower_path, userid_t id);
154extern int sdcardfs_on_fscrypt_key_removed(struct notifier_block *nb,
155 unsigned long action, void *data);
156
157/* file private data */
158struct sdcardfs_file_info {
159 struct file *lower_file;
160 const struct vm_operations_struct *lower_vm_ops;
161};
162
163struct sdcardfs_inode_data {
164 struct kref refcount;
165 bool abandoned;
166
167 perm_t perm;
168 userid_t userid;
169 uid_t d_uid;
170 bool under_android;
171 bool under_cache;
172 bool under_obb;
173};
174
175/* sdcardfs inode data in memory */
176struct sdcardfs_inode_info {
177 struct inode *lower_inode;
178 /* state derived based on current position in hierarchy */
179 struct sdcardfs_inode_data *data;
180
181 /* top folder for ownership */
182 spinlock_t top_lock;
183 struct sdcardfs_inode_data *top_data;
184
185 struct inode vfs_inode;
186};
187
188
189/* sdcardfs dentry data in memory */
190struct sdcardfs_dentry_info {
191 spinlock_t lock; /* protects lower_path */
192 struct path lower_path;
193 struct path orig_path;
194};
195
196struct sdcardfs_mount_options {
197 uid_t fs_low_uid;
198 gid_t fs_low_gid;
199 userid_t fs_user_id;
200 bool multiuser;
201 bool gid_derivation;
202 bool default_normal;
203 bool unshared_obb;
204 unsigned int reserved_mb;
205 bool nocache;
206};
207
208struct sdcardfs_vfsmount_options {
209 gid_t gid;
210 mode_t mask;
211};
212
213extern int parse_options_remount(struct super_block *sb, char *options, int silent,
214 struct sdcardfs_vfsmount_options *vfsopts);
215
216/* sdcardfs super-block data in memory */
217struct sdcardfs_sb_info {
218 struct super_block *sb;
219 struct super_block *lower_sb;
220 /* derived perm policy : some of options have been added
221 * to sdcardfs_mount_options (Android 4.4 support)
222 */
223 struct sdcardfs_mount_options options;
224 spinlock_t lock; /* protects obbpath */
225 char *obbpath_s;
226 struct path obbpath;
227 void *pkgl_id;
228 struct list_head list;
229 struct notifier_block fscrypt_nb;
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 */
239static 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 */
254static inline struct file *sdcardfs_lower_file(const struct file *f)
255{
256 return SDCARDFS_F(f)->lower_file;
257}
258
259static 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. */
265static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
266{
267 return SDCARDFS_I(i)->lower_inode;
268}
269
270static 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 */
276static inline struct super_block *sdcardfs_lower_super(
277 const struct super_block *sb)
278{
279 return SDCARDFS_SB(sb)->lower_sb;
280}
281
282static 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 */
289static 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) \
299static 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} \
308static inline void sdcardfs_put_##pname(const struct dentry *dent, \
309 struct path *pname) \
310{ \
311 path_put(pname); \
312 return; \
313} \
314static 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} \
322static 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} \
330static 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
345SDCARDFS_DENT_FUNC(lower_path)
346SDCARDFS_DENT_FUNC(orig_path)
347
348static inline bool sbinfo_has_sdcard_magic(struct sdcardfs_sb_info *sbinfo)
349{
350 return sbinfo && sbinfo->sb
351 && sbinfo->sb->s_magic == SDCARDFS_SUPER_MAGIC;
352}
353
354static inline struct sdcardfs_inode_data *data_get(
355 struct sdcardfs_inode_data *data)
356{
357 if (data)
358 kref_get(&data->refcount);
359 return data;
360}
361
362static inline struct sdcardfs_inode_data *top_data_get(
363 struct sdcardfs_inode_info *info)
364{
365 struct sdcardfs_inode_data *top_data;
366
367 spin_lock(&info->top_lock);
368 top_data = data_get(info->top_data);
369 spin_unlock(&info->top_lock);
370 return top_data;
371}
372
373extern void data_release(struct kref *ref);
374
375static inline void data_put(struct sdcardfs_inode_data *data)
376{
377 kref_put(&data->refcount, data_release);
378}
379
380static inline void release_own_data(struct sdcardfs_inode_info *info)
381{
382 /*
383 * This happens exactly once per inode. At this point, the inode that
384 * originally held this data is about to be freed, and all references
385 * to it are held as a top value, and will likely be released soon.
386 */
387 info->data->abandoned = true;
388 data_put(info->data);
389}
390
391static inline void set_top(struct sdcardfs_inode_info *info,
392 struct sdcardfs_inode_info *top_owner)
393{
394 struct sdcardfs_inode_data *old_top;
395 struct sdcardfs_inode_data *new_top = NULL;
396
397 if (top_owner)
398 new_top = top_data_get(top_owner);
399
400 spin_lock(&info->top_lock);
401 old_top = info->top_data;
402 info->top_data = new_top;
403 if (old_top)
404 data_put(old_top);
405 spin_unlock(&info->top_lock);
406}
407
408static inline int get_gid(struct vfsmount *mnt,
409 struct super_block *sb,
410 struct sdcardfs_inode_data *data)
411{
412 struct sdcardfs_vfsmount_options *vfsopts = mnt->data;
413 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(sb);
414
415 if (vfsopts->gid == AID_SDCARD_RW && !sbi->options.default_normal)
416 /* As an optimization, certain trusted system components only run
417 * as owner but operate across all users. Since we're now handing
418 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
419 * the user boundary enforcement for the default view. The UIDs
420 * assigned to app directories are still multiuser aware.
421 */
422 return AID_SDCARD_RW;
423 else
424 return multiuser_get_uid(data->userid, vfsopts->gid);
425}
426
427static inline int get_mode(struct vfsmount *mnt,
428 struct sdcardfs_inode_info *info,
429 struct sdcardfs_inode_data *data)
430{
431 int owner_mode;
432 int filtered_mode;
433 struct sdcardfs_vfsmount_options *opts = mnt->data;
434 int visible_mode = 0775 & ~opts->mask;
435
436
437 if (data->perm == PERM_PRE_ROOT) {
438 /* Top of multi-user view should always be visible to ensure
439 * secondary users can traverse inside.
440 */
441 visible_mode = 0711;
442 } else if (data->under_android) {
443 /* Block "other" access to Android directories, since only apps
444 * belonging to a specific user should be in there; we still
445 * leave +x open for the default view.
446 */
447 if (opts->gid == AID_SDCARD_RW)
448 visible_mode = visible_mode & ~0006;
449 else
450 visible_mode = visible_mode & ~0007;
451 }
452 owner_mode = info->lower_inode->i_mode & 0700;
453 filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
454 return filtered_mode;
455}
456
457static inline int has_graft_path(const struct dentry *dent)
458{
459 int ret = 0;
460
461 spin_lock(&SDCARDFS_D(dent)->lock);
462 if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
463 ret = 1;
464 spin_unlock(&SDCARDFS_D(dent)->lock);
465
466 return ret;
467}
468
469static inline void sdcardfs_get_real_lower(const struct dentry *dent,
470 struct path *real_lower)
471{
472 /* in case of a local obb dentry
473 * the orig_path should be returned
474 */
475 if (has_graft_path(dent))
476 sdcardfs_get_orig_path(dent, real_lower);
477 else
478 sdcardfs_get_lower_path(dent, real_lower);
479}
480
481static inline void sdcardfs_put_real_lower(const struct dentry *dent,
482 struct path *real_lower)
483{
484 if (has_graft_path(dent))
485 sdcardfs_put_orig_path(dent, real_lower);
486 else
487 sdcardfs_put_lower_path(dent, real_lower);
488}
489
490extern struct mutex sdcardfs_super_list_lock;
491extern struct list_head sdcardfs_super_list;
492
493/* for packagelist.c */
494extern appid_t get_appid(const char *app_name);
495extern appid_t get_ext_gid(const char *app_name);
496extern appid_t is_excluded(const char *app_name, userid_t userid);
497extern int check_caller_access_to_name(struct inode *parent_node, const struct qstr *name);
498extern int packagelist_init(void);
499extern void packagelist_exit(void);
500
501/* for derived_perm.c */
502#define BY_NAME (1 << 0)
503#define BY_USERID (1 << 1)
504struct limit_search {
505 unsigned int flags;
506 struct qstr name;
507 userid_t userid;
508};
509
510extern void setup_derived_state(struct inode *inode, perm_t perm,
511 userid_t userid, uid_t uid);
512extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
513extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name);
514extern void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit);
515
516extern void update_derived_permission_lock(struct dentry *dentry);
517void fixup_lower_ownership(struct dentry *dentry, const char *name);
518extern int need_graft_path(struct dentry *dentry);
519extern int is_base_obbpath(struct dentry *dentry);
520extern int is_obbpath_invalid(struct dentry *dentry);
521extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
522
523/* locking helpers */
524static inline struct dentry *lock_parent(struct dentry *dentry)
525{
526 struct dentry *dir = dget_parent(dentry);
527
528 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
529 return dir;
530}
531
532static inline void unlock_dir(struct dentry *dir)
533{
534 inode_unlock(d_inode(dir));
535 dput(dir);
536}
537
538static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
539{
540 int err;
541 struct dentry *dent;
542 struct iattr attrs;
543 struct path parent;
544
545 dent = kern_path_locked(path_s, &parent);
546 if (IS_ERR(dent)) {
547 err = PTR_ERR(dent);
548 if (err == -EEXIST)
549 err = 0;
550 goto out_unlock;
551 }
552
553 err = vfs_mkdir2(parent.mnt, d_inode(parent.dentry), dent, mode);
554 if (err) {
555 if (err == -EEXIST)
556 err = 0;
557 goto out_dput;
558 }
559
560 attrs.ia_uid = make_kuid(&init_user_ns, uid);
561 attrs.ia_gid = make_kgid(&init_user_ns, gid);
562 attrs.ia_valid = ATTR_UID | ATTR_GID;
563 inode_lock(d_inode(dent));
564 notify_change2(parent.mnt, dent, &attrs, NULL);
565 inode_unlock(d_inode(dent));
566
567out_dput:
568 dput(dent);
569
570out_unlock:
571 /* parent dentry locked by lookup_create */
572 inode_unlock(d_inode(parent.dentry));
573 path_put(&parent);
574 return err;
575}
576
577/*
578 * Return 1, if a disk has enough free space, otherwise 0.
579 * We assume that any files can not be overwritten.
580 */
581static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
582{
583 int err;
584 struct path lower_path;
585 struct kstatfs statfs;
586 u64 avail;
587 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
588
589 if (sbi->options.reserved_mb) {
590 /* Get fs stat of lower filesystem. */
591 sdcardfs_get_lower_path(dentry, &lower_path);
592 err = vfs_statfs(&lower_path, &statfs);
593 sdcardfs_put_lower_path(dentry, &lower_path);
594
595 if (unlikely(err))
596 return 0;
597
598 /* Invalid statfs informations. */
599 if (unlikely(statfs.f_bsize == 0))
600 return 0;
601
602 /* if you are checking directory, set size to f_bsize. */
603 if (unlikely(dir))
604 size = statfs.f_bsize;
605
606 /* available size */
607 avail = statfs.f_bavail * statfs.f_bsize;
608
609 /* not enough space */
610 if ((u64)size > avail)
611 return 0;
612
613 /* enough space */
614 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
615 return 1;
616
617 return 0;
618 } else
619 return 1;
620}
621
622/*
623 * Copies attrs and maintains sdcardfs managed attrs
624 * Since our permission check handles all special permissions, set those to be open
625 */
626static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src)
627{
628 dest->i_mode = (src->i_mode & S_IFMT) | S_IRWXU | S_IRWXG |
629 S_IROTH | S_IXOTH; /* 0775 */
630 dest->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(dest)->data->d_uid);
631 dest->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW);
632 dest->i_rdev = src->i_rdev;
633 dest->i_atime = src->i_atime;
634 dest->i_mtime = src->i_mtime;
635 dest->i_ctime = src->i_ctime;
636 dest->i_blkbits = src->i_blkbits;
637 dest->i_flags = src->i_flags;
638 set_nlink(dest, src->i_nlink);
639}
640
641static inline bool str_case_eq(const char *s1, const char *s2)
642{
643 return !strcasecmp(s1, s2);
644}
645
646static inline bool str_n_case_eq(const char *s1, const char *s2, size_t len)
647{
648 return !strncasecmp(s1, s2, len);
649}
650
651static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2)
652{
653 return q1->len == q2->len && str_n_case_eq(q1->name, q2->name, q2->len);
654}
655
656#define QSTR_LITERAL(string) QSTR_INIT(string, sizeof(string)-1)
657
658#endif /* not _SDCARDFS_H_ */