blob: 2a75ad873a7c6afe4380e793e5ef3978c666901a [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{
Daniel Campello35c9e242015-07-20 16:23:50 -070050 struct sdcardfs_inode_info *info = SDCARDFS_I(dentry->d_inode);
51 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
52 appid_t appid;
53
54 /* By default, each inode inherits from its parent.
55 * the properties are maintained on its private fields
56 * because the inode attributes will be modified with that of
57 * its lower inode.
58 * The derived state will be updated on the last
59 * stage of each system call by fix_derived_permission(inode).
60 */
61
62 inherit_derived_state(parent->d_inode, dentry->d_inode);
63
Daniel Campello35c9e242015-07-20 16:23:50 -070064 /* Derive custom permissions based on parent and current node */
65 switch (parent_info->perm) {
66 case PERM_INHERIT:
67 /* Already inherited above */
68 break;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080069 case PERM_PRE_ROOT:
Daniel Campello35c9e242015-07-20 16:23:50 -070070 /* Legacy internal layout places users at top level */
71 info->perm = PERM_ROOT;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080072 info->userid = simple_strtoul(newdentry->d_name.name, NULL, 10);
Daniel Campello35c9e242015-07-20 16:23:50 -070073 break;
74 case PERM_ROOT:
75 /* Assume masked off by default. */
Daniel Rosenberg497ac902016-02-03 21:08:21 -080076 if (!strcasecmp(newdentry->d_name.name, "Android")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070077 /* App-specific directories inside; let anyone traverse */
78 info->perm = PERM_ANDROID;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080079 info->under_android = true;
Daniel Campello35c9e242015-07-20 16:23:50 -070080 }
81 break;
82 case PERM_ANDROID:
Daniel Rosenberg497ac902016-02-03 21:08:21 -080083 if (!strcasecmp(newdentry->d_name.name, "data")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070084 /* App-specific directories inside; let anyone traverse */
85 info->perm = PERM_ANDROID_DATA;
Daniel Rosenberg497ac902016-02-03 21:08:21 -080086 } else if (!strcasecmp(newdentry->d_name.name, "obb")) {
Daniel Campello35c9e242015-07-20 16:23:50 -070087 /* App-specific directories inside; let anyone traverse */
88 info->perm = PERM_ANDROID_OBB;
Daniel Campello35c9e242015-07-20 16:23:50 -070089 /* Single OBB directory is always shared */
Daniel Rosenberg497ac902016-02-03 21:08:21 -080090 } else if (!strcasecmp(newdentry->d_name.name, "media")) {
91 /* App-specific directories inside; let anyone traverse */
92 info->perm = PERM_ANDROID_MEDIA;
Daniel Campello35c9e242015-07-20 16:23:50 -070093 }
94 break;
Daniel Campello35c9e242015-07-20 16:23:50 -070095 case PERM_ANDROID_DATA:
96 case PERM_ANDROID_OBB:
Daniel Rosenberg497ac902016-02-03 21:08:21 -080097 case PERM_ANDROID_MEDIA:
Daniel Rosenbergfbd34b62016-05-10 13:42:43 -070098 appid = get_appid(newdentry->d_name.name);
Daniel Campello35c9e242015-07-20 16:23:50 -070099 if (appid != 0) {
100 info->d_uid = multiuser_get_uid(parent_info->userid, appid);
101 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700102 break;
103 }
104}
105
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800106void get_derived_permission(struct dentry *parent, struct dentry *dentry)
107{
108 get_derived_permission_new(parent, dentry, dentry);
109}
110
111void get_derive_permissions_recursive(struct dentry *parent) {
112 struct dentry *dentry;
Daniel Rosenbergcb1b9452016-08-16 15:19:26 -0700113 spin_lock(&parent->d_lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800114 list_for_each_entry(dentry, &parent->d_subdirs, d_child) {
Julia Lawall774e4412016-06-01 10:28:49 -0700115 if (dentry->d_inode) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800116 get_derived_permission(parent, dentry);
117 fix_derived_permission(dentry->d_inode);
118 get_derive_permissions_recursive(dentry);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800119 }
120 }
Daniel Rosenbergcb1b9452016-08-16 15:19:26 -0700121 spin_unlock(&parent->d_lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800122}
123
Daniel Campello35c9e242015-07-20 16:23:50 -0700124/* main function for updating derived permission */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800125inline void update_derived_permission_lock(struct dentry *dentry)
Daniel Campello35c9e242015-07-20 16:23:50 -0700126{
127 struct dentry *parent;
128
129 if(!dentry || !dentry->d_inode) {
130 printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__);
131 return;
132 }
133 /* FIXME:
134 * 1. need to check whether the dentry is updated or not
135 * 2. remove the root dentry update
136 */
137 if(IS_ROOT(dentry)) {
138 //setup_default_pre_root_state(dentry->d_inode);
139 } else {
140 parent = dget_parent(dentry);
141 if(parent) {
142 get_derived_permission(parent, dentry);
143 dput(parent);
144 }
145 }
146 fix_derived_permission(dentry->d_inode);
147}
148
149int need_graft_path(struct dentry *dentry)
150{
151 int ret = 0;
152 struct dentry *parent = dget_parent(dentry);
153 struct sdcardfs_inode_info *parent_info= SDCARDFS_I(parent->d_inode);
154 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
155
156 if(parent_info->perm == PERM_ANDROID &&
157 !strcasecmp(dentry->d_name.name, "obb")) {
158
159 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800160 if(!(sbi->options.multiuser == false
Daniel Campello35c9e242015-07-20 16:23:50 -0700161 && parent_info->userid == 0)) {
162 ret = 1;
163 }
164 }
165 dput(parent);
166 return ret;
167}
168
169int is_obbpath_invalid(struct dentry *dent)
170{
171 int ret = 0;
172 struct sdcardfs_dentry_info *di = SDCARDFS_D(dent);
173 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb);
174 char *path_buf, *obbpath_s;
175
176 /* check the base obbpath has been changed.
177 * this routine can check an uninitialized obb dentry as well.
178 * regarding the uninitialized obb, refer to the sdcardfs_mkdir() */
179 spin_lock(&di->lock);
180 if(di->orig_path.dentry) {
181 if(!di->lower_path.dentry) {
182 ret = 1;
183 } else {
184 path_get(&di->lower_path);
185 //lower_parent = lock_parent(lower_path->dentry);
186
187 path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
188 if(!path_buf) {
189 ret = 1;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800190 printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__);
Daniel Campello35c9e242015-07-20 16:23:50 -0700191 } else {
192 obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX);
193 if (d_unhashed(di->lower_path.dentry) ||
194 strcasecmp(sbi->obbpath_s, obbpath_s)) {
195 ret = 1;
196 }
197 kfree(path_buf);
198 }
199
200 //unlock_dir(lower_parent);
201 path_put(&di->lower_path);
202 }
203 }
204 spin_unlock(&di->lock);
205 return ret;
206}
207
208int is_base_obbpath(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 spin_lock(&SDCARDFS_D(dentry)->lock);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800216 if (sbi->options.multiuser) {
217 if(parent_info->perm == PERM_PRE_ROOT &&
218 !strcasecmp(dentry->d_name.name, "obb")) {
219 ret = 1;
220 }
221 } else if (parent_info->perm == PERM_ANDROID &&
Daniel Campello35c9e242015-07-20 16:23:50 -0700222 !strcasecmp(dentry->d_name.name, "obb")) {
223 ret = 1;
224 }
Daniel Campello35c9e242015-07-20 16:23:50 -0700225 spin_unlock(&SDCARDFS_D(dentry)->lock);
Daniel Campello35c9e242015-07-20 16:23:50 -0700226 return ret;
227}
228
229/* The lower_path will be stored to the dentry's orig_path
230 * and the base obbpath will be copyed to the lower_path variable.
231 * if an error returned, there's no change in the lower_path
232 * returns: -ERRNO if error (0: no error) */
233int setup_obb_dentry(struct dentry *dentry, struct path *lower_path)
234{
235 int err = 0;
236 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
237 struct path obbpath;
238
239 /* A local obb dentry must have its own orig_path to support rmdir
240 * and mkdir of itself. Usually, we expect that the sbi->obbpath
241 * is avaiable on this stage. */
242 sdcardfs_set_orig_path(dentry, lower_path);
243
244 err = kern_path(sbi->obbpath_s,
245 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
246
247 if(!err) {
248 /* the obbpath base has been found */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800249 printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700250 pathcpy(lower_path, &obbpath);
251 } else {
252 /* if the sbi->obbpath is not available, we can optionally
253 * setup the lower_path with its orig_path.
254 * but, the current implementation just returns an error
255 * because the sdcard daemon also regards this case as
256 * a lookup fail. */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800257 printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n");
Daniel Campello35c9e242015-07-20 16:23:50 -0700258 }
259 return err;
260}
261
262