Loading...
1// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/compiler_types.h>
4#include <linux/errno.h>
5#include <linux/fs.h>
6#include <linux/fsnotify.h>
7#include <linux/gfp.h>
8#include <linux/idr.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>
36
37#include "binder_internal.h"
38
39#define FIRST_INODE 1
40#define SECOND_INODE 2
41#define INODE_OFFSET 3
42#define INTSTRLEN 21
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)
46
47static dev_t binderfs_dev;
48static DEFINE_MUTEX(binderfs_minors_mutex);
49static DEFINE_IDA(binderfs_minors);
50
51enum binderfs_param {
52 Opt_max,
53 Opt_stats_mode,
54};
55
56enum binderfs_stats_mode {
57 binderfs_stats_mode_unset,
58 binderfs_stats_mode_global,
59};
60
61static const struct constant_table binderfs_param_stats[] = {
62 { "global", binderfs_stats_mode_global },
63 {}
64};
65
66const struct fs_parameter_spec binderfs_fs_parameters[] = {
67 fsparam_u32("max", Opt_max),
68 fsparam_enum("stats", Opt_stats_mode, binderfs_param_stats),
69 {}
70};
71
72static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)
73{
74 return sb->s_fs_info;
75}
76
77bool is_binderfs_device(const struct inode *inode)
78{
79 if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
80 return true;
81
82 return false;
83}
84
85/**
86 * binderfs_binder_device_create - allocate inode from super block of a
87 * binderfs mount
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
91 *
92 * This function allocates a new binder_device and reserves a new minor
93 * number for it.
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.
100 *
101 * Return: 0 on success, negative errno on failure
102 */
103static int binderfs_binder_device_create(struct inode *ref_inode,
104 struct binderfs_device __user *userp,
105 struct binderfs_device *req)
106{
107 int minor, ret;
108 struct dentry *dentry, *root;
109 struct binder_device *device;
110 char *name = NULL;
111 size_t name_len;
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);
117#else
118 bool use_reserve = true;
119#endif
120
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,
127 GFP_KERNEL);
128 else
129 minor = -ENOSPC;
130 if (minor < 0) {
131 --info->device_count;
132 mutex_unlock(&binderfs_minors_mutex);
133 return minor;
134 }
135 mutex_unlock(&binderfs_minors_mutex);
136
137 ret = -ENOMEM;
138 device = kzalloc(sizeof(*device), GFP_KERNEL);
139 if (!device)
140 goto err;
141
142 inode = new_inode(sb);
143 if (!inode)
144 goto err;
145
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;
153
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);
158 if (!name)
159 goto err;
160
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);
168
169 req->major = MAJOR(binderfs_dev);
170 req->minor = minor;
171
172 if (userp && copy_to_user(userp, req, sizeof(*req))) {
173 ret = -EFAULT;
174 goto err;
175 }
176
177 root = sb->s_root;
178 inode_lock(d_inode(root));
179
180 /* look it up */
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);
185 goto err;
186 }
187
188 if (d_really_is_positive(dentry)) {
189 /* already exists */
190 dput(dentry);
191 inode_unlock(d_inode(root));
192 ret = -EEXIST;
193 goto err;
194 }
195
196 inode->i_private = device;
197 d_instantiate(dentry, inode);
198 fsnotify_create(root->d_inode, dentry);
199 inode_unlock(d_inode(root));
200
201 return 0;
202
203err:
204 kfree(name);
205 kfree(device);
206 mutex_lock(&binderfs_minors_mutex);
207 --info->device_count;
208 ida_free(&binderfs_minors, minor);
209 mutex_unlock(&binderfs_minors_mutex);
210 iput(inode);
211
212 return ret;
213}
214
215/**
216 * binderfs_ctl_ioctl - handle binder device node allocation requests
217 *
218 * The request handler for the binder-control device. All requests operate on
219 * the binderfs mount the binder-control device resides in:
220 * - BINDER_CTL_ADD
221 * Allocate a new binder device.
222 *
223 * Return: 0 on success, negative errno on failure
224 */
225static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
226 unsigned long arg)
227{
228 int ret = -EINVAL;
229 struct inode *inode = file_inode(file);
230 struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
231 struct binderfs_device device_req;
232
233 switch (cmd) {
234 case BINDER_CTL_ADD:
235 ret = copy_from_user(&device_req, device, sizeof(device_req));
236 if (ret) {
237 ret = -EFAULT;
238 break;
239 }
240
241 ret = binderfs_binder_device_create(inode, device, &device_req);
242 break;
243 default:
244 break;
245 }
246
247 return ret;
248}
249
250static void binderfs_evict_inode(struct inode *inode)
251{
252 struct binder_device *device = inode->i_private;
253 struct binderfs_info *info = BINDERFS_SB(inode->i_sb);
254
255 clear_inode(inode);
256
257 if (!S_ISCHR(inode->i_mode) || !device)
258 return;
259
260 mutex_lock(&binderfs_minors_mutex);
261 --info->device_count;
262 ida_free(&binderfs_minors, device->miscdev.minor);
263 mutex_unlock(&binderfs_minors_mutex);
264
265 if (refcount_dec_and_test(&device->ref)) {
266 kfree(device->context.name);
267 kfree(device);
268 }
269}
270
271static int binderfs_fs_context_parse_param(struct fs_context *fc,
272 struct fs_parameter *param)
273{
274 int opt;
275 struct binderfs_mount_opts *ctx = fc->fs_private;
276 struct fs_parse_result result;
277
278 opt = fs_parse(fc, binderfs_fs_parameters, param, &result);
279 if (opt < 0)
280 return opt;
281
282 switch (opt) {
283 case Opt_max:
284 if (result.uint_32 > BINDERFS_MAX_MINOR)
285 return invalfc(fc, "Bad value for '%s'", param->key);
286
287 ctx->max = result.uint_32;
288 break;
289 case Opt_stats_mode:
290 if (!capable(CAP_SYS_ADMIN))
291 return -EPERM;
292
293 ctx->stats_mode = result.uint_32;
294 break;
295 default:
296 return invalfc(fc, "Unsupported parameter '%s'", param->key);
297 }
298
299 return 0;
300}
301
302static int binderfs_fs_context_reconfigure(struct fs_context *fc)
303{
304 struct binderfs_mount_opts *ctx = fc->fs_private;
305 struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);
306
307 if (info->mount_opts.stats_mode != ctx->stats_mode)
308 return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");
309
310 info->mount_opts.stats_mode = ctx->stats_mode;
311 info->mount_opts.max = ctx->max;
312 return 0;
313}
314
315static int binderfs_show_options(struct seq_file *seq, struct dentry *root)
316{
317 struct binderfs_info *info = BINDERFS_SB(root->d_sb);
318
319 if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
320 seq_printf(seq, ",max=%d", info->mount_opts.max);
321
322 switch (info->mount_opts.stats_mode) {
323 case binderfs_stats_mode_unset:
324 break;
325 case binderfs_stats_mode_global:
326 seq_printf(seq, ",stats=global");
327 break;
328 }
329
330 return 0;
331}
332
333static void binderfs_put_super(struct super_block *sb)
334{
335 struct binderfs_info *info = sb->s_fs_info;
336
337 if (info && info->ipc_ns)
338 put_ipc_ns(info->ipc_ns);
339
340 kfree(info);
341 sb->s_fs_info = NULL;
342}
343
344static 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,
349};
350
351static inline bool is_binderfs_control_device(const struct dentry *dentry)
352{
353 struct binderfs_info *info = dentry->d_sb->s_fs_info;
354
355 return info->control_dentry == dentry;
356}
357
358static int binderfs_rename(struct inode *old_dir, struct dentry *old_dentry,
359 struct inode *new_dir, struct dentry *new_dentry,
360 unsigned int flags)
361{
362 if (is_binderfs_control_device(old_dentry) ||
363 is_binderfs_control_device(new_dentry))
364 return -EPERM;
365
366 return simple_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
367}
368
369static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
370{
371 if (is_binderfs_control_device(dentry))
372 return -EPERM;
373
374 return simple_unlink(dir, dentry);
375}
376
377static 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,
383};
384
385/**
386 * binderfs_binder_ctl_create - create a new binder-control device
387 * @sb: super block of the binderfs mount
388 *
389 * This function creates a new binder-control device node in the binderfs mount
390 * referred to by @sb.
391 *
392 * Return: 0 on success, negative errno on failure
393 */
394static int binderfs_binder_ctl_create(struct super_block *sb)
395{
396 int minor, ret;
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);
404#else
405 bool use_reserve = true;
406#endif
407
408 device = kzalloc(sizeof(*device), GFP_KERNEL);
409 if (!device)
410 return -ENOMEM;
411
412 /* If we have already created a binder-control node, return. */
413 if (info->control_dentry) {
414 ret = 0;
415 goto out;
416 }
417
418 ret = -ENOMEM;
419 inode = new_inode(sb);
420 if (!inode)
421 goto out;
422
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,
428 GFP_KERNEL);
429 mutex_unlock(&binderfs_minors_mutex);
430 if (minor < 0) {
431 ret = minor;
432 goto out;
433 }
434
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;
442
443 refcount_set(&device->ref, 1);
444 device->binderfs_inode = inode;
445 device->miscdev.minor = minor;
446
447 dentry = d_alloc_name(root, "binder-control");
448 if (!dentry)
449 goto out;
450
451 inode->i_private = device;
452 info->control_dentry = dentry;
453 d_add(dentry, inode);
454
455 return 0;
456
457out:
458 kfree(device);
459 iput(inode);
460
461 return ret;
462}
463
464static const struct inode_operations binderfs_dir_inode_operations = {
465 .lookup = simple_lookup,
466 .rename = binderfs_rename,
467 .unlink = binderfs_unlink,
468};
469
470static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
471{
472 struct inode *ret;
473
474 ret = new_inode(sb);
475 if (ret) {
476 ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
477 ret->i_mode = mode;
478 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
479 }
480 return ret;
481}
482
483static struct dentry *binderfs_create_dentry(struct dentry *parent,
484 const char *name)
485{
486 struct dentry *dentry;
487
488 dentry = lookup_one_len(name, parent, strlen(name));
489 if (IS_ERR(dentry))
490 return dentry;
491
492 /* Return error if the file/dir already exists. */
493 if (d_really_is_positive(dentry)) {
494 dput(dentry);
495 return ERR_PTR(-EEXIST);
496 }
497
498 return dentry;
499}
500
501void binderfs_remove_file(struct dentry *dentry)
502{
503 struct inode *parent_inode;
504
505 parent_inode = d_inode(dentry->d_parent);
506 inode_lock(parent_inode);
507 if (simple_positive(dentry)) {
508 dget(dentry);
509 simple_unlink(parent_inode, dentry);
510 d_delete(dentry);
511 dput(dentry);
512 }
513 inode_unlock(parent_inode);
514}
515
516struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
517 const struct file_operations *fops,
518 void *data)
519{
520 struct dentry *dentry;
521 struct inode *new_inode, *parent_inode;
522 struct super_block *sb;
523
524 parent_inode = d_inode(parent);
525 inode_lock(parent_inode);
526
527 dentry = binderfs_create_dentry(parent, name);
528 if (IS_ERR(dentry))
529 goto out;
530
531 sb = parent_inode->i_sb;
532 new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
533 if (!new_inode) {
534 dput(dentry);
535 dentry = ERR_PTR(-ENOMEM);
536 goto out;
537 }
538
539 new_inode->i_fop = fops;
540 new_inode->i_private = data;
541 d_instantiate(dentry, new_inode);
542 fsnotify_create(parent_inode, dentry);
543
544out:
545 inode_unlock(parent_inode);
546 return dentry;
547}
548
549static struct dentry *binderfs_create_dir(struct dentry *parent,
550 const char *name)
551{
552 struct dentry *dentry;
553 struct inode *new_inode, *parent_inode;
554 struct super_block *sb;
555
556 parent_inode = d_inode(parent);
557 inode_lock(parent_inode);
558
559 dentry = binderfs_create_dentry(parent, name);
560 if (IS_ERR(dentry))
561 goto out;
562
563 sb = parent_inode->i_sb;
564 new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
565 if (!new_inode) {
566 dput(dentry);
567 dentry = ERR_PTR(-ENOMEM);
568 goto out;
569 }
570
571 new_inode->i_fop = &simple_dir_operations;
572 new_inode->i_op = &simple_dir_inode_operations;
573
574 set_nlink(new_inode, 2);
575 d_instantiate(dentry, new_inode);
576 inc_nlink(parent_inode);
577 fsnotify_mkdir(parent_inode, dentry);
578
579out:
580 inode_unlock(parent_inode);
581 return dentry;
582}
583
584static int init_binder_logs(struct super_block *sb)
585{
586 struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
587 struct binderfs_info *info;
588 int ret = 0;
589
590 binder_logs_root_dir = binderfs_create_dir(sb->s_root,
591 "binder_logs");
592 if (IS_ERR(binder_logs_root_dir)) {
593 ret = PTR_ERR(binder_logs_root_dir);
594 goto out;
595 }
596
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);
601 goto out;
602 }
603
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);
608 goto out;
609 }
610
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);
615 goto out;
616 }
617
618 dentry = binderfs_create_file(binder_logs_root_dir,
619 "transaction_log",
620 &binder_transaction_log_fops,
621 &binder_transaction_log);
622 if (IS_ERR(dentry)) {
623 ret = PTR_ERR(dentry);
624 goto out;
625 }
626
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);
633 goto out;
634 }
635
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);
639 goto out;
640 }
641 info = sb->s_fs_info;
642 info->proc_log_dir = proc_log_dir;
643
644out:
645 return ret;
646}
647
648static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)
649{
650 int ret;
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 = {};
655 const char *name;
656 size_t len;
657
658 sb->s_blocksize = PAGE_SIZE;
659 sb->s_blocksize_bits = PAGE_SHIFT;
660
661 /*
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.
671 */
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;
676 sb->s_time_gran = 1;
677
678 sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
679 if (!sb->s_fs_info)
680 return -ENOMEM;
681 info = sb->s_fs_info;
682
683 info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
684
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;
693
694 inode = new_inode(sb);
695 if (!inode)
696 return -ENOMEM;
697
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;
703 set_nlink(inode, 2);
704
705 sb->s_root = d_make_root(inode);
706 if (!sb->s_root)
707 return -ENOMEM;
708
709 ret = binderfs_binder_ctl_create(sb);
710 if (ret)
711 return ret;
712
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);
717 if (ret)
718 return ret;
719 name += len;
720 if (*name == ',')
721 name++;
722 }
723
724 if (info->mount_opts.stats_mode == binderfs_stats_mode_global)
725 return init_binder_logs(sb);
726
727 return 0;
728}
729
730static int binderfs_fs_context_get_tree(struct fs_context *fc)
731{
732 return get_tree_nodev(fc, binderfs_fill_super);
733}
734
735static void binderfs_fs_context_free(struct fs_context *fc)
736{
737 struct binderfs_mount_opts *ctx = fc->fs_private;
738
739 kfree(ctx);
740}
741
742static 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,
747};
748
749static int binderfs_init_fs_context(struct fs_context *fc)
750{
751 struct binderfs_mount_opts *ctx;
752
753 ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);
754 if (!ctx)
755 return -ENOMEM;
756
757 ctx->max = BINDERFS_MAX_MINOR;
758 ctx->stats_mode = binderfs_stats_mode_unset;
759
760 fc->fs_private = ctx;
761 fc->ops = &binderfs_fs_context_ops;
762
763 return 0;
764}
765
766static struct file_system_type binder_fs_type = {
767 .name = "binder",
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,
772};
773
774int __init init_binderfs(void)
775{
776 int ret;
777 const char *name;
778 size_t len;
779
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)
784 return -E2BIG;
785 name += len;
786 if (*name == ',')
787 name++;
788 }
789
790 /* Allocate new major number for binderfs. */
791 ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
792 "binder");
793 if (ret)
794 return ret;
795
796 ret = register_filesystem(&binder_fs_type);
797 if (ret) {
798 unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
799 return ret;
800 }
801
802 return ret;
803}
1// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/compiler_types.h>
4#include <linux/errno.h>
5#include <linux/fs.h>
6#include <linux/fsnotify.h>
7#include <linux/gfp.h>
8#include <linux/idr.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>
36
37#include "binder_internal.h"
38
39#define FIRST_INODE 1
40#define SECOND_INODE 2
41#define INODE_OFFSET 3
42#define BINDERFS_MAX_MINOR (1U << MINORBITS)
43/* Ensure that the initial ipc namespace always has devices available. */
44#define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)
45
46static dev_t binderfs_dev;
47static DEFINE_MUTEX(binderfs_minors_mutex);
48static DEFINE_IDA(binderfs_minors);
49
50enum binderfs_param {
51 Opt_max,
52 Opt_stats_mode,
53};
54
55enum binderfs_stats_mode {
56 binderfs_stats_mode_unset,
57 binderfs_stats_mode_global,
58};
59
60struct binder_features {
61 bool oneway_spam_detection;
62 bool extended_error;
63};
64
65static const struct constant_table binderfs_param_stats[] = {
66 { "global", binderfs_stats_mode_global },
67 {}
68};
69
70static const struct fs_parameter_spec binderfs_fs_parameters[] = {
71 fsparam_u32("max", Opt_max),
72 fsparam_enum("stats", Opt_stats_mode, binderfs_param_stats),
73 {}
74};
75
76static struct binder_features binder_features = {
77 .oneway_spam_detection = true,
78 .extended_error = true,
79};
80
81static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)
82{
83 return sb->s_fs_info;
84}
85
86bool is_binderfs_device(const struct inode *inode)
87{
88 if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
89 return true;
90
91 return false;
92}
93
94/**
95 * binderfs_binder_device_create - allocate inode from super block of a
96 * binderfs mount
97 * @ref_inode: inode from wich 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
100 *
101 * This function allocates a new binder_device and reserves a new minor
102 * number for it.
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.
109 *
110 * Return: 0 on success, negative errno on failure
111 */
112static int binderfs_binder_device_create(struct inode *ref_inode,
113 struct binderfs_device __user *userp,
114 struct binderfs_device *req)
115{
116 int minor, ret;
117 struct dentry *dentry, *root;
118 struct binder_device *device;
119 char *name = NULL;
120 size_t name_len;
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);
126#else
127 bool use_reserve = true;
128#endif
129
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,
136 GFP_KERNEL);
137 else
138 minor = -ENOSPC;
139 if (minor < 0) {
140 --info->device_count;
141 mutex_unlock(&binderfs_minors_mutex);
142 return minor;
143 }
144 mutex_unlock(&binderfs_minors_mutex);
145
146 ret = -ENOMEM;
147 device = kzalloc(sizeof(*device), GFP_KERNEL);
148 if (!device)
149 goto err;
150
151 inode = new_inode(sb);
152 if (!inode)
153 goto err;
154
155 inode->i_ino = minor + INODE_OFFSET;
156 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(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;
162
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);
167 if (!name)
168 goto err;
169
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);
177
178 req->major = MAJOR(binderfs_dev);
179 req->minor = minor;
180
181 if (userp && copy_to_user(userp, req, sizeof(*req))) {
182 ret = -EFAULT;
183 goto err;
184 }
185
186 root = sb->s_root;
187 inode_lock(d_inode(root));
188
189 /* look it up */
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);
194 goto err;
195 }
196
197 if (d_really_is_positive(dentry)) {
198 /* already exists */
199 dput(dentry);
200 inode_unlock(d_inode(root));
201 ret = -EEXIST;
202 goto err;
203 }
204
205 inode->i_private = device;
206 d_instantiate(dentry, inode);
207 fsnotify_create(root->d_inode, dentry);
208 inode_unlock(d_inode(root));
209
210 return 0;
211
212err:
213 kfree(name);
214 kfree(device);
215 mutex_lock(&binderfs_minors_mutex);
216 --info->device_count;
217 ida_free(&binderfs_minors, minor);
218 mutex_unlock(&binderfs_minors_mutex);
219 iput(inode);
220
221 return ret;
222}
223
224/**
225 * binderfs_ctl_ioctl - handle binder device node allocation requests
226 *
227 * The request handler for the binder-control device. All requests operate on
228 * the binderfs mount the binder-control device resides in:
229 * - BINDER_CTL_ADD
230 * Allocate a new binder device.
231 *
232 * Return: 0 on success, negative errno on failure
233 */
234static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
235 unsigned long arg)
236{
237 int ret = -EINVAL;
238 struct inode *inode = file_inode(file);
239 struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
240 struct binderfs_device device_req;
241
242 switch (cmd) {
243 case BINDER_CTL_ADD:
244 ret = copy_from_user(&device_req, device, sizeof(device_req));
245 if (ret) {
246 ret = -EFAULT;
247 break;
248 }
249
250 ret = binderfs_binder_device_create(inode, device, &device_req);
251 break;
252 default:
253 break;
254 }
255
256 return ret;
257}
258
259static void binderfs_evict_inode(struct inode *inode)
260{
261 struct binder_device *device = inode->i_private;
262 struct binderfs_info *info = BINDERFS_SB(inode->i_sb);
263
264 clear_inode(inode);
265
266 if (!S_ISCHR(inode->i_mode) || !device)
267 return;
268
269 mutex_lock(&binderfs_minors_mutex);
270 --info->device_count;
271 ida_free(&binderfs_minors, device->miscdev.minor);
272 mutex_unlock(&binderfs_minors_mutex);
273
274 if (refcount_dec_and_test(&device->ref)) {
275 kfree(device->context.name);
276 kfree(device);
277 }
278}
279
280static int binderfs_fs_context_parse_param(struct fs_context *fc,
281 struct fs_parameter *param)
282{
283 int opt;
284 struct binderfs_mount_opts *ctx = fc->fs_private;
285 struct fs_parse_result result;
286
287 opt = fs_parse(fc, binderfs_fs_parameters, param, &result);
288 if (opt < 0)
289 return opt;
290
291 switch (opt) {
292 case Opt_max:
293 if (result.uint_32 > BINDERFS_MAX_MINOR)
294 return invalfc(fc, "Bad value for '%s'", param->key);
295
296 ctx->max = result.uint_32;
297 break;
298 case Opt_stats_mode:
299 if (!capable(CAP_SYS_ADMIN))
300 return -EPERM;
301
302 ctx->stats_mode = result.uint_32;
303 break;
304 default:
305 return invalfc(fc, "Unsupported parameter '%s'", param->key);
306 }
307
308 return 0;
309}
310
311static int binderfs_fs_context_reconfigure(struct fs_context *fc)
312{
313 struct binderfs_mount_opts *ctx = fc->fs_private;
314 struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);
315
316 if (info->mount_opts.stats_mode != ctx->stats_mode)
317 return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");
318
319 info->mount_opts.stats_mode = ctx->stats_mode;
320 info->mount_opts.max = ctx->max;
321 return 0;
322}
323
324static int binderfs_show_options(struct seq_file *seq, struct dentry *root)
325{
326 struct binderfs_info *info = BINDERFS_SB(root->d_sb);
327
328 if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
329 seq_printf(seq, ",max=%d", info->mount_opts.max);
330
331 switch (info->mount_opts.stats_mode) {
332 case binderfs_stats_mode_unset:
333 break;
334 case binderfs_stats_mode_global:
335 seq_printf(seq, ",stats=global");
336 break;
337 }
338
339 return 0;
340}
341
342static const struct super_operations binderfs_super_ops = {
343 .evict_inode = binderfs_evict_inode,
344 .show_options = binderfs_show_options,
345 .statfs = simple_statfs,
346};
347
348static inline bool is_binderfs_control_device(const struct dentry *dentry)
349{
350 struct binderfs_info *info = dentry->d_sb->s_fs_info;
351
352 return info->control_dentry == dentry;
353}
354
355static int binderfs_rename(struct user_namespace *mnt_userns,
356 struct inode *old_dir, struct dentry *old_dentry,
357 struct inode *new_dir, struct dentry *new_dentry,
358 unsigned int flags)
359{
360 if (is_binderfs_control_device(old_dentry) ||
361 is_binderfs_control_device(new_dentry))
362 return -EPERM;
363
364 return simple_rename(&init_user_ns, old_dir, old_dentry, new_dir,
365 new_dentry, flags);
366}
367
368static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
369{
370 if (is_binderfs_control_device(dentry))
371 return -EPERM;
372
373 return simple_unlink(dir, dentry);
374}
375
376static 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,
382};
383
384/**
385 * binderfs_binder_ctl_create - create a new binder-control device
386 * @sb: super block of the binderfs mount
387 *
388 * This function creates a new binder-control device node in the binderfs mount
389 * referred to by @sb.
390 *
391 * Return: 0 on success, negative errno on failure
392 */
393static int binderfs_binder_ctl_create(struct super_block *sb)
394{
395 int minor, ret;
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);
403#else
404 bool use_reserve = true;
405#endif
406
407 device = kzalloc(sizeof(*device), GFP_KERNEL);
408 if (!device)
409 return -ENOMEM;
410
411 /* If we have already created a binder-control node, return. */
412 if (info->control_dentry) {
413 ret = 0;
414 goto out;
415 }
416
417 ret = -ENOMEM;
418 inode = new_inode(sb);
419 if (!inode)
420 goto out;
421
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,
427 GFP_KERNEL);
428 mutex_unlock(&binderfs_minors_mutex);
429 if (minor < 0) {
430 ret = minor;
431 goto out;
432 }
433
434 inode->i_ino = SECOND_INODE;
435 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(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;
441
442 refcount_set(&device->ref, 1);
443 device->binderfs_inode = inode;
444 device->miscdev.minor = minor;
445
446 dentry = d_alloc_name(root, "binder-control");
447 if (!dentry)
448 goto out;
449
450 inode->i_private = device;
451 info->control_dentry = dentry;
452 d_add(dentry, inode);
453
454 return 0;
455
456out:
457 kfree(device);
458 iput(inode);
459
460 return ret;
461}
462
463static const struct inode_operations binderfs_dir_inode_operations = {
464 .lookup = simple_lookup,
465 .rename = binderfs_rename,
466 .unlink = binderfs_unlink,
467};
468
469static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
470{
471 struct inode *ret;
472
473 ret = new_inode(sb);
474 if (ret) {
475 ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
476 ret->i_mode = mode;
477 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
478 }
479 return ret;
480}
481
482static struct dentry *binderfs_create_dentry(struct dentry *parent,
483 const char *name)
484{
485 struct dentry *dentry;
486
487 dentry = lookup_one_len(name, parent, strlen(name));
488 if (IS_ERR(dentry))
489 return dentry;
490
491 /* Return error if the file/dir already exists. */
492 if (d_really_is_positive(dentry)) {
493 dput(dentry);
494 return ERR_PTR(-EEXIST);
495 }
496
497 return dentry;
498}
499
500void binderfs_remove_file(struct dentry *dentry)
501{
502 struct inode *parent_inode;
503
504 parent_inode = d_inode(dentry->d_parent);
505 inode_lock(parent_inode);
506 if (simple_positive(dentry)) {
507 dget(dentry);
508 simple_unlink(parent_inode, dentry);
509 d_delete(dentry);
510 dput(dentry);
511 }
512 inode_unlock(parent_inode);
513}
514
515struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
516 const struct file_operations *fops,
517 void *data)
518{
519 struct dentry *dentry;
520 struct inode *new_inode, *parent_inode;
521 struct super_block *sb;
522
523 parent_inode = d_inode(parent);
524 inode_lock(parent_inode);
525
526 dentry = binderfs_create_dentry(parent, name);
527 if (IS_ERR(dentry))
528 goto out;
529
530 sb = parent_inode->i_sb;
531 new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
532 if (!new_inode) {
533 dput(dentry);
534 dentry = ERR_PTR(-ENOMEM);
535 goto out;
536 }
537
538 new_inode->i_fop = fops;
539 new_inode->i_private = data;
540 d_instantiate(dentry, new_inode);
541 fsnotify_create(parent_inode, dentry);
542
543out:
544 inode_unlock(parent_inode);
545 return dentry;
546}
547
548static struct dentry *binderfs_create_dir(struct dentry *parent,
549 const char *name)
550{
551 struct dentry *dentry;
552 struct inode *new_inode, *parent_inode;
553 struct super_block *sb;
554
555 parent_inode = d_inode(parent);
556 inode_lock(parent_inode);
557
558 dentry = binderfs_create_dentry(parent, name);
559 if (IS_ERR(dentry))
560 goto out;
561
562 sb = parent_inode->i_sb;
563 new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
564 if (!new_inode) {
565 dput(dentry);
566 dentry = ERR_PTR(-ENOMEM);
567 goto out;
568 }
569
570 new_inode->i_fop = &simple_dir_operations;
571 new_inode->i_op = &simple_dir_inode_operations;
572
573 set_nlink(new_inode, 2);
574 d_instantiate(dentry, new_inode);
575 inc_nlink(parent_inode);
576 fsnotify_mkdir(parent_inode, dentry);
577
578out:
579 inode_unlock(parent_inode);
580 return dentry;
581}
582
583static int binder_features_show(struct seq_file *m, void *unused)
584{
585 bool *feature = m->private;
586
587 seq_printf(m, "%d\n", *feature);
588
589 return 0;
590}
591DEFINE_SHOW_ATTRIBUTE(binder_features);
592
593static int init_binder_features(struct super_block *sb)
594{
595 struct dentry *dentry, *dir;
596
597 dir = binderfs_create_dir(sb->s_root, "features");
598 if (IS_ERR(dir))
599 return PTR_ERR(dir);
600
601 dentry = binderfs_create_file(dir, "oneway_spam_detection",
602 &binder_features_fops,
603 &binder_features.oneway_spam_detection);
604 if (IS_ERR(dentry))
605 return PTR_ERR(dentry);
606
607 dentry = binderfs_create_file(dir, "extended_error",
608 &binder_features_fops,
609 &binder_features.extended_error);
610 if (IS_ERR(dentry))
611 return PTR_ERR(dentry);
612
613 return 0;
614}
615
616static int init_binder_logs(struct super_block *sb)
617{
618 struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
619 const struct binder_debugfs_entry *db_entry;
620 struct binderfs_info *info;
621 int ret = 0;
622
623 binder_logs_root_dir = binderfs_create_dir(sb->s_root,
624 "binder_logs");
625 if (IS_ERR(binder_logs_root_dir)) {
626 ret = PTR_ERR(binder_logs_root_dir);
627 goto out;
628 }
629
630 binder_for_each_debugfs_entry(db_entry) {
631 dentry = binderfs_create_file(binder_logs_root_dir,
632 db_entry->name,
633 db_entry->fops,
634 db_entry->data);
635 if (IS_ERR(dentry)) {
636 ret = PTR_ERR(dentry);
637 goto out;
638 }
639 }
640
641 proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");
642 if (IS_ERR(proc_log_dir)) {
643 ret = PTR_ERR(proc_log_dir);
644 goto out;
645 }
646 info = sb->s_fs_info;
647 info->proc_log_dir = proc_log_dir;
648
649out:
650 return ret;
651}
652
653static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)
654{
655 int ret;
656 struct binderfs_info *info;
657 struct binderfs_mount_opts *ctx = fc->fs_private;
658 struct inode *inode = NULL;
659 struct binderfs_device device_info = {};
660 const char *name;
661 size_t len;
662
663 sb->s_blocksize = PAGE_SIZE;
664 sb->s_blocksize_bits = PAGE_SHIFT;
665
666 /*
667 * The binderfs filesystem can be mounted by userns root in a
668 * non-initial userns. By default such mounts have the SB_I_NODEV flag
669 * set in s_iflags to prevent security issues where userns root can
670 * just create random device nodes via mknod() since it owns the
671 * filesystem mount. But binderfs does not allow to create any files
672 * including devices nodes. The only way to create binder devices nodes
673 * is through the binder-control device which userns root is explicitly
674 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
675 * necessary and safe.
676 */
677 sb->s_iflags &= ~SB_I_NODEV;
678 sb->s_iflags |= SB_I_NOEXEC;
679 sb->s_magic = BINDERFS_SUPER_MAGIC;
680 sb->s_op = &binderfs_super_ops;
681 sb->s_time_gran = 1;
682
683 sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
684 if (!sb->s_fs_info)
685 return -ENOMEM;
686 info = sb->s_fs_info;
687
688 info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
689
690 info->root_gid = make_kgid(sb->s_user_ns, 0);
691 if (!gid_valid(info->root_gid))
692 info->root_gid = GLOBAL_ROOT_GID;
693 info->root_uid = make_kuid(sb->s_user_ns, 0);
694 if (!uid_valid(info->root_uid))
695 info->root_uid = GLOBAL_ROOT_UID;
696 info->mount_opts.max = ctx->max;
697 info->mount_opts.stats_mode = ctx->stats_mode;
698
699 inode = new_inode(sb);
700 if (!inode)
701 return -ENOMEM;
702
703 inode->i_ino = FIRST_INODE;
704 inode->i_fop = &simple_dir_operations;
705 inode->i_mode = S_IFDIR | 0755;
706 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
707 inode->i_op = &binderfs_dir_inode_operations;
708 set_nlink(inode, 2);
709
710 sb->s_root = d_make_root(inode);
711 if (!sb->s_root)
712 return -ENOMEM;
713
714 ret = binderfs_binder_ctl_create(sb);
715 if (ret)
716 return ret;
717
718 name = binder_devices_param;
719 for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
720 strscpy(device_info.name, name, len + 1);
721 ret = binderfs_binder_device_create(inode, NULL, &device_info);
722 if (ret)
723 return ret;
724 name += len;
725 if (*name == ',')
726 name++;
727 }
728
729 ret = init_binder_features(sb);
730 if (ret)
731 return ret;
732
733 if (info->mount_opts.stats_mode == binderfs_stats_mode_global)
734 return init_binder_logs(sb);
735
736 return 0;
737}
738
739static int binderfs_fs_context_get_tree(struct fs_context *fc)
740{
741 return get_tree_nodev(fc, binderfs_fill_super);
742}
743
744static void binderfs_fs_context_free(struct fs_context *fc)
745{
746 struct binderfs_mount_opts *ctx = fc->fs_private;
747
748 kfree(ctx);
749}
750
751static const struct fs_context_operations binderfs_fs_context_ops = {
752 .free = binderfs_fs_context_free,
753 .get_tree = binderfs_fs_context_get_tree,
754 .parse_param = binderfs_fs_context_parse_param,
755 .reconfigure = binderfs_fs_context_reconfigure,
756};
757
758static int binderfs_init_fs_context(struct fs_context *fc)
759{
760 struct binderfs_mount_opts *ctx;
761
762 ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);
763 if (!ctx)
764 return -ENOMEM;
765
766 ctx->max = BINDERFS_MAX_MINOR;
767 ctx->stats_mode = binderfs_stats_mode_unset;
768
769 fc->fs_private = ctx;
770 fc->ops = &binderfs_fs_context_ops;
771
772 return 0;
773}
774
775static void binderfs_kill_super(struct super_block *sb)
776{
777 struct binderfs_info *info = sb->s_fs_info;
778
779 /*
780 * During inode eviction struct binderfs_info is needed.
781 * So first wipe the super_block then free struct binderfs_info.
782 */
783 kill_litter_super(sb);
784
785 if (info && info->ipc_ns)
786 put_ipc_ns(info->ipc_ns);
787
788 kfree(info);
789}
790
791static struct file_system_type binder_fs_type = {
792 .name = "binder",
793 .init_fs_context = binderfs_init_fs_context,
794 .parameters = binderfs_fs_parameters,
795 .kill_sb = binderfs_kill_super,
796 .fs_flags = FS_USERNS_MOUNT,
797};
798
799int __init init_binderfs(void)
800{
801 int ret;
802 const char *name;
803 size_t len;
804
805 /* Verify that the default binderfs device names are valid. */
806 name = binder_devices_param;
807 for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
808 if (len > BINDERFS_MAX_NAME)
809 return -E2BIG;
810 name += len;
811 if (*name == ',')
812 name++;
813 }
814
815 /* Allocate new major number for binderfs. */
816 ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
817 "binder");
818 if (ret)
819 return ret;
820
821 ret = register_filesystem(&binder_fs_type);
822 if (ret) {
823 unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
824 return ret;
825 }
826
827 return ret;
828}