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/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock_types.h>
26 #include <linux/stddef.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 #include <linux/uaccess.h>
30 #include <linux/user_namespace.h>
31 #include <linux/xarray.h>
32 #include <uapi/linux/android/binder.h>
33 #include <uapi/linux/android/binderfs.h>
35 #include "binder_internal.h"
38 #define SECOND_INODE 2
39 #define INODE_OFFSET 3
40 #define BINDERFS_MAX_MINOR (1U << MINORBITS)
41 /* Ensure that the initial ipc namespace always has devices available. */
42 #define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)
44 static dev_t binderfs_dev
;
45 static DEFINE_MUTEX(binderfs_minors_mutex
);
46 static DEFINE_IDA(binderfs_minors
);
53 enum binderfs_stats_mode
{
54 binderfs_stats_mode_unset
,
55 binderfs_stats_mode_global
,
58 struct binder_features
{
59 bool oneway_spam_detection
;
61 bool freeze_notification
;
64 static const struct constant_table binderfs_param_stats
[] = {
65 { "global", binderfs_stats_mode_global
},
69 static const struct fs_parameter_spec binderfs_fs_parameters
[] = {
70 fsparam_u32("max", Opt_max
),
71 fsparam_enum("stats", Opt_stats_mode
, binderfs_param_stats
),
75 static struct binder_features binder_features
= {
76 .oneway_spam_detection
= true,
77 .extended_error
= true,
78 .freeze_notification
= true,
81 static inline struct binderfs_info
*BINDERFS_SB(const struct super_block
*sb
)
86 bool is_binderfs_device(const struct inode
*inode
)
88 if (inode
->i_sb
->s_magic
== BINDERFS_SUPER_MAGIC
)
95 * binderfs_binder_device_create - allocate inode from super block of a
97 * @ref_inode: inode from which the super block will be taken
98 * @userp: buffer to copy information about new device for userspace to
99 * @req: struct binderfs_device as copied from userspace
101 * This function allocates a new binder_device and reserves a new minor
103 * Minor numbers are limited and tracked globally in binderfs_minors. The
104 * function will stash a struct binder_device for the specific binder
105 * device in i_private of the inode.
106 * It will go on to allocate a new inode from the super block of the
107 * filesystem mount, stash a struct binder_device in its i_private field
108 * and attach a dentry to that inode.
110 * Return: 0 on success, negative errno on failure
112 static int binderfs_binder_device_create(struct inode
*ref_inode
,
113 struct binderfs_device __user
*userp
,
114 struct binderfs_device
*req
)
117 struct dentry
*dentry
, *root
;
118 struct binder_device
*device
;
121 struct inode
*inode
= NULL
;
122 struct super_block
*sb
= ref_inode
->i_sb
;
123 struct binderfs_info
*info
= sb
->s_fs_info
;
124 #if defined(CONFIG_IPC_NS)
125 bool use_reserve
= (info
->ipc_ns
== &init_ipc_ns
);
127 bool use_reserve
= true;
130 /* Reserve new minor number for the new device. */
131 mutex_lock(&binderfs_minors_mutex
);
132 if (++info
->device_count
<= info
->mount_opts
.max
)
133 minor
= ida_alloc_max(&binderfs_minors
,
134 use_reserve
? BINDERFS_MAX_MINOR
:
135 BINDERFS_MAX_MINOR_CAPPED
,
140 --info
->device_count
;
141 mutex_unlock(&binderfs_minors_mutex
);
144 mutex_unlock(&binderfs_minors_mutex
);
147 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
151 inode
= new_inode(sb
);
155 inode
->i_ino
= minor
+ INODE_OFFSET
;
156 simple_inode_init_ts(inode
);
157 init_special_inode(inode
, S_IFCHR
| 0600,
158 MKDEV(MAJOR(binderfs_dev
), minor
));
159 inode
->i_fop
= &binder_fops
;
160 inode
->i_uid
= info
->root_uid
;
161 inode
->i_gid
= info
->root_gid
;
163 req
->name
[BINDERFS_MAX_NAME
] = '\0'; /* NUL-terminate */
164 name_len
= strlen(req
->name
);
165 /* Make sure to include terminating NUL byte */
166 name
= kmemdup(req
->name
, name_len
+ 1, GFP_KERNEL
);
170 refcount_set(&device
->ref
, 1);
171 device
->binderfs_inode
= inode
;
172 device
->context
.binder_context_mgr_uid
= INVALID_UID
;
173 device
->context
.name
= name
;
174 device
->miscdev
.name
= name
;
175 device
->miscdev
.minor
= minor
;
176 mutex_init(&device
->context
.context_mgr_node_lock
);
178 req
->major
= MAJOR(binderfs_dev
);
181 if (userp
&& copy_to_user(userp
, req
, sizeof(*req
))) {
187 inode_lock(d_inode(root
));
190 dentry
= lookup_one_len(name
, root
, name_len
);
191 if (IS_ERR(dentry
)) {
192 inode_unlock(d_inode(root
));
193 ret
= PTR_ERR(dentry
);
197 if (d_really_is_positive(dentry
)) {
200 inode_unlock(d_inode(root
));
205 inode
->i_private
= device
;
206 d_instantiate(dentry
, inode
);
207 fsnotify_create(root
->d_inode
, dentry
);
208 inode_unlock(d_inode(root
));
215 mutex_lock(&binderfs_minors_mutex
);
216 --info
->device_count
;
217 ida_free(&binderfs_minors
, minor
);
218 mutex_unlock(&binderfs_minors_mutex
);
225 * binder_ctl_ioctl - handle binder device node allocation requests
227 * The request handler for the binder-control device. All requests operate on
228 * the binderfs mount the binder-control device resides in:
230 * Allocate a new binder device.
232 * Return: %0 on success, negative errno on failure.
234 static long binder_ctl_ioctl(struct file
*file
, unsigned int cmd
,
238 struct inode
*inode
= file_inode(file
);
239 struct binderfs_device __user
*device
= (struct binderfs_device __user
*)arg
;
240 struct binderfs_device device_req
;
244 ret
= copy_from_user(&device_req
, device
, sizeof(device_req
));
250 ret
= binderfs_binder_device_create(inode
, device
, &device_req
);
259 static void binderfs_evict_inode(struct inode
*inode
)
261 struct binder_device
*device
= inode
->i_private
;
262 struct binderfs_info
*info
= BINDERFS_SB(inode
->i_sb
);
266 if (!S_ISCHR(inode
->i_mode
) || !device
)
269 mutex_lock(&binderfs_minors_mutex
);
270 --info
->device_count
;
271 ida_free(&binderfs_minors
, device
->miscdev
.minor
);
272 mutex_unlock(&binderfs_minors_mutex
);
274 if (refcount_dec_and_test(&device
->ref
)) {
275 kfree(device
->context
.name
);
280 static int binderfs_fs_context_parse_param(struct fs_context
*fc
,
281 struct fs_parameter
*param
)
284 struct binderfs_mount_opts
*ctx
= fc
->fs_private
;
285 struct fs_parse_result result
;
287 opt
= fs_parse(fc
, binderfs_fs_parameters
, param
, &result
);
293 if (result
.uint_32
> BINDERFS_MAX_MINOR
)
294 return invalfc(fc
, "Bad value for '%s'", param
->key
);
296 ctx
->max
= result
.uint_32
;
299 if (!capable(CAP_SYS_ADMIN
))
302 ctx
->stats_mode
= result
.uint_32
;
305 return invalfc(fc
, "Unsupported parameter '%s'", param
->key
);
311 static int binderfs_fs_context_reconfigure(struct fs_context
*fc
)
313 struct binderfs_mount_opts
*ctx
= fc
->fs_private
;
314 struct binderfs_info
*info
= BINDERFS_SB(fc
->root
->d_sb
);
316 if (info
->mount_opts
.stats_mode
!= ctx
->stats_mode
)
317 return invalfc(fc
, "Binderfs stats mode cannot be changed during a remount");
319 info
->mount_opts
.stats_mode
= ctx
->stats_mode
;
320 info
->mount_opts
.max
= ctx
->max
;
324 static int binderfs_show_options(struct seq_file
*seq
, struct dentry
*root
)
326 struct binderfs_info
*info
= BINDERFS_SB(root
->d_sb
);
328 if (info
->mount_opts
.max
<= BINDERFS_MAX_MINOR
)
329 seq_printf(seq
, ",max=%d", info
->mount_opts
.max
);
331 switch (info
->mount_opts
.stats_mode
) {
332 case binderfs_stats_mode_unset
:
334 case binderfs_stats_mode_global
:
335 seq_printf(seq
, ",stats=global");
342 static const struct super_operations binderfs_super_ops
= {
343 .evict_inode
= binderfs_evict_inode
,
344 .show_options
= binderfs_show_options
,
345 .statfs
= simple_statfs
,
348 static inline bool is_binderfs_control_device(const struct dentry
*dentry
)
350 struct binderfs_info
*info
= dentry
->d_sb
->s_fs_info
;
352 return info
->control_dentry
== dentry
;
355 static int binderfs_rename(struct mnt_idmap
*idmap
,
356 struct inode
*old_dir
, struct dentry
*old_dentry
,
357 struct inode
*new_dir
, struct dentry
*new_dentry
,
360 if (is_binderfs_control_device(old_dentry
) ||
361 is_binderfs_control_device(new_dentry
))
364 return simple_rename(idmap
, old_dir
, old_dentry
, new_dir
,
368 static int binderfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
370 if (is_binderfs_control_device(dentry
))
373 return simple_unlink(dir
, dentry
);
376 static const struct file_operations binder_ctl_fops
= {
377 .owner
= THIS_MODULE
,
378 .open
= nonseekable_open
,
379 .unlocked_ioctl
= binder_ctl_ioctl
,
380 .compat_ioctl
= binder_ctl_ioctl
,
381 .llseek
= noop_llseek
,
385 * binderfs_binder_ctl_create - create a new binder-control device
386 * @sb: super block of the binderfs mount
388 * This function creates a new binder-control device node in the binderfs mount
389 * referred to by @sb.
391 * Return: 0 on success, negative errno on failure
393 static int binderfs_binder_ctl_create(struct super_block
*sb
)
396 struct dentry
*dentry
;
397 struct binder_device
*device
;
398 struct inode
*inode
= NULL
;
399 struct dentry
*root
= sb
->s_root
;
400 struct binderfs_info
*info
= sb
->s_fs_info
;
401 #if defined(CONFIG_IPC_NS)
402 bool use_reserve
= (info
->ipc_ns
== &init_ipc_ns
);
404 bool use_reserve
= true;
407 device
= kzalloc(sizeof(*device
), GFP_KERNEL
);
411 /* If we have already created a binder-control node, return. */
412 if (info
->control_dentry
) {
418 inode
= new_inode(sb
);
422 /* Reserve a new minor number for the new device. */
423 mutex_lock(&binderfs_minors_mutex
);
424 minor
= ida_alloc_max(&binderfs_minors
,
425 use_reserve
? BINDERFS_MAX_MINOR
:
426 BINDERFS_MAX_MINOR_CAPPED
,
428 mutex_unlock(&binderfs_minors_mutex
);
434 inode
->i_ino
= SECOND_INODE
;
435 simple_inode_init_ts(inode
);
436 init_special_inode(inode
, S_IFCHR
| 0600,
437 MKDEV(MAJOR(binderfs_dev
), minor
));
438 inode
->i_fop
= &binder_ctl_fops
;
439 inode
->i_uid
= info
->root_uid
;
440 inode
->i_gid
= info
->root_gid
;
442 refcount_set(&device
->ref
, 1);
443 device
->binderfs_inode
= inode
;
444 device
->miscdev
.minor
= minor
;
446 dentry
= d_alloc_name(root
, "binder-control");
450 inode
->i_private
= device
;
451 info
->control_dentry
= dentry
;
452 d_add(dentry
, inode
);
463 static const struct inode_operations binderfs_dir_inode_operations
= {
464 .lookup
= simple_lookup
,
465 .rename
= binderfs_rename
,
466 .unlink
= binderfs_unlink
,
469 static struct inode
*binderfs_make_inode(struct super_block
*sb
, int mode
)
475 ret
->i_ino
= iunique(sb
, BINDERFS_MAX_MINOR
+ INODE_OFFSET
);
477 simple_inode_init_ts(ret
);
482 static struct dentry
*binderfs_create_dentry(struct dentry
*parent
,
485 struct dentry
*dentry
;
487 dentry
= lookup_one_len(name
, parent
, strlen(name
));
491 /* Return error if the file/dir already exists. */
492 if (d_really_is_positive(dentry
)) {
494 return ERR_PTR(-EEXIST
);
500 void binderfs_remove_file(struct dentry
*dentry
)
502 struct inode
*parent_inode
;
504 parent_inode
= d_inode(dentry
->d_parent
);
505 inode_lock(parent_inode
);
506 if (simple_positive(dentry
)) {
508 simple_unlink(parent_inode
, dentry
);
512 inode_unlock(parent_inode
);
515 struct dentry
*binderfs_create_file(struct dentry
*parent
, const char *name
,
516 const struct file_operations
*fops
,
519 struct dentry
*dentry
;
520 struct inode
*new_inode
, *parent_inode
;
521 struct super_block
*sb
;
523 parent_inode
= d_inode(parent
);
524 inode_lock(parent_inode
);
526 dentry
= binderfs_create_dentry(parent
, name
);
530 sb
= parent_inode
->i_sb
;
531 new_inode
= binderfs_make_inode(sb
, S_IFREG
| 0444);
534 dentry
= ERR_PTR(-ENOMEM
);
538 new_inode
->i_fop
= fops
;
539 new_inode
->i_private
= data
;
540 d_instantiate(dentry
, new_inode
);
541 fsnotify_create(parent_inode
, dentry
);
544 inode_unlock(parent_inode
);
548 static struct dentry
*binderfs_create_dir(struct dentry
*parent
,
551 struct dentry
*dentry
;
552 struct inode
*new_inode
, *parent_inode
;
553 struct super_block
*sb
;
555 parent_inode
= d_inode(parent
);
556 inode_lock(parent_inode
);
558 dentry
= binderfs_create_dentry(parent
, name
);
562 sb
= parent_inode
->i_sb
;
563 new_inode
= binderfs_make_inode(sb
, S_IFDIR
| 0755);
566 dentry
= ERR_PTR(-ENOMEM
);
570 new_inode
->i_fop
= &simple_dir_operations
;
571 new_inode
->i_op
= &simple_dir_inode_operations
;
573 set_nlink(new_inode
, 2);
574 d_instantiate(dentry
, new_inode
);
575 inc_nlink(parent_inode
);
576 fsnotify_mkdir(parent_inode
, dentry
);
579 inode_unlock(parent_inode
);
583 static int binder_features_show(struct seq_file
*m
, void *unused
)
585 bool *feature
= m
->private;
587 seq_printf(m
, "%d\n", *feature
);
591 DEFINE_SHOW_ATTRIBUTE(binder_features
);
593 static int init_binder_features(struct super_block
*sb
)
595 struct dentry
*dentry
, *dir
;
597 dir
= binderfs_create_dir(sb
->s_root
, "features");
601 dentry
= binderfs_create_file(dir
, "oneway_spam_detection",
602 &binder_features_fops
,
603 &binder_features
.oneway_spam_detection
);
605 return PTR_ERR(dentry
);
607 dentry
= binderfs_create_file(dir
, "extended_error",
608 &binder_features_fops
,
609 &binder_features
.extended_error
);
611 return PTR_ERR(dentry
);
613 dentry
= binderfs_create_file(dir
, "freeze_notification",
614 &binder_features_fops
,
615 &binder_features
.freeze_notification
);
617 return PTR_ERR(dentry
);
622 static int init_binder_logs(struct super_block
*sb
)
624 struct dentry
*binder_logs_root_dir
, *dentry
, *proc_log_dir
;
625 const struct binder_debugfs_entry
*db_entry
;
626 struct binderfs_info
*info
;
629 binder_logs_root_dir
= binderfs_create_dir(sb
->s_root
,
631 if (IS_ERR(binder_logs_root_dir
)) {
632 ret
= PTR_ERR(binder_logs_root_dir
);
636 binder_for_each_debugfs_entry(db_entry
) {
637 dentry
= binderfs_create_file(binder_logs_root_dir
,
641 if (IS_ERR(dentry
)) {
642 ret
= PTR_ERR(dentry
);
647 proc_log_dir
= binderfs_create_dir(binder_logs_root_dir
, "proc");
648 if (IS_ERR(proc_log_dir
)) {
649 ret
= PTR_ERR(proc_log_dir
);
652 info
= sb
->s_fs_info
;
653 info
->proc_log_dir
= proc_log_dir
;
659 static int binderfs_fill_super(struct super_block
*sb
, struct fs_context
*fc
)
662 struct binderfs_info
*info
;
663 struct binderfs_mount_opts
*ctx
= fc
->fs_private
;
664 struct inode
*inode
= NULL
;
665 struct binderfs_device device_info
= {};
669 sb
->s_blocksize
= PAGE_SIZE
;
670 sb
->s_blocksize_bits
= PAGE_SHIFT
;
673 * The binderfs filesystem can be mounted by userns root in a
674 * non-initial userns. By default such mounts have the SB_I_NODEV flag
675 * set in s_iflags to prevent security issues where userns root can
676 * just create random device nodes via mknod() since it owns the
677 * filesystem mount. But binderfs does not allow to create any files
678 * including devices nodes. The only way to create binder devices nodes
679 * is through the binder-control device which userns root is explicitly
680 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
681 * necessary and safe.
683 sb
->s_iflags
&= ~SB_I_NODEV
;
684 sb
->s_iflags
|= SB_I_NOEXEC
;
685 sb
->s_magic
= BINDERFS_SUPER_MAGIC
;
686 sb
->s_op
= &binderfs_super_ops
;
689 sb
->s_fs_info
= kzalloc(sizeof(struct binderfs_info
), GFP_KERNEL
);
692 info
= sb
->s_fs_info
;
694 info
->ipc_ns
= get_ipc_ns(current
->nsproxy
->ipc_ns
);
696 info
->root_gid
= make_kgid(sb
->s_user_ns
, 0);
697 if (!gid_valid(info
->root_gid
))
698 info
->root_gid
= GLOBAL_ROOT_GID
;
699 info
->root_uid
= make_kuid(sb
->s_user_ns
, 0);
700 if (!uid_valid(info
->root_uid
))
701 info
->root_uid
= GLOBAL_ROOT_UID
;
702 info
->mount_opts
.max
= ctx
->max
;
703 info
->mount_opts
.stats_mode
= ctx
->stats_mode
;
705 inode
= new_inode(sb
);
709 inode
->i_ino
= FIRST_INODE
;
710 inode
->i_fop
= &simple_dir_operations
;
711 inode
->i_mode
= S_IFDIR
| 0755;
712 simple_inode_init_ts(inode
);
713 inode
->i_op
= &binderfs_dir_inode_operations
;
716 sb
->s_root
= d_make_root(inode
);
720 ret
= binderfs_binder_ctl_create(sb
);
724 name
= binder_devices_param
;
725 for (len
= strcspn(name
, ","); len
> 0; len
= strcspn(name
, ",")) {
726 strscpy(device_info
.name
, name
, len
+ 1);
727 ret
= binderfs_binder_device_create(inode
, NULL
, &device_info
);
735 ret
= init_binder_features(sb
);
739 if (info
->mount_opts
.stats_mode
== binderfs_stats_mode_global
)
740 return init_binder_logs(sb
);
745 static int binderfs_fs_context_get_tree(struct fs_context
*fc
)
747 return get_tree_nodev(fc
, binderfs_fill_super
);
750 static void binderfs_fs_context_free(struct fs_context
*fc
)
752 struct binderfs_mount_opts
*ctx
= fc
->fs_private
;
757 static const struct fs_context_operations binderfs_fs_context_ops
= {
758 .free
= binderfs_fs_context_free
,
759 .get_tree
= binderfs_fs_context_get_tree
,
760 .parse_param
= binderfs_fs_context_parse_param
,
761 .reconfigure
= binderfs_fs_context_reconfigure
,
764 static int binderfs_init_fs_context(struct fs_context
*fc
)
766 struct binderfs_mount_opts
*ctx
;
768 ctx
= kzalloc(sizeof(struct binderfs_mount_opts
), GFP_KERNEL
);
772 ctx
->max
= BINDERFS_MAX_MINOR
;
773 ctx
->stats_mode
= binderfs_stats_mode_unset
;
775 fc
->fs_private
= ctx
;
776 fc
->ops
= &binderfs_fs_context_ops
;
781 static void binderfs_kill_super(struct super_block
*sb
)
783 struct binderfs_info
*info
= sb
->s_fs_info
;
786 * During inode eviction struct binderfs_info is needed.
787 * So first wipe the super_block then free struct binderfs_info.
789 kill_litter_super(sb
);
791 if (info
&& info
->ipc_ns
)
792 put_ipc_ns(info
->ipc_ns
);
797 static struct file_system_type binder_fs_type
= {
799 .init_fs_context
= binderfs_init_fs_context
,
800 .parameters
= binderfs_fs_parameters
,
801 .kill_sb
= binderfs_kill_super
,
802 .fs_flags
= FS_USERNS_MOUNT
,
805 int __init
init_binderfs(void)
811 /* Verify that the default binderfs device names are valid. */
812 name
= binder_devices_param
;
813 for (len
= strcspn(name
, ","); len
> 0; len
= strcspn(name
, ",")) {
814 if (len
> BINDERFS_MAX_NAME
)
821 /* Allocate new major number for binderfs. */
822 ret
= alloc_chrdev_region(&binderfs_dev
, 0, BINDERFS_MAX_MINOR
,
827 ret
= register_filesystem(&binder_fs_type
);
829 unregister_chrdev_region(binderfs_dev
, BINDERFS_MAX_MINOR
);