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