ANDROID: sdcardfs: Switch strcasecmp for internal call
[firefly-linux-kernel-4.4.55.git] / fs / sdcardfs / derived_perm.c
1 /*
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 */
24 static 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;
32         ci->under_android = pi->under_android;
33         ci->under_cache = pi->under_cache;
34         ci->under_obb = pi->under_obb;
35         set_top(ci, pi->top);
36 }
37
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)
41 {
42         struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
43
44         info->perm = perm;
45         info->userid = userid;
46         info->d_uid = uid;
47         info->under_android = under_android;
48         info->under_cache = false;
49         info->under_obb = false;
50         set_top(info, top);
51 }
52
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)
55 {
56         struct sdcardfs_inode_info *info = SDCARDFS_I(d_inode(dentry));
57         struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent));
58         appid_t appid;
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");
64
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
68          * its lower inode.
69          * These values are used by our custom permission call instead
70          * of using the inode permissions.
71          */
72
73         inherit_derived_state(d_inode(parent), d_inode(dentry));
74
75         /* Files don't get special labels */
76         if (!S_ISDIR(d_inode(dentry)->i_mode))
77                 return;
78         /* Derive custom permissions based on parent and current node */
79         switch (parent_info->perm) {
80                 case PERM_INHERIT:
81                 case PERM_ANDROID_PACKAGE_CACHE:
82                         /* Already inherited above */
83                         break;
84                 case PERM_PRE_ROOT:
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);
89                         break;
90                 case PERM_ROOT:
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);
97                         }
98                         break;
99                 case PERM_ANDROID:
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);
114                         }
115                         break;
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);
123                         }
124                         set_top(info, &info->vfs_inode);
125                         break;
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;
130                         }
131                         break;
132         }
133 }
134
135 void get_derived_permission(struct dentry *parent, struct dentry *dentry)
136 {
137         get_derived_permission_new(parent, dentry, &dentry->d_name);
138 }
139
140 static appid_t get_type(const char *name) {
141         const char *ext = strrchr(name, '.');
142         appid_t id;
143
144         if (ext && ext[0]) {
145                 ext = &ext[1];
146                 id = get_ext_gid(ext);
147                 return id?:AID_MEDIA_RW;
148         }
149         return AID_MEDIA_RW;
150 }
151
152 void fixup_lower_ownership(struct dentry* dentry, const char *name) {
153         struct path path;
154         struct inode *inode;
155         struct inode *delegated_inode = NULL;
156         int error;
157         struct sdcardfs_inode_info *info;
158         struct sdcardfs_inode_info *info_top;
159         perm_t perm;
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;
164
165         info = SDCARDFS_I(d_inode(dentry));
166         perm = info->perm;
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;
174                 release_top(info);
175         }
176
177         switch (perm) {
178                 case PERM_ROOT:
179                 case PERM_ANDROID:
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);
185                         break;
186                 case PERM_ANDROID_OBB:
187                         uid = AID_MEDIA_OBB;
188                         break;
189                 case PERM_PRE_ROOT:
190                 default:
191                         break;
192         }
193         switch (perm) {
194                 case PERM_ROOT:
195                 case PERM_ANDROID:
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);
200                         else
201                                 gid = multiuser_get_uid(info->userid, get_type(name));
202                         break;
203                 case PERM_ANDROID_OBB:
204                         gid = AID_MEDIA_OBB;
205                         break;
206                 case PERM_ANDROID_PACKAGE:
207                         if (info->d_uid != 0)
208                                 gid = multiuser_get_ext_gid(info->userid, info->d_uid);
209                         else
210                                 gid = multiuser_get_uid(info->userid, uid);
211                         break;
212                 case PERM_ANDROID_PACKAGE_CACHE:
213                         if (info->d_uid != 0)
214                                 gid = multiuser_get_cache_gid(info->userid, info->d_uid);
215                         else
216                                 gid = multiuser_get_uid(info->userid, uid);
217                         break;
218                 case PERM_PRE_ROOT:
219                 default:
220                         break;
221         }
222
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) {
226 retry_deleg:
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))
231                         newattrs.ia_valid |=
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);
235                 if (!error)
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);
240                         if (!error)
241                                 goto retry_deleg;
242                 }
243                 if (error)
244                         pr_err("sdcardfs: Failed to touch up lower fs gid/uid.\n");
245         }
246 }
247
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)
252                 return 1;
253         return 0;
254 }
255
256 static int needs_fixup(perm_t perm) {
257         if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB
258                         || perm == PERM_ANDROID_MEDIA)
259                 return 1;
260         return 0;
261 }
262
263 void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit) {
264         struct dentry *child;
265         struct sdcardfs_inode_info *info;
266         if (!dget(dentry))
267                 return;
268         if (!d_inode(dentry)) {
269                 dput(dentry);
270                 return;
271         }
272         info = SDCARDFS_I(d_inode(dentry));
273
274         if (needs_fixup(info->perm)) {
275                 spin_lock(&dentry->d_lock);
276                 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
277                         dget(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));
282                                         dput(child);
283                                         break;
284                                 }
285                         }
286                         dput(child);
287                 }
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);
293                 }
294                 spin_unlock(&dentry->d_lock);
295         }
296         dput(dentry);
297 }
298
299 void drop_recursive(struct dentry *parent) {
300         struct dentry *dentry;
301         struct sdcardfs_inode_info *info;
302         if (!d_inode(parent))
303                 return;
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);
310                                 d_drop(dentry);
311                         }
312                 }
313         }
314         spin_unlock(&parent->d_lock);
315 }
316
317 void fixup_top_recursive(struct dentry *parent) {
318         struct dentry *dentry;
319         struct sdcardfs_inode_info *info;
320
321         if (!d_inode(parent))
322                 return;
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);
331                         }
332                 }
333         }
334         spin_unlock(&parent->d_lock);
335 }
336
337 /* main function for updating derived permission */
338 inline void update_derived_permission_lock(struct dentry *dentry)
339 {
340         struct dentry *parent;
341
342         if(!dentry || !d_inode(dentry)) {
343                 printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__);
344                 return;
345         }
346         /* FIXME:
347          * 1. need to check whether the dentry is updated or not
348          * 2. remove the root dentry update
349          */
350         if(IS_ROOT(dentry)) {
351                 //setup_default_pre_root_state(d_inode(dentry));
352         } else {
353                 parent = dget_parent(dentry);
354                 if(parent) {
355                         get_derived_permission(parent, dentry);
356                         dput(parent);
357                 }
358         }
359         fixup_tmp_permissions(d_inode(dentry));
360 }
361
362 int need_graft_path(struct dentry *dentry)
363 {
364         int ret = 0;
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");
369
370         if(parent_info->perm == PERM_ANDROID &&
371                         qstr_case_eq(&dentry->d_name, &obb)) {
372
373                 /* /Android/obb is the base obbpath of DERIVED_UNIFIED */
374                 if(!(sbi->options.multiuser == false
375                                 && parent_info->userid == 0)) {
376                         ret = 1;
377                 }
378         }
379         dput(parent);
380         return ret;
381 }
382
383 int is_obbpath_invalid(struct dentry *dent)
384 {
385         int ret = 0;
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;
389
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) {
396                         ret = 1;
397                 } else {
398                         path_get(&di->lower_path);
399                         //lower_parent = lock_parent(lower_path->dentry);
400
401                         path_buf = kmalloc(PATH_MAX, GFP_ATOMIC);
402                         if(!path_buf) {
403                                 ret = 1;
404                                 printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__);
405                         } else {
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)) {
409                                         ret = 1;
410                                 }
411                                 kfree(path_buf);
412                         }
413
414                         //unlock_dir(lower_parent);
415                         path_put(&di->lower_path);
416                 }
417         }
418         spin_unlock(&di->lock);
419         return ret;
420 }
421
422 int is_base_obbpath(struct dentry *dentry)
423 {
424         int ret = 0;
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");
429
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)) {
434                         ret = 1;
435                 }
436         } else  if (parent_info->perm == PERM_ANDROID &&
437                         qstr_case_eq(&dentry->d_name, &q_obb)) {
438                 ret = 1;
439         }
440         spin_unlock(&SDCARDFS_D(dentry)->lock);
441         return ret;
442 }
443
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)
449 {
450         int err = 0;
451         struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
452         struct path obbpath;
453
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);
458
459         err = kern_path(sbi->obbpath_s,
460                         LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath);
461
462         if(!err) {
463                 /* the obbpath base has been found */
464                 printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n");
465                 pathcpy(lower_path, &obbpath);
466         } else {
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
471                  * a lookup fail. */
472                 printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n");
473         }
474         return err;
475 }
476
477