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;
63 static const struct constant_table binderfs_param_stats[] = {
64 { "global", binderfs_stats_mode_global },
68 static const struct fs_parameter_spec binderfs_fs_parameters[] = {
69 fsparam_u32("max", Opt_max),
70 fsparam_enum("stats", Opt_stats_mode, binderfs_param_stats),
74 static struct binder_features binder_features = {
75 .oneway_spam_detection = true,
76 .extended_error = true,
79 static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)
84 bool is_binderfs_device(const struct inode *inode)
86 if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
93 * binderfs_binder_device_create - allocate inode from super block of a
95 * @ref_inode: inode from which the super block will be taken
96 * @userp: buffer to copy information about new device for userspace to
97 * @req: struct binderfs_device as copied from userspace
99 * This function allocates a new binder_device and reserves a new minor
101 * Minor numbers are limited and tracked globally in binderfs_minors. The
102 * function will stash a struct binder_device for the specific binder
103 * device in i_private of the inode.
104 * It will go on to allocate a new inode from the super block of the
105 * filesystem mount, stash a struct binder_device in its i_private field
106 * and attach a dentry to that inode.
108 * Return: 0 on success, negative errno on failure
110 static int binderfs_binder_device_create(struct inode *ref_inode,
111 struct binderfs_device __user *userp,
112 struct binderfs_device *req)
115 struct dentry *dentry, *root;
116 struct binder_device *device;
119 struct inode *inode = NULL;
120 struct super_block *sb = ref_inode->i_sb;
121 struct binderfs_info *info = sb->s_fs_info;
122 #if defined(CONFIG_IPC_NS)
123 bool use_reserve = (info->ipc_ns == &init_ipc_ns);
125 bool use_reserve = true;
128 /* Reserve new minor number for the new device. */
129 mutex_lock(&binderfs_minors_mutex);
130 if (++info->device_count <= info->mount_opts.max)
131 minor = ida_alloc_max(&binderfs_minors,
132 use_reserve ? BINDERFS_MAX_MINOR :
133 BINDERFS_MAX_MINOR_CAPPED,
138 --info->device_count;
139 mutex_unlock(&binderfs_minors_mutex);
142 mutex_unlock(&binderfs_minors_mutex);
145 device = kzalloc(sizeof(*device), GFP_KERNEL);
149 inode = new_inode(sb);
153 inode->i_ino = minor + INODE_OFFSET;
154 simple_inode_init_ts(inode);
155 init_special_inode(inode, S_IFCHR | 0600,
156 MKDEV(MAJOR(binderfs_dev), minor));
157 inode->i_fop = &binder_fops;
158 inode->i_uid = info->root_uid;
159 inode->i_gid = info->root_gid;
161 req->name[BINDERFS_MAX_NAME] = '\0'; /* NUL-terminate */
162 name_len = strlen(req->name);
163 /* Make sure to include terminating NUL byte */
164 name = kmemdup(req->name, name_len + 1, GFP_KERNEL);
168 refcount_set(&device->ref, 1);
169 device->binderfs_inode = inode;
170 device->context.binder_context_mgr_uid = INVALID_UID;
171 device->context.name = name;
172 device->miscdev.name = name;
173 device->miscdev.minor = minor;
174 mutex_init(&device->context.context_mgr_node_lock);
176 req->major = MAJOR(binderfs_dev);
179 if (userp && copy_to_user(userp, req, sizeof(*req))) {
185 inode_lock(d_inode(root));
188 dentry = lookup_one_len(name, root, name_len);
189 if (IS_ERR(dentry)) {
190 inode_unlock(d_inode(root));
191 ret = PTR_ERR(dentry);
195 if (d_really_is_positive(dentry)) {
198 inode_unlock(d_inode(root));
203 inode->i_private = device;
204 d_instantiate(dentry, inode);
205 fsnotify_create(root->d_inode, dentry);
206 inode_unlock(d_inode(root));
213 mutex_lock(&binderfs_minors_mutex);
214 --info->device_count;
215 ida_free(&binderfs_minors, minor);
216 mutex_unlock(&binderfs_minors_mutex);
223 * binder_ctl_ioctl - handle binder device node allocation requests
225 * The request handler for the binder-control device. All requests operate on
226 * the binderfs mount the binder-control device resides in:
228 * Allocate a new binder device.
230 * Return: %0 on success, negative errno on failure.
232 static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
236 struct inode *inode = file_inode(file);
237 struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
238 struct binderfs_device device_req;
242 ret = copy_from_user(&device_req, device, sizeof(device_req));
248 ret = binderfs_binder_device_create(inode, device, &device_req);
257 static void binderfs_evict_inode(struct inode *inode)
259 struct binder_device *device = inode->i_private;
260 struct binderfs_info *info = BINDERFS_SB(inode->i_sb);
264 if (!S_ISCHR(inode->i_mode) || !device)
267 mutex_lock(&binderfs_minors_mutex);
268 --info->device_count;
269 ida_free(&binderfs_minors, device->miscdev.minor);
270 mutex_unlock(&binderfs_minors_mutex);
272 if (refcount_dec_and_test(&device->ref)) {
273 kfree(device->context.name);
278 static int binderfs_fs_context_parse_param(struct fs_context *fc,
279 struct fs_parameter *param)
282 struct binderfs_mount_opts *ctx = fc->fs_private;
283 struct fs_parse_result result;
285 opt = fs_parse(fc, binderfs_fs_parameters, param, &result);
291 if (result.uint_32 > BINDERFS_MAX_MINOR)
292 return invalfc(fc, "Bad value for '%s'", param->key);
294 ctx->max = result.uint_32;
297 if (!capable(CAP_SYS_ADMIN))
300 ctx->stats_mode = result.uint_32;
303 return invalfc(fc, "Unsupported parameter '%s'", param->key);
309 static int binderfs_fs_context_reconfigure(struct fs_context *fc)
311 struct binderfs_mount_opts *ctx = fc->fs_private;
312 struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);
314 if (info->mount_opts.stats_mode != ctx->stats_mode)
315 return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");
317 info->mount_opts.stats_mode = ctx->stats_mode;
318 info->mount_opts.max = ctx->max;
322 static int binderfs_show_options(struct seq_file *seq, struct dentry *root)
324 struct binderfs_info *info = BINDERFS_SB(root->d_sb);
326 if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
327 seq_printf(seq, ",max=%d", info->mount_opts.max);
329 switch (info->mount_opts.stats_mode) {
330 case binderfs_stats_mode_unset:
332 case binderfs_stats_mode_global:
333 seq_printf(seq, ",stats=global");
340 static const struct super_operations binderfs_super_ops = {
341 .evict_inode = binderfs_evict_inode,
342 .show_options = binderfs_show_options,
343 .statfs = simple_statfs,
346 static inline bool is_binderfs_control_device(const struct dentry *dentry)
348 struct binderfs_info *info = dentry->d_sb->s_fs_info;
350 return info->control_dentry == dentry;
353 static int binderfs_rename(struct mnt_idmap *idmap,
354 struct inode *old_dir, struct dentry *old_dentry,
355 struct inode *new_dir, struct dentry *new_dentry,
358 if (is_binderfs_control_device(old_dentry) ||
359 is_binderfs_control_device(new_dentry))
362 return simple_rename(idmap, old_dir, old_dentry, new_dir,
366 static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
368 if (is_binderfs_control_device(dentry))
371 return simple_unlink(dir, dentry);
374 static const struct file_operations binder_ctl_fops = {
375 .owner = THIS_MODULE,
376 .open = nonseekable_open,
377 .unlocked_ioctl = binder_ctl_ioctl,
378 .compat_ioctl = binder_ctl_ioctl,
379 .llseek = noop_llseek,
383 * binderfs_binder_ctl_create - create a new binder-control device
384 * @sb: super block of the binderfs mount
386 * This function creates a new binder-control device node in the binderfs mount
387 * referred to by @sb.
389 * Return: 0 on success, negative errno on failure
391 static int binderfs_binder_ctl_create(struct super_block *sb)
394 struct dentry *dentry;
395 struct binder_device *device;
396 struct inode *inode = NULL;
397 struct dentry *root = sb->s_root;
398 struct binderfs_info *info = sb->s_fs_info;
399 #if defined(CONFIG_IPC_NS)
400 bool use_reserve = (info->ipc_ns == &init_ipc_ns);
402 bool use_reserve = true;
405 device = kzalloc(sizeof(*device), GFP_KERNEL);
409 /* If we have already created a binder-control node, return. */
410 if (info->control_dentry) {
416 inode = new_inode(sb);
420 /* Reserve a new minor number for the new device. */
421 mutex_lock(&binderfs_minors_mutex);
422 minor = ida_alloc_max(&binderfs_minors,
423 use_reserve ? BINDERFS_MAX_MINOR :
424 BINDERFS_MAX_MINOR_CAPPED,
426 mutex_unlock(&binderfs_minors_mutex);
432 inode->i_ino = SECOND_INODE;
433 simple_inode_init_ts(inode);
434 init_special_inode(inode, S_IFCHR | 0600,
435 MKDEV(MAJOR(binderfs_dev), minor));
436 inode->i_fop = &binder_ctl_fops;
437 inode->i_uid = info->root_uid;
438 inode->i_gid = info->root_gid;
440 refcount_set(&device->ref, 1);
441 device->binderfs_inode = inode;
442 device->miscdev.minor = minor;
444 dentry = d_alloc_name(root, "binder-control");
448 inode->i_private = device;
449 info->control_dentry = dentry;
450 d_add(dentry, inode);
461 static const struct inode_operations binderfs_dir_inode_operations = {
462 .lookup = simple_lookup,
463 .rename = binderfs_rename,
464 .unlink = binderfs_unlink,
467 static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
473 ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
475 simple_inode_init_ts(ret);
480 static struct dentry *binderfs_create_dentry(struct dentry *parent,
483 struct dentry *dentry;
485 dentry = lookup_one_len(name, parent, strlen(name));
489 /* Return error if the file/dir already exists. */
490 if (d_really_is_positive(dentry)) {
492 return ERR_PTR(-EEXIST);
498 void binderfs_remove_file(struct dentry *dentry)
500 struct inode *parent_inode;
502 parent_inode = d_inode(dentry->d_parent);
503 inode_lock(parent_inode);
504 if (simple_positive(dentry)) {
506 simple_unlink(parent_inode, dentry);
510 inode_unlock(parent_inode);
513 struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
514 const struct file_operations *fops,
517 struct dentry *dentry;
518 struct inode *new_inode, *parent_inode;
519 struct super_block *sb;
521 parent_inode = d_inode(parent);
522 inode_lock(parent_inode);
524 dentry = binderfs_create_dentry(parent, name);
528 sb = parent_inode->i_sb;
529 new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
532 dentry = ERR_PTR(-ENOMEM);
536 new_inode->i_fop = fops;
537 new_inode->i_private = data;
538 d_instantiate(dentry, new_inode);
539 fsnotify_create(parent_inode, dentry);
542 inode_unlock(parent_inode);
546 static struct dentry *binderfs_create_dir(struct dentry *parent,
549 struct dentry *dentry;
550 struct inode *new_inode, *parent_inode;
551 struct super_block *sb;
553 parent_inode = d_inode(parent);
554 inode_lock(parent_inode);
556 dentry = binderfs_create_dentry(parent, name);
560 sb = parent_inode->i_sb;
561 new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
564 dentry = ERR_PTR(-ENOMEM);
568 new_inode->i_fop = &simple_dir_operations;
569 new_inode->i_op = &simple_dir_inode_operations;
571 set_nlink(new_inode, 2);
572 d_instantiate(dentry, new_inode);
573 inc_nlink(parent_inode);
574 fsnotify_mkdir(parent_inode, dentry);
577 inode_unlock(parent_inode);
581 static int binder_features_show(struct seq_file *m, void *unused)
583 bool *feature = m->private;
585 seq_printf(m, "%d\n", *feature);
589 DEFINE_SHOW_ATTRIBUTE(binder_features);
591 static int init_binder_features(struct super_block *sb)
593 struct dentry *dentry, *dir;
595 dir = binderfs_create_dir(sb->s_root, "features");
599 dentry = binderfs_create_file(dir, "oneway_spam_detection",
600 &binder_features_fops,
601 &binder_features.oneway_spam_detection);
603 return PTR_ERR(dentry);
605 dentry = binderfs_create_file(dir, "extended_error",
606 &binder_features_fops,
607 &binder_features.extended_error);
609 return PTR_ERR(dentry);
614 static int init_binder_logs(struct super_block *sb)
616 struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
617 const struct binder_debugfs_entry *db_entry;
618 struct binderfs_info *info;
621 binder_logs_root_dir = binderfs_create_dir(sb->s_root,
623 if (IS_ERR(binder_logs_root_dir)) {
624 ret = PTR_ERR(binder_logs_root_dir);
628 binder_for_each_debugfs_entry(db_entry) {
629 dentry = binderfs_create_file(binder_logs_root_dir,
633 if (IS_ERR(dentry)) {
634 ret = PTR_ERR(dentry);
639 proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");
640 if (IS_ERR(proc_log_dir)) {
641 ret = PTR_ERR(proc_log_dir);
644 info = sb->s_fs_info;
645 info->proc_log_dir = proc_log_dir;
651 static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)
654 struct binderfs_info *info;
655 struct binderfs_mount_opts *ctx = fc->fs_private;
656 struct inode *inode = NULL;
657 struct binderfs_device device_info = {};
661 sb->s_blocksize = PAGE_SIZE;
662 sb->s_blocksize_bits = PAGE_SHIFT;
665 * The binderfs filesystem can be mounted by userns root in a
666 * non-initial userns. By default such mounts have the SB_I_NODEV flag
667 * set in s_iflags to prevent security issues where userns root can
668 * just create random device nodes via mknod() since it owns the
669 * filesystem mount. But binderfs does not allow to create any files
670 * including devices nodes. The only way to create binder devices nodes
671 * is through the binder-control device which userns root is explicitly
672 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
673 * necessary and safe.
675 sb->s_iflags &= ~SB_I_NODEV;
676 sb->s_iflags |= SB_I_NOEXEC;
677 sb->s_magic = BINDERFS_SUPER_MAGIC;
678 sb->s_op = &binderfs_super_ops;
681 sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
684 info = sb->s_fs_info;
686 info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
688 info->root_gid = make_kgid(sb->s_user_ns, 0);
689 if (!gid_valid(info->root_gid))
690 info->root_gid = GLOBAL_ROOT_GID;
691 info->root_uid = make_kuid(sb->s_user_ns, 0);
692 if (!uid_valid(info->root_uid))
693 info->root_uid = GLOBAL_ROOT_UID;
694 info->mount_opts.max = ctx->max;
695 info->mount_opts.stats_mode = ctx->stats_mode;
697 inode = new_inode(sb);
701 inode->i_ino = FIRST_INODE;
702 inode->i_fop = &simple_dir_operations;
703 inode->i_mode = S_IFDIR | 0755;
704 simple_inode_init_ts(inode);
705 inode->i_op = &binderfs_dir_inode_operations;
708 sb->s_root = d_make_root(inode);
712 ret = binderfs_binder_ctl_create(sb);
716 name = binder_devices_param;
717 for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
718 strscpy(device_info.name, name, len + 1);
719 ret = binderfs_binder_device_create(inode, NULL, &device_info);
727 ret = init_binder_features(sb);
731 if (info->mount_opts.stats_mode == binderfs_stats_mode_global)
732 return init_binder_logs(sb);
737 static int binderfs_fs_context_get_tree(struct fs_context *fc)
739 return get_tree_nodev(fc, binderfs_fill_super);
742 static void binderfs_fs_context_free(struct fs_context *fc)
744 struct binderfs_mount_opts *ctx = fc->fs_private;
749 static const struct fs_context_operations binderfs_fs_context_ops = {
750 .free = binderfs_fs_context_free,
751 .get_tree = binderfs_fs_context_get_tree,
752 .parse_param = binderfs_fs_context_parse_param,
753 .reconfigure = binderfs_fs_context_reconfigure,
756 static int binderfs_init_fs_context(struct fs_context *fc)
758 struct binderfs_mount_opts *ctx;
760 ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);
764 ctx->max = BINDERFS_MAX_MINOR;
765 ctx->stats_mode = binderfs_stats_mode_unset;
767 fc->fs_private = ctx;
768 fc->ops = &binderfs_fs_context_ops;
773 static void binderfs_kill_super(struct super_block *sb)
775 struct binderfs_info *info = sb->s_fs_info;
778 * During inode eviction struct binderfs_info is needed.
779 * So first wipe the super_block then free struct binderfs_info.
781 kill_litter_super(sb);
783 if (info && info->ipc_ns)
784 put_ipc_ns(info->ipc_ns);
789 static struct file_system_type binder_fs_type = {
791 .init_fs_context = binderfs_init_fs_context,
792 .parameters = binderfs_fs_parameters,
793 .kill_sb = binderfs_kill_super,
794 .fs_flags = FS_USERNS_MOUNT,
797 int __init init_binderfs(void)
803 /* Verify that the default binderfs device names are valid. */
804 name = binder_devices_param;
805 for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
806 if (len > BINDERFS_MAX_NAME)
813 /* Allocate new major number for binderfs. */
814 ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
819 ret = register_filesystem(&binder_fs_type);
821 unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);