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