blob: 5b8ce915c6809eb173f9efc907ac299b52ef9bce [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>
Christian Brauner095cf5022020-03-13 16:34:27 +010021#include <linux/fs_parser.h>
Christian Brauner3ad20fe2018-12-14 13:11:14 +010022#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
Christian Brauner095cf5022020-03-13 16:34:27 +010051enum binderfs_param {
Christian Brauner849d5402019-01-02 12:32:18 +010052 Opt_max,
Hridya Valsarajuf0083452019-09-03 09:16:52 -070053 Opt_stats_mode,
Christian Brauner849d5402019-01-02 12:32:18 +010054};
55
Hridya Valsarajuf0083452019-09-03 09:16:52 -070056enum binderfs_stats_mode {
Christian Brauner095cf5022020-03-13 16:34:27 +010057 binderfs_stats_mode_unset,
58 binderfs_stats_mode_global,
Hridya Valsarajuf0083452019-09-03 09:16:52 -070059};
60
Christian Brauner095cf5022020-03-13 16:34:27 +010061static const struct constant_table binderfs_param_stats[] = {
62 { "global", binderfs_stats_mode_global },
63 {}
Christian Brauner849d5402019-01-02 12:32:18 +010064};
65
Christian Brauner095cf5022020-03-13 16:34:27 +010066const 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
72static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)
Christian Brauner3ad20fe2018-12-14 13:11:14 +010073{
Christian Brauner095cf5022020-03-13 16:34:27 +010074 return sb->s_fs_info;
Christian Brauner3ad20fe2018-12-14 13:11:14 +010075}
76
77bool 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 *
Christian Brauner01b3f1fc2019-01-21 11:48:05 +010092 * This function allocates a new binder_device and reserves a new minor
Christian Brauner3ad20fe2018-12-14 13:11:14 +010093 * 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 */
103static 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;
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100108 struct dentry *dentry, *root;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100109 struct binder_device *device;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100110 char *name = NULL;
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100111 size_t name_len;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100112 struct inode *inode = NULL;
113 struct super_block *sb = ref_inode->i_sb;
114 struct binderfs_info *info = sb->s_fs_info;
Christian Brauner7fefaad2019-01-12 01:06:03 +0100115#if defined(CONFIG_IPC_NS)
Christian Brauner36bdf3c2019-01-11 11:19:40 +0100116 bool use_reserve = (info->ipc_ns == &init_ipc_ns);
Christian Brauner7fefaad2019-01-12 01:06:03 +0100117#else
118 bool use_reserve = true;
119#endif
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100120
121 /* Reserve new minor number for the new device. */
122 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100123 if (++info->device_count <= info->mount_opts.max)
Christian Brauner36bdf3c2019-01-11 11:19:40 +0100124 minor = ida_alloc_max(&binderfs_minors,
125 use_reserve ? BINDERFS_MAX_MINOR :
126 BINDERFS_MAX_MINOR_CAPPED,
Christian Brauner849d5402019-01-02 12:32:18 +0100127 GFP_KERNEL);
128 else
129 minor = -ENOSPC;
130 if (minor < 0) {
131 --info->device_count;
132 mutex_unlock(&binderfs_minors_mutex);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100133 return minor;
Christian Brauner849d5402019-01-02 12:32:18 +0100134 }
135 mutex_unlock(&binderfs_minors_mutex);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100136
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
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100154 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);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100158 if (!name)
159 goto err;
160
Christian Braunerf0fe2c02020-03-03 17:43:40 +0100161 refcount_set(&device->ref, 1);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100162 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
Hridya Valsarajuca2864c2019-09-04 13:07:03 +0200172 if (userp && copy_to_user(userp, req, sizeof(*req))) {
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100173 ret = -EFAULT;
174 goto err;
175 }
176
177 root = sb->s_root;
178 inode_lock(d_inode(root));
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100179
180 /* look it up */
181 dentry = lookup_one_len(name, root, name_len);
182 if (IS_ERR(dentry)) {
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100183 inode_unlock(d_inode(root));
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100184 ret = PTR_ERR(dentry);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100185 goto err;
186 }
187
Christian Brauner01b3f1fc2019-01-21 11:48:05 +0100188 if (d_really_is_positive(dentry)) {
189 /* already exists */
190 dput(dentry);
191 inode_unlock(d_inode(root));
192 ret = -EEXIST;
193 goto err;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100194 }
195
196 inode->i_private = device;
Christian Brauner01684db2019-01-21 11:48:08 +0100197 d_instantiate(dentry, inode);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100198 fsnotify_create(root->d_inode, dentry);
199 inode_unlock(d_inode(root));
200
201 return 0;
202
203err:
204 kfree(name);
205 kfree(device);
206 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100207 --info->device_count;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100208 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 */
225static 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
250static void binderfs_evict_inode(struct inode *inode)
251{
252 struct binder_device *device = inode->i_private;
Christian Brauner095cf5022020-03-13 16:34:27 +0100253 struct binderfs_info *info = BINDERFS_SB(inode->i_sb);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100254
255 clear_inode(inode);
256
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700257 if (!S_ISCHR(inode->i_mode) || !device)
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100258 return;
259
260 mutex_lock(&binderfs_minors_mutex);
Christian Brauner849d5402019-01-02 12:32:18 +0100261 --info->device_count;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100262 ida_free(&binderfs_minors, device->miscdev.minor);
263 mutex_unlock(&binderfs_minors_mutex);
264
Christian Braunerf0fe2c02020-03-03 17:43:40 +0100265 if (refcount_dec_and_test(&device->ref)) {
266 kfree(device->context.name);
267 kfree(device);
268 }
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100269}
270
Christian Brauner095cf5022020-03-13 16:34:27 +0100271static int binderfs_fs_context_parse_param(struct fs_context *fc,
272 struct fs_parameter *param)
Christian Brauner849d5402019-01-02 12:32:18 +0100273{
Christian Brauner095cf5022020-03-13 16:34:27 +0100274 int opt;
275 struct binderfs_mount_opts *ctx = fc->fs_private;
276 struct fs_parse_result result;
Christian Brauner849d5402019-01-02 12:32:18 +0100277
Christian Brauner095cf5022020-03-13 16:34:27 +0100278 opt = fs_parse(fc, binderfs_fs_parameters, param, &result);
279 if (opt < 0)
280 return opt;
Christian Brauner849d5402019-01-02 12:32:18 +0100281
Christian Brauner095cf5022020-03-13 16:34:27 +0100282 switch (opt) {
283 case Opt_max:
284 if (result.uint_32 > BINDERFS_MAX_MINOR)
285 return invalfc(fc, "Bad value for '%s'", param->key);
Christian Brauner849d5402019-01-02 12:32:18 +0100286
Christian Brauner095cf5022020-03-13 16:34:27 +0100287 ctx->max = result.uint_32;
288 break;
289 case Opt_stats_mode:
290 if (!capable(CAP_SYS_ADMIN))
291 return -EPERM;
Christian Brauner849d5402019-01-02 12:32:18 +0100292
Christian Brauner095cf5022020-03-13 16:34:27 +0100293 ctx->stats_mode = result.uint_32;
294 break;
295 default:
296 return invalfc(fc, "Unsupported parameter '%s'", param->key);
Christian Brauner849d5402019-01-02 12:32:18 +0100297 }
298
299 return 0;
300}
301
Christian Brauner095cf5022020-03-13 16:34:27 +0100302static int binderfs_fs_context_reconfigure(struct fs_context *fc)
Christian Brauner849d5402019-01-02 12:32:18 +0100303{
Christian Brauner095cf5022020-03-13 16:34:27 +0100304 struct binderfs_mount_opts *ctx = fc->fs_private;
305 struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700306
Christian Brauner095cf5022020-03-13 16:34:27 +0100307 if (info->mount_opts.stats_mode != ctx->stats_mode)
308 return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700309
Christian Brauner095cf5022020-03-13 16:34:27 +0100310 info->mount_opts.stats_mode = ctx->stats_mode;
311 info->mount_opts.max = ctx->max;
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700312 return 0;
Christian Brauner849d5402019-01-02 12:32:18 +0100313}
314
Christian Brauner095cf5022020-03-13 16:34:27 +0100315static int binderfs_show_options(struct seq_file *seq, struct dentry *root)
Christian Brauner849d5402019-01-02 12:32:18 +0100316{
Christian Brauner095cf5022020-03-13 16:34:27 +0100317 struct binderfs_info *info = BINDERFS_SB(root->d_sb);
Christian Brauner849d5402019-01-02 12:32:18 +0100318
Christian Brauner849d5402019-01-02 12:32:18 +0100319 if (info->mount_opts.max <= BINDERFS_MAX_MINOR)
320 seq_printf(seq, ",max=%d", info->mount_opts.max);
Christian Brauner095cf5022020-03-13 16:34:27 +0100321
322 switch (info->mount_opts.stats_mode) {
323 case binderfs_stats_mode_unset:
324 break;
325 case binderfs_stats_mode_global:
Hridya Valsarajuf0083452019-09-03 09:16:52 -0700326 seq_printf(seq, ",stats=global");
Christian Brauner095cf5022020-03-13 16:34:27 +0100327 break;
328 }
Christian Brauner849d5402019-01-02 12:32:18 +0100329
330 return 0;
331}
332
Christian Brauner095cf5022020-03-13 16:34:27 +0100333static void binderfs_put_super(struct super_block *sb)
334{
335 struct binderfs_info *info = sb->s_fs_info;
336
337 if (info && info->ipc_ns)
338 put_ipc_ns(info->ipc_ns);
339
340 kfree(info);
341 sb->s_fs_info = NULL;
342}
343
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100344static const struct super_operations binderfs_super_ops = {
Christian Brauner849d5402019-01-02 12:32:18 +0100345 .evict_inode = binderfs_evict_inode,
Christian Brauner095cf5022020-03-13 16:34:27 +0100346 .show_options = binderfs_show_options,
Christian Brauner849d5402019-01-02 12:32:18 +0100347 .statfs = simple_statfs,
Christian Brauner095cf5022020-03-13 16:34:27 +0100348 .put_super = binderfs_put_super,
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100349};
350
Christian Braunere98e6fa2019-01-21 11:48:03 +0100351static inline bool is_binderfs_control_device(const struct dentry *dentry)
352{
353 struct binderfs_info *info = dentry->d_sb->s_fs_info;
354 return info->control_dentry == dentry;
355}
356
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100357static int binderfs_rename(struct inode *old_dir, struct dentry *old_dentry,
358 struct inode *new_dir, struct dentry *new_dentry,
359 unsigned int flags)
360{
Christian Braunere98e6fa2019-01-21 11:48:03 +0100361 if (is_binderfs_control_device(old_dentry) ||
362 is_binderfs_control_device(new_dentry))
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100363 return -EPERM;
364
Christian Braunere98e6fa2019-01-21 11:48:03 +0100365 return simple_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100366}
367
368static int binderfs_unlink(struct inode *dir, struct dentry *dentry)
369{
Christian Braunere98e6fa2019-01-21 11:48:03 +0100370 if (is_binderfs_control_device(dentry))
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100371 return -EPERM;
372
373 return simple_unlink(dir, dentry);
374}
375
376static const struct file_operations binder_ctl_fops = {
377 .owner = THIS_MODULE,
378 .open = nonseekable_open,
379 .unlocked_ioctl = binder_ctl_ioctl,
380 .compat_ioctl = binder_ctl_ioctl,
381 .llseek = noop_llseek,
382};
383
384/**
385 * binderfs_binder_ctl_create - create a new binder-control device
386 * @sb: super block of the binderfs mount
387 *
388 * This function creates a new binder-control device node in the binderfs mount
389 * referred to by @sb.
390 *
391 * Return: 0 on success, negative errno on failure
392 */
393static int binderfs_binder_ctl_create(struct super_block *sb)
394{
395 int minor, ret;
396 struct dentry *dentry;
397 struct binder_device *device;
398 struct inode *inode = NULL;
399 struct dentry *root = sb->s_root;
400 struct binderfs_info *info = sb->s_fs_info;
Christian Braunerda8ddba2019-01-23 12:41:15 +0100401#if defined(CONFIG_IPC_NS)
402 bool use_reserve = (info->ipc_ns == &init_ipc_ns);
403#else
404 bool use_reserve = true;
405#endif
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100406
407 device = kzalloc(sizeof(*device), GFP_KERNEL);
408 if (!device)
409 return -ENOMEM;
410
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100411 /* If we have already created a binder-control node, return. */
412 if (info->control_dentry) {
413 ret = 0;
414 goto out;
415 }
416
417 ret = -ENOMEM;
418 inode = new_inode(sb);
419 if (!inode)
420 goto out;
421
422 /* Reserve a new minor number for the new device. */
423 mutex_lock(&binderfs_minors_mutex);
Christian Braunerda8ddba2019-01-23 12:41:15 +0100424 minor = ida_alloc_max(&binderfs_minors,
425 use_reserve ? BINDERFS_MAX_MINOR :
426 BINDERFS_MAX_MINOR_CAPPED,
427 GFP_KERNEL);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100428 mutex_unlock(&binderfs_minors_mutex);
429 if (minor < 0) {
430 ret = minor;
431 goto out;
432 }
433
434 inode->i_ino = SECOND_INODE;
435 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
436 init_special_inode(inode, S_IFCHR | 0600,
437 MKDEV(MAJOR(binderfs_dev), minor));
438 inode->i_fop = &binder_ctl_fops;
439 inode->i_uid = info->root_uid;
440 inode->i_gid = info->root_gid;
441
442 device->binderfs_inode = inode;
443 device->miscdev.minor = minor;
444
445 dentry = d_alloc_name(root, "binder-control");
446 if (!dentry)
447 goto out;
448
449 inode->i_private = device;
450 info->control_dentry = dentry;
451 d_add(dentry, inode);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100452
453 return 0;
454
455out:
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100456 kfree(device);
457 iput(inode);
458
459 return ret;
460}
461
462static const struct inode_operations binderfs_dir_inode_operations = {
463 .lookup = simple_lookup,
464 .rename = binderfs_rename,
465 .unlink = binderfs_unlink,
466};
467
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700468static struct inode *binderfs_make_inode(struct super_block *sb, int mode)
469{
470 struct inode *ret;
471
472 ret = new_inode(sb);
473 if (ret) {
474 ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);
475 ret->i_mode = mode;
476 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
477 }
478 return ret;
479}
480
481static struct dentry *binderfs_create_dentry(struct dentry *parent,
482 const char *name)
483{
484 struct dentry *dentry;
485
486 dentry = lookup_one_len(name, parent, strlen(name));
487 if (IS_ERR(dentry))
488 return dentry;
489
490 /* Return error if the file/dir already exists. */
491 if (d_really_is_positive(dentry)) {
492 dput(dentry);
493 return ERR_PTR(-EEXIST);
494 }
495
496 return dentry;
497}
498
Hridya Valsaraju4feb80f2019-09-03 09:16:55 -0700499void binderfs_remove_file(struct dentry *dentry)
500{
501 struct inode *parent_inode;
502
503 parent_inode = d_inode(dentry->d_parent);
504 inode_lock(parent_inode);
505 if (simple_positive(dentry)) {
506 dget(dentry);
507 simple_unlink(parent_inode, dentry);
508 d_delete(dentry);
509 dput(dentry);
510 }
511 inode_unlock(parent_inode);
512}
513
514struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
515 const struct file_operations *fops,
516 void *data)
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700517{
518 struct dentry *dentry;
519 struct inode *new_inode, *parent_inode;
520 struct super_block *sb;
521
522 parent_inode = d_inode(parent);
523 inode_lock(parent_inode);
524
525 dentry = binderfs_create_dentry(parent, name);
526 if (IS_ERR(dentry))
527 goto out;
528
529 sb = parent_inode->i_sb;
530 new_inode = binderfs_make_inode(sb, S_IFREG | 0444);
531 if (!new_inode) {
532 dput(dentry);
533 dentry = ERR_PTR(-ENOMEM);
534 goto out;
535 }
536
537 new_inode->i_fop = fops;
538 new_inode->i_private = data;
539 d_instantiate(dentry, new_inode);
540 fsnotify_create(parent_inode, dentry);
541
542out:
543 inode_unlock(parent_inode);
544 return dentry;
545}
546
547static struct dentry *binderfs_create_dir(struct dentry *parent,
548 const char *name)
549{
550 struct dentry *dentry;
551 struct inode *new_inode, *parent_inode;
552 struct super_block *sb;
553
554 parent_inode = d_inode(parent);
555 inode_lock(parent_inode);
556
557 dentry = binderfs_create_dentry(parent, name);
558 if (IS_ERR(dentry))
559 goto out;
560
561 sb = parent_inode->i_sb;
562 new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);
563 if (!new_inode) {
564 dput(dentry);
565 dentry = ERR_PTR(-ENOMEM);
566 goto out;
567 }
568
569 new_inode->i_fop = &simple_dir_operations;
570 new_inode->i_op = &simple_dir_inode_operations;
571
572 set_nlink(new_inode, 2);
573 d_instantiate(dentry, new_inode);
574 inc_nlink(parent_inode);
575 fsnotify_mkdir(parent_inode, dentry);
576
577out:
578 inode_unlock(parent_inode);
579 return dentry;
580}
581
582static int init_binder_logs(struct super_block *sb)
583{
Hridya Valsaraju4feb80f2019-09-03 09:16:55 -0700584 struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;
585 struct binderfs_info *info;
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700586 int ret = 0;
587
588 binder_logs_root_dir = binderfs_create_dir(sb->s_root,
589 "binder_logs");
590 if (IS_ERR(binder_logs_root_dir)) {
591 ret = PTR_ERR(binder_logs_root_dir);
592 goto out;
593 }
594
595 dentry = binderfs_create_file(binder_logs_root_dir, "stats",
596 &binder_stats_fops, NULL);
597 if (IS_ERR(dentry)) {
598 ret = PTR_ERR(dentry);
599 goto out;
600 }
601
602 dentry = binderfs_create_file(binder_logs_root_dir, "state",
603 &binder_state_fops, NULL);
604 if (IS_ERR(dentry)) {
605 ret = PTR_ERR(dentry);
606 goto out;
607 }
608
609 dentry = binderfs_create_file(binder_logs_root_dir, "transactions",
610 &binder_transactions_fops, NULL);
Hridya Valsaraju03e2e072019-09-03 09:16:54 -0700611 if (IS_ERR(dentry)) {
612 ret = PTR_ERR(dentry);
613 goto out;
614 }
615
616 dentry = binderfs_create_file(binder_logs_root_dir,
617 "transaction_log",
618 &binder_transaction_log_fops,
619 &binder_transaction_log);
620 if (IS_ERR(dentry)) {
621 ret = PTR_ERR(dentry);
622 goto out;
623 }
624
625 dentry = binderfs_create_file(binder_logs_root_dir,
626 "failed_transaction_log",
627 &binder_transaction_log_fops,
628 &binder_transaction_log_failed);
Hridya Valsaraju4feb80f2019-09-03 09:16:55 -0700629 if (IS_ERR(dentry)) {
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700630 ret = PTR_ERR(dentry);
Hridya Valsaraju4feb80f2019-09-03 09:16:55 -0700631 goto out;
632 }
633
634 proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");
635 if (IS_ERR(proc_log_dir)) {
636 ret = PTR_ERR(proc_log_dir);
637 goto out;
638 }
639 info = sb->s_fs_info;
640 info->proc_log_dir = proc_log_dir;
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700641
642out:
643 return ret;
644}
645
Christian Brauner095cf5022020-03-13 16:34:27 +0100646static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100647{
Christian Brauner36975fc2019-01-21 11:48:04 +0100648 int ret;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100649 struct binderfs_info *info;
Christian Brauner095cf5022020-03-13 16:34:27 +0100650 struct binderfs_mount_opts *ctx = fc->fs_private;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100651 struct inode *inode = NULL;
Hridya Valsarajuca2864c2019-09-04 13:07:03 +0200652 struct binderfs_device device_info = { 0 };
653 const char *name;
654 size_t len;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100655
656 sb->s_blocksize = PAGE_SIZE;
657 sb->s_blocksize_bits = PAGE_SHIFT;
658
659 /*
660 * The binderfs filesystem can be mounted by userns root in a
661 * non-initial userns. By default such mounts have the SB_I_NODEV flag
662 * set in s_iflags to prevent security issues where userns root can
663 * just create random device nodes via mknod() since it owns the
664 * filesystem mount. But binderfs does not allow to create any files
665 * including devices nodes. The only way to create binder devices nodes
666 * is through the binder-control device which userns root is explicitly
667 * allowed to do. So removing the SB_I_NODEV flag from s_iflags is both
668 * necessary and safe.
669 */
670 sb->s_iflags &= ~SB_I_NODEV;
671 sb->s_iflags |= SB_I_NOEXEC;
672 sb->s_magic = BINDERFS_SUPER_MAGIC;
673 sb->s_op = &binderfs_super_ops;
674 sb->s_time_gran = 1;
675
Christian Brauner36975fc2019-01-21 11:48:04 +0100676 sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);
677 if (!sb->s_fs_info)
678 return -ENOMEM;
679 info = sb->s_fs_info;
680
681 info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100682
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100683 info->root_gid = make_kgid(sb->s_user_ns, 0);
684 if (!gid_valid(info->root_gid))
685 info->root_gid = GLOBAL_ROOT_GID;
686 info->root_uid = make_kuid(sb->s_user_ns, 0);
687 if (!uid_valid(info->root_uid))
688 info->root_uid = GLOBAL_ROOT_UID;
Christian Brauner095cf5022020-03-13 16:34:27 +0100689 info->mount_opts.max = ctx->max;
690 info->mount_opts.stats_mode = ctx->stats_mode;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100691
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100692 inode = new_inode(sb);
693 if (!inode)
Christian Brauner36975fc2019-01-21 11:48:04 +0100694 return -ENOMEM;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100695
696 inode->i_ino = FIRST_INODE;
697 inode->i_fop = &simple_dir_operations;
698 inode->i_mode = S_IFDIR | 0755;
699 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
700 inode->i_op = &binderfs_dir_inode_operations;
701 set_nlink(inode, 2);
702
703 sb->s_root = d_make_root(inode);
704 if (!sb->s_root)
Christian Brauner36975fc2019-01-21 11:48:04 +0100705 return -ENOMEM;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100706
Hridya Valsarajuca2864c2019-09-04 13:07:03 +0200707 ret = binderfs_binder_ctl_create(sb);
708 if (ret)
709 return ret;
710
711 name = binder_devices_param;
712 for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
713 strscpy(device_info.name, name, len + 1);
714 ret = binderfs_binder_device_create(inode, NULL, &device_info);
715 if (ret)
716 return ret;
717 name += len;
718 if (*name == ',')
719 name++;
720 }
721
Christian Brauner095cf5022020-03-13 16:34:27 +0100722 if (info->mount_opts.stats_mode == binderfs_stats_mode_global)
Hridya Valsaraju0e13e452019-09-03 09:16:53 -0700723 return init_binder_logs(sb);
724
Hridya Valsarajuca2864c2019-09-04 13:07:03 +0200725 return 0;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100726}
727
Christian Brauner095cf5022020-03-13 16:34:27 +0100728static int binderfs_fs_context_get_tree(struct fs_context *fc)
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100729{
Christian Brauner095cf5022020-03-13 16:34:27 +0100730 return get_tree_nodev(fc, binderfs_fill_super);
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100731}
732
Christian Brauner095cf5022020-03-13 16:34:27 +0100733static void binderfs_fs_context_free(struct fs_context *fc)
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100734{
Christian Brauner095cf5022020-03-13 16:34:27 +0100735 struct binderfs_mount_opts *ctx = fc->fs_private;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100736
Christian Brauner095cf5022020-03-13 16:34:27 +0100737 kfree(ctx);
738}
Christian Brauner41984792019-01-21 11:48:06 +0100739
Christian Brauner095cf5022020-03-13 16:34:27 +0100740static const struct fs_context_operations binderfs_fs_context_ops = {
741 .free = binderfs_fs_context_free,
742 .get_tree = binderfs_fs_context_get_tree,
743 .parse_param = binderfs_fs_context_parse_param,
744 .reconfigure = binderfs_fs_context_reconfigure,
745};
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100746
Christian Brauner095cf5022020-03-13 16:34:27 +0100747static int binderfs_init_fs_context(struct fs_context *fc)
748{
749 struct binderfs_mount_opts *ctx = fc->fs_private;
750
751 ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);
752 if (!ctx)
753 return -ENOMEM;
754
755 ctx->max = BINDERFS_MAX_MINOR;
756 ctx->stats_mode = binderfs_stats_mode_unset;
757
758 fc->fs_private = ctx;
759 fc->ops = &binderfs_fs_context_ops;
760
761 return 0;
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100762}
763
764static struct file_system_type binder_fs_type = {
Christian Brauner095cf5022020-03-13 16:34:27 +0100765 .name = "binder",
766 .init_fs_context = binderfs_init_fs_context,
767 .parameters = binderfs_fs_parameters,
768 .kill_sb = kill_litter_super,
769 .fs_flags = FS_USERNS_MOUNT,
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100770};
771
Christian Brauner5b9633a2019-01-31 01:25:02 +0100772int __init init_binderfs(void)
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100773{
774 int ret;
Hridya Valsaraju028fb582019-09-04 13:07:04 +0200775 const char *name;
776 size_t len;
777
778 /* Verify that the default binderfs device names are valid. */
779 name = binder_devices_param;
780 for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {
781 if (len > BINDERFS_MAX_NAME)
782 return -E2BIG;
783 name += len;
784 if (*name == ',')
785 name++;
786 }
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100787
788 /* Allocate new major number for binderfs. */
789 ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,
790 "binder");
791 if (ret)
792 return ret;
793
794 ret = register_filesystem(&binder_fs_type);
795 if (ret) {
796 unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);
797 return ret;
798 }
799
Christian Brauner3ad20fe2018-12-14 13:11:14 +0100800 return ret;
801}