Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 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; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 32 | ci->under_android = pi->under_android; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* helper function for derived state */ |
| 36 | void setup_derived_state(struct inode *inode, perm_t perm, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 37 | userid_t userid, uid_t uid, bool under_android) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 38 | { |
| 39 | struct sdcardfs_inode_info *info = SDCARDFS_I(inode); |
| 40 | |
| 41 | info->perm = perm; |
| 42 | info->userid = userid; |
| 43 | info->d_uid = uid; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 44 | info->under_android = under_android; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 47 | /* While renaming, there is a point where we want the path from dentry, but the name from newdentry */ |
| 48 | void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, struct dentry *newdentry) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 49 | { |
| 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 Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 65 | /* 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 70 | case PERM_PRE_ROOT: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 71 | /* Legacy internal layout places users at top level */ |
| 72 | info->perm = PERM_ROOT; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 73 | info->userid = simple_strtoul(newdentry->d_name.name, NULL, 10); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 74 | break; |
| 75 | case PERM_ROOT: |
| 76 | /* Assume masked off by default. */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 77 | if (!strcasecmp(newdentry->d_name.name, "Android")) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 78 | /* App-specific directories inside; let anyone traverse */ |
| 79 | info->perm = PERM_ANDROID; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 80 | info->under_android = true; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 81 | } |
| 82 | break; |
| 83 | case PERM_ANDROID: |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 84 | if (!strcasecmp(newdentry->d_name.name, "data")) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 85 | /* App-specific directories inside; let anyone traverse */ |
| 86 | info->perm = PERM_ANDROID_DATA; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 87 | } else if (!strcasecmp(newdentry->d_name.name, "obb")) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 88 | /* App-specific directories inside; let anyone traverse */ |
| 89 | info->perm = PERM_ANDROID_OBB; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 90 | /* Single OBB directory is always shared */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 91 | } else if (!strcasecmp(newdentry->d_name.name, "media")) { |
| 92 | /* App-specific directories inside; let anyone traverse */ |
| 93 | info->perm = PERM_ANDROID_MEDIA; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 94 | } |
| 95 | break; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 96 | case PERM_ANDROID_DATA: |
| 97 | case PERM_ANDROID_OBB: |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 98 | case PERM_ANDROID_MEDIA: |
| 99 | appid = get_appid(sbi->pkgl_id, newdentry->d_name.name); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 100 | if (appid != 0) { |
| 101 | info->d_uid = multiuser_get_uid(parent_info->userid, appid); |
| 102 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 107 | void get_derived_permission(struct dentry *parent, struct dentry *dentry) |
| 108 | { |
| 109 | get_derived_permission_new(parent, dentry, dentry); |
| 110 | } |
| 111 | |
| 112 | void 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 Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 125 | /* main function for updating derived permission */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 126 | inline void update_derived_permission_lock(struct dentry *dentry) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 127 | { |
| 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 138 | mutex_lock(&dentry->d_inode->i_mutex); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 139 | 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 149 | mutex_unlock(&dentry->d_inode->i_mutex); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 163 | if(!(sbi->options.multiuser == false |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 164 | && parent_info->userid == 0)) { |
| 165 | ret = 1; |
| 166 | } |
| 167 | } |
| 168 | dput(parent); |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | int 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 193 | printk(KERN_ERR "sdcardfs: fail to allocate path_buf in %s.\n", __func__); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 194 | } 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 | |
| 211 | int 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 219 | 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 Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 225 | !strcasecmp(dentry->d_name.name, "obb")) { |
| 226 | ret = 1; |
| 227 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 228 | spin_unlock(&SDCARDFS_D(dentry)->lock); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 229 | 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) */ |
| 236 | int 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 252 | printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 253 | 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame^] | 260 | printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 261 | } |
| 262 | return err; |
| 263 | } |
| 264 | |
| 265 | |