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