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