xref: /linux/drivers/android/binderfs.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
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 
BINDERFS_SB(const struct super_block * sb)81 static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)
82 {
83 	return sb->s_fs_info;
84 }
85 
is_binderfs_device(const struct inode * inode)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  */
binderfs_binder_device_create(struct inode * ref_inode,struct binderfs_device __user * userp,struct binderfs_device * req)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 	struct inode *inode = NULL;
121 	struct super_block *sb = ref_inode->i_sb;
122 	struct binderfs_info *info = sb->s_fs_info;
123 #if defined(CONFIG_IPC_NS)
124 	bool use_reserve = (info->ipc_ns == &init_ipc_ns);
125 #else
126 	bool use_reserve = true;
127 #endif
128 
129 	/* Reserve new minor number for the new device. */
130 	mutex_lock(&binderfs_minors_mutex);
131 	if (++info->device_count <= info->mount_opts.max)
132 		minor = ida_alloc_max(&binderfs_minors,
133 				      use_reserve ? BINDERFS_MAX_MINOR :
134 						    BINDERFS_MAX_MINOR_CAPPED,
135 				      GFP_KERNEL);
136 	else
137 		minor = -ENOSPC;
138 	if (minor < 0) {
139 		--info->device_count;
140 		mutex_unlock(&binderfs_minors_mutex);
141 		return minor;
142 	}
143 	mutex_unlock(&binderfs_minors_mutex);
144 
145 	ret = -ENOMEM;
146 	device = kzalloc(sizeof(*device), GFP_KERNEL);
147 	if (!device)
148 		goto err;
149 
150 	inode = new_inode(sb);
151 	if (!inode)
152 		goto err;
153 
154 	inode->i_ino = minor + INODE_OFFSET;
155 	simple_inode_init_ts(inode);
156 	init_special_inode(inode, S_IFCHR | 0600,
157 			   MKDEV(MAJOR(binderfs_dev), minor));
158 	inode->i_fop = &binder_fops;
159 	inode->i_uid = info->root_uid;
160 	inode->i_gid = info->root_gid;
161 
162 	req->name[BINDERFS_MAX_NAME] = '\0'; /* NUL-terminate */
163 	name = kstrdup(req->name, GFP_KERNEL);
164 	if (!name)
165 		goto err;
166 
167 	refcount_set(&device->ref, 1);
168 	device->binderfs_inode = inode;
169 	device->context.binder_context_mgr_uid = INVALID_UID;
170 	device->context.name = name;
171 	device->miscdev.name = name;
172 	device->miscdev.minor = minor;
173 	mutex_init(&device->context.context_mgr_node_lock);
174 
175 	req->major = MAJOR(binderfs_dev);
176 	req->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 	inode_lock(d_inode(root));
185 
186 	/* look it up */
187 	dentry = lookup_noperm(&QSTR(name), root);
188 	if (IS_ERR(dentry)) {
189 		inode_unlock(d_inode(root));
190 		ret = PTR_ERR(dentry);
191 		goto err;
192 	}
193 
194 	if (d_really_is_positive(dentry)) {
195 		/* already exists */
196 		dput(dentry);
197 		inode_unlock(d_inode(root));
198 		ret = -EEXIST;
199 		goto err;
200 	}
201 
202 	inode->i_private = device;
203 	d_instantiate(dentry, inode);
204 	fsnotify_create(root->d_inode, dentry);
205 	inode_unlock(d_inode(root));
206 
207 	binder_add_device(device);
208 
209 	return 0;
210 
211 err:
212 	kfree(name);
213 	kfree(device);
214 	mutex_lock(&binderfs_minors_mutex);
215 	--info->device_count;
216 	ida_free(&binderfs_minors, minor);
217 	mutex_unlock(&binderfs_minors_mutex);
218 	iput(inode);
219 
220 	return ret;
221 }
222 
223 /**
224  * binder_ctl_ioctl - handle binder device node allocation requests
225  *
226  * The request handler for the binder-control device. All requests operate on
227  * the binderfs mount the binder-control device resides in:
228  * - BINDER_CTL_ADD
229  *   Allocate a new binder device.
230  *
231  * Return: %0 on success, negative errno on failure.
232  */
binder_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)233 static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
234 			     unsigned long arg)
235 {
236 	int ret = -EINVAL;
237 	struct inode *inode = file_inode(file);
238 	struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
239 	struct binderfs_device device_req;
240 
241 	switch (cmd) {
242 	case BINDER_CTL_ADD:
243 		ret = copy_from_user(&device_req, device, sizeof(device_req));
244 		if (ret) {
245 			ret = -EFAULT;
246 			break;
247 		}
248 
249 		ret = binderfs_binder_device_create(inode, device, &device_req);
250 		break;
251 	default:
252 		break;
253 	}
254 
255 	return ret;
256 }
257 
binderfs_evict_inode(struct inode * inode)258 static void binderfs_evict_inode(struct inode *inode)
259 {
260 	struct binder_device *device = inode->i_private;
261 	struct binderfs_info *info = BINDERFS_SB(inode->i_sb);
262 
263 	clear_inode(inode);
264 
265 	if (!S_ISCHR(inode->i_mode) || !device)
266 		return;
267 
268 	mutex_lock(&binderfs_minors_mutex);
269 	--info->device_count;
270 	ida_free(&binderfs_minors, device->miscdev.minor);
271 	mutex_unlock(&binderfs_minors_mutex);
272 
273 	if (refcount_dec_and_test(&device->ref)) {
274 		binder_remove_device(device);
275 		kfree(device->context.name);
276 		kfree(device);
277 	}
278 }
279 
binderfs_fs_context_parse_param(struct fs_context * fc,struct fs_parameter * param)280 static 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 
binderfs_fs_context_reconfigure(struct fs_context * fc)311 static 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 
binderfs_show_options(struct seq_file * seq,struct dentry * root)324 static 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 
342 static const struct super_operations binderfs_super_ops = {
343 	.evict_inode    = binderfs_evict_inode,
344 	.show_options	= binderfs_show_options,
345 	.statfs         = simple_statfs,
346 };
347 
is_binderfs_control_device(const struct dentry * dentry)348 static 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 
binderfs_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)355 static int binderfs_rename(struct mnt_idmap *idmap,
356 			   struct inode *old_dir, struct dentry *old_dentry,
357 			   struct inode *new_dir, struct dentry *new_dentry,
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(idmap, old_dir, old_dentry, new_dir,
365 			     new_dentry, flags);
366 }
367 
binderfs_unlink(struct inode * dir,struct dentry * dentry)368 static 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 
376 static const struct file_operations binder_ctl_fops = {
377 	.owner		= THIS_MODULE,
378 	.open		= nonseekable_open,
379 	.unlocked_ioctl	= binder_ctl_ioctl,
380 	.compat_ioctl	= binder_ctl_ioctl,
381 	.llseek		= noop_llseek,
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  */
binderfs_binder_ctl_create(struct super_block * sb)393 static 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 	simple_inode_init_ts(inode);
436 	init_special_inode(inode, S_IFCHR | 0600,
437 			   MKDEV(MAJOR(binderfs_dev), minor));
438 	inode->i_fop = &binder_ctl_fops;
439 	inode->i_uid = info->root_uid;
440 	inode->i_gid = info->root_gid;
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 
456 out:
457 	kfree(device);
458 	iput(inode);
459 
460 	return ret;
461 }
462 
463 static const struct inode_operations binderfs_dir_inode_operations = {
464 	.lookup = simple_lookup,
465 	.rename = binderfs_rename,
466 	.unlink = binderfs_unlink,
467 };
468 
binderfs_make_inode(struct super_block * sb,int mode)469 static 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 		simple_inode_init_ts(ret);
478 	}
479 	return ret;
480 }
481 
binderfs_create_dentry(struct dentry * parent,const char * name)482 static struct dentry *binderfs_create_dentry(struct dentry *parent,
483 					     const char *name)
484 {
485 	struct dentry *dentry;
486 
487 	dentry = lookup_noperm(&QSTR(name), parent);
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 
binderfs_create_file(struct dentry * parent,const char * name,const struct file_operations * fops,void * data)500 struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
501 				    const struct file_operations *fops,
502 				    void *data)
503 {
504 	struct dentry *dentry;
505 	struct inode *new_inode, *parent_inode;
506 	struct super_block *sb;
507 
508 	parent_inode = d_inode(parent);
509 	inode_lock(parent_inode);
510 
511 	dentry = binderfs_create_dentry(parent, name);
512 	if (IS_ERR(dentry))
513 		goto out;
514 
515 	sb = parent_inode->i_sb;
516 	new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
517 	if (!new_inode) {
518 		dput(dentry);
519 		dentry = ERR_PTR(-ENOMEM);
520 		goto out;
521 	}
522 
523 	new_inode->i_fop = fops;
524 	new_inode->i_private = data;
525 	d_instantiate(dentry, new_inode);
526 	fsnotify_create(parent_inode, dentry);
527 
528 out:
529 	inode_unlock(parent_inode);
530 	return dentry;
531 }
532 
binderfs_create_dir(struct dentry * parent,const char * name)533 static struct dentry *binderfs_create_dir(struct dentry *parent,
534 					  const char *name)
535 {
536 	struct dentry *dentry;
537 	struct inode *new_inode, *parent_inode;
538 	struct super_block *sb;
539 
540 	parent_inode = d_inode(parent);
541 	inode_lock(parent_inode);
542 
543 	dentry = binderfs_create_dentry(parent, name);
544 	if (IS_ERR(dentry))
545 		goto out;
546 
547 	sb = parent_inode->i_sb;
548 	new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
549 	if (!new_inode) {
550 		dput(dentry);
551 		dentry = ERR_PTR(-ENOMEM);
552 		goto out;
553 	}
554 
555 	new_inode->i_fop = &simple_dir_operations;
556 	new_inode->i_op = &simple_dir_inode_operations;
557 
558 	set_nlink(new_inode, 2);
559 	d_instantiate(dentry, new_inode);
560 	inc_nlink(parent_inode);
561 	fsnotify_mkdir(parent_inode, dentry);
562 
563 out:
564 	inode_unlock(parent_inode);
565 	return dentry;
566 }
567 
binder_features_show(struct seq_file * m,void * unused)568 static int binder_features_show(struct seq_file *m, void *unused)
569 {
570 	bool *feature = m->private;
571 
572 	seq_printf(m, "%d\n", *feature);
573 
574 	return 0;
575 }
576 DEFINE_SHOW_ATTRIBUTE(binder_features);
577 
init_binder_features(struct super_block * sb)578 static int init_binder_features(struct super_block *sb)
579 {
580 	struct dentry *dentry, *dir;
581 
582 	dir = binderfs_create_dir(sb->s_root, "features");
583 	if (IS_ERR(dir))
584 		return PTR_ERR(dir);
585 
586 	dentry = binderfs_create_file(dir, "oneway_spam_detection",
587 				      &binder_features_fops,
588 				      &binder_features.oneway_spam_detection);
589 	if (IS_ERR(dentry))
590 		return PTR_ERR(dentry);
591 
592 	dentry = binderfs_create_file(dir, "extended_error",
593 				      &binder_features_fops,
594 				      &binder_features.extended_error);
595 	if (IS_ERR(dentry))
596 		return PTR_ERR(dentry);
597 
598 	dentry = binderfs_create_file(dir, "freeze_notification",
599 				      &binder_features_fops,
600 				      &binder_features.freeze_notification);
601 	if (IS_ERR(dentry))
602 		return PTR_ERR(dentry);
603 
604 	return 0;
605 }
606 
init_binder_logs(struct super_block * sb)607 static int init_binder_logs(struct super_block *sb)
608 {
609 	struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
610 	const struct binder_debugfs_entry *db_entry;
611 	struct binderfs_info *info;
612 	int ret = 0;
613 
614 	binder_logs_root_dir = binderfs_create_dir(sb->s_root,
615 						   "binder_logs");
616 	if (IS_ERR(binder_logs_root_dir)) {
617 		ret = PTR_ERR(binder_logs_root_dir);
618 		goto out;
619 	}
620 
621 	binder_for_each_debugfs_entry(db_entry) {
622 		dentry = binderfs_create_file(binder_logs_root_dir,
623 					      db_entry->name,
624 					      db_entry->fops,
625 					      db_entry->data);
626 		if (IS_ERR(dentry)) {
627 			ret = PTR_ERR(dentry);
628 			goto out;
629 		}
630 	}
631 
632 	proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");
633 	if (IS_ERR(proc_log_dir)) {
634 		ret = PTR_ERR(proc_log_dir);
635 		goto out;
636 	}
637 	info = sb->s_fs_info;
638 	info->proc_log_dir = proc_log_dir;
639 
640 out:
641 	return ret;
642 }
643 
binderfs_fill_super(struct super_block * sb,struct fs_context * fc)644 static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)
645 {
646 	int ret;
647 	struct binderfs_info *info;
648 	struct binderfs_mount_opts *ctx = fc->fs_private;
649 	struct inode *inode = NULL;
650 	struct binderfs_device device_info = {};
651 	const char *name;
652 	size_t len;
653 
654 	sb->s_blocksize = PAGE_SIZE;
655 	sb->s_blocksize_bits = PAGE_SHIFT;
656 
657 	/*
658 	 * The binderfs filesystem can be mounted by userns root in a
659 	 * non-initial userns. By default such mounts have the SB_I_NODEV flag
660 	 * set in s_iflags to prevent security issues where userns root can
661 	 * just create random device nodes via mknod() since it owns the
662 	 * filesystem mount. But binderfs does not allow to create any files
663 	 * including devices nodes. The only way to create binder devices nodes
664 	 * is through the binder-control device which userns root is explicitly
665 	 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
666 	 * necessary and safe.
667 	 */
668 	sb->s_iflags &= ~SB_I_NODEV;
669 	sb->s_iflags |= SB_I_NOEXEC;
670 	sb->s_magic = BINDERFS_SUPER_MAGIC;
671 	sb->s_op = &binderfs_super_ops;
672 	sb->s_time_gran = 1;
673 
674 	sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
675 	if (!sb->s_fs_info)
676 		return -ENOMEM;
677 	info = sb->s_fs_info;
678 
679 	info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
680 
681 	info->root_gid = make_kgid(sb->s_user_ns, 0);
682 	if (!gid_valid(info->root_gid))
683 		info->root_gid = GLOBAL_ROOT_GID;
684 	info->root_uid = make_kuid(sb->s_user_ns, 0);
685 	if (!uid_valid(info->root_uid))
686 		info->root_uid = GLOBAL_ROOT_UID;
687 	info->mount_opts.max = ctx->max;
688 	info->mount_opts.stats_mode = ctx->stats_mode;
689 
690 	inode = new_inode(sb);
691 	if (!inode)
692 		return -ENOMEM;
693 
694 	inode->i_ino = FIRST_INODE;
695 	inode->i_fop = &simple_dir_operations;
696 	inode->i_mode = S_IFDIR | 0755;
697 	simple_inode_init_ts(inode);
698 	inode->i_op = &binderfs_dir_inode_operations;
699 	set_nlink(inode, 2);
700 
701 	sb->s_root = d_make_root(inode);
702 	if (!sb->s_root)
703 		return -ENOMEM;
704 
705 	ret = binderfs_binder_ctl_create(sb);
706 	if (ret)
707 		return ret;
708 
709 	name = binder_devices_param;
710 	for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
711 		strscpy(device_info.name, name, len + 1);
712 		ret = binderfs_binder_device_create(inode, NULL, &device_info);
713 		if (ret)
714 			return ret;
715 		name += len;
716 		if (*name == ',')
717 			name++;
718 	}
719 
720 	ret = init_binder_features(sb);
721 	if (ret)
722 		return ret;
723 
724 	if (info->mount_opts.stats_mode == binderfs_stats_mode_global)
725 		return init_binder_logs(sb);
726 
727 	return 0;
728 }
729 
binderfs_fs_context_get_tree(struct fs_context * fc)730 static int binderfs_fs_context_get_tree(struct fs_context *fc)
731 {
732 	return get_tree_nodev(fc, binderfs_fill_super);
733 }
734 
binderfs_fs_context_free(struct fs_context * fc)735 static 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 
742 static 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 
binderfs_init_fs_context(struct fs_context * fc)749 static 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 
binderfs_kill_super(struct super_block * sb)766 static void binderfs_kill_super(struct super_block *sb)
767 {
768 	struct binderfs_info *info = sb->s_fs_info;
769 
770 	/*
771 	 * During inode eviction struct binderfs_info is needed.
772 	 * So first wipe the super_block then free struct binderfs_info.
773 	 */
774 	kill_litter_super(sb);
775 
776 	if (info && info->ipc_ns)
777 		put_ipc_ns(info->ipc_ns);
778 
779 	kfree(info);
780 }
781 
782 static struct file_system_type binder_fs_type = {
783 	.name			= "binder",
784 	.init_fs_context	= binderfs_init_fs_context,
785 	.parameters		= binderfs_fs_parameters,
786 	.kill_sb		= binderfs_kill_super,
787 	.fs_flags		= FS_USERNS_MOUNT,
788 };
789 
init_binderfs(void)790 int __init init_binderfs(void)
791 {
792 	int ret;
793 	const char *name;
794 	size_t len;
795 
796 	/* Verify that the default binderfs device names are valid. */
797 	name = binder_devices_param;
798 	for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
799 		if (len > BINDERFS_MAX_NAME)
800 			return -E2BIG;
801 		name += len;
802 		if (*name == ',')
803 			name++;
804 	}
805 
806 	/* Allocate new major number for binderfs. */
807 	ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
808 				  "binder");
809 	if (ret)
810 		return ret;
811 
812 	ret = register_filesystem(&binder_fs_type);
813 	if (ret) {
814 		unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
815 		return ret;
816 	}
817 
818 	return ret;
819 }
820