• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <trace/hooks/binder.h>
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 	bool transaction_report;
63 };
64 
65 static const struct constant_table binderfs_param_stats[] = {
66 	{ "global", binderfs_stats_mode_global },
67 	{}
68 };
69 
70 static 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 
76 static struct binder_features binder_features = {
77 	.oneway_spam_detection = true,
78 	.extended_error = true,
79 	.freeze_notification = true,
80 	.transaction_report = true,
81 };
82 
BINDERFS_SB(const struct super_block * sb)83 static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)
84 {
85 	return sb->s_fs_info;
86 }
87 
is_binderfs_device(const struct inode * inode)88 bool is_binderfs_device(const struct inode *inode)
89 {
90 	if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
91 		return true;
92 
93 	return false;
94 }
95 
96 /**
97  * binderfs_binder_device_create - allocate inode from super block of a
98  *                                 binderfs mount
99  * @ref_inode: inode from which the super block will be taken
100  * @userp:     buffer to copy information about new device for userspace to
101  * @req:       struct binderfs_device as copied from userspace
102  *
103  * This function allocates a new binder_device and reserves a new minor
104  * number for it.
105  * Minor numbers are limited and tracked globally in binderfs_minors. The
106  * function will stash a struct binder_device for the specific binder
107  * device in i_private of the inode.
108  * It will go on to allocate a new inode from the super block of the
109  * filesystem mount, stash a struct binder_device in its i_private field
110  * and attach a dentry to that inode.
111  *
112  * Return: 0 on success, negative errno on failure
113  */
binderfs_binder_device_create(struct inode * ref_inode,struct binderfs_device __user * userp,struct binderfs_device * req)114 static int binderfs_binder_device_create(struct inode *ref_inode,
115 					 struct binderfs_device __user *userp,
116 					 struct binderfs_device *req)
117 {
118 	int minor, ret;
119 	struct dentry *dentry, *root;
120 	struct binder_device *device;
121 	char *name = NULL;
122 	size_t name_len;
123 	struct inode *inode = NULL;
124 	struct super_block *sb = ref_inode->i_sb;
125 	struct binderfs_info *info = sb->s_fs_info;
126 #if defined(CONFIG_IPC_NS)
127 	bool use_reserve = (info->ipc_ns == &init_ipc_ns);
128 #else
129 	bool use_reserve = true;
130 #endif
131 
132 	/* Reserve new minor number for the new device. */
133 	mutex_lock(&binderfs_minors_mutex);
134 	if (++info->device_count <= info->mount_opts.max)
135 		minor = ida_alloc_max(&binderfs_minors,
136 				      use_reserve ? BINDERFS_MAX_MINOR :
137 						    BINDERFS_MAX_MINOR_CAPPED,
138 				      GFP_KERNEL);
139 	else
140 		minor = -ENOSPC;
141 	if (minor < 0) {
142 		--info->device_count;
143 		mutex_unlock(&binderfs_minors_mutex);
144 		return minor;
145 	}
146 	mutex_unlock(&binderfs_minors_mutex);
147 
148 	ret = -ENOMEM;
149 	device = kzalloc(sizeof(*device), GFP_KERNEL);
150 	if (!device)
151 		goto err;
152 
153 	inode = new_inode(sb);
154 	if (!inode)
155 		goto err;
156 
157 	inode->i_ino = minor + INODE_OFFSET;
158 	simple_inode_init_ts(inode);
159 	init_special_inode(inode, S_IFCHR | 0600,
160 			   MKDEV(MAJOR(binderfs_dev), minor));
161 	inode->i_fop = &binder_fops;
162 	inode->i_uid = info->root_uid;
163 	inode->i_gid = info->root_gid;
164 
165 	req->name[BINDERFS_MAX_NAME] = '\0'; /* NUL-terminate */
166 	name_len = strlen(req->name);
167 	/* Make sure to include terminating NUL byte */
168 	name = kmemdup(req->name, name_len + 1, GFP_KERNEL);
169 	if (!name)
170 		goto err;
171 
172 	refcount_set(&device->ref, 1);
173 	device->binderfs_inode = inode;
174 	device->context.binder_context_mgr_uid = INVALID_UID;
175 	device->context.name = name;
176 	device->miscdev.name = name;
177 	device->miscdev.minor = minor;
178 	mutex_init(&device->context.context_mgr_node_lock);
179 
180 	req->major = MAJOR(binderfs_dev);
181 	req->minor = minor;
182 
183 	if (userp && copy_to_user(userp, req, sizeof(*req))) {
184 		ret = -EFAULT;
185 		goto err;
186 	}
187 
188 	root = sb->s_root;
189 	inode_lock(d_inode(root));
190 
191 	/* look it up */
192 	dentry = lookup_one_len(name, root, name_len);
193 	if (IS_ERR(dentry)) {
194 		inode_unlock(d_inode(root));
195 		ret = PTR_ERR(dentry);
196 		goto err;
197 	}
198 
199 	if (d_really_is_positive(dentry)) {
200 		/* already exists */
201 		dput(dentry);
202 		inode_unlock(d_inode(root));
203 		ret = -EEXIST;
204 		goto err;
205 	}
206 
207 	inode->i_private = device;
208 	d_instantiate(dentry, inode);
209 	fsnotify_create(root->d_inode, dentry);
210 	inode_unlock(d_inode(root));
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  */
binder_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)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 
binderfs_evict_inode(struct inode * inode)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 		kfree(device->context.name);
278 		kfree(device);
279 	}
280 }
281 
binderfs_fs_context_parse_param(struct fs_context * fc,struct fs_parameter * param)282 static int binderfs_fs_context_parse_param(struct fs_context *fc,
283 					   struct fs_parameter *param)
284 {
285 	int opt;
286 	struct binderfs_mount_opts *ctx = fc->fs_private;
287 	struct fs_parse_result result;
288 
289 	opt = fs_parse(fc, binderfs_fs_parameters, param, &result);
290 	if (opt < 0)
291 		return opt;
292 
293 	switch (opt) {
294 	case Opt_max:
295 		if (result.uint_32 > BINDERFS_MAX_MINOR)
296 			return invalfc(fc, "Bad value for '%s'", param->key);
297 
298 		ctx->max = result.uint_32;
299 		break;
300 	case Opt_stats_mode:
301 		if (!capable(CAP_SYS_ADMIN))
302 			return -EPERM;
303 
304 		ctx->stats_mode = result.uint_32;
305 		break;
306 	default:
307 		return invalfc(fc, "Unsupported parameter '%s'", param->key);
308 	}
309 
310 	return 0;
311 }
312 
binderfs_fs_context_reconfigure(struct fs_context * fc)313 static int binderfs_fs_context_reconfigure(struct fs_context *fc)
314 {
315 	struct binderfs_mount_opts *ctx = fc->fs_private;
316 	struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);
317 
318 	if (info->mount_opts.stats_mode != ctx->stats_mode)
319 		return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");
320 
321 	info->mount_opts.stats_mode = ctx->stats_mode;
322 	info->mount_opts.max = ctx->max;
323 	return 0;
324 }
325 
binderfs_show_options(struct seq_file * seq,struct dentry * root)326 static int binderfs_show_options(struct seq_file *seq, struct dentry *root)
327 {
328 	struct binderfs_info *info = BINDERFS_SB(root->d_sb);
329 
330 	if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
331 		seq_printf(seq, ",max=%d", info->mount_opts.max);
332 
333 	switch (info->mount_opts.stats_mode) {
334 	case binderfs_stats_mode_unset:
335 		break;
336 	case binderfs_stats_mode_global:
337 		seq_printf(seq, ",stats=global");
338 		break;
339 	}
340 
341 	return 0;
342 }
343 
344 static const struct super_operations binderfs_super_ops = {
345 	.evict_inode    = binderfs_evict_inode,
346 	.show_options	= binderfs_show_options,
347 	.statfs         = simple_statfs,
348 };
349 
is_binderfs_control_device(const struct dentry * dentry)350 static inline bool is_binderfs_control_device(const struct dentry *dentry)
351 {
352 	struct binderfs_info *info = dentry->d_sb->s_fs_info;
353 
354 	return info->control_dentry == dentry;
355 }
356 
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)357 static int binderfs_rename(struct mnt_idmap *idmap,
358 			   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(idmap, old_dir, old_dentry, new_dir,
367 			     new_dentry, flags);
368 }
369 
binderfs_unlink(struct inode * dir,struct dentry * dentry)370 static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
371 {
372 	if (is_binderfs_control_device(dentry))
373 		return -EPERM;
374 
375 	return simple_unlink(dir, dentry);
376 }
377 
378 static const struct file_operations binder_ctl_fops = {
379 	.owner		= THIS_MODULE,
380 	.open		= nonseekable_open,
381 	.unlocked_ioctl	= binder_ctl_ioctl,
382 	.compat_ioctl	= binder_ctl_ioctl,
383 	.llseek		= noop_llseek,
384 };
385 
386 /**
387  * binderfs_binder_ctl_create - create a new binder-control device
388  * @sb: super block of the binderfs mount
389  *
390  * This function creates a new binder-control device node in the binderfs mount
391  * referred to by @sb.
392  *
393  * Return: 0 on success, negative errno on failure
394  */
binderfs_binder_ctl_create(struct super_block * sb)395 static int binderfs_binder_ctl_create(struct super_block *sb)
396 {
397 	int minor, ret;
398 	struct dentry *dentry;
399 	struct binder_device *device;
400 	struct inode *inode = NULL;
401 	struct dentry *root = sb->s_root;
402 	struct binderfs_info *info = sb->s_fs_info;
403 #if defined(CONFIG_IPC_NS)
404 	bool use_reserve = (info->ipc_ns == &init_ipc_ns);
405 #else
406 	bool use_reserve = true;
407 #endif
408 
409 	device = kzalloc(sizeof(*device), GFP_KERNEL);
410 	if (!device)
411 		return -ENOMEM;
412 
413 	/* If we have already created a binder-control node, return. */
414 	if (info->control_dentry) {
415 		ret = 0;
416 		goto out;
417 	}
418 
419 	ret = -ENOMEM;
420 	inode = new_inode(sb);
421 	if (!inode)
422 		goto out;
423 
424 	/* Reserve a new minor number for the new device. */
425 	mutex_lock(&binderfs_minors_mutex);
426 	minor = ida_alloc_max(&binderfs_minors,
427 			      use_reserve ? BINDERFS_MAX_MINOR :
428 					    BINDERFS_MAX_MINOR_CAPPED,
429 			      GFP_KERNEL);
430 	mutex_unlock(&binderfs_minors_mutex);
431 	if (minor < 0) {
432 		ret = minor;
433 		goto out;
434 	}
435 
436 	inode->i_ino = SECOND_INODE;
437 	simple_inode_init_ts(inode);
438 	init_special_inode(inode, S_IFCHR | 0600,
439 			   MKDEV(MAJOR(binderfs_dev), minor));
440 	inode->i_fop = &binder_ctl_fops;
441 	inode->i_uid = info->root_uid;
442 	inode->i_gid = info->root_gid;
443 
444 	refcount_set(&device->ref, 1);
445 	device->binderfs_inode = inode;
446 	device->miscdev.minor = minor;
447 
448 	dentry = d_alloc_name(root, "binder-control");
449 	if (!dentry)
450 		goto out;
451 
452 	inode->i_private = device;
453 	info->control_dentry = dentry;
454 	d_add(dentry, inode);
455 
456 	return 0;
457 
458 out:
459 	kfree(device);
460 	iput(inode);
461 
462 	return ret;
463 }
464 
465 static const struct inode_operations binderfs_dir_inode_operations = {
466 	.lookup = simple_lookup,
467 	.rename = binderfs_rename,
468 	.unlink = binderfs_unlink,
469 };
470 
binderfs_make_inode(struct super_block * sb,int mode)471 static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
472 {
473 	struct inode *ret;
474 
475 	ret = new_inode(sb);
476 	if (ret) {
477 		ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
478 		ret->i_mode = mode;
479 		simple_inode_init_ts(ret);
480 	}
481 	return ret;
482 }
483 
binderfs_create_dentry(struct dentry * parent,const char * name)484 static struct dentry *binderfs_create_dentry(struct dentry *parent,
485 					     const char *name)
486 {
487 	struct dentry *dentry;
488 
489 	dentry = lookup_one_len(name, parent, strlen(name));
490 	if (IS_ERR(dentry))
491 		return dentry;
492 
493 	/* Return error if the file/dir already exists. */
494 	if (d_really_is_positive(dentry)) {
495 		dput(dentry);
496 		return ERR_PTR(-EEXIST);
497 	}
498 
499 	return dentry;
500 }
501 
binderfs_remove_file(struct dentry * dentry)502 void binderfs_remove_file(struct dentry *dentry)
503 {
504 	struct inode *parent_inode;
505 
506 	parent_inode = d_inode(dentry->d_parent);
507 	inode_lock(parent_inode);
508 	if (simple_positive(dentry)) {
509 		dget(dentry);
510 		simple_unlink(parent_inode, dentry);
511 		d_delete(dentry);
512 		dput(dentry);
513 	}
514 	inode_unlock(parent_inode);
515 }
516 
binderfs_create_file(struct dentry * parent,const char * name,const struct file_operations * fops,void * data)517 struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
518 				    const struct file_operations *fops,
519 				    void *data)
520 {
521 	struct dentry *dentry;
522 	struct inode *new_inode, *parent_inode;
523 	struct super_block *sb;
524 
525 	parent_inode = d_inode(parent);
526 	inode_lock(parent_inode);
527 
528 	dentry = binderfs_create_dentry(parent, name);
529 	if (IS_ERR(dentry))
530 		goto out;
531 
532 	sb = parent_inode->i_sb;
533 	new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
534 	if (!new_inode) {
535 		dput(dentry);
536 		dentry = ERR_PTR(-ENOMEM);
537 		goto out;
538 	}
539 
540 	new_inode->i_fop = fops;
541 	new_inode->i_private = data;
542 	d_instantiate(dentry, new_inode);
543 	fsnotify_create(parent_inode, dentry);
544 
545 out:
546 	inode_unlock(parent_inode);
547 	return dentry;
548 }
549 
binderfs_create_dir(struct dentry * parent,const char * name)550 static struct dentry *binderfs_create_dir(struct dentry *parent,
551 					  const char *name)
552 {
553 	struct dentry *dentry;
554 	struct inode *new_inode, *parent_inode;
555 	struct super_block *sb;
556 
557 	parent_inode = d_inode(parent);
558 	inode_lock(parent_inode);
559 
560 	dentry = binderfs_create_dentry(parent, name);
561 	if (IS_ERR(dentry))
562 		goto out;
563 
564 	sb = parent_inode->i_sb;
565 	new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
566 	if (!new_inode) {
567 		dput(dentry);
568 		dentry = ERR_PTR(-ENOMEM);
569 		goto out;
570 	}
571 
572 	new_inode->i_fop = &simple_dir_operations;
573 	new_inode->i_op = &simple_dir_inode_operations;
574 
575 	set_nlink(new_inode, 2);
576 	d_instantiate(dentry, new_inode);
577 	inc_nlink(parent_inode);
578 	fsnotify_mkdir(parent_inode, dentry);
579 
580 out:
581 	inode_unlock(parent_inode);
582 	return dentry;
583 }
584 
binder_features_show(struct seq_file * m,void * unused)585 static int binder_features_show(struct seq_file *m, void *unused)
586 {
587 	bool *feature = m->private;
588 
589 	seq_printf(m, "%d\n", *feature);
590 
591 	return 0;
592 }
593 DEFINE_SHOW_ATTRIBUTE(binder_features);
594 
init_binder_features(struct super_block * sb)595 static int init_binder_features(struct super_block *sb)
596 {
597 	struct dentry *dentry, *dir;
598 
599 	dir = binderfs_create_dir(sb->s_root, "features");
600 	if (IS_ERR(dir))
601 		return PTR_ERR(dir);
602 
603 	dentry = binderfs_create_file(dir, "oneway_spam_detection",
604 				      &binder_features_fops,
605 				      &binder_features.oneway_spam_detection);
606 	if (IS_ERR(dentry))
607 		return PTR_ERR(dentry);
608 
609 	dentry = binderfs_create_file(dir, "extended_error",
610 				      &binder_features_fops,
611 				      &binder_features.extended_error);
612 	if (IS_ERR(dentry))
613 		return PTR_ERR(dentry);
614 
615 	dentry = binderfs_create_file(dir, "freeze_notification",
616 				      &binder_features_fops,
617 				      &binder_features.freeze_notification);
618 	if (IS_ERR(dentry))
619 		return PTR_ERR(dentry);
620 
621 	dentry = binderfs_create_file(dir, "transaction_report",
622 				      &binder_features_fops,
623 				      &binder_features.transaction_report);
624 	if (IS_ERR(dentry))
625 		return PTR_ERR(dentry);
626 
627 	return 0;
628 }
629 
init_binder_logs(struct super_block * sb)630 static int init_binder_logs(struct super_block *sb)
631 {
632 	struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
633 	const struct binder_debugfs_entry *db_entry;
634 	struct binderfs_info *info;
635 	int ret = 0;
636 
637 	binder_logs_root_dir = binderfs_create_dir(sb->s_root,
638 						   "binder_logs");
639 	if (IS_ERR(binder_logs_root_dir)) {
640 		ret = PTR_ERR(binder_logs_root_dir);
641 		goto out;
642 	}
643 
644 	binder_for_each_debugfs_entry(db_entry) {
645 		dentry = binderfs_create_file(binder_logs_root_dir,
646 					      db_entry->name,
647 					      db_entry->fops,
648 					      db_entry->data);
649 		if (IS_ERR(dentry)) {
650 			ret = PTR_ERR(dentry);
651 			goto out;
652 		}
653 	}
654 
655 	proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");
656 	if (IS_ERR(proc_log_dir)) {
657 		ret = PTR_ERR(proc_log_dir);
658 		goto out;
659 	}
660 	trace_android_rvh_init_binder_logs(sb);
661 	info = sb->s_fs_info;
662 	info->proc_log_dir = proc_log_dir;
663 
664 out:
665 	return ret;
666 }
667 
binderfs_fill_super(struct super_block * sb,struct fs_context * fc)668 static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)
669 {
670 	int ret;
671 	struct binderfs_info *info;
672 	struct binderfs_mount_opts *ctx = fc->fs_private;
673 	struct inode *inode = NULL;
674 	struct binderfs_device device_info = {};
675 	const char *name;
676 	size_t len;
677 
678 	sb->s_blocksize = PAGE_SIZE;
679 	sb->s_blocksize_bits = PAGE_SHIFT;
680 
681 	/*
682 	 * The binderfs filesystem can be mounted by userns root in a
683 	 * non-initial userns. By default such mounts have the SB_I_NODEV flag
684 	 * set in s_iflags to prevent security issues where userns root can
685 	 * just create random device nodes via mknod() since it owns the
686 	 * filesystem mount. But binderfs does not allow to create any files
687 	 * including devices nodes. The only way to create binder devices nodes
688 	 * is through the binder-control device which userns root is explicitly
689 	 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
690 	 * necessary and safe.
691 	 */
692 	sb->s_iflags &= ~SB_I_NODEV;
693 	sb->s_iflags |= SB_I_NOEXEC;
694 	sb->s_magic = BINDERFS_SUPER_MAGIC;
695 	sb->s_op = &binderfs_super_ops;
696 	sb->s_time_gran = 1;
697 
698 	sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
699 	if (!sb->s_fs_info)
700 		return -ENOMEM;
701 	info = sb->s_fs_info;
702 
703 	info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
704 
705 	info->root_gid = make_kgid(sb->s_user_ns, 0);
706 	if (!gid_valid(info->root_gid))
707 		info->root_gid = GLOBAL_ROOT_GID;
708 	info->root_uid = make_kuid(sb->s_user_ns, 0);
709 	if (!uid_valid(info->root_uid))
710 		info->root_uid = GLOBAL_ROOT_UID;
711 	info->mount_opts.max = ctx->max;
712 	info->mount_opts.stats_mode = ctx->stats_mode;
713 
714 	inode = new_inode(sb);
715 	if (!inode)
716 		return -ENOMEM;
717 
718 	inode->i_ino = FIRST_INODE;
719 	inode->i_fop = &simple_dir_operations;
720 	inode->i_mode = S_IFDIR | 0755;
721 	simple_inode_init_ts(inode);
722 	inode->i_op = &binderfs_dir_inode_operations;
723 	set_nlink(inode, 2);
724 
725 	sb->s_root = d_make_root(inode);
726 	if (!sb->s_root)
727 		return -ENOMEM;
728 
729 	ret = binderfs_binder_ctl_create(sb);
730 	if (ret)
731 		return ret;
732 
733 	name = binder_devices_param;
734 	for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
735 		strscpy(device_info.name, name, len + 1);
736 		ret = binderfs_binder_device_create(inode, NULL, &device_info);
737 		if (ret)
738 			return ret;
739 		name += len;
740 		if (*name == ',')
741 			name++;
742 	}
743 
744 	ret = init_binder_features(sb);
745 	if (ret)
746 		return ret;
747 
748 	if (info->mount_opts.stats_mode == binderfs_stats_mode_global)
749 		return init_binder_logs(sb);
750 
751 	return 0;
752 }
753 
binderfs_fs_context_get_tree(struct fs_context * fc)754 static int binderfs_fs_context_get_tree(struct fs_context *fc)
755 {
756 	return get_tree_nodev(fc, binderfs_fill_super);
757 }
758 
binderfs_fs_context_free(struct fs_context * fc)759 static void binderfs_fs_context_free(struct fs_context *fc)
760 {
761 	struct binderfs_mount_opts *ctx = fc->fs_private;
762 
763 	kfree(ctx);
764 }
765 
766 static const struct fs_context_operations binderfs_fs_context_ops = {
767 	.free		= binderfs_fs_context_free,
768 	.get_tree	= binderfs_fs_context_get_tree,
769 	.parse_param	= binderfs_fs_context_parse_param,
770 	.reconfigure	= binderfs_fs_context_reconfigure,
771 };
772 
binderfs_init_fs_context(struct fs_context * fc)773 static int binderfs_init_fs_context(struct fs_context *fc)
774 {
775 	struct binderfs_mount_opts *ctx;
776 
777 	if (on_binderfs_mount())
778 		return -EINVAL;
779 
780 	ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);
781 	if (!ctx)
782 		return -ENOMEM;
783 
784 	ctx->max = BINDERFS_MAX_MINOR;
785 	ctx->stats_mode = binderfs_stats_mode_unset;
786 
787 	fc->fs_private = ctx;
788 	fc->ops = &binderfs_fs_context_ops;
789 
790 	return 0;
791 }
792 
binderfs_kill_super(struct super_block * sb)793 static void binderfs_kill_super(struct super_block *sb)
794 {
795 	struct binderfs_info *info = sb->s_fs_info;
796 
797 	/*
798 	 * During inode eviction struct binderfs_info is needed.
799 	 * So first wipe the super_block then free struct binderfs_info.
800 	 */
801 	kill_litter_super(sb);
802 
803 	if (info && info->ipc_ns)
804 		put_ipc_ns(info->ipc_ns);
805 
806 	kfree(info);
807 }
808 
809 static struct file_system_type binder_fs_type = {
810 	.name			= "binder",
811 	.init_fs_context	= binderfs_init_fs_context,
812 	.parameters		= binderfs_fs_parameters,
813 	.kill_sb		= binderfs_kill_super,
814 	.fs_flags		= FS_USERNS_MOUNT,
815 };
816 
init_binderfs(void)817 int __init init_binderfs(void)
818 {
819 	int ret;
820 	const char *name;
821 	size_t len;
822 
823 	/* Verify that the default binderfs device names are valid. */
824 	name = binder_devices_param;
825 	for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
826 		if (len > BINDERFS_MAX_NAME)
827 			return -E2BIG;
828 		name += len;
829 		if (*name == ',')
830 			name++;
831 	}
832 
833 	/* Allocate new major number for binderfs. */
834 	ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
835 				  "binder");
836 	if (ret)
837 		return ret;
838 
839 	ret = register_filesystem(&binder_fs_type);
840 	if (ret) {
841 		unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
842 		return ret;
843 	}
844 
845 	return ret;
846 }
847 
unload_binderfs(void)848 void unload_binderfs(void)
849 {
850 	unregister_filesystem(&binder_fs_type);
851 	unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
852 }
853