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