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