blob: 89daf69efbaa45d27425060358228d5d0d18cf56 [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 Rosenberg5080d242016-05-18 16:57:10 -070033 set_top(ci, pi->top);
Daniel Campello35c9e242015-07-20 16:23:50 -070034}
35
36/* helper function for derived state */
Daniel Rosenberg5080d242016-05-18 16:57:10 -070037void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid,
38 uid_t uid, bool under_android, struct inode *top)
Daniel Campello35c9e242015-07-20 16:23:50 -070039{
40 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
41
42 info->perm = perm;
43 info->userid = userid;
44 info->d_uid = uid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080045 info->under_android = under_android;
Daniel Rosenberg5080d242016-05-18 16:57:10 -070046 set_top(info, top);
Daniel Campello35c9e242015-07-20 16:23:50 -070047}
48
Daniel Rosenberg497ac902016-02-03 21:08:21 -080049/* While renaming, there is a point where we want the path from dentry, but the name from newdentry */
50void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry)
Daniel Campello35c9e242015-07-20 16:23:50 -070051{
Daniel Campello35c9e242015-07-20 16:23:50 -070052 struct sdcardfs_inode_info *info = SDCARDFS_I(dentry->d_inode);
53 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
54 appid_t appid;
55
56 /* By default, each inode inherits from its parent.
57 * the properties are maintained on its private fields
58 * because the inode attributes will be modified with that of
59 * its lower inode.
60 * The derived state will be updated on the last
61 * stage of each system call by fix_derived_permission(inode).
62 */
63
64 inherit_derived_state(parent->d_inode, dentry->d_inode);
65
Daniel Campello35c9e242015-07-20 16:23:50 -070066 /* Derive custom permissions based on parent and current node */
67 switch (parent_info->perm) {
68 case PERM_INHERIT:
69 /* Already inherited above */
70 break;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080071 case PERM_PRE_ROOT:
Daniel Campello35c9e242015-07-20 16:23:50 -070072 /* Legacy internal layout places users at top level */
73 info->perm = PERM_ROOT;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080074 info->userid = simple_strtoul(newdentry->d_name.name, NULL, 10);
Daniel Rosenberg5080d242016-05-18 16:57:10 -070075 set_top(info, &info->vfs_inode);
Daniel Campello35c9e242015-07-20 16:23:50 -070076 break;
77 case PERM_ROOT:
78 /* Assume masked off by default. */
Daniel Rosenberg497ac902016-02-03 21:08:21 -080079 if (!strcasecmp(newdentry->d_name.name, "Android")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070080 /* App-specific directories inside; let anyone traverse */
81 info->perm = PERM_ANDROID;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080082 info->under_android = true;
Daniel Rosenberg5080d242016-05-18 16:57:10 -070083 set_top(info, &info->vfs_inode);
Daniel Campello35c9e242015-07-20 16:23:50 -070084 }
85 break;
86 case PERM_ANDROID:
Daniel Rosenberg497ac902016-02-03 21:08:21 -080087 if (!strcasecmp(newdentry->d_name.name, "data")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070088 /* App-specific directories inside; let anyone traverse */
89 info->perm = PERM_ANDROID_DATA;
Daniel Rosenberg5080d242016-05-18 16:57:10 -070090 set_top(info, &info->vfs_inode);
Daniel Rosenberg497ac902016-02-03 21:08:21 -080091 } else if (!strcasecmp(newdentry->d_name.name, "obb")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070092 /* App-specific directories inside; let anyone traverse */
93 info->perm = PERM_ANDROID_OBB;
Daniel Rosenberg5080d242016-05-18 16:57:10 -070094 set_top(info, &info->vfs_inode);
Daniel Campello35c9e242015-07-20 16:23:50 -070095 /* Single OBB directory is always shared */
Daniel Rosenberg497ac902016-02-03 21:08:21 -080096 } else if (!strcasecmp(newdentry->d_name.name, "media")) {
97 /* App-specific directories inside; let anyone traverse */
98 info->perm = PERM_ANDROID_MEDIA;
Daniel Rosenberg5080d242016-05-18 16:57:10 -070099 set_top(info, &info->vfs_inode);
Daniel Campello35c9e242015-07-20 16:23:50 -0700100 }
101 break;
Daniel Campello35c9e242015-07-20 16:23:50 -0700102 case PERM_ANDROID_DATA:
103 case PERM_ANDROID_OBB:
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800104 case PERM_ANDROID_MEDIA:
Daniel Rosenbergfbd34b62016-05-10 13:42:43 -0700105 appid = get_appid(newdentry->d_name.name);
Daniel Campello35c9e242015-07-20 16:23:50 -0700106 if (appid != 0) {
107 info->d_uid = multiuser_get_uid(parent_info->userid, appid);
108 }
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700109 set_top(info, &info->vfs_inode);
Daniel Campello35c9e242015-07-20 16:23:50 -0700110 break;
111 }
112}
113
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800114void get_derived_permission(struct dentry *parent, struct dentry *dentry)
115{
116 get_derived_permission_new(parent, dentry, dentry);
117}
118
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700119static int descendant_may_need_fixup(perm_t perm) {
120 if (perm == PERM_PRE_ROOT || perm == PERM_ROOT || perm == PERM_ANDROID)
121 return 1;
122 return 0;
123}
124
125static int needs_fixup(perm_t perm) {
126 if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
127 || perm == PERM_ANDROID_MEDIA)
128 return 1;
129 return 0;
130}
131
132void fixup_perms_recursive(struct dentry *dentry, const char* name, size_t len) {
133 struct dentry *child;
134 struct sdcardfs_inode_info *info;
135 if (!dget(dentry))
136 return;
137 if (!dentry->d_inode) {
138 dput(dentry);
139 return;
140 }
141 info = SDCARDFS_I(d_inode(dentry));
142
143 if (needs_fixup(info->perm)) {
144 mutex_lock(&d_inode(dentry)->i_mutex);
145 child = lookup_one_len(name, dentry, len);
146 mutex_unlock(&d_inode(dentry)->i_mutex);
147 if (!IS_ERR(child)) {
148 if (child->d_inode) {
149 get_derived_permission(dentry, child);
150 fix_derived_permission(d_inode(child));
151 }
152 dput(child);
153 }
154 } else if (descendant_may_need_fixup(info->perm)) {
155 mutex_lock(&d_inode(dentry)->i_mutex);
156 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
157 fixup_perms_recursive(child, name, len);
158 }
159 mutex_unlock(&d_inode(dentry)->i_mutex);
160 }
161 dput(dentry);
162}
163
164void fixup_top_recursive(struct dentry *parent) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800165 struct dentry *dentry;
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700166 struct sdcardfs_inode_info *info;
167 if (!d_inode(parent))
168 return;
169 info = SDCARDFS_I(d_inode(parent));
Daniel Rosenbergcb1b9452016-08-16 15:19:26 -0700170 spin_lock(&parent->d_lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800171 list_for_each_entry(dentry, &parent->d_subdirs, d_child) {
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700172 if (d_inode(dentry)) {
173 if (SDCARDFS_I(d_inode(parent))->top != SDCARDFS_I(d_inode(dentry))->top) {
174 get_derived_permission(parent, dentry);
175 fix_derived_permission(d_inode(dentry));
176 fixup_top_recursive(dentry);
177 }
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800178 }
179 }
Daniel Rosenbergcb1b9452016-08-16 15:19:26 -0700180 spin_unlock(&parent->d_lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800181}
182
Daniel Campello35c9e242015-07-20 16:23:50 -0700183/* main function for updating derived permission */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800184inline void update_derived_permission_lock(struct dentry *dentry)
Daniel Campello35c9e242015-07-20 16:23:50 -0700185{
186 struct dentry *parent;
187
188 if(!dentry || !dentry->d_inode) {
189 printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__);
190 return;
191 }
192 /* FIXME:
193 * 1. need to check whether the dentry is updated or not
194 * 2. remove the root dentry update
195 */
196 if(IS_ROOT(dentry)) {
197 //setup_default_pre_root_state(dentry->d_inode);
198 } else {
199 parent = dget_parent(dentry);
200 if(parent) {
201 get_derived_permission(parent, dentry);
202 dput(parent);
203 }
204 }
205 fix_derived_permission(dentry->d_inode);
206}
207
208int need_graft_path(struct dentry *dentry)
209{
210 int ret = 0;
211 struct dentry *parent = dget_parent(dentry);
212 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
213 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
214
215 if(parent_info->perm == PERM_ANDROID &&
216 !strcasecmp(dentry->d_name.name, "obb")) {
217
218 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800219 if(!(sbi->options.multiuser == false
Daniel Campello35c9e242015-07-20 16:23:50 -0700220 && parent_info->userid == 0)) {
221 ret = 1;
222 }
223 }
224 dput(parent);
225 return ret;
226}
227
228int is_obbpath_invalid(struct dentry *dent)
229{
230 int ret = 0;
231 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
232 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
233 char *path_buf, *obbpath_s;
234
235 /* check the base obbpath has been changed.
236 * this routine can check an uninitialized obb dentry as well.
237 * regarding the uninitialized obb, refer to the sdcardfs_mkdir() */
238 spin_lock(&di->lock);
239 if(di->orig_path.dentry) {
240 if(!di->lower_path.dentry) {
241 ret = 1;
242 } else {
243 path_get(&di->lower_path);
244 //lower_parent = lock_parent(lower_path->dentry);
245
246 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
247 if(!path_buf) {
248 ret = 1;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800249 printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700250 } else {
251 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
252 if (d_unhashed(di->lower_path.dentry) ||
253 strcasecmp(sbi->obbpath_s, obbpath_s)) {
254 ret = 1;
255 }
256 kfree(path_buf);
257 }
258
259 //unlock_dir(lower_parent);
260 path_put(&di->lower_path);
261 }
262 }
263 spin_unlock(&di->lock);
264 return ret;
265}
266
267int is_base_obbpath(struct dentry *dentry)
268{
269 int ret = 0;
270 struct dentry *parent = dget_parent(dentry);
271 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
272 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
273
274 spin_lock(&SDCARDFS_D(dentry)->lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800275 if (sbi->options.multiuser) {
276 if(parent_info->perm == PERM_PRE_ROOT &&
277 !strcasecmp(dentry->d_name.name, "obb")) {
278 ret = 1;
279 }
280 } else if (parent_info->perm == PERM_ANDROID &&
Daniel Campello35c9e242015-07-20 16:23:50 -0700281 !strcasecmp(dentry->d_name.name, "obb")) {
282 ret = 1;
283 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700284 spin_unlock(&SDCARDFS_D(dentry)->lock);
Daniel Campello35c9e242015-07-20 16:23:50 -0700285 return ret;
286}
287
288/* The lower_path will be stored to the dentry's orig_path
289 * and the base obbpath will be copyed to the lower_path variable.
290 * if an error returned, there's no change in the lower_path
291 * returns: -ERRNO if error (0: no error) */
292int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
293{
294 int err = 0;
295 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
296 struct path obbpath;
297
298 /* A local obb dentry must have its own orig_path to support rmdir
299 * and mkdir of itself. Usually, we expect that the sbi->obbpath
300 * is avaiable on this stage. */
301 sdcardfs_set_orig_path(dentry, lower_path);
302
303 err = kern_path(sbi->obbpath_s,
304 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
305
306 if(!err) {
307 /* the obbpath base has been found */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800308 printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700309 pathcpy(lower_path, &obbpath);
310 } else {
311 /* if the sbi->obbpath is not available, we can optionally
312 * setup the lower_path with its orig_path.
313 * but, the current implementation just returns an error
314 * because the sdcard daemon also regards this case as
315 * a lookup fail. */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800316 printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700317 }
318 return err;
319}
320
321