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