blob: 89a2ee1a02f6513ea2fd52f3ab30d4f56bb25bd6 [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>
14#include <linux/magic.h>
15#include <linux/major.h>
16#include <linux/miscdevice.h>
17#include <linux/module.h>
18#include <linux/mutex.h>
19#include <linux/mount.h>
20#include <linux/parser.h>
21#include <linux/radix-tree.h>
22#include <linux/sched.h>
Christian Brauner849d5402019-01-02 12:32:18 +010023#include <linux/seq_file.h>
Christian Brauner3ad20fe2018-12-14 13:11:14 +010024#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/asm-generic/errno-base.h>
33#include <uapi/linux/android/binder.h>
Christian Braunerc13295a2019-01-11 00:25:41 +010034#include <uapi/linux/android/binderfs.h>
Christian Brauner3ad20fe2018-12-14 13:11:14 +010035
36#include "binder_internal.h"
37
38#define FIRST_INODE 1
39#define SECOND_INODE 2
40#define INODE_OFFSET 3
41#define INTSTRLEN 21
42#define BINDERFS_MAX_MINOR (1U << MINORBITS)
Christian Brauner36bdf3c2019-01-11 11:19:40 +010043/* Ensure that the initial ipc namespace always has devices available. */
44#define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)
Christian Brauner3ad20fe2018-12-14 13:11:14 +010045
Christian Brauner3ad20fe2018-12-14 13:11:14 +010046static dev_t binderfs_dev;
47static DEFINE_MUTEX(binderfs_minors_mutex);
48static DEFINE_IDA(binderfs_minors);
49
50/**
Christian Brauner849d5402019-01-02 12:32:18 +010051 * binderfs_mount_opts - mount options for binderfs
52 * @max: maximum number of allocatable binderfs binder devices
53 */
54struct binderfs_mount_opts {
55 int max;
56};
57
58enum {
59 Opt_max,
60 Opt_err
61};
62
63static const match_table_t tokens = {
64 { Opt_max, "max=%d" },
65 { Opt_err, NULL }
66};
67
68/**
Christian Brauner3ad20fe2018-12-14 13:11:14 +010069 * binderfs_info - information about a binderfs mount
70 * @ipc_ns: The ipc namespace the binderfs mount belongs to.
71 * @control_dentry: This records the dentry of this binderfs mount
72 * binder-control device.
73 * @root_uid: uid that needs to be used when a new binder device is
74 * created.
75 * @root_gid: gid that needs to be used when a new binder device is
76 * created.
Christian Brauner849d5402019-01-02 12:32:18 +010077 * @mount_opts: The mount options in use.
78 * @device_count: The current number of allocated binder devices.
Christian Brauner3ad20fe2018-12-14 13:11:14 +010079 */
80struct binderfs_info {
81 struct ipc_namespace *ipc_ns;
82 struct dentry *control_dentry;
83 kuid_t root_uid;
84 kgid_t root_gid;
Christian Brauner849d5402019-01-02 12:32:18 +010085 struct binderfs_mount_opts mount_opts;
86 int device_count;
Christian Brauner3ad20fe2018-12-14 13:11:14 +010087};
88
89static inline struct binderfs_info *BINDERFS_I(const struct inode *inode)
90{
91 return inode->i_sb->s_fs_info;
92}
93
94bool is_binderfs_device(const struct inode *inode)
95{
96 if (inode->i_sb->s_magic == BINDERFS_SUPER_MAGIC)
97 return true;
98
99 return false;
100}
101
102/**
103 * binderfs_binder_device_create - allocate inode from super block of a
104 * binderfs mount
105 * @ref_inode: inode from wich the super block will be taken
106 * @userp: buffer to copy information about new device for userspace to
107 * @req: struct binderfs_device as copied from userspace
108 *
109 * This function allocated a new binder_device and reserves a new minor
110 * number for it.
111 * Minor numbers are limited and tracked globally in binderfs_minors. The
112 * function will stash a struct binder_device for the specific binder
113 * device in i_private of the inode.
114 * It will go on to allocate a new inode from the super block of the
115 * filesystem mount, stash a struct binder_device in its i_private field
116 * and attach a dentry to that inode.
117 *
118 * Return: 0 on success, negative errno on failure
119 */
120static int binderfs_binder_device_create(struct inode *ref_inode,
121 struct binderfs_device __user *userp,
122 struct binderfs_device *req)
123{
124 int minor, ret;
125 struct dentry *dentry, *dup, *root;
126 struct binder_device *device;
127 size_t name_len = BINDERFS_MAX_NAME + 1;
128 char *name = NULL;
129 struct inode *inode = NULL;
130 struct super_block *sb = ref_inode->i_sb;
131 struct binderfs_info *info = sb->s_fs_info;
Christian Brauner7fefaad2019-01-12 01:06:03 +0100132#if defined(CONFIG_IPC_NS)
Christian Brauner36bdf3c2019-01-11 11:19:40 +0100133 bool use_reserve = (info->ipc_ns == &init_ipc_ns);
Christian Brauner7fefaad2019-01-12 01:06:03 +0100134#else
135 bool use_reserve = true;
136#endif
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100137
138 /* Reserve new minor number for the new device. */
139 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100140 if (++info->device_count <= info->mount_opts.max)
Christian Brauner36bdf3c2019-01-11 11:19:40 +0100141 minor = ida_alloc_max(&binderfs_minors,
142 use_reserve ? BINDERFS_MAX_MINOR :
143 BINDERFS_MAX_MINOR_CAPPED,
Christian Brauner849d5402019-01-02 12:32:18 +0100144 GFP_KERNEL);
145 else
146 minor = -ENOSPC;
147 if (minor < 0) {
148 --info->device_count;
149 mutex_unlock(&binderfs_minors_mutex);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100150 return minor;
Christian Brauner849d5402019-01-02 12:32:18 +0100151 }
152 mutex_unlock(&binderfs_minors_mutex);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100153
154 ret = -ENOMEM;
155 device = kzalloc(sizeof(*device), GFP_KERNEL);
156 if (!device)
157 goto err;
158
159 inode = new_inode(sb);
160 if (!inode)
161 goto err;
162
163 inode->i_ino = minor + INODE_OFFSET;
164 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
165 init_special_inode(inode, S_IFCHR | 0600,
166 MKDEV(MAJOR(binderfs_dev), minor));
167 inode->i_fop = &binder_fops;
168 inode->i_uid = info->root_uid;
169 inode->i_gid = info->root_gid;
170
171 name = kmalloc(name_len, GFP_KERNEL);
172 if (!name)
173 goto err;
174
175 strscpy(name, req->name, name_len);
176
177 device->binderfs_inode = inode;
178 device->context.binder_context_mgr_uid = INVALID_UID;
179 device->context.name = name;
180 device->miscdev.name = name;
181 device->miscdev.minor = minor;
182 mutex_init(&device->context.context_mgr_node_lock);
183
184 req->major = MAJOR(binderfs_dev);
185 req->minor = minor;
186
187 ret = copy_to_user(userp, req, sizeof(*req));
188 if (ret) {
189 ret = -EFAULT;
190 goto err;
191 }
192
193 root = sb->s_root;
194 inode_lock(d_inode(root));
195 dentry = d_alloc_name(root, name);
196 if (!dentry) {
197 inode_unlock(d_inode(root));
198 ret = -ENOMEM;
199 goto err;
200 }
201
202 /* Verify that the name userspace gave us is not already in use. */
203 dup = d_lookup(root, &dentry->d_name);
204 if (dup) {
205 if (d_really_is_positive(dup)) {
206 dput(dup);
207 dput(dentry);
208 inode_unlock(d_inode(root));
209 ret = -EEXIST;
210 goto err;
211 }
212 dput(dup);
213 }
214
215 inode->i_private = device;
216 d_add(dentry, inode);
217 fsnotify_create(root->d_inode, dentry);
218 inode_unlock(d_inode(root));
219
220 return 0;
221
222err:
223 kfree(name);
224 kfree(device);
225 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100226 --info->device_count;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100227 ida_free(&binderfs_minors, minor);
228 mutex_unlock(&binderfs_minors_mutex);
229 iput(inode);
230
231 return ret;
232}
233
234/**
235 * binderfs_ctl_ioctl - handle binder device node allocation requests
236 *
237 * The request handler for the binder-control device. All requests operate on
238 * the binderfs mount the binder-control device resides in:
239 * - BINDER_CTL_ADD
240 * Allocate a new binder device.
241 *
242 * Return: 0 on success, negative errno on failure
243 */
244static long binder_ctl_ioctl(struct file *file, unsigned int cmd,
245 unsigned long arg)
246{
247 int ret = -EINVAL;
248 struct inode *inode = file_inode(file);
249 struct binderfs_device __user *device = (struct binderfs_device __user *)arg;
250 struct binderfs_device device_req;
251
252 switch (cmd) {
253 case BINDER_CTL_ADD:
254 ret = copy_from_user(&device_req, device, sizeof(device_req));
255 if (ret) {
256 ret = -EFAULT;
257 break;
258 }
259
260 ret = binderfs_binder_device_create(inode, device, &device_req);
261 break;
262 default:
263 break;
264 }
265
266 return ret;
267}
268
269static void binderfs_evict_inode(struct inode *inode)
270{
271 struct binder_device *device = inode->i_private;
Christian Brauner849d5402019-01-02 12:32:18 +0100272 struct binderfs_info *info = BINDERFS_I(inode);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100273
274 clear_inode(inode);
275
276 if (!device)
277 return;
278
279 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100280 --info->device_count;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100281 ida_free(&binderfs_minors, device->miscdev.minor);
282 mutex_unlock(&binderfs_minors_mutex);
283
284 kfree(device->context.name);
285 kfree(device);
286}
287
Christian Brauner849d5402019-01-02 12:32:18 +0100288/**
289 * binderfs_parse_mount_opts - parse binderfs mount options
290 * @data: options to set (can be NULL in which case defaults are used)
291 */
292static int binderfs_parse_mount_opts(char *data,
293 struct binderfs_mount_opts *opts)
294{
295 char *p;
296 opts->max = BINDERFS_MAX_MINOR;
297
298 while ((p = strsep(&data, ",")) != NULL) {
299 substring_t args[MAX_OPT_ARGS];
300 int token;
301 int max_devices;
302
303 if (!*p)
304 continue;
305
306 token = match_token(p, tokens, args);
307 switch (token) {
308 case Opt_max:
309 if (match_int(&args[0], &max_devices) ||
310 (max_devices < 0 ||
311 (max_devices > BINDERFS_MAX_MINOR)))
312 return -EINVAL;
313
314 opts->max = max_devices;
315 break;
316 default:
317 pr_err("Invalid mount options\n");
318 return -EINVAL;
319 }
320 }
321
322 return 0;
323}
324
325static int binderfs_remount(struct super_block *sb, int *flags, char *data)
326{
327 struct binderfs_info *info = sb->s_fs_info;
328 return binderfs_parse_mount_opts(data, &info->mount_opts);
329}
330
331static int binderfs_show_mount_opts(struct seq_file *seq, struct dentry *root)
332{
333 struct binderfs_info *info;
334
335 info = root->d_sb->s_fs_info;
336 if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
337 seq_printf(seq, ",max=%d", info->mount_opts.max);
338
339 return 0;
340}
341
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100342static const struct super_operations binderfs_super_ops = {
Christian Brauner849d5402019-01-02 12:32:18 +0100343 .evict_inode = binderfs_evict_inode,
344 .remount_fs = binderfs_remount,
345 .show_options = binderfs_show_mount_opts,
346 .statfs = simple_statfs,
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100347};
348
Christian Braunere98e6fa2019-01-21 11:48:03 +0100349static inline bool is_binderfs_control_device(const struct dentry *dentry)
350{
351 struct binderfs_info *info = dentry->d_sb->s_fs_info;
352 return info->control_dentry == dentry;
353}
354
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100355static int binderfs_rename(struct inode *old_dir, struct dentry *old_dentry,
356 struct inode *new_dir, struct dentry *new_dentry,
357 unsigned int flags)
358{
Christian Braunere98e6fa2019-01-21 11:48:03 +0100359 if (is_binderfs_control_device(old_dentry) ||
360 is_binderfs_control_device(new_dentry))
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100361 return -EPERM;
362
Christian Braunere98e6fa2019-01-21 11:48:03 +0100363 return simple_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100364}
365
366static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
367{
Christian Braunere98e6fa2019-01-21 11:48:03 +0100368 if (is_binderfs_control_device(dentry))
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100369 return -EPERM;
370
371 return simple_unlink(dir, dentry);
372}
373
374static const struct file_operations binder_ctl_fops = {
375 .owner = THIS_MODULE,
376 .open = nonseekable_open,
377 .unlocked_ioctl = binder_ctl_ioctl,
378 .compat_ioctl = binder_ctl_ioctl,
379 .llseek = noop_llseek,
380};
381
382/**
383 * binderfs_binder_ctl_create - create a new binder-control device
384 * @sb: super block of the binderfs mount
385 *
386 * This function creates a new binder-control device node in the binderfs mount
387 * referred to by @sb.
388 *
389 * Return: 0 on success, negative errno on failure
390 */
391static int binderfs_binder_ctl_create(struct super_block *sb)
392{
393 int minor, ret;
394 struct dentry *dentry;
395 struct binder_device *device;
396 struct inode *inode = NULL;
397 struct dentry *root = sb->s_root;
398 struct binderfs_info *info = sb->s_fs_info;
399
400 device = kzalloc(sizeof(*device), GFP_KERNEL);
401 if (!device)
402 return -ENOMEM;
403
404 inode_lock(d_inode(root));
405
406 /* If we have already created a binder-control node, return. */
407 if (info->control_dentry) {
408 ret = 0;
409 goto out;
410 }
411
412 ret = -ENOMEM;
413 inode = new_inode(sb);
414 if (!inode)
415 goto out;
416
417 /* Reserve a new minor number for the new device. */
418 mutex_lock(&binderfs_minors_mutex);
419 minor = ida_alloc_max(&binderfs_minors, BINDERFS_MAX_MINOR, GFP_KERNEL);
420 mutex_unlock(&binderfs_minors_mutex);
421 if (minor < 0) {
422 ret = minor;
423 goto out;
424 }
425
426 inode->i_ino = SECOND_INODE;
427 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
428 init_special_inode(inode, S_IFCHR | 0600,
429 MKDEV(MAJOR(binderfs_dev), minor));
430 inode->i_fop = &binder_ctl_fops;
431 inode->i_uid = info->root_uid;
432 inode->i_gid = info->root_gid;
433
434 device->binderfs_inode = inode;
435 device->miscdev.minor = minor;
436
437 dentry = d_alloc_name(root, "binder-control");
438 if (!dentry)
439 goto out;
440
441 inode->i_private = device;
442 info->control_dentry = dentry;
443 d_add(dentry, inode);
444 inode_unlock(d_inode(root));
445
446 return 0;
447
448out:
449 inode_unlock(d_inode(root));
450 kfree(device);
451 iput(inode);
452
453 return ret;
454}
455
456static const struct inode_operations binderfs_dir_inode_operations = {
457 .lookup = simple_lookup,
458 .rename = binderfs_rename,
459 .unlink = binderfs_unlink,
460};
461
462static int binderfs_fill_super(struct super_block *sb, void *data, int silent)
463{
Christian Brauner36975fc2019-01-21 11:48:04 +0100464 int ret;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100465 struct binderfs_info *info;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100466 struct inode *inode = NULL;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100467
468 sb->s_blocksize = PAGE_SIZE;
469 sb->s_blocksize_bits = PAGE_SHIFT;
470
471 /*
472 * The binderfs filesystem can be mounted by userns root in a
473 * non-initial userns. By default such mounts have the SB_I_NODEV flag
474 * set in s_iflags to prevent security issues where userns root can
475 * just create random device nodes via mknod() since it owns the
476 * filesystem mount. But binderfs does not allow to create any files
477 * including devices nodes. The only way to create binder devices nodes
478 * is through the binder-control device which userns root is explicitly
479 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
480 * necessary and safe.
481 */
482 sb->s_iflags &= ~SB_I_NODEV;
483 sb->s_iflags |= SB_I_NOEXEC;
484 sb->s_magic = BINDERFS_SUPER_MAGIC;
485 sb->s_op = &binderfs_super_ops;
486 sb->s_time_gran = 1;
487
Christian Brauner36975fc2019-01-21 11:48:04 +0100488 sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
489 if (!sb->s_fs_info)
490 return -ENOMEM;
491 info = sb->s_fs_info;
492
493 info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100494
Christian Brauner849d5402019-01-02 12:32:18 +0100495 ret = binderfs_parse_mount_opts(data, &info->mount_opts);
496 if (ret)
Christian Brauner36975fc2019-01-21 11:48:04 +0100497 return ret;
Christian Brauner849d5402019-01-02 12:32:18 +0100498
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100499 info->root_gid = make_kgid(sb->s_user_ns, 0);
500 if (!gid_valid(info->root_gid))
501 info->root_gid = GLOBAL_ROOT_GID;
502 info->root_uid = make_kuid(sb->s_user_ns, 0);
503 if (!uid_valid(info->root_uid))
504 info->root_uid = GLOBAL_ROOT_UID;
505
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100506 inode = new_inode(sb);
507 if (!inode)
Christian Brauner36975fc2019-01-21 11:48:04 +0100508 return -ENOMEM;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100509
510 inode->i_ino = FIRST_INODE;
511 inode->i_fop = &simple_dir_operations;
512 inode->i_mode = S_IFDIR | 0755;
513 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
514 inode->i_op = &binderfs_dir_inode_operations;
515 set_nlink(inode, 2);
516
517 sb->s_root = d_make_root(inode);
518 if (!sb->s_root)
Christian Brauner36975fc2019-01-21 11:48:04 +0100519 return -ENOMEM;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100520
Christian Brauner36975fc2019-01-21 11:48:04 +0100521 return binderfs_binder_ctl_create(sb);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100522}
523
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100524static struct dentry *binderfs_mount(struct file_system_type *fs_type,
525 int flags, const char *dev_name,
526 void *data)
527{
Christian Braunerb6c770d2019-01-06 15:05:41 +0100528 return mount_nodev(fs_type, flags, data, binderfs_fill_super);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100529}
530
531static void binderfs_kill_super(struct super_block *sb)
532{
533 struct binderfs_info *info = sb->s_fs_info;
534
535 if (info && info->ipc_ns)
536 put_ipc_ns(info->ipc_ns);
537
538 kfree(info);
539 kill_litter_super(sb);
540}
541
542static struct file_system_type binder_fs_type = {
543 .name = "binder",
544 .mount = binderfs_mount,
545 .kill_sb = binderfs_kill_super,
546 .fs_flags = FS_USERNS_MOUNT,
547};
548
549static int __init init_binderfs(void)
550{
551 int ret;
552
553 /* Allocate new major number for binderfs. */
554 ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
555 "binder");
556 if (ret)
557 return ret;
558
559 ret = register_filesystem(&binder_fs_type);
560 if (ret) {
561 unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
562 return ret;
563 }
564
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100565 return ret;
566}
567
568device_initcall(init_binderfs);