1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/compiler_types.h>
4 #include <linux/errno.h>
6 #include <linux/fsnotify.h>
9 #include <linux/init.h>
10 #include <linux/ipc_namespace.h>
11 #include <linux/kdev_t.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/namei.h>
15 #include <linux/magic.h>
16 #include <linux/major.h>
17 #include <linux/miscdevice.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/mount.h>
21 #include <linux/fs_parser.h>
22 #include <linux/radix-tree.h>
23 #include <linux/sched.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock_types.h>
27 #include <linux/stddef.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/uaccess.h>
31 #include <linux/user_namespace.h>
32 #include <linux/xarray.h>
33 #include <uapi/asm-generic/errno-base.h>
34 #include <uapi/linux/android/binder.h>
35 #include <uapi/linux/android/binderfs.h>
37 #include "binder_internal.h"
40 #define SECOND_INODE 2
41 #define INODE_OFFSET 3
43 #define BINDERFS_MAX_MINOR (1U << MINORBITS)
44 /* Ensure that the initial ipc namespace always has devices available. */
45 #define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)
47 static dev_t binderfs_dev
;
48 static DEFINE_MUTEX(binderfs_minors_mutex
);
49 static DEFINE_IDA(binderfs_minors
);
56 enum binderfs_stats_mode
{
57 binderfs_stats_mode_unset
,
58 binderfs_stats_mode_global
,
61 static const struct constant_table binderfs_param_stats
[] = {
62 { "global", binderfs_stats_mode_global
},
66 static const struct fs_parameter_spec binderfs_fs_parameters
[] = {
67 fsparam_u32("max", Opt_max
),
68 fsparam_enum("stats", Opt_stats_mode
, binderfs_param_stats
),
72 static inline struct binderfs_info
*BINDERFS_SB(const struct super_block
*sb
)
77 bool is_binderfs_device(const struct inode
*inode
)
79 if (inode
->i_sb
->s_magic
== BINDERFS_SUPER_MAGIC
)
86 * binderfs_binder_device_create - allocate inode from super block of a
88 * @ref_inode: inode from wich the super block will be taken
89 * @userp: buffer to copy information about new device for userspace to
90 * @req: struct binderfs_device as copied from userspace
92 * This function allocates a new binder_device and reserves a new minor
94 * Minor numbers are limited and tracked globally in binderfs_minors. The
95 * function will stash a struct binder_device for the specific binder
96 * device in i_private of the inode.
97 * It will go on to allocate a new inode from the super block of the
98 * filesystem mount, stash a struct binder_device in its i_private field
99 * and attach a dentry to that inode.
101 * Return: 0 on success, negative errno on failure
103 static int binderfs_binder_device_create(struct inode
*ref_inode
,
104 struct binderfs_device __user
*userp
,
105 struct binderfs_device
*req
)
108 struct dentry
*dentry
, *root
;
109 struct binder_device
*device
;
112 struct inode
*inode
= NULL
;
113 struct super_block
*sb
= ref_inode
->i_sb
;
114 struct binderfs_info
*info
= sb
->s_fs_info
;
115 #if defined(CONFIG_IPC_NS)
116 bool use_reserve
= (info
->ipc_ns
== &init_ipc_ns
);
118 bool use_reserve
= true;
121 /* Reserve new minor number for the new device. */
122 mutex_lock(&binderfs_minors_mutex
);
123 if (++info
->device_count
<= info
->mount_opts
.max
)
124 minor
= ida_alloc_max(&binderfs_minors
,
125 use_reserve
? BINDERFS_MAX_MINOR
:
126 BINDERFS_MAX_MINOR_CAPPED
,
131 --info
->device_count
;
132 mutex_unlock(&binderfs_minors_mutex
);
135 mutex_unlock(&binderfs_minors_mutex
);
138 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
142 inode
= new_inode(sb
);
146 inode
->i_ino
= minor
+ INODE_OFFSET
;
147 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
148 init_special_inode(inode
, S_IFCHR
| 0600,
149 MKDEV(MAJOR(binderfs_dev
), minor
));
150 inode
->i_fop
= &binder_fops
;
151 inode
->i_uid
= info
->root_uid
;
152 inode
->i_gid
= info
->root_gid
;
154 req
->name
[BINDERFS_MAX_NAME
] = '\0'; /* NUL-terminate */
155 name_len
= strlen(req
->name
);
156 /* Make sure to include terminating NUL byte */
157 name
= kmemdup(req
->name
, name_len
+ 1, GFP_KERNEL
);
161 refcount_set(&device
->ref
, 1);
162 device
->binderfs_inode
= inode
;
163 device
->context
.binder_context_mgr_uid
= INVALID_UID
;
164 device
->context
.name
= name
;
165 device
->miscdev
.name
= name
;
166 device
->miscdev
.minor
= minor
;
167 mutex_init(&device
->context
.context_mgr_node_lock
);
169 req
->major
= MAJOR(binderfs_dev
);
172 if (userp
&& copy_to_user(userp
, req
, sizeof(*req
))) {
178 inode_lock(d_inode(root
));
181 dentry
= lookup_one_len(name
, root
, name_len
);
182 if (IS_ERR(dentry
)) {
183 inode_unlock(d_inode(root
));
184 ret
= PTR_ERR(dentry
);
188 if (d_really_is_positive(dentry
)) {
191 inode_unlock(d_inode(root
));
196 inode
->i_private
= device
;
197 d_instantiate(dentry
, inode
);
198 fsnotify_create(root
->d_inode
, dentry
);
199 inode_unlock(d_inode(root
));
206 mutex_lock(&binderfs_minors_mutex
);
207 --info
->device_count
;
208 ida_free(&binderfs_minors
, minor
);
209 mutex_unlock(&binderfs_minors_mutex
);
216 * binderfs_ctl_ioctl - handle binder device node allocation requests
218 * The request handler for the binder-control device. All requests operate on
219 * the binderfs mount the binder-control device resides in:
221 * Allocate a new binder device.
223 * Return: 0 on success, negative errno on failure
225 static long binder_ctl_ioctl(struct file
*file
, unsigned int cmd
,
229 struct inode
*inode
= file_inode(file
);
230 struct binderfs_device __user
*device
= (struct binderfs_device __user
*)arg
;
231 struct binderfs_device device_req
;
235 ret
= copy_from_user(&device_req
, device
, sizeof(device_req
));
241 ret
= binderfs_binder_device_create(inode
, device
, &device_req
);
250 static void binderfs_evict_inode(struct inode
*inode
)
252 struct binder_device
*device
= inode
->i_private
;
253 struct binderfs_info
*info
= BINDERFS_SB(inode
->i_sb
);
257 if (!S_ISCHR(inode
->i_mode
) || !device
)
260 mutex_lock(&binderfs_minors_mutex
);
261 --info
->device_count
;
262 ida_free(&binderfs_minors
, device
->miscdev
.minor
);
263 mutex_unlock(&binderfs_minors_mutex
);
265 if (refcount_dec_and_test(&device
->ref
)) {
266 kfree(device
->context
.name
);
271 static int binderfs_fs_context_parse_param(struct fs_context
*fc
,
272 struct fs_parameter
*param
)
275 struct binderfs_mount_opts
*ctx
= fc
->fs_private
;
276 struct fs_parse_result result
;
278 opt
= fs_parse(fc
, binderfs_fs_parameters
, param
, &result
);
284 if (result
.uint_32
> BINDERFS_MAX_MINOR
)
285 return invalfc(fc
, "Bad value for '%s'", param
->key
);
287 ctx
->max
= result
.uint_32
;
290 if (!capable(CAP_SYS_ADMIN
))
293 ctx
->stats_mode
= result
.uint_32
;
296 return invalfc(fc
, "Unsupported parameter '%s'", param
->key
);
302 static int binderfs_fs_context_reconfigure(struct fs_context
*fc
)
304 struct binderfs_mount_opts
*ctx
= fc
->fs_private
;
305 struct binderfs_info
*info
= BINDERFS_SB(fc
->root
->d_sb
);
307 if (info
->mount_opts
.stats_mode
!= ctx
->stats_mode
)
308 return invalfc(fc
, "Binderfs stats mode cannot be changed during a remount");
310 info
->mount_opts
.stats_mode
= ctx
->stats_mode
;
311 info
->mount_opts
.max
= ctx
->max
;
315 static int binderfs_show_options(struct seq_file
*seq
, struct dentry
*root
)
317 struct binderfs_info
*info
= BINDERFS_SB(root
->d_sb
);
319 if (info
->mount_opts
.max
<= BINDERFS_MAX_MINOR
)
320 seq_printf(seq
, ",max=%d", info
->mount_opts
.max
);
322 switch (info
->mount_opts
.stats_mode
) {
323 case binderfs_stats_mode_unset
:
325 case binderfs_stats_mode_global
:
326 seq_printf(seq
, ",stats=global");
333 static void binderfs_put_super(struct super_block
*sb
)
335 struct binderfs_info
*info
= sb
->s_fs_info
;
337 if (info
&& info
->ipc_ns
)
338 put_ipc_ns(info
->ipc_ns
);
341 sb
->s_fs_info
= NULL
;
344 static const struct super_operations binderfs_super_ops
= {
345 .evict_inode
= binderfs_evict_inode
,
346 .show_options
= binderfs_show_options
,
347 .statfs
= simple_statfs
,
348 .put_super
= binderfs_put_super
,
351 static inline bool is_binderfs_control_device(const struct dentry
*dentry
)
353 struct binderfs_info
*info
= dentry
->d_sb
->s_fs_info
;
355 return info
->control_dentry
== dentry
;
358 static int binderfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
359 struct inode
*new_dir
, struct dentry
*new_dentry
,
362 if (is_binderfs_control_device(old_dentry
) ||
363 is_binderfs_control_device(new_dentry
))
366 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
369 static int binderfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
371 if (is_binderfs_control_device(dentry
))
374 return simple_unlink(dir
, dentry
);
377 static const struct file_operations binder_ctl_fops
= {
378 .owner
= THIS_MODULE
,
379 .open
= nonseekable_open
,
380 .unlocked_ioctl
= binder_ctl_ioctl
,
381 .compat_ioctl
= binder_ctl_ioctl
,
382 .llseek
= noop_llseek
,
386 * binderfs_binder_ctl_create - create a new binder-control device
387 * @sb: super block of the binderfs mount
389 * This function creates a new binder-control device node in the binderfs mount
390 * referred to by @sb.
392 * Return: 0 on success, negative errno on failure
394 static int binderfs_binder_ctl_create(struct super_block
*sb
)
397 struct dentry
*dentry
;
398 struct binder_device
*device
;
399 struct inode
*inode
= NULL
;
400 struct dentry
*root
= sb
->s_root
;
401 struct binderfs_info
*info
= sb
->s_fs_info
;
402 #if defined(CONFIG_IPC_NS)
403 bool use_reserve
= (info
->ipc_ns
== &init_ipc_ns
);
405 bool use_reserve
= true;
408 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
412 /* If we have already created a binder-control node, return. */
413 if (info
->control_dentry
) {
419 inode
= new_inode(sb
);
423 /* Reserve a new minor number for the new device. */
424 mutex_lock(&binderfs_minors_mutex
);
425 minor
= ida_alloc_max(&binderfs_minors
,
426 use_reserve
? BINDERFS_MAX_MINOR
:
427 BINDERFS_MAX_MINOR_CAPPED
,
429 mutex_unlock(&binderfs_minors_mutex
);
435 inode
->i_ino
= SECOND_INODE
;
436 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
437 init_special_inode(inode
, S_IFCHR
| 0600,
438 MKDEV(MAJOR(binderfs_dev
), minor
));
439 inode
->i_fop
= &binder_ctl_fops
;
440 inode
->i_uid
= info
->root_uid
;
441 inode
->i_gid
= info
->root_gid
;
443 refcount_set(&device
->ref
, 1);
444 device
->binderfs_inode
= inode
;
445 device
->miscdev
.minor
= minor
;
447 dentry
= d_alloc_name(root
, "binder-control");
451 inode
->i_private
= device
;
452 info
->control_dentry
= dentry
;
453 d_add(dentry
, inode
);
464 static const struct inode_operations binderfs_dir_inode_operations
= {
465 .lookup
= simple_lookup
,
466 .rename
= binderfs_rename
,
467 .unlink
= binderfs_unlink
,
470 static struct inode
*binderfs_make_inode(struct super_block
*sb
, int mode
)
476 ret
->i_ino
= iunique(sb
, BINDERFS_MAX_MINOR
+ INODE_OFFSET
);
478 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= current_time(ret
);
483 static struct dentry
*binderfs_create_dentry(struct dentry
*parent
,
486 struct dentry
*dentry
;
488 dentry
= lookup_one_len(name
, parent
, strlen(name
));
492 /* Return error if the file/dir already exists. */
493 if (d_really_is_positive(dentry
)) {
495 return ERR_PTR(-EEXIST
);
501 void binderfs_remove_file(struct dentry
*dentry
)
503 struct inode
*parent_inode
;
505 parent_inode
= d_inode(dentry
->d_parent
);
506 inode_lock(parent_inode
);
507 if (simple_positive(dentry
)) {
509 simple_unlink(parent_inode
, dentry
);
513 inode_unlock(parent_inode
);
516 struct dentry
*binderfs_create_file(struct dentry
*parent
, const char *name
,
517 const struct file_operations
*fops
,
520 struct dentry
*dentry
;
521 struct inode
*new_inode
, *parent_inode
;
522 struct super_block
*sb
;
524 parent_inode
= d_inode(parent
);
525 inode_lock(parent_inode
);
527 dentry
= binderfs_create_dentry(parent
, name
);
531 sb
= parent_inode
->i_sb
;
532 new_inode
= binderfs_make_inode(sb
, S_IFREG
| 0444);
535 dentry
= ERR_PTR(-ENOMEM
);
539 new_inode
->i_fop
= fops
;
540 new_inode
->i_private
= data
;
541 d_instantiate(dentry
, new_inode
);
542 fsnotify_create(parent_inode
, dentry
);
545 inode_unlock(parent_inode
);
549 static struct dentry
*binderfs_create_dir(struct dentry
*parent
,
552 struct dentry
*dentry
;
553 struct inode
*new_inode
, *parent_inode
;
554 struct super_block
*sb
;
556 parent_inode
= d_inode(parent
);
557 inode_lock(parent_inode
);
559 dentry
= binderfs_create_dentry(parent
, name
);
563 sb
= parent_inode
->i_sb
;
564 new_inode
= binderfs_make_inode(sb
, S_IFDIR
| 0755);
567 dentry
= ERR_PTR(-ENOMEM
);
571 new_inode
->i_fop
= &simple_dir_operations
;
572 new_inode
->i_op
= &simple_dir_inode_operations
;
574 set_nlink(new_inode
, 2);
575 d_instantiate(dentry
, new_inode
);
576 inc_nlink(parent_inode
);
577 fsnotify_mkdir(parent_inode
, dentry
);
580 inode_unlock(parent_inode
);
584 static int init_binder_logs(struct super_block
*sb
)
586 struct dentry
*binder_logs_root_dir
, *dentry
, *proc_log_dir
;
587 struct binderfs_info
*info
;
590 binder_logs_root_dir
= binderfs_create_dir(sb
->s_root
,
592 if (IS_ERR(binder_logs_root_dir
)) {
593 ret
= PTR_ERR(binder_logs_root_dir
);
597 dentry
= binderfs_create_file(binder_logs_root_dir
, "stats",
598 &binder_stats_fops
, NULL
);
599 if (IS_ERR(dentry
)) {
600 ret
= PTR_ERR(dentry
);
604 dentry
= binderfs_create_file(binder_logs_root_dir
, "state",
605 &binder_state_fops
, NULL
);
606 if (IS_ERR(dentry
)) {
607 ret
= PTR_ERR(dentry
);
611 dentry
= binderfs_create_file(binder_logs_root_dir
, "transactions",
612 &binder_transactions_fops
, NULL
);
613 if (IS_ERR(dentry
)) {
614 ret
= PTR_ERR(dentry
);
618 dentry
= binderfs_create_file(binder_logs_root_dir
,
620 &binder_transaction_log_fops
,
621 &binder_transaction_log
);
622 if (IS_ERR(dentry
)) {
623 ret
= PTR_ERR(dentry
);
627 dentry
= binderfs_create_file(binder_logs_root_dir
,
628 "failed_transaction_log",
629 &binder_transaction_log_fops
,
630 &binder_transaction_log_failed
);
631 if (IS_ERR(dentry
)) {
632 ret
= PTR_ERR(dentry
);
636 proc_log_dir
= binderfs_create_dir(binder_logs_root_dir
, "proc");
637 if (IS_ERR(proc_log_dir
)) {
638 ret
= PTR_ERR(proc_log_dir
);
641 info
= sb
->s_fs_info
;
642 info
->proc_log_dir
= proc_log_dir
;
648 static int binderfs_fill_super(struct super_block
*sb
, struct fs_context
*fc
)
651 struct binderfs_info
*info
;
652 struct binderfs_mount_opts
*ctx
= fc
->fs_private
;
653 struct inode
*inode
= NULL
;
654 struct binderfs_device device_info
= {};
658 sb
->s_blocksize
= PAGE_SIZE
;
659 sb
->s_blocksize_bits
= PAGE_SHIFT
;
662 * The binderfs filesystem can be mounted by userns root in a
663 * non-initial userns. By default such mounts have the SB_I_NODEV flag
664 * set in s_iflags to prevent security issues where userns root can
665 * just create random device nodes via mknod() since it owns the
666 * filesystem mount. But binderfs does not allow to create any files
667 * including devices nodes. The only way to create binder devices nodes
668 * is through the binder-control device which userns root is explicitly
669 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
670 * necessary and safe.
672 sb
->s_iflags
&= ~SB_I_NODEV
;
673 sb
->s_iflags
|= SB_I_NOEXEC
;
674 sb
->s_magic
= BINDERFS_SUPER_MAGIC
;
675 sb
->s_op
= &binderfs_super_ops
;
678 sb
->s_fs_info
= kzalloc(sizeof(struct binderfs_info
), GFP_KERNEL
);
681 info
= sb
->s_fs_info
;
683 info
->ipc_ns
= get_ipc_ns(current
->nsproxy
->ipc_ns
);
685 info
->root_gid
= make_kgid(sb
->s_user_ns
, 0);
686 if (!gid_valid(info
->root_gid
))
687 info
->root_gid
= GLOBAL_ROOT_GID
;
688 info
->root_uid
= make_kuid(sb
->s_user_ns
, 0);
689 if (!uid_valid(info
->root_uid
))
690 info
->root_uid
= GLOBAL_ROOT_UID
;
691 info
->mount_opts
.max
= ctx
->max
;
692 info
->mount_opts
.stats_mode
= ctx
->stats_mode
;
694 inode
= new_inode(sb
);
698 inode
->i_ino
= FIRST_INODE
;
699 inode
->i_fop
= &simple_dir_operations
;
700 inode
->i_mode
= S_IFDIR
| 0755;
701 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
702 inode
->i_op
= &binderfs_dir_inode_operations
;
705 sb
->s_root
= d_make_root(inode
);
709 ret
= binderfs_binder_ctl_create(sb
);
713 name
= binder_devices_param
;
714 for (len
= strcspn(name
, ","); len
> 0; len
= strcspn(name
, ",")) {
715 strscpy(device_info
.name
, name
, len
+ 1);
716 ret
= binderfs_binder_device_create(inode
, NULL
, &device_info
);
724 if (info
->mount_opts
.stats_mode
== binderfs_stats_mode_global
)
725 return init_binder_logs(sb
);
730 static int binderfs_fs_context_get_tree(struct fs_context
*fc
)
732 return get_tree_nodev(fc
, binderfs_fill_super
);
735 static void binderfs_fs_context_free(struct fs_context
*fc
)
737 struct binderfs_mount_opts
*ctx
= fc
->fs_private
;
742 static const struct fs_context_operations binderfs_fs_context_ops
= {
743 .free
= binderfs_fs_context_free
,
744 .get_tree
= binderfs_fs_context_get_tree
,
745 .parse_param
= binderfs_fs_context_parse_param
,
746 .reconfigure
= binderfs_fs_context_reconfigure
,
749 static int binderfs_init_fs_context(struct fs_context
*fc
)
751 struct binderfs_mount_opts
*ctx
;
753 ctx
= kzalloc(sizeof(struct binderfs_mount_opts
), GFP_KERNEL
);
757 ctx
->max
= BINDERFS_MAX_MINOR
;
758 ctx
->stats_mode
= binderfs_stats_mode_unset
;
760 fc
->fs_private
= ctx
;
761 fc
->ops
= &binderfs_fs_context_ops
;
766 static struct file_system_type binder_fs_type
= {
768 .init_fs_context
= binderfs_init_fs_context
,
769 .parameters
= binderfs_fs_parameters
,
770 .kill_sb
= kill_litter_super
,
771 .fs_flags
= FS_USERNS_MOUNT
,
774 int __init
init_binderfs(void)
780 /* Verify that the default binderfs device names are valid. */
781 name
= binder_devices_param
;
782 for (len
= strcspn(name
, ","); len
> 0; len
= strcspn(name
, ",")) {
783 if (len
> BINDERFS_MAX_NAME
)
790 /* Allocate new major number for binderfs. */
791 ret
= alloc_chrdev_region(&binderfs_dev
, 0, BINDERFS_MAX_MINOR
,
796 ret
= register_filesystem(&binder_fs_type
);
798 unregister_chrdev_region(binderfs_dev
, BINDERFS_MAX_MINOR
);