2 * fs/sdcardfs/derived_perm.c
4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
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
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.
23 /* copy derived state from parent inode */
24 static void inherit_derived_state(struct inode *parent, struct inode *child)
26 struct sdcardfs_inode_info *pi = SDCARDFS_I(parent);
27 struct sdcardfs_inode_info *ci = SDCARDFS_I(child);
29 ci->perm = PERM_INHERIT;
30 ci->userid = pi->userid;
31 ci->d_uid = pi->d_uid;
32 ci->under_android = pi->under_android;
33 ci->under_cache = pi->under_cache;
34 ci->under_obb = pi->under_obb;
38 /* helper function for derived state */
39 void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
40 uid_t uid, bool under_android, struct inode *top)
42 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
45 info->userid = userid;
47 info->under_android = under_android;
48 info->under_cache = false;
49 info->under_obb = false;
53 /* While renaming, there is a point where we want the path from dentry, but the name from newdentry */
54 void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name)
56 struct sdcardfs_inode_info *info = SDCARDFS_I(d_inode(dentry));
57 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent));
59 struct qstr q_Android = QSTR_LITERAL("Android");
60 struct qstr q_data = QSTR_LITERAL("data");
61 struct qstr q_obb = QSTR_LITERAL("obb");
62 struct qstr q_media = QSTR_LITERAL("media");
63 struct qstr q_cache = QSTR_LITERAL("cache");
65 /* By default, each inode inherits from its parent.
66 * the properties are maintained on its private fields
67 * because the inode attributes will be modified with that of
69 * These values are used by our custom permission call instead
70 * of using the inode permissions.
73 inherit_derived_state(d_inode(parent), d_inode(dentry));
75 /* Files don't get special labels */
76 if (!S_ISDIR(d_inode(dentry)->i_mode))
78 /* Derive custom permissions based on parent and current node */
79 switch (parent_info->perm) {
81 case PERM_ANDROID_PACKAGE_CACHE:
82 /* Already inherited above */
85 /* Legacy internal layout places users at top level */
86 info->perm = PERM_ROOT;
87 info->userid = simple_strtoul(name->name, NULL, 10);
88 set_top(info, &info->vfs_inode);
91 /* Assume masked off by default. */
92 if (qstr_case_eq(name, &q_Android)) {
93 /* App-specific directories inside; let anyone traverse */
94 info->perm = PERM_ANDROID;
95 info->under_android = true;
96 set_top(info, &info->vfs_inode);
100 if (qstr_case_eq(name, &q_data)) {
101 /* App-specific directories inside; let anyone traverse */
102 info->perm = PERM_ANDROID_DATA;
103 set_top(info, &info->vfs_inode);
104 } else if (qstr_case_eq(name, &q_obb)) {
105 /* App-specific directories inside; let anyone traverse */
106 info->perm = PERM_ANDROID_OBB;
107 info->under_obb = true;
108 set_top(info, &info->vfs_inode);
109 /* Single OBB directory is always shared */
110 } else if (qstr_case_eq(name, &q_media)) {
111 /* App-specific directories inside; let anyone traverse */
112 info->perm = PERM_ANDROID_MEDIA;
113 set_top(info, &info->vfs_inode);
116 case PERM_ANDROID_OBB:
117 case PERM_ANDROID_DATA:
118 case PERM_ANDROID_MEDIA:
119 info->perm = PERM_ANDROID_PACKAGE;
120 appid = get_appid(name->name);
121 if (appid != 0 && !is_excluded(name->name, parent_info->userid)) {
122 info->d_uid = multiuser_get_uid(parent_info->userid, appid);
124 set_top(info, &info->vfs_inode);
126 case PERM_ANDROID_PACKAGE:
127 if (qstr_case_eq(name, &q_cache)) {
128 info->perm = PERM_ANDROID_PACKAGE_CACHE;
129 info->under_cache = true;
135 void get_derived_permission(struct dentry *parent, struct dentry *dentry)
137 get_derived_permission_new(parent, dentry, &dentry->d_name);
140 static appid_t get_type(const char *name) {
141 const char *ext = strrchr(name, '.');
146 id = get_ext_gid(ext);
147 return id?:AID_MEDIA_RW;
152 void fixup_lower_ownership(struct dentry* dentry, const char *name) {
155 struct inode *delegated_inode = NULL;
157 struct sdcardfs_inode_info *info;
158 struct sdcardfs_inode_info *info_top;
160 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
161 uid_t uid = sbi->options.fs_low_uid;
162 gid_t gid = sbi->options.fs_low_gid;
163 struct iattr newattrs;
165 info = SDCARDFS_I(d_inode(dentry));
167 if (info->under_obb) {
168 perm = PERM_ANDROID_OBB;
169 } else if (info->under_cache) {
170 perm = PERM_ANDROID_PACKAGE_CACHE;
171 } else if (perm == PERM_INHERIT) {
172 info_top = SDCARDFS_I(grab_top(info));
173 perm = info_top->perm;
180 case PERM_ANDROID_DATA:
181 case PERM_ANDROID_MEDIA:
182 case PERM_ANDROID_PACKAGE:
183 case PERM_ANDROID_PACKAGE_CACHE:
184 uid = multiuser_get_uid(info->userid, uid);
186 case PERM_ANDROID_OBB:
196 case PERM_ANDROID_DATA:
197 case PERM_ANDROID_MEDIA:
198 if (S_ISDIR(d_inode(dentry)->i_mode))
199 gid = multiuser_get_uid(info->userid, AID_MEDIA_RW);
201 gid = multiuser_get_uid(info->userid, get_type(name));
203 case PERM_ANDROID_OBB:
206 case PERM_ANDROID_PACKAGE:
207 if (info->d_uid != 0)
208 gid = multiuser_get_ext_gid(info->userid, info->d_uid);
210 gid = multiuser_get_uid(info->userid, uid);
212 case PERM_ANDROID_PACKAGE_CACHE:
213 if (info->d_uid != 0)
214 gid = multiuser_get_cache_gid(info->userid, info->d_uid);
216 gid = multiuser_get_uid(info->userid, uid);
223 sdcardfs_get_lower_path(dentry, &path);
224 inode = d_inode(path.dentry);
225 if (d_inode(path.dentry)->i_gid.val != gid || d_inode(path.dentry)->i_uid.val != uid) {
227 newattrs.ia_valid = ATTR_GID | ATTR_UID | ATTR_FORCE;
228 newattrs.ia_uid = make_kuid(current_user_ns(), uid);
229 newattrs.ia_gid = make_kgid(current_user_ns(), gid);
230 if (!S_ISDIR(inode->i_mode))
232 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
233 mutex_lock(&inode->i_mutex);
234 error = security_path_chown(&path, newattrs.ia_uid, newattrs.ia_gid);
236 error = notify_change2(path.mnt, path.dentry, &newattrs, &delegated_inode);
237 mutex_unlock(&inode->i_mutex);
238 if (delegated_inode) {
239 error = break_deleg_wait(&delegated_inode);
244 pr_err("sdcardfs: Failed to touch up lower fs gid/uid.\n");
248 static int descendant_may_need_fixup(struct sdcardfs_inode_info *info, struct limit_search *limit) {
249 if (info->perm == PERM_ROOT)
250 return (limit->flags & BY_USERID)?info->userid == limit->userid:1;
251 if (info->perm == PERM_PRE_ROOT || info->perm == PERM_ANDROID)
256 static int needs_fixup(perm_t perm) {
257 if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
258 || perm == PERM_ANDROID_MEDIA)
263 void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit) {
264 struct dentry *child;
265 struct sdcardfs_inode_info *info;
268 if (!d_inode(dentry)) {
272 info = SDCARDFS_I(d_inode(dentry));
274 if (needs_fixup(info->perm)) {
275 spin_lock(&dentry->d_lock);
276 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
278 if (!(limit->flags & BY_NAME) || !strncasecmp(child->d_name.name, limit->name, limit->length)) {
279 if (d_inode(child)) {
280 get_derived_permission(dentry, child);
281 fixup_tmp_permissions(d_inode(child));
288 spin_unlock(&dentry->d_lock);
289 } else if (descendant_may_need_fixup(info, limit)) {
290 spin_lock(&dentry->d_lock);
291 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
292 fixup_perms_recursive(child, limit);
294 spin_unlock(&dentry->d_lock);
299 void drop_recursive(struct dentry *parent) {
300 struct dentry *dentry;
301 struct sdcardfs_inode_info *info;
302 if (!d_inode(parent))
304 info = SDCARDFS_I(d_inode(parent));
305 spin_lock(&parent->d_lock);
306 list_for_each_entry(dentry, &parent->d_subdirs, d_child) {
307 if (d_inode(dentry)) {
308 if (SDCARDFS_I(d_inode(parent))->top != SDCARDFS_I(d_inode(dentry))->top) {
309 drop_recursive(dentry);
314 spin_unlock(&parent->d_lock);
317 void fixup_top_recursive(struct dentry *parent) {
318 struct dentry *dentry;
319 struct sdcardfs_inode_info *info;
321 if (!d_inode(parent))
323 info = SDCARDFS_I(d_inode(parent));
324 spin_lock(&parent->d_lock);
325 list_for_each_entry(dentry, &parent->d_subdirs, d_child) {
326 if (d_inode(dentry)) {
327 if (SDCARDFS_I(d_inode(parent))->top != SDCARDFS_I(d_inode(dentry))->top) {
328 get_derived_permission(parent, dentry);
329 fixup_tmp_permissions(d_inode(dentry));
330 fixup_top_recursive(dentry);
334 spin_unlock(&parent->d_lock);
337 /* main function for updating derived permission */
338 inline void update_derived_permission_lock(struct dentry *dentry)
340 struct dentry *parent;
342 if(!dentry || !d_inode(dentry)) {
343 printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__);
347 * 1. need to check whether the dentry is updated or not
348 * 2. remove the root dentry update
350 if(IS_ROOT(dentry)) {
351 //setup_default_pre_root_state(d_inode(dentry));
353 parent = dget_parent(dentry);
355 get_derived_permission(parent, dentry);
359 fixup_tmp_permissions(d_inode(dentry));
362 int need_graft_path(struct dentry *dentry)
365 struct dentry *parent = dget_parent(dentry);
366 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent));
367 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
368 struct qstr obb = QSTR_LITERAL("obb");
370 if(parent_info->perm == PERM_ANDROID &&
371 qstr_case_eq(&dentry->d_name, &obb)) {
373 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
374 if(!(sbi->options.multiuser == false
375 && parent_info->userid == 0)) {
383 int is_obbpath_invalid(struct dentry *dent)
386 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
387 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
388 char *path_buf, *obbpath_s;
390 /* check the base obbpath has been changed.
391 * this routine can check an uninitialized obb dentry as well.
392 * regarding the uninitialized obb, refer to the sdcardfs_mkdir() */
393 spin_lock(&di->lock);
394 if(di->orig_path.dentry) {
395 if(!di->lower_path.dentry) {
398 path_get(&di->lower_path);
399 //lower_parent = lock_parent(lower_path->dentry);
401 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
404 printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__);
406 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
407 if (d_unhashed(di->lower_path.dentry) ||
408 !str_case_eq(sbi->obbpath_s, obbpath_s)) {
414 //unlock_dir(lower_parent);
415 path_put(&di->lower_path);
418 spin_unlock(&di->lock);
422 int is_base_obbpath(struct dentry *dentry)
425 struct dentry *parent = dget_parent(dentry);
426 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent));
427 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
428 struct qstr q_obb = QSTR_LITERAL("obb");
430 spin_lock(&SDCARDFS_D(dentry)->lock);
431 if (sbi->options.multiuser) {
432 if(parent_info->perm == PERM_PRE_ROOT &&
433 qstr_case_eq(&dentry->d_name, &q_obb)) {
436 } else if (parent_info->perm == PERM_ANDROID &&
437 qstr_case_eq(&dentry->d_name, &q_obb)) {
440 spin_unlock(&SDCARDFS_D(dentry)->lock);
444 /* The lower_path will be stored to the dentry's orig_path
445 * and the base obbpath will be copyed to the lower_path variable.
446 * if an error returned, there's no change in the lower_path
447 * returns: -ERRNO if error (0: no error) */
448 int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
451 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
454 /* A local obb dentry must have its own orig_path to support rmdir
455 * and mkdir of itself. Usually, we expect that the sbi->obbpath
456 * is avaiable on this stage. */
457 sdcardfs_set_orig_path(dentry, lower_path);
459 err = kern_path(sbi->obbpath_s,
460 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
463 /* the obbpath base has been found */
464 printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n");
465 pathcpy(lower_path, &obbpath);
467 /* if the sbi->obbpath is not available, we can optionally
468 * setup the lower_path with its orig_path.
469 * but, the current implementation just returns an error
470 * because the sdcard daemon also regards this case as
472 printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n");