blob: 128b3e56851fbff4f822ea0b9206271edccd8e12 [file] [log] [blame]
Daniel Campello35c9e242015-07-20 16:23:50 -07001/*
2 * fs/sdcardfs/derived_perm.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
23/* copy derived state from parent inode */
24static void inherit_derived_state(struct inode *parent, struct inode *child)
25{
26 struct sdcardfs_inode_info *pi = SDCARDFS_I(parent);
27 struct sdcardfs_inode_info *ci = SDCARDFS_I(child);
28
29 ci->perm = PERM_INHERIT;
30 ci->userid = pi->userid;
31 ci->d_uid = pi->d_uid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080032 ci->under_android = pi->under_android;
Daniel Campello35c9e242015-07-20 16:23:50 -070033}
34
35/* helper function for derived state */
36void setup_derived_state(struct inode *inode, perm_t perm,
Daniel Rosenberg497ac902016-02-03 21:08:21 -080037 userid_t userid, uid_t uid, bool under_android)
Daniel Campello35c9e242015-07-20 16:23:50 -070038{
39 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
40
41 info->perm = perm;
42 info->userid = userid;
43 info->d_uid = uid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080044 info->under_android = under_android;
Daniel Campello35c9e242015-07-20 16:23:50 -070045}
46
Daniel Rosenberg497ac902016-02-03 21:08:21 -080047/* While renaming, there is a point where we want the path from dentry, but the name from newdentry */
48void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry)
Daniel Campello35c9e242015-07-20 16:23:50 -070049{
50 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
51 struct sdcardfs_inode_info *info = SDCARDFS_I(dentry->d_inode);
52 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
53 appid_t appid;
54
55 /* By default, each inode inherits from its parent.
56 * the properties are maintained on its private fields
57 * because the inode attributes will be modified with that of
58 * its lower inode.
59 * The derived state will be updated on the last
60 * stage of each system call by fix_derived_permission(inode).
61 */
62
63 inherit_derived_state(parent->d_inode, dentry->d_inode);
64
Daniel Campello35c9e242015-07-20 16:23:50 -070065 /* Derive custom permissions based on parent and current node */
66 switch (parent_info->perm) {
67 case PERM_INHERIT:
68 /* Already inherited above */
69 break;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080070 case PERM_PRE_ROOT:
Daniel Campello35c9e242015-07-20 16:23:50 -070071 /* Legacy internal layout places users at top level */
72 info->perm = PERM_ROOT;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080073 info->userid = simple_strtoul(newdentry->d_name.name, NULL, 10);
Daniel Campello35c9e242015-07-20 16:23:50 -070074 break;
75 case PERM_ROOT:
76 /* Assume masked off by default. */
Daniel Rosenberg497ac902016-02-03 21:08:21 -080077 if (!strcasecmp(newdentry->d_name.name, "Android")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070078 /* App-specific directories inside; let anyone traverse */
79 info->perm = PERM_ANDROID;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080080 info->under_android = true;
Daniel Campello35c9e242015-07-20 16:23:50 -070081 }
82 break;
83 case PERM_ANDROID:
Daniel Rosenberg497ac902016-02-03 21:08:21 -080084 if (!strcasecmp(newdentry->d_name.name, "data")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070085 /* App-specific directories inside; let anyone traverse */
86 info->perm = PERM_ANDROID_DATA;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080087 } else if (!strcasecmp(newdentry->d_name.name, "obb")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070088 /* App-specific directories inside; let anyone traverse */
89 info->perm = PERM_ANDROID_OBB;
Daniel Campello35c9e242015-07-20 16:23:50 -070090 /* Single OBB directory is always shared */
Daniel Rosenberg497ac902016-02-03 21:08:21 -080091 } else if (!strcasecmp(newdentry->d_name.name, "media")) {
92 /* App-specific directories inside; let anyone traverse */
93 info->perm = PERM_ANDROID_MEDIA;
Daniel Campello35c9e242015-07-20 16:23:50 -070094 }
95 break;
Daniel Campello35c9e242015-07-20 16:23:50 -070096 case PERM_ANDROID_DATA:
97 case PERM_ANDROID_OBB:
Daniel Rosenberg497ac902016-02-03 21:08:21 -080098 case PERM_ANDROID_MEDIA:
99 appid = get_appid(sbi->pkgl_id, newdentry->d_name.name);
Daniel Campello35c9e242015-07-20 16:23:50 -0700100 if (appid != 0) {
101 info->d_uid = multiuser_get_uid(parent_info->userid, appid);
102 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700103 break;
104 }
105}
106
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800107void get_derived_permission(struct dentry *parent, struct dentry *dentry)
108{
109 get_derived_permission_new(parent, dentry, dentry);
110}
111
112void get_derive_permissions_recursive(struct dentry *parent) {
113 struct dentry *dentry;
114 list_for_each_entry(dentry, &parent->d_subdirs, d_child) {
115 if (dentry && dentry->d_inode) {
116 mutex_lock(&dentry->d_inode->i_mutex);
117 get_derived_permission(parent, dentry);
118 fix_derived_permission(dentry->d_inode);
119 get_derive_permissions_recursive(dentry);
120 mutex_unlock(&dentry->d_inode->i_mutex);
121 }
122 }
123}
124
Daniel Campello35c9e242015-07-20 16:23:50 -0700125/* main function for updating derived permission */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800126inline void update_derived_permission_lock(struct dentry *dentry)
Daniel Campello35c9e242015-07-20 16:23:50 -0700127{
128 struct dentry *parent;
129
130 if(!dentry || !dentry->d_inode) {
131 printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__);
132 return;
133 }
134 /* FIXME:
135 * 1. need to check whether the dentry is updated or not
136 * 2. remove the root dentry update
137 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800138 mutex_lock(&dentry->d_inode->i_mutex);
Daniel Campello35c9e242015-07-20 16:23:50 -0700139 if(IS_ROOT(dentry)) {
140 //setup_default_pre_root_state(dentry->d_inode);
141 } else {
142 parent = dget_parent(dentry);
143 if(parent) {
144 get_derived_permission(parent, dentry);
145 dput(parent);
146 }
147 }
148 fix_derived_permission(dentry->d_inode);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800149 mutex_unlock(&dentry->d_inode->i_mutex);
Daniel Campello35c9e242015-07-20 16:23:50 -0700150}
151
152int need_graft_path(struct dentry *dentry)
153{
154 int ret = 0;
155 struct dentry *parent = dget_parent(dentry);
156 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
157 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
158
159 if(parent_info->perm == PERM_ANDROID &&
160 !strcasecmp(dentry->d_name.name, "obb")) {
161
162 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800163 if(!(sbi->options.multiuser == false
Daniel Campello35c9e242015-07-20 16:23:50 -0700164 && parent_info->userid == 0)) {
165 ret = 1;
166 }
167 }
168 dput(parent);
169 return ret;
170}
171
172int is_obbpath_invalid(struct dentry *dent)
173{
174 int ret = 0;
175 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
176 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
177 char *path_buf, *obbpath_s;
178
179 /* check the base obbpath has been changed.
180 * this routine can check an uninitialized obb dentry as well.
181 * regarding the uninitialized obb, refer to the sdcardfs_mkdir() */
182 spin_lock(&di->lock);
183 if(di->orig_path.dentry) {
184 if(!di->lower_path.dentry) {
185 ret = 1;
186 } else {
187 path_get(&di->lower_path);
188 //lower_parent = lock_parent(lower_path->dentry);
189
190 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
191 if(!path_buf) {
192 ret = 1;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800193 printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700194 } else {
195 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
196 if (d_unhashed(di->lower_path.dentry) ||
197 strcasecmp(sbi->obbpath_s, obbpath_s)) {
198 ret = 1;
199 }
200 kfree(path_buf);
201 }
202
203 //unlock_dir(lower_parent);
204 path_put(&di->lower_path);
205 }
206 }
207 spin_unlock(&di->lock);
208 return ret;
209}
210
211int is_base_obbpath(struct dentry *dentry)
212{
213 int ret = 0;
214 struct dentry *parent = dget_parent(dentry);
215 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
216 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
217
218 spin_lock(&SDCARDFS_D(dentry)->lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800219 if (sbi->options.multiuser) {
220 if(parent_info->perm == PERM_PRE_ROOT &&
221 !strcasecmp(dentry->d_name.name, "obb")) {
222 ret = 1;
223 }
224 } else if (parent_info->perm == PERM_ANDROID &&
Daniel Campello35c9e242015-07-20 16:23:50 -0700225 !strcasecmp(dentry->d_name.name, "obb")) {
226 ret = 1;
227 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700228 spin_unlock(&SDCARDFS_D(dentry)->lock);
Daniel Campello35c9e242015-07-20 16:23:50 -0700229 return ret;
230}
231
232/* The lower_path will be stored to the dentry's orig_path
233 * and the base obbpath will be copyed to the lower_path variable.
234 * if an error returned, there's no change in the lower_path
235 * returns: -ERRNO if error (0: no error) */
236int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
237{
238 int err = 0;
239 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
240 struct path obbpath;
241
242 /* A local obb dentry must have its own orig_path to support rmdir
243 * and mkdir of itself. Usually, we expect that the sbi->obbpath
244 * is avaiable on this stage. */
245 sdcardfs_set_orig_path(dentry, lower_path);
246
247 err = kern_path(sbi->obbpath_s,
248 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
249
250 if(!err) {
251 /* the obbpath base has been found */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800252 printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700253 pathcpy(lower_path, &obbpath);
254 } else {
255 /* if the sbi->obbpath is not available, we can optionally
256 * setup the lower_path with its orig_path.
257 * but, the current implementation just returns an error
258 * because the sdcard daemon also regards this case as
259 * a lookup fail. */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800260 printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700261 }
262 return err;
263}
264
265