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