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