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 Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 33 | ci->under_cache = pi->under_cache; |
| 34 | ci->under_obb = pi->under_obb; |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 35 | set_top(ci, pi->top); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* helper function for derived state */ |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 39 | void setup_derived_state(struct inode *inode, perm_t perm, userid_t userid, |
| 40 | uid_t uid, bool under_android, struct inode *top) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 41 | { |
| 42 | struct sdcardfs_inode_info *info = SDCARDFS_I(inode); |
| 43 | |
| 44 | info->perm = perm; |
| 45 | info->userid = userid; |
| 46 | info->d_uid = uid; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 47 | info->under_android = under_android; |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 48 | info->under_cache = false; |
| 49 | info->under_obb = false; |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 50 | set_top(info, top); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 53 | /* While renaming, there is a point where we want the path from dentry, but the name from newdentry */ |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 54 | void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 55 | { |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 56 | struct sdcardfs_inode_info *info = SDCARDFS_I(d_inode(dentry)); |
| 57 | struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 58 | appid_t appid; |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 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"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 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. |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 69 | * These values are used by our custom permission call instead |
| 70 | * of using the inode permissions. |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 71 | */ |
| 72 | |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 73 | inherit_derived_state(d_inode(parent), d_inode(dentry)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 74 | |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 75 | /* Files don't get special labels */ |
| 76 | if (!S_ISDIR(d_inode(dentry)->i_mode)) |
| 77 | return; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 78 | /* Derive custom permissions based on parent and current node */ |
| 79 | switch (parent_info->perm) { |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 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; |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 87 | info->userid = simple_strtoul(name->name, NULL, 10); |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 88 | set_top(info, &info->vfs_inode); |
| 89 | break; |
| 90 | case PERM_ROOT: |
| 91 | /* Assume masked off by default. */ |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 92 | if (qstr_case_eq(name, &q_Android)) { |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 93 | /* App-specific directories inside; let anyone traverse */ |
| 94 | info->perm = PERM_ANDROID; |
| 95 | info->under_android = true; |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 96 | set_top(info, &info->vfs_inode); |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 97 | } |
| 98 | break; |
| 99 | case PERM_ANDROID: |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 100 | if (qstr_case_eq(name, &q_data)) { |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 101 | /* App-specific directories inside; let anyone traverse */ |
| 102 | info->perm = PERM_ANDROID_DATA; |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 103 | set_top(info, &info->vfs_inode); |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 104 | } else if (qstr_case_eq(name, &q_obb)) { |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 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 */ |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 110 | } else if (qstr_case_eq(name, &q_media)) { |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 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; |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 120 | appid = get_appid(name->name); |
| 121 | if (appid != 0 && !is_excluded(name->name, parent_info->userid)) { |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 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: |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 127 | if (qstr_case_eq(name, &q_cache)) { |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 128 | info->perm = PERM_ANDROID_PACKAGE_CACHE; |
| 129 | info->under_cache = true; |
| 130 | } |
| 131 | break; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 135 | void get_derived_permission(struct dentry *parent, struct dentry *dentry) |
| 136 | { |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 137 | get_derived_permission_new(parent, dentry, &dentry->d_name); |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static appid_t get_type(const char *name) |
| 141 | { |
| 142 | const char *ext = strrchr(name, '.'); |
| 143 | appid_t id; |
| 144 | |
| 145 | if (ext && ext[0]) { |
| 146 | ext = &ext[1]; |
| 147 | id = get_ext_gid(ext); |
| 148 | return id?:AID_MEDIA_RW; |
| 149 | } |
| 150 | return AID_MEDIA_RW; |
| 151 | } |
| 152 | |
| 153 | void fixup_lower_ownership(struct dentry *dentry, const char *name) |
| 154 | { |
| 155 | struct path path; |
| 156 | struct inode *inode; |
| 157 | struct inode *delegated_inode = NULL; |
| 158 | int error; |
| 159 | struct sdcardfs_inode_info *info; |
| 160 | struct sdcardfs_inode_info *info_top; |
| 161 | perm_t perm; |
| 162 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
| 163 | uid_t uid = sbi->options.fs_low_uid; |
| 164 | gid_t gid = sbi->options.fs_low_gid; |
| 165 | struct iattr newattrs; |
| 166 | |
| 167 | info = SDCARDFS_I(d_inode(dentry)); |
| 168 | perm = info->perm; |
| 169 | if (info->under_obb) { |
| 170 | perm = PERM_ANDROID_OBB; |
| 171 | } else if (info->under_cache) { |
| 172 | perm = PERM_ANDROID_PACKAGE_CACHE; |
| 173 | } else if (perm == PERM_INHERIT) { |
| 174 | info_top = SDCARDFS_I(grab_top(info)); |
| 175 | perm = info_top->perm; |
| 176 | release_top(info); |
| 177 | } |
| 178 | |
| 179 | switch (perm) { |
| 180 | case PERM_ROOT: |
| 181 | case PERM_ANDROID: |
| 182 | case PERM_ANDROID_DATA: |
| 183 | case PERM_ANDROID_MEDIA: |
| 184 | case PERM_ANDROID_PACKAGE: |
| 185 | case PERM_ANDROID_PACKAGE_CACHE: |
| 186 | uid = multiuser_get_uid(info->userid, uid); |
| 187 | break; |
| 188 | case PERM_ANDROID_OBB: |
| 189 | uid = AID_MEDIA_OBB; |
| 190 | break; |
| 191 | case PERM_PRE_ROOT: |
| 192 | default: |
| 193 | break; |
| 194 | } |
| 195 | switch (perm) { |
| 196 | case PERM_ROOT: |
| 197 | case PERM_ANDROID: |
| 198 | case PERM_ANDROID_DATA: |
| 199 | case PERM_ANDROID_MEDIA: |
| 200 | if (S_ISDIR(d_inode(dentry)->i_mode)) |
| 201 | gid = multiuser_get_uid(info->userid, AID_MEDIA_RW); |
| 202 | else |
| 203 | gid = multiuser_get_uid(info->userid, get_type(name)); |
| 204 | break; |
| 205 | case PERM_ANDROID_OBB: |
| 206 | gid = AID_MEDIA_OBB; |
| 207 | break; |
| 208 | case PERM_ANDROID_PACKAGE: |
| 209 | if (info->d_uid != 0) |
| 210 | gid = multiuser_get_ext_gid(info->userid, info->d_uid); |
| 211 | else |
| 212 | gid = multiuser_get_uid(info->userid, uid); |
| 213 | break; |
| 214 | case PERM_ANDROID_PACKAGE_CACHE: |
| 215 | if (info->d_uid != 0) |
| 216 | gid = multiuser_get_cache_gid(info->userid, info->d_uid); |
| 217 | else |
| 218 | gid = multiuser_get_uid(info->userid, uid); |
| 219 | break; |
| 220 | case PERM_PRE_ROOT: |
| 221 | default: |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | sdcardfs_get_lower_path(dentry, &path); |
| 226 | inode = d_inode(path.dentry); |
| 227 | if (d_inode(path.dentry)->i_gid.val != gid || d_inode(path.dentry)->i_uid.val != uid) { |
| 228 | retry_deleg: |
| 229 | newattrs.ia_valid = ATTR_GID | ATTR_UID | ATTR_FORCE; |
| 230 | newattrs.ia_uid = make_kuid(current_user_ns(), uid); |
| 231 | newattrs.ia_gid = make_kgid(current_user_ns(), gid); |
| 232 | if (!S_ISDIR(inode->i_mode)) |
| 233 | newattrs.ia_valid |= |
| 234 | ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV; |
| 235 | inode_lock(inode); |
| 236 | error = security_path_chown(&path, newattrs.ia_uid, newattrs.ia_gid); |
| 237 | if (!error) |
| 238 | error = notify_change2(path.mnt, path.dentry, &newattrs, &delegated_inode); |
| 239 | inode_unlock(inode); |
| 240 | if (delegated_inode) { |
| 241 | error = break_deleg_wait(&delegated_inode); |
| 242 | if (!error) |
| 243 | goto retry_deleg; |
| 244 | } |
| 245 | if (error) |
| 246 | pr_err("sdcardfs: Failed to touch up lower fs gid/uid.\n"); |
| 247 | } |
Daniel Rosenberg | f61bc5a | 2017-02-16 17:55:22 -0800 | [diff] [blame] | 248 | sdcardfs_put_lower_path(dentry, &path); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 251 | static int descendant_may_need_fixup(struct sdcardfs_inode_info *info, struct limit_search *limit) |
| 252 | { |
| 253 | if (info->perm == PERM_ROOT) |
| 254 | return (limit->flags & BY_USERID)?info->userid == limit->userid:1; |
| 255 | if (info->perm == PERM_PRE_ROOT || info->perm == PERM_ANDROID) |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 256 | return 1; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int needs_fixup(perm_t perm) { |
| 261 | if (perm == PERM_ANDROID_DATA || perm == PERM_ANDROID_OBB |
| 262 | || perm == PERM_ANDROID_MEDIA) |
| 263 | return 1; |
| 264 | return 0; |
| 265 | } |
| 266 | |
Daniel Rosenberg | fb4d191 | 2017-03-02 15:11:27 -0800 | [diff] [blame] | 267 | static void __fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit, int depth) |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 268 | { |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 269 | struct dentry *child; |
| 270 | struct sdcardfs_inode_info *info; |
Daniel Rosenberg | fb4d191 | 2017-03-02 15:11:27 -0800 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * All paths will terminate their recursion on hitting PERM_ANDROID_OBB, |
| 274 | * PERM_ANDROID_MEDIA, or PERM_ANDROID_DATA. This happens at a depth of |
| 275 | * at most 3. |
| 276 | */ |
| 277 | WARN(depth > 3, "%s: Max expected depth exceeded!\n", __func__); |
| 278 | spin_lock_nested(&dentry->d_lock, depth); |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 279 | if (!d_inode(dentry)) { |
Daniel Rosenberg | fb4d191 | 2017-03-02 15:11:27 -0800 | [diff] [blame] | 280 | spin_unlock(&dentry->d_lock); |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | info = SDCARDFS_I(d_inode(dentry)); |
| 284 | |
| 285 | if (needs_fixup(info->perm)) { |
Daniel Rosenberg | 3adfc03 | 2016-12-27 12:36:29 -0800 | [diff] [blame] | 286 | list_for_each_entry(child, &dentry->d_subdirs, d_child) { |
Daniel Rosenberg | fb4d191 | 2017-03-02 15:11:27 -0800 | [diff] [blame] | 287 | spin_lock_nested(&child->d_lock, depth + 1); |
Daniel Rosenberg | 721274a | 2017-03-08 17:20:02 -0800 | [diff] [blame^] | 288 | if (!(limit->flags & BY_NAME) || qstr_case_eq(&child->d_name, &limit->name)) { |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 289 | if (d_inode(child)) { |
| 290 | get_derived_permission(dentry, child); |
| 291 | fixup_tmp_permissions(d_inode(child)); |
Daniel Rosenberg | fb4d191 | 2017-03-02 15:11:27 -0800 | [diff] [blame] | 292 | spin_unlock(&child->d_lock); |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 293 | break; |
Daniel Rosenberg | 3adfc03 | 2016-12-27 12:36:29 -0800 | [diff] [blame] | 294 | } |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 295 | } |
Daniel Rosenberg | fb4d191 | 2017-03-02 15:11:27 -0800 | [diff] [blame] | 296 | spin_unlock(&child->d_lock); |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 297 | } |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 298 | } else if (descendant_may_need_fixup(info, limit)) { |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 299 | list_for_each_entry(child, &dentry->d_subdirs, d_child) { |
Daniel Rosenberg | fb4d191 | 2017-03-02 15:11:27 -0800 | [diff] [blame] | 300 | __fixup_perms_recursive(child, limit, depth + 1); |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 301 | } |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 302 | } |
Daniel Rosenberg | fb4d191 | 2017-03-02 15:11:27 -0800 | [diff] [blame] | 303 | spin_unlock(&dentry->d_lock); |
| 304 | } |
| 305 | |
| 306 | void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit) |
| 307 | { |
| 308 | __fixup_perms_recursive(dentry, limit, 0); |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 311 | void drop_recursive(struct dentry *parent) |
| 312 | { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 313 | struct dentry *dentry; |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 314 | struct sdcardfs_inode_info *info; |
| 315 | if (!d_inode(parent)) |
| 316 | return; |
| 317 | info = SDCARDFS_I(d_inode(parent)); |
Daniel Rosenberg | cb1b945 | 2016-08-16 15:19:26 -0700 | [diff] [blame] | 318 | spin_lock(&parent->d_lock); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 319 | list_for_each_entry(dentry, &parent->d_subdirs, d_child) { |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 320 | if (d_inode(dentry)) { |
| 321 | if (SDCARDFS_I(d_inode(parent))->top != SDCARDFS_I(d_inode(dentry))->top) { |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 322 | drop_recursive(dentry); |
| 323 | d_drop(dentry); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | spin_unlock(&parent->d_lock); |
| 328 | } |
| 329 | |
| 330 | void fixup_top_recursive(struct dentry *parent) |
| 331 | { |
| 332 | struct dentry *dentry; |
| 333 | struct sdcardfs_inode_info *info; |
| 334 | |
| 335 | if (!d_inode(parent)) |
| 336 | return; |
| 337 | info = SDCARDFS_I(d_inode(parent)); |
| 338 | spin_lock(&parent->d_lock); |
| 339 | list_for_each_entry(dentry, &parent->d_subdirs, d_child) { |
| 340 | if (d_inode(dentry)) { |
| 341 | if (SDCARDFS_I(d_inode(parent))->top != SDCARDFS_I(d_inode(dentry))->top) { |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 342 | get_derived_permission(parent, dentry); |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 343 | fixup_tmp_permissions(d_inode(dentry)); |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 344 | fixup_top_recursive(dentry); |
| 345 | } |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
Daniel Rosenberg | cb1b945 | 2016-08-16 15:19:26 -0700 | [diff] [blame] | 348 | spin_unlock(&parent->d_lock); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 351 | /* main function for updating derived permission */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 352 | inline void update_derived_permission_lock(struct dentry *dentry) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 353 | { |
| 354 | struct dentry *parent; |
| 355 | |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 356 | if(!dentry || !d_inode(dentry)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 357 | printk(KERN_ERR "sdcardfs: %s: invalid dentry\n", __func__); |
| 358 | return; |
| 359 | } |
| 360 | /* FIXME: |
| 361 | * 1. need to check whether the dentry is updated or not |
| 362 | * 2. remove the root dentry update |
| 363 | */ |
| 364 | if(IS_ROOT(dentry)) { |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 365 | //setup_default_pre_root_state(d_inode(dentry)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 366 | } else { |
| 367 | parent = dget_parent(dentry); |
| 368 | if(parent) { |
| 369 | get_derived_permission(parent, dentry); |
| 370 | dput(parent); |
| 371 | } |
| 372 | } |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 373 | fixup_tmp_permissions(d_inode(dentry)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | int need_graft_path(struct dentry *dentry) |
| 377 | { |
| 378 | int ret = 0; |
| 379 | struct dentry *parent = dget_parent(dentry); |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 380 | struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 381 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 382 | struct qstr obb = QSTR_LITERAL("obb"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 383 | |
| 384 | if(parent_info->perm == PERM_ANDROID && |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 385 | qstr_case_eq(&dentry->d_name, &obb)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 386 | |
| 387 | /* /Android/obb is the base obbpath of DERIVED_UNIFIED */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 388 | if(!(sbi->options.multiuser == false |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 389 | && parent_info->userid == 0)) { |
| 390 | ret = 1; |
| 391 | } |
| 392 | } |
| 393 | dput(parent); |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | int is_obbpath_invalid(struct dentry *dent) |
| 398 | { |
| 399 | int ret = 0; |
| 400 | struct sdcardfs_dentry_info *di = SDCARDFS_D(dent); |
| 401 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dent->d_sb); |
| 402 | char *path_buf, *obbpath_s; |
| 403 | |
| 404 | /* check the base obbpath has been changed. |
| 405 | * this routine can check an uninitialized obb dentry as well. |
| 406 | * regarding the uninitialized obb, refer to the sdcardfs_mkdir() */ |
| 407 | spin_lock(&di->lock); |
| 408 | if(di->orig_path.dentry) { |
| 409 | if(!di->lower_path.dentry) { |
| 410 | ret = 1; |
| 411 | } else { |
| 412 | path_get(&di->lower_path); |
| 413 | //lower_parent = lock_parent(lower_path->dentry); |
| 414 | |
| 415 | path_buf = kmalloc(PATH_MAX, GFP_ATOMIC); |
| 416 | if(!path_buf) { |
| 417 | ret = 1; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 418 | 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] | 419 | } else { |
| 420 | obbpath_s = d_path(&di->lower_path, path_buf, PATH_MAX); |
| 421 | if (d_unhashed(di->lower_path.dentry) || |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 422 | !str_case_eq(sbi->obbpath_s, obbpath_s)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 423 | ret = 1; |
| 424 | } |
| 425 | kfree(path_buf); |
| 426 | } |
| 427 | |
| 428 | //unlock_dir(lower_parent); |
| 429 | path_put(&di->lower_path); |
| 430 | } |
| 431 | } |
| 432 | spin_unlock(&di->lock); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | int is_base_obbpath(struct dentry *dentry) |
| 437 | { |
| 438 | int ret = 0; |
| 439 | struct dentry *parent = dget_parent(dentry); |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 440 | struct sdcardfs_inode_info *parent_info= SDCARDFS_I(d_inode(parent)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 441 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 442 | struct qstr q_obb = QSTR_LITERAL("obb"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 443 | |
| 444 | spin_lock(&SDCARDFS_D(dentry)->lock); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 445 | if (sbi->options.multiuser) { |
| 446 | if(parent_info->perm == PERM_PRE_ROOT && |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 447 | qstr_case_eq(&dentry->d_name, &q_obb)) { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 448 | ret = 1; |
| 449 | } |
| 450 | } else if (parent_info->perm == PERM_ANDROID && |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 451 | qstr_case_eq(&dentry->d_name, &q_obb)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 452 | ret = 1; |
| 453 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 454 | spin_unlock(&SDCARDFS_D(dentry)->lock); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | /* The lower_path will be stored to the dentry's orig_path |
| 459 | * and the base obbpath will be copyed to the lower_path variable. |
| 460 | * if an error returned, there's no change in the lower_path |
| 461 | * returns: -ERRNO if error (0: no error) */ |
| 462 | int setup_obb_dentry(struct dentry *dentry, struct path *lower_path) |
| 463 | { |
| 464 | int err = 0; |
| 465 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
| 466 | struct path obbpath; |
| 467 | |
| 468 | /* A local obb dentry must have its own orig_path to support rmdir |
| 469 | * and mkdir of itself. Usually, we expect that the sbi->obbpath |
| 470 | * is avaiable on this stage. */ |
| 471 | sdcardfs_set_orig_path(dentry, lower_path); |
| 472 | |
| 473 | err = kern_path(sbi->obbpath_s, |
| 474 | LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &obbpath); |
| 475 | |
| 476 | if(!err) { |
| 477 | /* the obbpath base has been found */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 478 | printk(KERN_INFO "sdcardfs: the sbi->obbpath is found\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 479 | pathcpy(lower_path, &obbpath); |
| 480 | } else { |
| 481 | /* if the sbi->obbpath is not available, we can optionally |
| 482 | * setup the lower_path with its orig_path. |
| 483 | * but, the current implementation just returns an error |
| 484 | * because the sdcard daemon also regards this case as |
| 485 | * a lookup fail. */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 486 | printk(KERN_INFO "sdcardfs: the sbi->obbpath is not available\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 487 | } |
| 488 | return err; |
| 489 | } |
| 490 | |
| 491 | |