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