blob: f2539b6a4968e59839fd29b9265358083f71086c [file] [log] [blame]
Christian Brauner3ad20fe2018-12-14 13:11:14 +01001/* 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>
Christian Brauner01b3f1fc2019-01-21 11:48:05 +010014#include <linux/namei.h>
Christian Brauner3ad20fe2018-12-14 13:11:14 +010015#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/parser.h>
22#include <linux/radix-tree.h>
23#include <linux/sched.h>
Christian Brauner849d5402019-01-02 12:32:18 +010024#include <linux/seq_file.h>
Christian Brauner3ad20fe2018-12-14 13:11:14 +010025#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>
Christian Braunerc13295a2019-01-11 00:25:41 +010035#include <uapi/linux/android/binderfs.h>
Christian Brauner3ad20fe2018-12-14 13:11:14 +010036
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)
Christian Brauner36bdf3c2019-01-11 11:19:40 +010044/* Ensure that the initial ipc namespace always has devices available. */
45#define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)
Christian Brauner3ad20fe2018-12-14 13:11:14 +010046
Christian Brauner3ad20fe2018-12-14 13:11:14 +010047static dev_t binderfs_dev;
48static DEFINE_MUTEX(binderfs_minors_mutex);
49static DEFINE_IDA(binderfs_minors);
50
51/**
Christian Brauner849d5402019-01-02 12:32:18 +010052 * binderfs_mount_opts - mount options for binderfs
53 * @max: maximum number of allocatable binderfs binder devices
Hridya Valsarajuf0083452019-09-03 09:16:52 -070054 * @stats_mode: enable binder stats in binderfs.
Christian Brauner849d5402019-01-02 12:32:18 +010055 */
56struct binderfs_mount_opts {
57 int max;
Hridya Valsarajuf0083452019-09-03 09:16:52 -070058 int stats_mode;
Christian Brauner849d5402019-01-02 12:32:18 +010059};
60
61enum {
62 Opt_max,
Hridya Valsarajuf0083452019-09-03 09:16:52 -070063 Opt_stats_mode,
Christian Brauner849d5402019-01-02 12:32:18 +010064 Opt_err
65};
66
Hridya Valsarajuf0083452019-09-03 09:16:52 -070067enum binderfs_stats_mode {
68 STATS_NONE,
69 STATS_GLOBAL,
70};
71
Christian Brauner849d5402019-01-02 12:32:18 +010072static const match_table_t tokens = {
73 { Opt_max, "max=%d" },
Hridya Valsarajuf0083452019-09-03 09:16:52 -070074 { Opt_stats_mode, "stats=%s" },
Christian Brauner849d5402019-01-02 12:32:18 +010075 { Opt_err, NULL }
76};
77
78/**
Christian Brauner3ad20fe2018-12-14 13:11:14 +010079 * binderfs_info - information about a binderfs mount
80 * @ipc_ns: The ipc namespace the binderfs mount belongs to.
81 * @control_dentry: This records the dentry of this binderfs mount
82 * binder-control device.
83 * @root_uid: uid that needs to be used when a new binder device is
84 * created.
85 * @root_gid: gid that needs to be used when a new binder device is
86 * created.
Christian Brauner849d5402019-01-02 12:32:18 +010087 * @mount_opts: The mount options in use.
88 * @device_count: The current number of allocated binder devices.
Christian Brauner3ad20fe2018-12-14 13:11:14 +010089 */
90struct binderfs_info {
91 struct ipc_namespace *ipc_ns;
92 struct dentry *control_dentry;
93 kuid_t root_uid;
94 kgid_t root_gid;
Christian Brauner849d5402019-01-02 12:32:18 +010095 struct binderfs_mount_opts mount_opts;
96 int device_count;
Christian Brauner3ad20fe2018-12-14 13:11:14 +010097};
98
99static inline struct binderfs_info *BINDERFS_I(const struct inode *inode)
100{
101 return inode->i_sb->s_fs_info;
102}
103
104bool is_binderfs_device(const struct inode *inode)
105{
106 if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
107 return true;
108
109 return false;
110}
111
112/**
113 * binderfs_binder_device_create - allocate inode from super block of a
114 * binderfs mount
115 * @ref_inode: inode from wich the super block will be taken
116 * @userp: buffer to copy information about new device for userspace to
117 * @req: struct binderfs_device as copied from userspace
118 *
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100119 * This function allocates a new binder_device and reserves a new minor
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100120 * number for it.
121 * Minor numbers are limited and tracked globally in binderfs_minors. The
122 * function will stash a struct binder_device for the specific binder
123 * device in i_private of the inode.
124 * It will go on to allocate a new inode from the super block of the
125 * filesystem mount, stash a struct binder_device in its i_private field
126 * and attach a dentry to that inode.
127 *
128 * Return: 0 on success, negative errno on failure
129 */
130static int binderfs_binder_device_create(struct inode *ref_inode,
131 struct binderfs_device __user *userp,
132 struct binderfs_device *req)
133{
134 int minor, ret;
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100135 struct dentry *dentry, *root;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100136 struct binder_device *device;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100137 char *name = NULL;
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100138 size_t name_len;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100139 struct inode *inode = NULL;
140 struct super_block *sb = ref_inode->i_sb;
141 struct binderfs_info *info = sb->s_fs_info;
Christian Brauner7fefaad2019-01-12 01:06:03 +0100142#if defined(CONFIG_IPC_NS)
Christian Brauner36bdf3c2019-01-11 11:19:40 +0100143 bool use_reserve = (info->ipc_ns == &init_ipc_ns);
Christian Brauner7fefaad2019-01-12 01:06:03 +0100144#else
145 bool use_reserve = true;
146#endif
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100147
148 /* Reserve new minor number for the new device. */
149 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100150 if (++info->device_count <= info->mount_opts.max)
Christian Brauner36bdf3c2019-01-11 11:19:40 +0100151 minor = ida_alloc_max(&binderfs_minors,
152 use_reserve ? BINDERFS_MAX_MINOR :
153 BINDERFS_MAX_MINOR_CAPPED,
Christian Brauner849d5402019-01-02 12:32:18 +0100154 GFP_KERNEL);
155 else
156 minor = -ENOSPC;
157 if (minor < 0) {
158 --info->device_count;
159 mutex_unlock(&binderfs_minors_mutex);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100160 return minor;
Christian Brauner849d5402019-01-02 12:32:18 +0100161 }
162 mutex_unlock(&binderfs_minors_mutex);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100163
164 ret = -ENOMEM;
165 device = kzalloc(sizeof(*device), GFP_KERNEL);
166 if (!device)
167 goto err;
168
169 inode = new_inode(sb);
170 if (!inode)
171 goto err;
172
173 inode->i_ino = minor + INODE_OFFSET;
174 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
175 init_special_inode(inode, S_IFCHR | 0600,
176 MKDEV(MAJOR(binderfs_dev), minor));
177 inode->i_fop = &binder_fops;
178 inode->i_uid = info->root_uid;
179 inode->i_gid = info->root_gid;
180
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100181 req->name[BINDERFS_MAX_NAME] = '\0'; /* NUL-terminate */
182 name_len = strlen(req->name);
183 /* Make sure to include terminating NUL byte */
184 name = kmemdup(req->name, name_len + 1, GFP_KERNEL);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100185 if (!name)
186 goto err;
187
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100188 device->binderfs_inode = inode;
189 device->context.binder_context_mgr_uid = INVALID_UID;
190 device->context.name = name;
191 device->miscdev.name = name;
192 device->miscdev.minor = minor;
193 mutex_init(&device->context.context_mgr_node_lock);
194
195 req->major = MAJOR(binderfs_dev);
196 req->minor = minor;
197
Hridya Valsarajuca2864c2019-09-04 13:07:03 +0200198 if (userp && copy_to_user(userp, req, sizeof(*req))) {
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100199 ret = -EFAULT;
200 goto err;
201 }
202
203 root = sb->s_root;
204 inode_lock(d_inode(root));
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100205
206 /* look it up */
207 dentry = lookup_one_len(name, root, name_len);
208 if (IS_ERR(dentry)) {
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100209 inode_unlock(d_inode(root));
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100210 ret = PTR_ERR(dentry);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100211 goto err;
212 }
213
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100214 if (d_really_is_positive(dentry)) {
215 /* already exists */
216 dput(dentry);
217 inode_unlock(d_inode(root));
218 ret = -EEXIST;
219 goto err;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100220 }
221
222 inode->i_private = device;
Christian Brauner01684db2019-01-21 11:48:08 +0100223 d_instantiate(dentry, inode);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100224 fsnotify_create(root->d_inode, dentry);
225 inode_unlock(d_inode(root));
226
227 return 0;
228
229err:
230 kfree(name);
231 kfree(device);
232 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100233 --info->device_count;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100234 ida_free(&binderfs_minors, minor);
235 mutex_unlock(&binderfs_minors_mutex);
236 iput(inode);
237
238 return ret;
239}
240
241/**
242 * binderfs_ctl_ioctl - handle binder device node allocation requests
243 *
244 * The request handler for the binder-control device. All requests operate on
245 * the binderfs mount the binder-control device resides in:
246 * - BINDER_CTL_ADD
247 * Allocate a new binder device.
248 *
249 * Return: 0 on success, negative errno on failure
250 */
251static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
252 unsigned long arg)
253{
254 int ret = -EINVAL;
255 struct inode *inode = file_inode(file);
256 struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
257 struct binderfs_device device_req;
258
259 switch (cmd) {
260 case BINDER_CTL_ADD:
261 ret = copy_from_user(&device_req, device, sizeof(device_req));
262 if (ret) {
263 ret = -EFAULT;
264 break;
265 }
266
267 ret = binderfs_binder_device_create(inode, device, &device_req);
268 break;
269 default:
270 break;
271 }
272
273 return ret;
274}
275
276static void binderfs_evict_inode(struct inode *inode)
277{
278 struct binder_device *device = inode->i_private;
Christian Brauner849d5402019-01-02 12:32:18 +0100279 struct binderfs_info *info = BINDERFS_I(inode);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100280
281 clear_inode(inode);
282
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700283 if (!S_ISCHR(inode->i_mode) || !device)
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100284 return;
285
286 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100287 --info->device_count;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100288 ida_free(&binderfs_minors, device->miscdev.minor);
289 mutex_unlock(&binderfs_minors_mutex);
290
291 kfree(device->context.name);
292 kfree(device);
293}
294
Christian Brauner849d5402019-01-02 12:32:18 +0100295/**
296 * binderfs_parse_mount_opts - parse binderfs mount options
297 * @data: options to set (can be NULL in which case defaults are used)
298 */
299static int binderfs_parse_mount_opts(char *data,
300 struct binderfs_mount_opts *opts)
301{
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700302 char *p, *stats;
Christian Brauner849d5402019-01-02 12:32:18 +0100303 opts->max = BINDERFS_MAX_MINOR;
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700304 opts->stats_mode = STATS_NONE;
Christian Brauner849d5402019-01-02 12:32:18 +0100305
306 while ((p = strsep(&data, ",")) != NULL) {
307 substring_t args[MAX_OPT_ARGS];
308 int token;
309 int max_devices;
310
311 if (!*p)
312 continue;
313
314 token = match_token(p, tokens, args);
315 switch (token) {
316 case Opt_max:
317 if (match_int(&args[0], &max_devices) ||
318 (max_devices < 0 ||
319 (max_devices > BINDERFS_MAX_MINOR)))
320 return -EINVAL;
321
322 opts->max = max_devices;
323 break;
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700324 case Opt_stats_mode:
325 if (!capable(CAP_SYS_ADMIN))
326 return -EINVAL;
327
328 stats = match_strdup(&args[0]);
329 if (!stats)
330 return -ENOMEM;
331
332 if (strcmp(stats, "global") != 0) {
333 kfree(stats);
334 return -EINVAL;
335 }
336
337 opts->stats_mode = STATS_GLOBAL;
338 kfree(stats);
339 break;
Christian Brauner849d5402019-01-02 12:32:18 +0100340 default:
341 pr_err("Invalid mount options\n");
342 return -EINVAL;
343 }
344 }
345
346 return 0;
347}
348
349static int binderfs_remount(struct super_block *sb, int *flags, char *data)
350{
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700351 int prev_stats_mode, ret;
Christian Brauner849d5402019-01-02 12:32:18 +0100352 struct binderfs_info *info = sb->s_fs_info;
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700353
354 prev_stats_mode = info->mount_opts.stats_mode;
355 ret = binderfs_parse_mount_opts(data, &info->mount_opts);
356 if (ret)
357 return ret;
358
359 if (prev_stats_mode != info->mount_opts.stats_mode) {
360 pr_err("Binderfs stats mode cannot be changed during a remount\n");
361 info->mount_opts.stats_mode = prev_stats_mode;
362 return -EINVAL;
363 }
364
365 return 0;
Christian Brauner849d5402019-01-02 12:32:18 +0100366}
367
368static int binderfs_show_mount_opts(struct seq_file *seq, struct dentry *root)
369{
370 struct binderfs_info *info;
371
372 info = root->d_sb->s_fs_info;
373 if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
374 seq_printf(seq, ",max=%d", info->mount_opts.max);
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700375 if (info->mount_opts.stats_mode == STATS_GLOBAL)
376 seq_printf(seq, ",stats=global");
Christian Brauner849d5402019-01-02 12:32:18 +0100377
378 return 0;
379}
380
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100381static const struct super_operations binderfs_super_ops = {
Christian Brauner849d5402019-01-02 12:32:18 +0100382 .evict_inode = binderfs_evict_inode,
383 .remount_fs = binderfs_remount,
384 .show_options = binderfs_show_mount_opts,
385 .statfs = simple_statfs,
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100386};
387
Christian Braunere98e6fa2019-01-21 11:48:03 +0100388static inline bool is_binderfs_control_device(const struct dentry *dentry)
389{
390 struct binderfs_info *info = dentry->d_sb->s_fs_info;
391 return info->control_dentry == dentry;
392}
393
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100394static int binderfs_rename(struct inode *old_dir, struct dentry *old_dentry,
395 struct inode *new_dir, struct dentry *new_dentry,
396 unsigned int flags)
397{
Christian Braunere98e6fa2019-01-21 11:48:03 +0100398 if (is_binderfs_control_device(old_dentry) ||
399 is_binderfs_control_device(new_dentry))
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100400 return -EPERM;
401
Christian Braunere98e6fa2019-01-21 11:48:03 +0100402 return simple_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100403}
404
405static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
406{
Christian Braunere98e6fa2019-01-21 11:48:03 +0100407 if (is_binderfs_control_device(dentry))
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100408 return -EPERM;
409
410 return simple_unlink(dir, dentry);
411}
412
413static const struct file_operations binder_ctl_fops = {
414 .owner = THIS_MODULE,
415 .open = nonseekable_open,
416 .unlocked_ioctl = binder_ctl_ioctl,
417 .compat_ioctl = binder_ctl_ioctl,
418 .llseek = noop_llseek,
419};
420
421/**
422 * binderfs_binder_ctl_create - create a new binder-control device
423 * @sb: super block of the binderfs mount
424 *
425 * This function creates a new binder-control device node in the binderfs mount
426 * referred to by @sb.
427 *
428 * Return: 0 on success, negative errno on failure
429 */
430static int binderfs_binder_ctl_create(struct super_block *sb)
431{
432 int minor, ret;
433 struct dentry *dentry;
434 struct binder_device *device;
435 struct inode *inode = NULL;
436 struct dentry *root = sb->s_root;
437 struct binderfs_info *info = sb->s_fs_info;
Christian Braunerda8ddba2019-01-23 12:41:15 +0100438#if defined(CONFIG_IPC_NS)
439 bool use_reserve = (info->ipc_ns == &init_ipc_ns);
440#else
441 bool use_reserve = true;
442#endif
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100443
444 device = kzalloc(sizeof(*device), GFP_KERNEL);
445 if (!device)
446 return -ENOMEM;
447
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100448 /* If we have already created a binder-control node, return. */
449 if (info->control_dentry) {
450 ret = 0;
451 goto out;
452 }
453
454 ret = -ENOMEM;
455 inode = new_inode(sb);
456 if (!inode)
457 goto out;
458
459 /* Reserve a new minor number for the new device. */
460 mutex_lock(&binderfs_minors_mutex);
Christian Braunerda8ddba2019-01-23 12:41:15 +0100461 minor = ida_alloc_max(&binderfs_minors,
462 use_reserve ? BINDERFS_MAX_MINOR :
463 BINDERFS_MAX_MINOR_CAPPED,
464 GFP_KERNEL);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100465 mutex_unlock(&binderfs_minors_mutex);
466 if (minor < 0) {
467 ret = minor;
468 goto out;
469 }
470
471 inode->i_ino = SECOND_INODE;
472 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
473 init_special_inode(inode, S_IFCHR | 0600,
474 MKDEV(MAJOR(binderfs_dev), minor));
475 inode->i_fop = &binder_ctl_fops;
476 inode->i_uid = info->root_uid;
477 inode->i_gid = info->root_gid;
478
479 device->binderfs_inode = inode;
480 device->miscdev.minor = minor;
481
482 dentry = d_alloc_name(root, "binder-control");
483 if (!dentry)
484 goto out;
485
486 inode->i_private = device;
487 info->control_dentry = dentry;
488 d_add(dentry, inode);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100489
490 return 0;
491
492out:
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100493 kfree(device);
494 iput(inode);
495
496 return ret;
497}
498
499static const struct inode_operations binderfs_dir_inode_operations = {
500 .lookup = simple_lookup,
501 .rename = binderfs_rename,
502 .unlink = binderfs_unlink,
503};
504
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700505static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
506{
507 struct inode *ret;
508
509 ret = new_inode(sb);
510 if (ret) {
511 ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
512 ret->i_mode = mode;
513 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
514 }
515 return ret;
516}
517
518static struct dentry *binderfs_create_dentry(struct dentry *parent,
519 const char *name)
520{
521 struct dentry *dentry;
522
523 dentry = lookup_one_len(name, parent, strlen(name));
524 if (IS_ERR(dentry))
525 return dentry;
526
527 /* Return error if the file/dir already exists. */
528 if (d_really_is_positive(dentry)) {
529 dput(dentry);
530 return ERR_PTR(-EEXIST);
531 }
532
533 return dentry;
534}
535
536static struct dentry *binderfs_create_file(struct dentry *parent,
537 const char *name,
538 const struct file_operations *fops,
539 void *data)
540{
541 struct dentry *dentry;
542 struct inode *new_inode, *parent_inode;
543 struct super_block *sb;
544
545 parent_inode = d_inode(parent);
546 inode_lock(parent_inode);
547
548 dentry = binderfs_create_dentry(parent, name);
549 if (IS_ERR(dentry))
550 goto out;
551
552 sb = parent_inode->i_sb;
553 new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
554 if (!new_inode) {
555 dput(dentry);
556 dentry = ERR_PTR(-ENOMEM);
557 goto out;
558 }
559
560 new_inode->i_fop = fops;
561 new_inode->i_private = data;
562 d_instantiate(dentry, new_inode);
563 fsnotify_create(parent_inode, dentry);
564
565out:
566 inode_unlock(parent_inode);
567 return dentry;
568}
569
570static struct dentry *binderfs_create_dir(struct dentry *parent,
571 const char *name)
572{
573 struct dentry *dentry;
574 struct inode *new_inode, *parent_inode;
575 struct super_block *sb;
576
577 parent_inode = d_inode(parent);
578 inode_lock(parent_inode);
579
580 dentry = binderfs_create_dentry(parent, name);
581 if (IS_ERR(dentry))
582 goto out;
583
584 sb = parent_inode->i_sb;
585 new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
586 if (!new_inode) {
587 dput(dentry);
588 dentry = ERR_PTR(-ENOMEM);
589 goto out;
590 }
591
592 new_inode->i_fop = &simple_dir_operations;
593 new_inode->i_op = &simple_dir_inode_operations;
594
595 set_nlink(new_inode, 2);
596 d_instantiate(dentry, new_inode);
597 inc_nlink(parent_inode);
598 fsnotify_mkdir(parent_inode, dentry);
599
600out:
601 inode_unlock(parent_inode);
602 return dentry;
603}
604
605static int init_binder_logs(struct super_block *sb)
606{
607 struct dentry *binder_logs_root_dir, *dentry;
608 int ret = 0;
609
610 binder_logs_root_dir = binderfs_create_dir(sb->s_root,
611 "binder_logs");
612 if (IS_ERR(binder_logs_root_dir)) {
613 ret = PTR_ERR(binder_logs_root_dir);
614 goto out;
615 }
616
617 dentry = binderfs_create_file(binder_logs_root_dir, "stats",
618 &binder_stats_fops, NULL);
619 if (IS_ERR(dentry)) {
620 ret = PTR_ERR(dentry);
621 goto out;
622 }
623
624 dentry = binderfs_create_file(binder_logs_root_dir, "state",
625 &binder_state_fops, NULL);
626 if (IS_ERR(dentry)) {
627 ret = PTR_ERR(dentry);
628 goto out;
629 }
630
631 dentry = binderfs_create_file(binder_logs_root_dir, "transactions",
632 &binder_transactions_fops, NULL);
633 if (IS_ERR(dentry))
634 ret = PTR_ERR(dentry);
635
636out:
637 return ret;
638}
639
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100640static int binderfs_fill_super(struct super_block *sb, void *data, int silent)
641{
Christian Brauner36975fc2019-01-21 11:48:04 +0100642 int ret;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100643 struct binderfs_info *info;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100644 struct inode *inode = NULL;
Hridya Valsarajuca2864c2019-09-04 13:07:03 +0200645 struct binderfs_device device_info = { 0 };
646 const char *name;
647 size_t len;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100648
649 sb->s_blocksize = PAGE_SIZE;
650 sb->s_blocksize_bits = PAGE_SHIFT;
651
652 /*
653 * The binderfs filesystem can be mounted by userns root in a
654 * non-initial userns. By default such mounts have the SB_I_NODEV flag
655 * set in s_iflags to prevent security issues where userns root can
656 * just create random device nodes via mknod() since it owns the
657 * filesystem mount. But binderfs does not allow to create any files
658 * including devices nodes. The only way to create binder devices nodes
659 * is through the binder-control device which userns root is explicitly
660 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
661 * necessary and safe.
662 */
663 sb->s_iflags &= ~SB_I_NODEV;
664 sb->s_iflags |= SB_I_NOEXEC;
665 sb->s_magic = BINDERFS_SUPER_MAGIC;
666 sb->s_op = &binderfs_super_ops;
667 sb->s_time_gran = 1;
668
Christian Brauner36975fc2019-01-21 11:48:04 +0100669 sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
670 if (!sb->s_fs_info)
671 return -ENOMEM;
672 info = sb->s_fs_info;
673
674 info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100675
Christian Brauner849d5402019-01-02 12:32:18 +0100676 ret = binderfs_parse_mount_opts(data, &info->mount_opts);
677 if (ret)
Christian Brauner36975fc2019-01-21 11:48:04 +0100678 return ret;
Christian Brauner849d5402019-01-02 12:32:18 +0100679
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100680 info->root_gid = make_kgid(sb->s_user_ns, 0);
681 if (!gid_valid(info->root_gid))
682 info->root_gid = GLOBAL_ROOT_GID;
683 info->root_uid = make_kuid(sb->s_user_ns, 0);
684 if (!uid_valid(info->root_uid))
685 info->root_uid = GLOBAL_ROOT_UID;
686
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100687 inode = new_inode(sb);
688 if (!inode)
Christian Brauner36975fc2019-01-21 11:48:04 +0100689 return -ENOMEM;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100690
691 inode->i_ino = FIRST_INODE;
692 inode->i_fop = &simple_dir_operations;
693 inode->i_mode = S_IFDIR | 0755;
694 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
695 inode->i_op = &binderfs_dir_inode_operations;
696 set_nlink(inode, 2);
697
698 sb->s_root = d_make_root(inode);
699 if (!sb->s_root)
Christian Brauner36975fc2019-01-21 11:48:04 +0100700 return -ENOMEM;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100701
Hridya Valsarajuca2864c2019-09-04 13:07:03 +0200702 ret = binderfs_binder_ctl_create(sb);
703 if (ret)
704 return ret;
705
706 name = binder_devices_param;
707 for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
708 strscpy(device_info.name, name, len + 1);
709 ret = binderfs_binder_device_create(inode, NULL, &device_info);
710 if (ret)
711 return ret;
712 name += len;
713 if (*name == ',')
714 name++;
715 }
716
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700717 if (info->mount_opts.stats_mode == STATS_GLOBAL)
718 return init_binder_logs(sb);
719
Hridya Valsarajuca2864c2019-09-04 13:07:03 +0200720 return 0;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100721}
722
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100723static struct dentry *binderfs_mount(struct file_system_type *fs_type,
724 int flags, const char *dev_name,
725 void *data)
726{
Christian Braunerb6c770d2019-01-06 15:05:41 +0100727 return mount_nodev(fs_type, flags, data, binderfs_fill_super);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100728}
729
730static void binderfs_kill_super(struct super_block *sb)
731{
732 struct binderfs_info *info = sb->s_fs_info;
733
Christian Brauner41984792019-01-21 11:48:06 +0100734 kill_litter_super(sb);
735
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100736 if (info && info->ipc_ns)
737 put_ipc_ns(info->ipc_ns);
738
739 kfree(info);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100740}
741
742static struct file_system_type binder_fs_type = {
743 .name = "binder",
744 .mount = binderfs_mount,
745 .kill_sb = binderfs_kill_super,
746 .fs_flags = FS_USERNS_MOUNT,
747};
748
Christian Brauner5b9633a2019-01-31 01:25:02 +0100749int __init init_binderfs(void)
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100750{
751 int ret;
Hridya Valsaraju028fb582019-09-04 13:07:04 +0200752 const char *name;
753 size_t len;
754
755 /* Verify that the default binderfs device names are valid. */
756 name = binder_devices_param;
757 for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
758 if (len > BINDERFS_MAX_NAME)
759 return -E2BIG;
760 name += len;
761 if (*name == ',')
762 name++;
763 }
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100764
765 /* Allocate new major number for binderfs. */
766 ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
767 "binder");
768 if (ret)
769 return ret;
770
771 ret = register_filesystem(&binder_fs_type);
772 if (ret) {
773 unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
774 return ret;
775 }
776
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100777 return ret;
778}