Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/sdcardfs/sdcardfs.h |
| 3 | * |
| 4 | * The sdcardfs v2.0 |
| 5 | * This file system replaces the sdcard daemon on Android |
| 6 | * On version 2.0, some of the daemon functions have been ported |
| 7 | * to support the multi-user concepts of Android 4.4 |
| 8 | * |
| 9 | * Copyright (c) 2013 Samsung Electronics Co. Ltd |
| 10 | * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun, |
| 11 | * Sunghwan Yun, Sungjong Seo |
| 12 | * |
| 13 | * This program has been developed as a stackable file system based on |
| 14 | * the WrapFS which written by |
| 15 | * |
| 16 | * Copyright (c) 1998-2011 Erez Zadok |
| 17 | * Copyright (c) 2009 Shrikar Archak |
| 18 | * Copyright (c) 2003-2011 Stony Brook University |
| 19 | * Copyright (c) 2003-2011 The Research Foundation of SUNY |
| 20 | * |
| 21 | * This file is dual licensed. It may be redistributed and/or modified |
| 22 | * under the terms of the Apache 2.0 License OR version 2 of the GNU |
| 23 | * General Public License. |
| 24 | */ |
| 25 | |
| 26 | #ifndef _SDCARDFS_H_ |
| 27 | #define _SDCARDFS_H_ |
| 28 | |
| 29 | #include <linux/dcache.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/fs.h> |
Daniel Rosenberg | 85626b4 | 2017-02-24 15:49:45 -0800 | [diff] [blame] | 32 | #include <linux/aio.h> |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 33 | #include <linux/kref.h> |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 34 | #include <linux/mm.h> |
| 35 | #include <linux/mount.h> |
| 36 | #include <linux/namei.h> |
| 37 | #include <linux/seq_file.h> |
| 38 | #include <linux/statfs.h> |
| 39 | #include <linux/fs_stack.h> |
| 40 | #include <linux/magic.h> |
| 41 | #include <linux/uaccess.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <linux/sched.h> |
| 44 | #include <linux/types.h> |
| 45 | #include <linux/security.h> |
| 46 | #include <linux/string.h> |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 47 | #include <linux/list.h> |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 48 | #include "multiuser.h" |
| 49 | |
| 50 | /* the file system name */ |
| 51 | #define SDCARDFS_NAME "sdcardfs" |
| 52 | |
| 53 | /* sdcardfs root inode number */ |
| 54 | #define SDCARDFS_ROOT_INO 1 |
| 55 | |
| 56 | /* useful for tracking code reachability */ |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 57 | #define UDBG pr_default("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 58 | |
| 59 | #define SDCARDFS_DIRENT_SIZE 256 |
| 60 | |
| 61 | /* temporary static uid settings for development */ |
| 62 | #define AID_ROOT 0 /* uid for accessing /mnt/sdcard & extSdcard */ |
| 63 | #define AID_MEDIA_RW 1023 /* internal media storage write access */ |
| 64 | |
| 65 | #define AID_SDCARD_RW 1015 /* external storage write access */ |
| 66 | #define AID_SDCARD_R 1028 /* external storage read access */ |
| 67 | #define AID_SDCARD_PICS 1033 /* external storage photos access */ |
| 68 | #define AID_SDCARD_AV 1034 /* external storage audio/video access */ |
| 69 | #define AID_SDCARD_ALL 1035 /* access all users external storage */ |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 70 | #define AID_MEDIA_OBB 1059 /* obb files */ |
| 71 | |
| 72 | #define AID_SDCARD_IMAGE 1057 |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 73 | |
| 74 | #define AID_PACKAGE_INFO 1027 |
| 75 | |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Permissions are handled by our permission function. |
| 79 | * We don't want anyone who happens to look at our inode value to prematurely |
| 80 | * block access, so store more permissive values. These are probably never |
| 81 | * used. |
| 82 | */ |
| 83 | #define fixup_tmp_permissions(x) \ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 84 | do { \ |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 85 | (x)->i_uid = make_kuid(&init_user_ns, \ |
| 86 | SDCARDFS_I(x)->data->d_uid); \ |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 87 | (x)->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW); \ |
| 88 | (x)->i_mode = ((x)->i_mode & S_IFMT) | 0775;\ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 89 | } while (0) |
| 90 | |
| 91 | /* OVERRIDE_CRED() and REVERT_CRED() |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 92 | * OVERRIDE_CRED() |
| 93 | * backup original task->cred |
| 94 | * and modifies task->cred->fsuid/fsgid to specified value. |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 95 | * REVERT_CRED() |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 96 | * restore original task->cred->fsuid/fsgid. |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 97 | * These two macro should be used in pair, and OVERRIDE_CRED() should be |
| 98 | * placed at the beginning of a function, right after variable declaration. |
| 99 | */ |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 100 | #define OVERRIDE_CRED(sdcardfs_sbi, saved_cred, info) \ |
| 101 | do { \ |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 102 | saved_cred = override_fsids(sdcardfs_sbi, info->data); \ |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 103 | if (!saved_cred) \ |
| 104 | return -ENOMEM; \ |
| 105 | } while (0) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 106 | |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 107 | #define OVERRIDE_CRED_PTR(sdcardfs_sbi, saved_cred, info) \ |
| 108 | do { \ |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 109 | saved_cred = override_fsids(sdcardfs_sbi, info->data); \ |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 110 | if (!saved_cred) \ |
| 111 | return ERR_PTR(-ENOMEM); \ |
| 112 | } while (0) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 113 | |
| 114 | #define REVERT_CRED(saved_cred) revert_fsids(saved_cred) |
| 115 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 116 | /* Android 5.0 support */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 117 | |
| 118 | /* Permission mode for a specific node. Controls how file permissions |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 119 | * are derived for children nodes. |
| 120 | */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 121 | typedef enum { |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 122 | /* Nothing special; this node should just inherit from its parent. */ |
| 123 | PERM_INHERIT, |
| 124 | /* This node is one level above a normal root; used for legacy layouts |
| 125 | * which use the first level to represent user_id. |
| 126 | */ |
| 127 | PERM_PRE_ROOT, |
| 128 | /* This node is "/" */ |
| 129 | PERM_ROOT, |
| 130 | /* This node is "/Android" */ |
| 131 | PERM_ANDROID, |
| 132 | /* This node is "/Android/data" */ |
| 133 | PERM_ANDROID_DATA, |
| 134 | /* This node is "/Android/obb" */ |
| 135 | PERM_ANDROID_OBB, |
| 136 | /* This node is "/Android/media" */ |
| 137 | PERM_ANDROID_MEDIA, |
| 138 | /* This node is "/Android/[data|media|obb]/[package]" */ |
| 139 | PERM_ANDROID_PACKAGE, |
| 140 | /* This node is "/Android/[data|media|obb]/[package]/cache" */ |
| 141 | PERM_ANDROID_PACKAGE_CACHE, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 142 | } perm_t; |
| 143 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 144 | struct sdcardfs_sb_info; |
| 145 | struct sdcardfs_mount_options; |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 146 | struct sdcardfs_inode_info; |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 147 | struct sdcardfs_inode_data; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 148 | |
| 149 | /* Do not directly use this function. Use OVERRIDE_CRED() instead. */ |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 150 | const struct cred *override_fsids(struct sdcardfs_sb_info *sbi, |
| 151 | struct sdcardfs_inode_data *data); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 152 | /* Do not directly use this function, use REVERT_CRED() instead. */ |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 153 | void revert_fsids(const struct cred *old_cred); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 154 | |
| 155 | /* operations vectors defined in specific files */ |
| 156 | extern const struct file_operations sdcardfs_main_fops; |
| 157 | extern const struct file_operations sdcardfs_dir_fops; |
| 158 | extern const struct inode_operations sdcardfs_main_iops; |
| 159 | extern const struct inode_operations sdcardfs_dir_iops; |
| 160 | extern const struct inode_operations sdcardfs_symlink_iops; |
| 161 | extern const struct super_operations sdcardfs_sops; |
| 162 | extern const struct dentry_operations sdcardfs_ci_dops; |
| 163 | extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops; |
| 164 | extern const struct vm_operations_struct sdcardfs_vm_ops; |
| 165 | |
| 166 | extern int sdcardfs_init_inode_cache(void); |
| 167 | extern void sdcardfs_destroy_inode_cache(void); |
| 168 | extern int sdcardfs_init_dentry_cache(void); |
| 169 | extern void sdcardfs_destroy_dentry_cache(void); |
| 170 | extern int new_dentry_private_data(struct dentry *dentry); |
| 171 | extern void free_dentry_private_data(struct dentry *dentry); |
| 172 | extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry, |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 173 | unsigned int flags); |
| 174 | extern struct inode *sdcardfs_iget(struct super_block *sb, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 175 | struct inode *lower_inode, userid_t id); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 176 | extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 177 | struct path *lower_path, userid_t id); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 178 | |
| 179 | /* file private data */ |
| 180 | struct sdcardfs_file_info { |
| 181 | struct file *lower_file; |
| 182 | const struct vm_operations_struct *lower_vm_ops; |
| 183 | }; |
| 184 | |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 185 | struct sdcardfs_inode_data { |
| 186 | struct kref refcount; |
| 187 | bool abandoned; |
| 188 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 189 | perm_t perm; |
| 190 | userid_t userid; |
| 191 | uid_t d_uid; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 192 | bool under_android; |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 193 | bool under_cache; |
| 194 | bool under_obb; |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | /* sdcardfs inode data in memory */ |
| 198 | struct sdcardfs_inode_info { |
| 199 | struct inode *lower_inode; |
| 200 | /* state derived based on current position in hierarchy */ |
| 201 | struct sdcardfs_inode_data *data; |
| 202 | |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 203 | /* top folder for ownership */ |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 204 | struct sdcardfs_inode_data *top_data; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 205 | |
| 206 | struct inode vfs_inode; |
| 207 | }; |
| 208 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 209 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 210 | /* sdcardfs dentry data in memory */ |
| 211 | struct sdcardfs_dentry_info { |
| 212 | spinlock_t lock; /* protects lower_path */ |
| 213 | struct path lower_path; |
| 214 | struct path orig_path; |
| 215 | }; |
| 216 | |
| 217 | struct sdcardfs_mount_options { |
| 218 | uid_t fs_low_uid; |
| 219 | gid_t fs_low_gid; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 220 | userid_t fs_user_id; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 221 | bool multiuser; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 222 | unsigned int reserved_mb; |
| 223 | }; |
| 224 | |
Daniel Rosenberg | 317e770 | 2016-10-26 17:36:05 -0700 | [diff] [blame] | 225 | struct sdcardfs_vfsmount_options { |
| 226 | gid_t gid; |
| 227 | mode_t mask; |
| 228 | }; |
| 229 | |
| 230 | extern int parse_options_remount(struct super_block *sb, char *options, int silent, |
| 231 | struct sdcardfs_vfsmount_options *vfsopts); |
| 232 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 233 | /* sdcardfs super-block data in memory */ |
| 234 | struct sdcardfs_sb_info { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 235 | struct super_block *sb; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 236 | struct super_block *lower_sb; |
| 237 | /* derived perm policy : some of options have been added |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 238 | * to sdcardfs_mount_options (Android 4.4 support) |
| 239 | */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 240 | struct sdcardfs_mount_options options; |
| 241 | spinlock_t lock; /* protects obbpath */ |
| 242 | char *obbpath_s; |
| 243 | struct path obbpath; |
| 244 | void *pkgl_id; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 245 | struct list_head list; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | /* |
| 249 | * inode to private data |
| 250 | * |
| 251 | * Since we use containers and the struct inode is _inside_ the |
| 252 | * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL |
| 253 | * inode pointer), return a valid non-NULL pointer. |
| 254 | */ |
| 255 | static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode) |
| 256 | { |
| 257 | return container_of(inode, struct sdcardfs_inode_info, vfs_inode); |
| 258 | } |
| 259 | |
| 260 | /* dentry to private data */ |
| 261 | #define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata) |
| 262 | |
| 263 | /* superblock to private data */ |
| 264 | #define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info) |
| 265 | |
| 266 | /* file to private Data */ |
| 267 | #define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data)) |
| 268 | |
| 269 | /* file to lower file */ |
| 270 | static inline struct file *sdcardfs_lower_file(const struct file *f) |
| 271 | { |
| 272 | return SDCARDFS_F(f)->lower_file; |
| 273 | } |
| 274 | |
| 275 | static inline void sdcardfs_set_lower_file(struct file *f, struct file *val) |
| 276 | { |
| 277 | SDCARDFS_F(f)->lower_file = val; |
| 278 | } |
| 279 | |
| 280 | /* inode to lower inode. */ |
| 281 | static inline struct inode *sdcardfs_lower_inode(const struct inode *i) |
| 282 | { |
| 283 | return SDCARDFS_I(i)->lower_inode; |
| 284 | } |
| 285 | |
| 286 | static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val) |
| 287 | { |
| 288 | SDCARDFS_I(i)->lower_inode = val; |
| 289 | } |
| 290 | |
| 291 | /* superblock to lower superblock */ |
| 292 | static inline struct super_block *sdcardfs_lower_super( |
| 293 | const struct super_block *sb) |
| 294 | { |
| 295 | return SDCARDFS_SB(sb)->lower_sb; |
| 296 | } |
| 297 | |
| 298 | static inline void sdcardfs_set_lower_super(struct super_block *sb, |
| 299 | struct super_block *val) |
| 300 | { |
| 301 | SDCARDFS_SB(sb)->lower_sb = val; |
| 302 | } |
| 303 | |
| 304 | /* path based (dentry/mnt) macros */ |
| 305 | static inline void pathcpy(struct path *dst, const struct path *src) |
| 306 | { |
| 307 | dst->dentry = src->dentry; |
| 308 | dst->mnt = src->mnt; |
| 309 | } |
| 310 | |
| 311 | /* sdcardfs_get_pname functions calls path_get() |
| 312 | * therefore, the caller must call "proper" path_put functions |
| 313 | */ |
| 314 | #define SDCARDFS_DENT_FUNC(pname) \ |
| 315 | static inline void sdcardfs_get_##pname(const struct dentry *dent, \ |
| 316 | struct path *pname) \ |
| 317 | { \ |
| 318 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 319 | pathcpy(pname, &SDCARDFS_D(dent)->pname); \ |
| 320 | path_get(pname); \ |
| 321 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 322 | return; \ |
| 323 | } \ |
| 324 | static inline void sdcardfs_put_##pname(const struct dentry *dent, \ |
| 325 | struct path *pname) \ |
| 326 | { \ |
| 327 | path_put(pname); \ |
| 328 | return; \ |
| 329 | } \ |
| 330 | static inline void sdcardfs_set_##pname(const struct dentry *dent, \ |
| 331 | struct path *pname) \ |
| 332 | { \ |
| 333 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 334 | pathcpy(&SDCARDFS_D(dent)->pname, pname); \ |
| 335 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 336 | return; \ |
| 337 | } \ |
| 338 | static inline void sdcardfs_reset_##pname(const struct dentry *dent) \ |
| 339 | { \ |
| 340 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
| 341 | SDCARDFS_D(dent)->pname.dentry = NULL; \ |
| 342 | SDCARDFS_D(dent)->pname.mnt = NULL; \ |
| 343 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 344 | return; \ |
| 345 | } \ |
| 346 | static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \ |
| 347 | { \ |
| 348 | struct path pname; \ |
| 349 | spin_lock(&SDCARDFS_D(dent)->lock); \ |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 350 | if (SDCARDFS_D(dent)->pname.dentry) { \ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 351 | pathcpy(&pname, &SDCARDFS_D(dent)->pname); \ |
| 352 | SDCARDFS_D(dent)->pname.dentry = NULL; \ |
| 353 | SDCARDFS_D(dent)->pname.mnt = NULL; \ |
| 354 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 355 | path_put(&pname); \ |
| 356 | } else \ |
| 357 | spin_unlock(&SDCARDFS_D(dent)->lock); \ |
| 358 | return; \ |
| 359 | } |
| 360 | |
| 361 | SDCARDFS_DENT_FUNC(lower_path) |
| 362 | SDCARDFS_DENT_FUNC(orig_path) |
| 363 | |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 364 | static inline bool sbinfo_has_sdcard_magic(struct sdcardfs_sb_info *sbinfo) |
| 365 | { |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 366 | return sbinfo && sbinfo->sb |
| 367 | && sbinfo->sb->s_magic == SDCARDFS_SUPER_MAGIC; |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 370 | static inline struct sdcardfs_inode_data *data_get( |
| 371 | struct sdcardfs_inode_data *data) |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 372 | { |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 373 | if (data) |
| 374 | kref_get(&data->refcount); |
| 375 | return data; |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 378 | static inline struct sdcardfs_inode_data *top_data_get( |
| 379 | struct sdcardfs_inode_info *info) |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 380 | { |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 381 | return data_get(info->top_data); |
| 382 | } |
| 383 | |
| 384 | extern void data_release(struct kref *ref); |
| 385 | |
| 386 | static inline void data_put(struct sdcardfs_inode_data *data) |
| 387 | { |
| 388 | kref_put(&data->refcount, data_release); |
| 389 | } |
| 390 | |
| 391 | static inline void release_own_data(struct sdcardfs_inode_info *info) |
| 392 | { |
| 393 | /* |
| 394 | * This happens exactly once per inode. At this point, the inode that |
| 395 | * originally held this data is about to be freed, and all references |
| 396 | * to it are held as a top value, and will likely be released soon. |
| 397 | */ |
| 398 | info->data->abandoned = true; |
| 399 | data_put(info->data); |
| 400 | } |
| 401 | |
| 402 | static inline void set_top(struct sdcardfs_inode_info *info, |
| 403 | struct sdcardfs_inode_data *top) |
| 404 | { |
| 405 | struct sdcardfs_inode_data *old_top = info->top_data; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 406 | |
| 407 | if (top) |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 408 | data_get(top); |
| 409 | info->top_data = top; |
| 410 | if (old_top) |
| 411 | data_put(old_top); |
Daniel Rosenberg | 5080d24 | 2016-05-18 16:57:10 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 414 | static inline int get_gid(struct vfsmount *mnt, |
| 415 | struct sdcardfs_inode_data *data) |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 416 | { |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 417 | struct sdcardfs_vfsmount_options *opts = mnt->data; |
| 418 | |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 419 | if (opts->gid == AID_SDCARD_RW) |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 420 | /* As an optimization, certain trusted system components only run |
| 421 | * as owner but operate across all users. Since we're now handing |
| 422 | * out the sdcard_rw GID only to trusted apps, we're okay relaxing |
| 423 | * the user boundary enforcement for the default view. The UIDs |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 424 | * assigned to app directories are still multiuser aware. |
| 425 | */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 426 | return AID_SDCARD_RW; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 427 | else |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 428 | return multiuser_get_uid(data->userid, opts->gid); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 429 | } |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 430 | |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 431 | static inline int get_mode(struct vfsmount *mnt, |
| 432 | struct sdcardfs_inode_info *info, |
| 433 | struct sdcardfs_inode_data *data) |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 434 | { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 435 | int owner_mode; |
| 436 | int filtered_mode; |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 437 | struct sdcardfs_vfsmount_options *opts = mnt->data; |
| 438 | int visible_mode = 0775 & ~opts->mask; |
| 439 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 440 | |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 441 | if (data->perm == PERM_PRE_ROOT) { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 442 | /* Top of multi-user view should always be visible to ensure |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 443 | * secondary users can traverse inside. |
| 444 | */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 445 | visible_mode = 0711; |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 446 | } else if (data->under_android) { |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 447 | /* Block "other" access to Android directories, since only apps |
| 448 | * belonging to a specific user should be in there; we still |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 449 | * leave +x open for the default view. |
| 450 | */ |
| 451 | if (opts->gid == AID_SDCARD_RW) |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 452 | visible_mode = visible_mode & ~0006; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 453 | else |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 454 | visible_mode = visible_mode & ~0007; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 455 | } |
| 456 | owner_mode = info->lower_inode->i_mode & 0700; |
| 457 | filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6)); |
| 458 | return filtered_mode; |
| 459 | } |
| 460 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 461 | static inline int has_graft_path(const struct dentry *dent) |
| 462 | { |
| 463 | int ret = 0; |
| 464 | |
| 465 | spin_lock(&SDCARDFS_D(dent)->lock); |
| 466 | if (SDCARDFS_D(dent)->orig_path.dentry != NULL) |
| 467 | ret = 1; |
| 468 | spin_unlock(&SDCARDFS_D(dent)->lock); |
| 469 | |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | static inline void sdcardfs_get_real_lower(const struct dentry *dent, |
| 474 | struct path *real_lower) |
| 475 | { |
| 476 | /* in case of a local obb dentry |
| 477 | * the orig_path should be returned |
| 478 | */ |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 479 | if (has_graft_path(dent)) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 480 | sdcardfs_get_orig_path(dent, real_lower); |
| 481 | else |
| 482 | sdcardfs_get_lower_path(dent, real_lower); |
| 483 | } |
| 484 | |
| 485 | static inline void sdcardfs_put_real_lower(const struct dentry *dent, |
| 486 | struct path *real_lower) |
| 487 | { |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 488 | if (has_graft_path(dent)) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 489 | sdcardfs_put_orig_path(dent, real_lower); |
| 490 | else |
| 491 | sdcardfs_put_lower_path(dent, real_lower); |
| 492 | } |
| 493 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 494 | extern struct mutex sdcardfs_super_list_lock; |
| 495 | extern struct list_head sdcardfs_super_list; |
| 496 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 497 | /* for packagelist.c */ |
Daniel Rosenberg | fbd34b6 | 2016-05-10 13:42:43 -0700 | [diff] [blame] | 498 | extern appid_t get_appid(const char *app_name); |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 499 | extern appid_t get_ext_gid(const char *app_name); |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 500 | extern appid_t is_excluded(const char *app_name, userid_t userid); |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 501 | extern int check_caller_access_to_name(struct inode *parent_node, const struct qstr *name); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 502 | extern int packagelist_init(void); |
| 503 | extern void packagelist_exit(void); |
| 504 | |
| 505 | /* for derived_perm.c */ |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 506 | #define BY_NAME (1 << 0) |
| 507 | #define BY_USERID (1 << 1) |
| 508 | struct limit_search { |
| 509 | unsigned int flags; |
Daniel Rosenberg | 721274a | 2017-03-08 17:20:02 -0800 | [diff] [blame] | 510 | struct qstr name; |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 511 | userid_t userid; |
| 512 | }; |
| 513 | |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 514 | extern void setup_derived_state(struct inode *inode, perm_t perm, |
| 515 | userid_t userid, uid_t uid, bool under_android, |
| 516 | struct sdcardfs_inode_data *top); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 517 | extern void get_derived_permission(struct dentry *parent, struct dentry *dentry); |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 518 | extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name); |
Daniel Rosenberg | d8caaf9 | 2017-01-22 15:32:49 -0800 | [diff] [blame] | 519 | extern void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 520 | |
| 521 | extern void update_derived_permission_lock(struct dentry *dentry); |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 522 | void fixup_lower_ownership(struct dentry *dentry, const char *name); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 523 | extern int need_graft_path(struct dentry *dentry); |
| 524 | extern int is_base_obbpath(struct dentry *dentry); |
| 525 | extern int is_obbpath_invalid(struct dentry *dentry); |
| 526 | extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path); |
| 527 | |
| 528 | /* locking helpers */ |
| 529 | static inline struct dentry *lock_parent(struct dentry *dentry) |
| 530 | { |
| 531 | struct dentry *dir = dget_parent(dentry); |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 532 | |
Amit Pundir | b47e110 | 2016-06-01 21:53:20 +0530 | [diff] [blame] | 533 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 534 | return dir; |
| 535 | } |
| 536 | |
| 537 | static inline void unlock_dir(struct dentry *dir) |
| 538 | { |
Amit Pundir | b47e110 | 2016-06-01 21:53:20 +0530 | [diff] [blame] | 539 | inode_unlock(d_inode(dir)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 540 | dput(dir); |
| 541 | } |
| 542 | |
| 543 | static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode) |
| 544 | { |
| 545 | int err; |
| 546 | struct dentry *dent; |
| 547 | struct iattr attrs; |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 548 | struct path parent; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 549 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 550 | dent = kern_path_locked(path_s, &parent); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 551 | if (IS_ERR(dent)) { |
| 552 | err = PTR_ERR(dent); |
| 553 | if (err == -EEXIST) |
| 554 | err = 0; |
| 555 | goto out_unlock; |
| 556 | } |
| 557 | |
Daniel Rosenberg | 1844d9e | 2016-10-26 16:48:45 -0700 | [diff] [blame] | 558 | err = vfs_mkdir2(parent.mnt, d_inode(parent.dentry), dent, mode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 559 | if (err) { |
| 560 | if (err == -EEXIST) |
| 561 | err = 0; |
| 562 | goto out_dput; |
| 563 | } |
| 564 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 565 | attrs.ia_uid = make_kuid(&init_user_ns, uid); |
| 566 | attrs.ia_gid = make_kgid(&init_user_ns, gid); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 567 | attrs.ia_valid = ATTR_UID | ATTR_GID; |
Amit Pundir | b47e110 | 2016-06-01 21:53:20 +0530 | [diff] [blame] | 568 | inode_lock(d_inode(dent)); |
Daniel Rosenberg | 1844d9e | 2016-10-26 16:48:45 -0700 | [diff] [blame] | 569 | notify_change2(parent.mnt, dent, &attrs, NULL); |
Amit Pundir | b47e110 | 2016-06-01 21:53:20 +0530 | [diff] [blame] | 570 | inode_unlock(d_inode(dent)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 571 | |
| 572 | out_dput: |
| 573 | dput(dent); |
| 574 | |
| 575 | out_unlock: |
| 576 | /* parent dentry locked by lookup_create */ |
Amit Pundir | b47e110 | 2016-06-01 21:53:20 +0530 | [diff] [blame] | 577 | inode_unlock(d_inode(parent.dentry)); |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 578 | path_put(&parent); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 579 | return err; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Return 1, if a disk has enough free space, otherwise 0. |
| 584 | * We assume that any files can not be overwritten. |
| 585 | */ |
| 586 | static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir) |
| 587 | { |
| 588 | int err; |
| 589 | struct path lower_path; |
| 590 | struct kstatfs statfs; |
| 591 | u64 avail; |
| 592 | struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb); |
| 593 | |
| 594 | if (sbi->options.reserved_mb) { |
| 595 | /* Get fs stat of lower filesystem. */ |
| 596 | sdcardfs_get_lower_path(dentry, &lower_path); |
| 597 | err = vfs_statfs(&lower_path, &statfs); |
| 598 | sdcardfs_put_lower_path(dentry, &lower_path); |
| 599 | |
| 600 | if (unlikely(err)) |
| 601 | return 0; |
| 602 | |
| 603 | /* Invalid statfs informations. */ |
| 604 | if (unlikely(statfs.f_bsize == 0)) |
| 605 | return 0; |
| 606 | |
| 607 | /* if you are checking directory, set size to f_bsize. */ |
| 608 | if (unlikely(dir)) |
| 609 | size = statfs.f_bsize; |
| 610 | |
| 611 | /* available size */ |
| 612 | avail = statfs.f_bavail * statfs.f_bsize; |
| 613 | |
| 614 | /* not enough space */ |
| 615 | if ((u64)size > avail) |
| 616 | return 0; |
| 617 | |
| 618 | /* enough space */ |
| 619 | if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024)) |
| 620 | return 1; |
| 621 | |
| 622 | return 0; |
| 623 | } else |
| 624 | return 1; |
| 625 | } |
| 626 | |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 627 | /* |
| 628 | * Copies attrs and maintains sdcardfs managed attrs |
| 629 | * Since our permission check handles all special permissions, set those to be open |
| 630 | */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 631 | static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src) |
| 632 | { |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 633 | dest->i_mode = (src->i_mode & S_IFMT) | S_IRWXU | S_IRWXG | |
| 634 | S_IROTH | S_IXOTH; /* 0775 */ |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 635 | dest->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(dest)->data->d_uid); |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 636 | dest->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 637 | dest->i_rdev = src->i_rdev; |
| 638 | dest->i_atime = src->i_atime; |
| 639 | dest->i_mtime = src->i_mtime; |
| 640 | dest->i_ctime = src->i_ctime; |
| 641 | dest->i_blkbits = src->i_blkbits; |
| 642 | dest->i_flags = src->i_flags; |
| 643 | set_nlink(dest, src->i_nlink); |
| 644 | } |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 645 | |
| 646 | static inline bool str_case_eq(const char *s1, const char *s2) |
| 647 | { |
| 648 | return !strcasecmp(s1, s2); |
| 649 | } |
| 650 | |
Daniel Rosenberg | 721274a | 2017-03-08 17:20:02 -0800 | [diff] [blame] | 651 | static inline bool str_n_case_eq(const char *s1, const char *s2, size_t len) |
| 652 | { |
| 653 | return !strncasecmp(s1, s2, len); |
| 654 | } |
| 655 | |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 656 | static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2) |
| 657 | { |
| 658 | return q1->len == q2->len && str_case_eq(q1->name, q2->name); |
| 659 | } |
| 660 | |
| 661 | #define QSTR_LITERAL(string) QSTR_INIT(string, sizeof(string)-1) |
| 662 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 663 | #endif /* not _SDCARDFS_H_ */ |