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