blob: 826afb5c7e88360a42f7dff18879851b76440f9a [file] [log] [blame]
Daniel Campello35c9e242015-07-20 16:23:50 -07001/*
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 Rosenberg85626b42017-02-24 15:49:45 -080032#include <linux/aio.h>
Daniel Rosenberga56a1052017-05-15 14:03:15 -070033#include <linux/kref.h>
Daniel Campello35c9e242015-07-20 16:23:50 -070034#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 Rosenberg497ac902016-02-03 21:08:21 -080047#include <linux/list.h>
Daniel Campello35c9e242015-07-20 16:23:50 -070048#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 Rosenberg77ecf212017-03-16 17:46:13 -070057#define UDBG pr_default("DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
Daniel Campello35c9e242015-07-20 16:23:50 -070058
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 Rosenbergad905252017-01-25 13:48:45 -080070#define AID_MEDIA_OBB 1059 /* obb files */
71
72#define AID_SDCARD_IMAGE 1057
Daniel Campello35c9e242015-07-20 16:23:50 -070073
74#define AID_PACKAGE_INFO 1027
75
Daniel Rosenberg90219272016-10-26 20:27:20 -070076
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 Campello35c9e242015-07-20 16:23:50 -070084 do { \
Daniel Rosenberga56a1052017-05-15 14:03:15 -070085 (x)->i_uid = make_kuid(&init_user_ns, \
86 SDCARDFS_I(x)->data->d_uid); \
Daniel Rosenberg90219272016-10-26 20:27:20 -070087 (x)->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW); \
88 (x)->i_mode = ((x)->i_mode & S_IFMT) | 0775;\
Daniel Campello35c9e242015-07-20 16:23:50 -070089 } while (0)
90
91/* OVERRIDE_CRED() and REVERT_CRED()
Daniel Rosenberg5e024f62017-03-16 17:42:58 -070092 * OVERRIDE_CRED()
93 * backup original task->cred
94 * and modifies task->cred->fsuid/fsgid to specified value.
Daniel Campello35c9e242015-07-20 16:23:50 -070095 * REVERT_CRED()
Daniel Rosenberg5e024f62017-03-16 17:42:58 -070096 * restore original task->cred->fsuid/fsgid.
Daniel Campello35c9e242015-07-20 16:23:50 -070097 * 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 Rosenbergad905252017-01-25 13:48:45 -0800100#define OVERRIDE_CRED(sdcardfs_sbi, saved_cred, info) \
101 do { \
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700102 saved_cred = override_fsids(sdcardfs_sbi, info->data); \
Daniel Rosenbergad905252017-01-25 13:48:45 -0800103 if (!saved_cred) \
104 return -ENOMEM; \
105 } while (0)
Daniel Campello35c9e242015-07-20 16:23:50 -0700106
Daniel Rosenbergad905252017-01-25 13:48:45 -0800107#define OVERRIDE_CRED_PTR(sdcardfs_sbi, saved_cred, info) \
108 do { \
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700109 saved_cred = override_fsids(sdcardfs_sbi, info->data); \
Daniel Rosenbergad905252017-01-25 13:48:45 -0800110 if (!saved_cred) \
111 return ERR_PTR(-ENOMEM); \
112 } while (0)
Daniel Campello35c9e242015-07-20 16:23:50 -0700113
114#define REVERT_CRED(saved_cred) revert_fsids(saved_cred)
115
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800116/* Android 5.0 support */
Daniel Campello35c9e242015-07-20 16:23:50 -0700117
118/* Permission mode for a specific node. Controls how file permissions
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700119 * are derived for children nodes.
120 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700121typedef enum {
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700122 /* 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 Campello35c9e242015-07-20 16:23:50 -0700142} perm_t;
143
Daniel Campello35c9e242015-07-20 16:23:50 -0700144struct sdcardfs_sb_info;
145struct sdcardfs_mount_options;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800146struct sdcardfs_inode_info;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700147struct sdcardfs_inode_data;
Daniel Campello35c9e242015-07-20 16:23:50 -0700148
149/* Do not directly use this function. Use OVERRIDE_CRED() instead. */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700150const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
151 struct sdcardfs_inode_data *data);
Daniel Campello35c9e242015-07-20 16:23:50 -0700152/* Do not directly use this function, use REVERT_CRED() instead. */
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700153void revert_fsids(const struct cred *old_cred);
Daniel Campello35c9e242015-07-20 16:23:50 -0700154
155/* operations vectors defined in specific files */
156extern const struct file_operations sdcardfs_main_fops;
157extern const struct file_operations sdcardfs_dir_fops;
158extern const struct inode_operations sdcardfs_main_iops;
159extern const struct inode_operations sdcardfs_dir_iops;
160extern const struct inode_operations sdcardfs_symlink_iops;
161extern const struct super_operations sdcardfs_sops;
162extern const struct dentry_operations sdcardfs_ci_dops;
163extern const struct address_space_operations sdcardfs_aops, sdcardfs_dummy_aops;
164extern const struct vm_operations_struct sdcardfs_vm_ops;
165
166extern int sdcardfs_init_inode_cache(void);
167extern void sdcardfs_destroy_inode_cache(void);
168extern int sdcardfs_init_dentry_cache(void);
169extern void sdcardfs_destroy_dentry_cache(void);
170extern int new_dentry_private_data(struct dentry *dentry);
171extern void free_dentry_private_data(struct dentry *dentry);
172extern struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
Daniel Campellod1d080c2015-07-20 16:27:37 -0700173 unsigned int flags);
174extern struct inode *sdcardfs_iget(struct super_block *sb,
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800175 struct inode *lower_inode, userid_t id);
Daniel Campello35c9e242015-07-20 16:23:50 -0700176extern int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800177 struct path *lower_path, userid_t id);
Daniel Campello35c9e242015-07-20 16:23:50 -0700178
179/* file private data */
180struct sdcardfs_file_info {
181 struct file *lower_file;
182 const struct vm_operations_struct *lower_vm_ops;
183};
184
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700185struct sdcardfs_inode_data {
186 struct kref refcount;
187 bool abandoned;
188
Daniel Campello35c9e242015-07-20 16:23:50 -0700189 perm_t perm;
190 userid_t userid;
191 uid_t d_uid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800192 bool under_android;
Daniel Rosenbergad905252017-01-25 13:48:45 -0800193 bool under_cache;
194 bool under_obb;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700195};
196
197/* sdcardfs inode data in memory */
198struct 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 Rosenberg5080d242016-05-18 16:57:10 -0700203 /* top folder for ownership */
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800204 spinlock_t top_lock;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700205 struct sdcardfs_inode_data *top_data;
Daniel Campello35c9e242015-07-20 16:23:50 -0700206
207 struct inode vfs_inode;
208};
209
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800210
Daniel Campello35c9e242015-07-20 16:23:50 -0700211/* sdcardfs dentry data in memory */
212struct sdcardfs_dentry_info {
213 spinlock_t lock; /* protects lower_path */
214 struct path lower_path;
215 struct path orig_path;
216};
217
218struct sdcardfs_mount_options {
219 uid_t fs_low_uid;
220 gid_t fs_low_gid;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800221 userid_t fs_user_id;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800222 bool multiuser;
Daniel Rosenberg7d10e432017-07-19 17:25:07 -0700223 bool gid_derivation;
Daniel Rosenberge12a9c42018-01-18 16:17:16 -0800224 bool default_normal;
Daniel Campello35c9e242015-07-20 16:23:50 -0700225 unsigned int reserved_mb;
226};
227
Daniel Rosenberg317e7702016-10-26 17:36:05 -0700228struct sdcardfs_vfsmount_options {
229 gid_t gid;
230 mode_t mask;
231};
232
233extern int parse_options_remount(struct super_block *sb, char *options, int silent,
234 struct sdcardfs_vfsmount_options *vfsopts);
235
Daniel Campello35c9e242015-07-20 16:23:50 -0700236/* sdcardfs super-block data in memory */
237struct sdcardfs_sb_info {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800238 struct super_block *sb;
Daniel Campello35c9e242015-07-20 16:23:50 -0700239 struct super_block *lower_sb;
240 /* derived perm policy : some of options have been added
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700241 * to sdcardfs_mount_options (Android 4.4 support)
242 */
Daniel Campello35c9e242015-07-20 16:23:50 -0700243 struct sdcardfs_mount_options options;
244 spinlock_t lock; /* protects obbpath */
245 char *obbpath_s;
246 struct path obbpath;
247 void *pkgl_id;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800248 struct list_head list;
Daniel Campello35c9e242015-07-20 16:23:50 -0700249};
250
251/*
252 * inode to private data
253 *
254 * Since we use containers and the struct inode is _inside_ the
255 * sdcardfs_inode_info structure, SDCARDFS_I will always (given a non-NULL
256 * inode pointer), return a valid non-NULL pointer.
257 */
258static inline struct sdcardfs_inode_info *SDCARDFS_I(const struct inode *inode)
259{
260 return container_of(inode, struct sdcardfs_inode_info, vfs_inode);
261}
262
263/* dentry to private data */
264#define SDCARDFS_D(dent) ((struct sdcardfs_dentry_info *)(dent)->d_fsdata)
265
266/* superblock to private data */
267#define SDCARDFS_SB(super) ((struct sdcardfs_sb_info *)(super)->s_fs_info)
268
269/* file to private Data */
270#define SDCARDFS_F(file) ((struct sdcardfs_file_info *)((file)->private_data))
271
272/* file to lower file */
273static inline struct file *sdcardfs_lower_file(const struct file *f)
274{
275 return SDCARDFS_F(f)->lower_file;
276}
277
278static inline void sdcardfs_set_lower_file(struct file *f, struct file *val)
279{
280 SDCARDFS_F(f)->lower_file = val;
281}
282
283/* inode to lower inode. */
284static inline struct inode *sdcardfs_lower_inode(const struct inode *i)
285{
286 return SDCARDFS_I(i)->lower_inode;
287}
288
289static inline void sdcardfs_set_lower_inode(struct inode *i, struct inode *val)
290{
291 SDCARDFS_I(i)->lower_inode = val;
292}
293
294/* superblock to lower superblock */
295static inline struct super_block *sdcardfs_lower_super(
296 const struct super_block *sb)
297{
298 return SDCARDFS_SB(sb)->lower_sb;
299}
300
301static inline void sdcardfs_set_lower_super(struct super_block *sb,
302 struct super_block *val)
303{
304 SDCARDFS_SB(sb)->lower_sb = val;
305}
306
307/* path based (dentry/mnt) macros */
308static inline void pathcpy(struct path *dst, const struct path *src)
309{
310 dst->dentry = src->dentry;
311 dst->mnt = src->mnt;
312}
313
314/* sdcardfs_get_pname functions calls path_get()
315 * therefore, the caller must call "proper" path_put functions
316 */
317#define SDCARDFS_DENT_FUNC(pname) \
318static inline void sdcardfs_get_##pname(const struct dentry *dent, \
319 struct path *pname) \
320{ \
321 spin_lock(&SDCARDFS_D(dent)->lock); \
322 pathcpy(pname, &SDCARDFS_D(dent)->pname); \
323 path_get(pname); \
324 spin_unlock(&SDCARDFS_D(dent)->lock); \
325 return; \
326} \
327static inline void sdcardfs_put_##pname(const struct dentry *dent, \
328 struct path *pname) \
329{ \
330 path_put(pname); \
331 return; \
332} \
333static inline void sdcardfs_set_##pname(const struct dentry *dent, \
334 struct path *pname) \
335{ \
336 spin_lock(&SDCARDFS_D(dent)->lock); \
337 pathcpy(&SDCARDFS_D(dent)->pname, pname); \
338 spin_unlock(&SDCARDFS_D(dent)->lock); \
339 return; \
340} \
341static inline void sdcardfs_reset_##pname(const struct dentry *dent) \
342{ \
343 spin_lock(&SDCARDFS_D(dent)->lock); \
344 SDCARDFS_D(dent)->pname.dentry = NULL; \
345 SDCARDFS_D(dent)->pname.mnt = NULL; \
346 spin_unlock(&SDCARDFS_D(dent)->lock); \
347 return; \
348} \
349static inline void sdcardfs_put_reset_##pname(const struct dentry *dent) \
350{ \
351 struct path pname; \
352 spin_lock(&SDCARDFS_D(dent)->lock); \
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700353 if (SDCARDFS_D(dent)->pname.dentry) { \
Daniel Campello35c9e242015-07-20 16:23:50 -0700354 pathcpy(&pname, &SDCARDFS_D(dent)->pname); \
355 SDCARDFS_D(dent)->pname.dentry = NULL; \
356 SDCARDFS_D(dent)->pname.mnt = NULL; \
357 spin_unlock(&SDCARDFS_D(dent)->lock); \
358 path_put(&pname); \
359 } else \
360 spin_unlock(&SDCARDFS_D(dent)->lock); \
361 return; \
362}
363
364SDCARDFS_DENT_FUNC(lower_path)
365SDCARDFS_DENT_FUNC(orig_path)
366
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800367static inline bool sbinfo_has_sdcard_magic(struct sdcardfs_sb_info *sbinfo)
368{
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700369 return sbinfo && sbinfo->sb
370 && sbinfo->sb->s_magic == SDCARDFS_SUPER_MAGIC;
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800371}
372
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700373static inline struct sdcardfs_inode_data *data_get(
374 struct sdcardfs_inode_data *data)
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700375{
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700376 if (data)
377 kref_get(&data->refcount);
378 return data;
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700379}
380
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700381static inline struct sdcardfs_inode_data *top_data_get(
382 struct sdcardfs_inode_info *info)
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700383{
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800384 struct sdcardfs_inode_data *top_data;
385
386 spin_lock(&info->top_lock);
387 top_data = data_get(info->top_data);
388 spin_unlock(&info->top_lock);
389 return top_data;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700390}
391
392extern void data_release(struct kref *ref);
393
394static inline void data_put(struct sdcardfs_inode_data *data)
395{
396 kref_put(&data->refcount, data_release);
397}
398
399static inline void release_own_data(struct sdcardfs_inode_info *info)
400{
401 /*
402 * This happens exactly once per inode. At this point, the inode that
403 * originally held this data is about to be freed, and all references
404 * to it are held as a top value, and will likely be released soon.
405 */
406 info->data->abandoned = true;
407 data_put(info->data);
408}
409
410static inline void set_top(struct sdcardfs_inode_info *info,
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800411 struct sdcardfs_inode_info *top_owner)
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700412{
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800413 struct sdcardfs_inode_data *old_top;
414 struct sdcardfs_inode_data *new_top = NULL;
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700415
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800416 if (top_owner)
417 new_top = top_data_get(top_owner);
418
419 spin_lock(&info->top_lock);
420 old_top = info->top_data;
421 info->top_data = new_top;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700422 if (old_top)
423 data_put(old_top);
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800424 spin_unlock(&info->top_lock);
Daniel Rosenberg5080d242016-05-18 16:57:10 -0700425}
426
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700427static inline int get_gid(struct vfsmount *mnt,
Daniel Rosenberge12a9c42018-01-18 16:17:16 -0800428 struct super_block *sb,
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700429 struct sdcardfs_inode_data *data)
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700430{
Daniel Rosenberge12a9c42018-01-18 16:17:16 -0800431 struct sdcardfs_vfsmount_options *vfsopts = mnt->data;
432 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(sb);
Daniel Rosenberg90219272016-10-26 20:27:20 -0700433
Daniel Rosenberge12a9c42018-01-18 16:17:16 -0800434 if (vfsopts->gid == AID_SDCARD_RW && !sbi->options.default_normal)
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800435 /* As an optimization, certain trusted system components only run
436 * as owner but operate across all users. Since we're now handing
437 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
438 * the user boundary enforcement for the default view. The UIDs
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700439 * assigned to app directories are still multiuser aware.
440 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800441 return AID_SDCARD_RW;
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700442 else
Daniel Rosenberge12a9c42018-01-18 16:17:16 -0800443 return multiuser_get_uid(data->userid, vfsopts->gid);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800444}
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700445
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700446static inline int get_mode(struct vfsmount *mnt,
447 struct sdcardfs_inode_info *info,
448 struct sdcardfs_inode_data *data)
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700449{
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800450 int owner_mode;
451 int filtered_mode;
Daniel Rosenberg90219272016-10-26 20:27:20 -0700452 struct sdcardfs_vfsmount_options *opts = mnt->data;
453 int visible_mode = 0775 & ~opts->mask;
454
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800455
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700456 if (data->perm == PERM_PRE_ROOT) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800457 /* Top of multi-user view should always be visible to ensure
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700458 * secondary users can traverse inside.
459 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800460 visible_mode = 0711;
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700461 } else if (data->under_android) {
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800462 /* Block "other" access to Android directories, since only apps
463 * belonging to a specific user should be in there; we still
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700464 * leave +x open for the default view.
465 */
466 if (opts->gid == AID_SDCARD_RW)
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800467 visible_mode = visible_mode & ~0006;
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700468 else
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800469 visible_mode = visible_mode & ~0007;
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800470 }
471 owner_mode = info->lower_inode->i_mode & 0700;
472 filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
473 return filtered_mode;
474}
475
Daniel Campello35c9e242015-07-20 16:23:50 -0700476static inline int has_graft_path(const struct dentry *dent)
477{
478 int ret = 0;
479
480 spin_lock(&SDCARDFS_D(dent)->lock);
481 if (SDCARDFS_D(dent)->orig_path.dentry != NULL)
482 ret = 1;
483 spin_unlock(&SDCARDFS_D(dent)->lock);
484
485 return ret;
486}
487
488static inline void sdcardfs_get_real_lower(const struct dentry *dent,
489 struct path *real_lower)
490{
491 /* in case of a local obb dentry
492 * the orig_path should be returned
493 */
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700494 if (has_graft_path(dent))
Daniel Campello35c9e242015-07-20 16:23:50 -0700495 sdcardfs_get_orig_path(dent, real_lower);
496 else
497 sdcardfs_get_lower_path(dent, real_lower);
498}
499
500static inline void sdcardfs_put_real_lower(const struct dentry *dent,
501 struct path *real_lower)
502{
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700503 if (has_graft_path(dent))
Daniel Campello35c9e242015-07-20 16:23:50 -0700504 sdcardfs_put_orig_path(dent, real_lower);
505 else
506 sdcardfs_put_lower_path(dent, real_lower);
507}
508
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800509extern struct mutex sdcardfs_super_list_lock;
510extern struct list_head sdcardfs_super_list;
511
Daniel Campello35c9e242015-07-20 16:23:50 -0700512/* for packagelist.c */
Daniel Rosenbergfbd34b62016-05-10 13:42:43 -0700513extern appid_t get_appid(const char *app_name);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800514extern appid_t get_ext_gid(const char *app_name);
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800515extern appid_t is_excluded(const char *app_name, userid_t userid);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800516extern int check_caller_access_to_name(struct inode *parent_node, const struct qstr *name);
Daniel Campello35c9e242015-07-20 16:23:50 -0700517extern int packagelist_init(void);
518extern void packagelist_exit(void);
519
520/* for derived_perm.c */
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800521#define BY_NAME (1 << 0)
522#define BY_USERID (1 << 1)
523struct limit_search {
524 unsigned int flags;
Daniel Rosenberg721274a2017-03-08 17:20:02 -0800525 struct qstr name;
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800526 userid_t userid;
527};
528
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700529extern void setup_derived_state(struct inode *inode, perm_t perm,
Daniel Rosenberg47af77b2018-02-01 16:52:22 -0800530 userid_t userid, uid_t uid);
Daniel Campello35c9e242015-07-20 16:23:50 -0700531extern void get_derived_permission(struct dentry *parent, struct dentry *dentry);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800532extern void get_derived_permission_new(struct dentry *parent, struct dentry *dentry, const struct qstr *name);
Daniel Rosenbergd8caaf92017-01-22 15:32:49 -0800533extern void fixup_perms_recursive(struct dentry *dentry, struct limit_search *limit);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800534
535extern void update_derived_permission_lock(struct dentry *dentry);
Daniel Rosenbergad905252017-01-25 13:48:45 -0800536void fixup_lower_ownership(struct dentry *dentry, const char *name);
Daniel Campello35c9e242015-07-20 16:23:50 -0700537extern int need_graft_path(struct dentry *dentry);
538extern int is_base_obbpath(struct dentry *dentry);
539extern int is_obbpath_invalid(struct dentry *dentry);
540extern int setup_obb_dentry(struct dentry *dentry, struct path *lower_path);
541
542/* locking helpers */
543static inline struct dentry *lock_parent(struct dentry *dentry)
544{
545 struct dentry *dir = dget_parent(dentry);
Daniel Rosenberg5e024f62017-03-16 17:42:58 -0700546
Amit Pundirb47e1102016-06-01 21:53:20 +0530547 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
Daniel Campello35c9e242015-07-20 16:23:50 -0700548 return dir;
549}
550
551static inline void unlock_dir(struct dentry *dir)
552{
Amit Pundirb47e1102016-06-01 21:53:20 +0530553 inode_unlock(d_inode(dir));
Daniel Campello35c9e242015-07-20 16:23:50 -0700554 dput(dir);
555}
556
557static inline int prepare_dir(const char *path_s, uid_t uid, gid_t gid, mode_t mode)
558{
559 int err;
560 struct dentry *dent;
561 struct iattr attrs;
Daniel Campellod1d080c2015-07-20 16:27:37 -0700562 struct path parent;
Daniel Campello35c9e242015-07-20 16:23:50 -0700563
Daniel Campellod1d080c2015-07-20 16:27:37 -0700564 dent = kern_path_locked(path_s, &parent);
Daniel Campello35c9e242015-07-20 16:23:50 -0700565 if (IS_ERR(dent)) {
566 err = PTR_ERR(dent);
567 if (err == -EEXIST)
568 err = 0;
569 goto out_unlock;
570 }
571
Daniel Rosenberg1844d9e2016-10-26 16:48:45 -0700572 err = vfs_mkdir2(parent.mnt, d_inode(parent.dentry), dent, mode);
Daniel Campello35c9e242015-07-20 16:23:50 -0700573 if (err) {
574 if (err == -EEXIST)
575 err = 0;
576 goto out_dput;
577 }
578
Daniel Campellod1d080c2015-07-20 16:27:37 -0700579 attrs.ia_uid = make_kuid(&init_user_ns, uid);
580 attrs.ia_gid = make_kgid(&init_user_ns, gid);
Daniel Campello35c9e242015-07-20 16:23:50 -0700581 attrs.ia_valid = ATTR_UID | ATTR_GID;
Amit Pundirb47e1102016-06-01 21:53:20 +0530582 inode_lock(d_inode(dent));
Daniel Rosenberg1844d9e2016-10-26 16:48:45 -0700583 notify_change2(parent.mnt, dent, &attrs, NULL);
Amit Pundirb47e1102016-06-01 21:53:20 +0530584 inode_unlock(d_inode(dent));
Daniel Campello35c9e242015-07-20 16:23:50 -0700585
586out_dput:
587 dput(dent);
588
589out_unlock:
590 /* parent dentry locked by lookup_create */
Amit Pundirb47e1102016-06-01 21:53:20 +0530591 inode_unlock(d_inode(parent.dentry));
Daniel Campellod1d080c2015-07-20 16:27:37 -0700592 path_put(&parent);
Daniel Campello35c9e242015-07-20 16:23:50 -0700593 return err;
594}
595
596/*
597 * Return 1, if a disk has enough free space, otherwise 0.
598 * We assume that any files can not be overwritten.
599 */
600static inline int check_min_free_space(struct dentry *dentry, size_t size, int dir)
601{
602 int err;
603 struct path lower_path;
604 struct kstatfs statfs;
605 u64 avail;
606 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
607
608 if (sbi->options.reserved_mb) {
609 /* Get fs stat of lower filesystem. */
610 sdcardfs_get_lower_path(dentry, &lower_path);
611 err = vfs_statfs(&lower_path, &statfs);
612 sdcardfs_put_lower_path(dentry, &lower_path);
613
614 if (unlikely(err))
615 return 0;
616
617 /* Invalid statfs informations. */
618 if (unlikely(statfs.f_bsize == 0))
619 return 0;
620
621 /* if you are checking directory, set size to f_bsize. */
622 if (unlikely(dir))
623 size = statfs.f_bsize;
624
625 /* available size */
626 avail = statfs.f_bavail * statfs.f_bsize;
627
628 /* not enough space */
629 if ((u64)size > avail)
630 return 0;
631
632 /* enough space */
633 if ((avail - size) > (sbi->options.reserved_mb * 1024 * 1024))
634 return 1;
635
636 return 0;
637 } else
638 return 1;
639}
640
Daniel Rosenberg90219272016-10-26 20:27:20 -0700641/*
642 * Copies attrs and maintains sdcardfs managed attrs
643 * Since our permission check handles all special permissions, set those to be open
644 */
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800645static inline void sdcardfs_copy_and_fix_attrs(struct inode *dest, const struct inode *src)
646{
Daniel Rosenberg90219272016-10-26 20:27:20 -0700647 dest->i_mode = (src->i_mode & S_IFMT) | S_IRWXU | S_IRWXG |
648 S_IROTH | S_IXOTH; /* 0775 */
Daniel Rosenberga56a1052017-05-15 14:03:15 -0700649 dest->i_uid = make_kuid(&init_user_ns, SDCARDFS_I(dest)->data->d_uid);
Daniel Rosenberg90219272016-10-26 20:27:20 -0700650 dest->i_gid = make_kgid(&init_user_ns, AID_SDCARD_RW);
Daniel Rosenberg497ac902016-02-03 21:08:21 -0800651 dest->i_rdev = src->i_rdev;
652 dest->i_atime = src->i_atime;
653 dest->i_mtime = src->i_mtime;
654 dest->i_ctime = src->i_ctime;
655 dest->i_blkbits = src->i_blkbits;
656 dest->i_flags = src->i_flags;
657 set_nlink(dest, src->i_nlink);
658}
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800659
660static inline bool str_case_eq(const char *s1, const char *s2)
661{
662 return !strcasecmp(s1, s2);
663}
664
Daniel Rosenberg721274a2017-03-08 17:20:02 -0800665static inline bool str_n_case_eq(const char *s1, const char *s2, size_t len)
666{
667 return !strncasecmp(s1, s2, len);
668}
669
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800670static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2)
671{
Ritesh Harjani55a84952018-03-19 15:49:54 +0530672 return q1->len == q2->len && str_n_case_eq(q1->name, q2->name, q2->len);
Daniel Rosenberg5004c5f2017-01-31 20:07:51 -0800673}
674
675#define QSTR_LITERAL(string) QSTR_INIT(string, sizeof(string)-1)
676
Daniel Campello35c9e242015-07-20 16:23:50 -0700677#endif /* not _SDCARDFS_H_ */