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