Path: blob/master/drivers/android/binder/rust_binderfs.c
49643 views
// SPDX-License-Identifier: GPL-2.012#include <linux/compiler_types.h>3#include <linux/errno.h>4#include <linux/fs.h>5#include <linux/fsnotify.h>6#include <linux/gfp.h>7#include <linux/idr.h>8#include <linux/init.h>9#include <linux/ipc_namespace.h>10#include <linux/kdev_t.h>11#include <linux/kernel.h>12#include <linux/list.h>13#include <linux/namei.h>14#include <linux/magic.h>15#include <linux/major.h>16#include <linux/miscdevice.h>17#include <linux/module.h>18#include <linux/mutex.h>19#include <linux/mount.h>20#include <linux/fs_parser.h>21#include <linux/sched.h>22#include <linux/seq_file.h>23#include <linux/slab.h>24#include <linux/spinlock_types.h>25#include <linux/stddef.h>26#include <linux/string.h>27#include <linux/types.h>28#include <linux/uaccess.h>29#include <linux/user_namespace.h>30#include <linux/xarray.h>31#include <uapi/asm-generic/errno-base.h>32#include <uapi/linux/android/binder.h>33#include <uapi/linux/android/binderfs.h>3435#include "rust_binder.h"36#include "rust_binder_internal.h"3738#define FIRST_INODE 139#define SECOND_INODE 240#define INODE_OFFSET 341#define BINDERFS_MAX_MINOR (1U << MINORBITS)42/* Ensure that the initial ipc namespace always has devices available. */43#define BINDERFS_MAX_MINOR_CAPPED (BINDERFS_MAX_MINOR - 4)4445DEFINE_SHOW_ATTRIBUTE(rust_binder_stats);46DEFINE_SHOW_ATTRIBUTE(rust_binder_state);47DEFINE_SHOW_ATTRIBUTE(rust_binder_transactions);48DEFINE_SHOW_ATTRIBUTE(rust_binder_proc);4950char *rust_binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;51module_param_named(rust_devices, rust_binder_devices_param, charp, 0444);5253static dev_t binderfs_dev;54static DEFINE_MUTEX(binderfs_minors_mutex);55static DEFINE_IDA(binderfs_minors);5657enum binderfs_param {58Opt_max,59Opt_stats_mode,60};6162enum binderfs_stats_mode {63binderfs_stats_mode_unset,64binderfs_stats_mode_global,65};6667struct binder_features {68bool oneway_spam_detection;69bool extended_error;70bool freeze_notification;71};7273static const struct constant_table binderfs_param_stats[] = {74{ "global", binderfs_stats_mode_global },75{}76};7778static const struct fs_parameter_spec binderfs_fs_parameters[] = {79fsparam_u32("max", Opt_max),80fsparam_enum("stats", Opt_stats_mode, binderfs_param_stats),81{}82};8384static struct binder_features binder_features = {85.oneway_spam_detection = true,86.extended_error = true,87.freeze_notification = true,88};8990static inline struct binderfs_info *BINDERFS_SB(const struct super_block *sb)91{92return sb->s_fs_info;93}9495/**96* binderfs_binder_device_create - allocate inode from super block of a97* binderfs mount98* @ref_inode: inode from wich the super block will be taken99* @userp: buffer to copy information about new device for userspace to100* @req: struct binderfs_device as copied from userspace101*102* This function allocates a new binder_device and reserves a new minor103* number for it.104* Minor numbers are limited and tracked globally in binderfs_minors. The105* function will stash a struct binder_device for the specific binder106* device in i_private of the inode.107* It will go on to allocate a new inode from the super block of the108* filesystem mount, stash a struct binder_device in its i_private field109* and attach a dentry to that inode.110*111* Return: 0 on success, negative errno on failure112*/113static int binderfs_binder_device_create(struct inode *ref_inode,114struct binderfs_device __user *userp,115struct binderfs_device *req)116{117int minor, ret;118struct dentry *dentry, *root;119struct binder_device *device = NULL;120rust_binder_context ctx = NULL;121struct inode *inode = NULL;122struct super_block *sb = ref_inode->i_sb;123struct binderfs_info *info = sb->s_fs_info;124#if defined(CONFIG_IPC_NS)125bool use_reserve = (info->ipc_ns == &init_ipc_ns);126#else127bool use_reserve = true;128#endif129130/* Reserve new minor number for the new device. */131mutex_lock(&binderfs_minors_mutex);132if (++info->device_count <= info->mount_opts.max)133minor = ida_alloc_max(&binderfs_minors,134use_reserve ? BINDERFS_MAX_MINOR - 1 :135BINDERFS_MAX_MINOR_CAPPED - 1,136GFP_KERNEL);137else138minor = -ENOSPC;139if (minor < 0) {140--info->device_count;141mutex_unlock(&binderfs_minors_mutex);142return minor;143}144mutex_unlock(&binderfs_minors_mutex);145146ret = -ENOMEM;147device = kzalloc(sizeof(*device), GFP_KERNEL);148if (!device)149goto err;150151req->name[BINDERFS_MAX_NAME] = '\0'; /* NUL-terminate */152153ctx = rust_binder_new_context(req->name);154if (!ctx)155goto err;156157inode = new_inode(sb);158if (!inode)159goto err;160161inode->i_ino = minor + INODE_OFFSET;162simple_inode_init_ts(inode);163init_special_inode(inode, S_IFCHR | 0600,164MKDEV(MAJOR(binderfs_dev), minor));165inode->i_fop = &rust_binder_fops;166inode->i_uid = info->root_uid;167inode->i_gid = info->root_gid;168169req->major = MAJOR(binderfs_dev);170req->minor = minor;171device->ctx = ctx;172device->minor = minor;173174if (userp && copy_to_user(userp, req, sizeof(*req))) {175ret = -EFAULT;176goto err;177}178179root = sb->s_root;180dentry = simple_start_creating(root, req->name);181if (IS_ERR(dentry)) {182ret = PTR_ERR(dentry);183goto err;184}185186inode->i_private = device;187d_make_persistent(dentry, inode);188189fsnotify_create(root->d_inode, dentry);190simple_done_creating(dentry);191192return 0;193194err:195kfree(device);196rust_binder_remove_context(ctx);197mutex_lock(&binderfs_minors_mutex);198--info->device_count;199ida_free(&binderfs_minors, minor);200mutex_unlock(&binderfs_minors_mutex);201iput(inode);202203return ret;204}205206/**207* binder_ctl_ioctl - handle binder device node allocation requests208*209* The request handler for the binder-control device. All requests operate on210* the binderfs mount the binder-control device resides in:211* - BINDER_CTL_ADD212* Allocate a new binder device.213*214* Return: %0 on success, negative errno on failure.215*/216static long binder_ctl_ioctl(struct file *file, unsigned int cmd,217unsigned long arg)218{219int ret = -EINVAL;220struct inode *inode = file_inode(file);221struct binderfs_device __user *device = (struct binderfs_device __user *)arg;222struct binderfs_device device_req;223224switch (cmd) {225case BINDER_CTL_ADD:226ret = copy_from_user(&device_req, device, sizeof(device_req));227if (ret) {228ret = -EFAULT;229break;230}231232ret = binderfs_binder_device_create(inode, device, &device_req);233break;234default:235break;236}237238return ret;239}240241static void binderfs_evict_inode(struct inode *inode)242{243struct binder_device *device = inode->i_private;244struct binderfs_info *info = BINDERFS_SB(inode->i_sb);245246clear_inode(inode);247248if (!S_ISCHR(inode->i_mode) || !device)249return;250251mutex_lock(&binderfs_minors_mutex);252--info->device_count;253ida_free(&binderfs_minors, device->minor);254mutex_unlock(&binderfs_minors_mutex);255256/* ctx is null for binder-control, but this function ignores null pointers */257rust_binder_remove_context(device->ctx);258259kfree(device);260}261262static int binderfs_fs_context_parse_param(struct fs_context *fc,263struct fs_parameter *param)264{265int opt;266struct binderfs_mount_opts *ctx = fc->fs_private;267struct fs_parse_result result;268269opt = fs_parse(fc, binderfs_fs_parameters, param, &result);270if (opt < 0)271return opt;272273switch (opt) {274case Opt_max:275if (result.uint_32 > BINDERFS_MAX_MINOR)276return invalfc(fc, "Bad value for '%s'", param->key);277278ctx->max = result.uint_32;279break;280case Opt_stats_mode:281if (!capable(CAP_SYS_ADMIN))282return -EPERM;283284ctx->stats_mode = result.uint_32;285break;286default:287return invalfc(fc, "Unsupported parameter '%s'", param->key);288}289290return 0;291}292293static int binderfs_fs_context_reconfigure(struct fs_context *fc)294{295struct binderfs_mount_opts *ctx = fc->fs_private;296struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);297298if (info->mount_opts.stats_mode != ctx->stats_mode)299return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");300301info->mount_opts.stats_mode = ctx->stats_mode;302info->mount_opts.max = ctx->max;303return 0;304}305306static int binderfs_show_options(struct seq_file *seq, struct dentry *root)307{308struct binderfs_info *info = BINDERFS_SB(root->d_sb);309310if (info->mount_opts.max <= BINDERFS_MAX_MINOR)311seq_printf(seq, ",max=%d", info->mount_opts.max);312313switch (info->mount_opts.stats_mode) {314case binderfs_stats_mode_unset:315break;316case binderfs_stats_mode_global:317seq_puts(seq, ",stats=global");318break;319}320321return 0;322}323324static const struct super_operations binderfs_super_ops = {325.evict_inode = binderfs_evict_inode,326.show_options = binderfs_show_options,327.statfs = simple_statfs,328};329330static inline bool is_binderfs_control_device(const struct dentry *dentry)331{332struct binderfs_info *info = dentry->d_sb->s_fs_info;333334return info->control_dentry == dentry;335}336337static int binderfs_rename(struct mnt_idmap *idmap,338struct inode *old_dir, struct dentry *old_dentry,339struct inode *new_dir, struct dentry *new_dentry,340unsigned int flags)341{342if (is_binderfs_control_device(old_dentry) ||343is_binderfs_control_device(new_dentry))344return -EPERM;345346return simple_rename(idmap, old_dir, old_dentry, new_dir,347new_dentry, flags);348}349350static int binderfs_unlink(struct inode *dir, struct dentry *dentry)351{352if (is_binderfs_control_device(dentry))353return -EPERM;354355return simple_unlink(dir, dentry);356}357358static const struct file_operations binder_ctl_fops = {359.owner = THIS_MODULE,360.open = nonseekable_open,361.unlocked_ioctl = binder_ctl_ioctl,362.compat_ioctl = binder_ctl_ioctl,363.llseek = noop_llseek,364};365366/**367* binderfs_binder_ctl_create - create a new binder-control device368* @sb: super block of the binderfs mount369*370* This function creates a new binder-control device node in the binderfs mount371* referred to by @sb.372*373* Return: 0 on success, negative errno on failure374*/375static int binderfs_binder_ctl_create(struct super_block *sb)376{377int minor, ret;378struct dentry *dentry;379struct binder_device *device;380struct inode *inode = NULL;381struct dentry *root = sb->s_root;382struct binderfs_info *info = sb->s_fs_info;383#if defined(CONFIG_IPC_NS)384bool use_reserve = (info->ipc_ns == &init_ipc_ns);385#else386bool use_reserve = true;387#endif388389device = kzalloc(sizeof(*device), GFP_KERNEL);390if (!device)391return -ENOMEM;392393ret = -ENOMEM;394inode = new_inode(sb);395if (!inode)396goto out;397398/* Reserve a new minor number for the new device. */399mutex_lock(&binderfs_minors_mutex);400minor = ida_alloc_max(&binderfs_minors,401use_reserve ? BINDERFS_MAX_MINOR - 1 :402BINDERFS_MAX_MINOR_CAPPED - 1,403GFP_KERNEL);404mutex_unlock(&binderfs_minors_mutex);405if (minor < 0) {406ret = minor;407goto out;408}409410inode->i_ino = SECOND_INODE;411simple_inode_init_ts(inode);412init_special_inode(inode, S_IFCHR | 0600,413MKDEV(MAJOR(binderfs_dev), minor));414inode->i_fop = &binder_ctl_fops;415inode->i_uid = info->root_uid;416inode->i_gid = info->root_gid;417418device->minor = minor;419device->ctx = NULL;420421dentry = d_alloc_name(root, "binder-control");422if (!dentry)423goto out;424425inode->i_private = device;426info->control_dentry = dentry;427d_make_persistent(dentry, inode);428dput(dentry);429430return 0;431432out:433kfree(device);434iput(inode);435436return ret;437}438439static const struct inode_operations binderfs_dir_inode_operations = {440.lookup = simple_lookup,441.rename = binderfs_rename,442.unlink = binderfs_unlink,443};444445static struct inode *binderfs_make_inode(struct super_block *sb, int mode)446{447struct inode *ret;448449ret = new_inode(sb);450if (ret) {451ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);452ret->i_mode = mode;453simple_inode_init_ts(ret);454}455return ret;456}457458void rust_binderfs_remove_file(struct dentry *dentry)459{460simple_recursive_removal(dentry, NULL);461}462463static struct dentry *rust_binderfs_create_file(struct dentry *parent, const char *name,464const struct file_operations *fops,465void *data)466{467struct dentry *dentry;468struct inode *new_inode;469470new_inode = binderfs_make_inode(parent->d_sb, S_IFREG | 0444);471if (!new_inode)472return ERR_PTR(-ENOMEM);473new_inode->i_fop = fops;474new_inode->i_private = data;475476dentry = simple_start_creating(parent, name);477if (IS_ERR(dentry)) {478iput(new_inode);479return dentry;480}481482d_make_persistent(dentry, new_inode);483fsnotify_create(parent->d_inode, dentry);484simple_done_creating(dentry);485return dentry;486}487488struct dentry *rust_binderfs_create_proc_file(struct inode *nodp, int pid)489{490struct binderfs_info *info = nodp->i_sb->s_fs_info;491struct dentry *dir = info->proc_log_dir;492char strbuf[20 + 1];493void *data = (void *)(unsigned long) pid;494495if (!dir)496return NULL;497498snprintf(strbuf, sizeof(strbuf), "%u", pid);499return rust_binderfs_create_file(dir, strbuf, &rust_binder_proc_fops, data);500}501502static struct dentry *binderfs_create_dir(struct dentry *parent,503const char *name)504{505struct dentry *dentry;506struct inode *new_inode;507508new_inode = binderfs_make_inode(parent->d_sb, S_IFDIR | 0755);509if (!new_inode)510return ERR_PTR(-ENOMEM);511512new_inode->i_fop = &simple_dir_operations;513new_inode->i_op = &simple_dir_inode_operations;514515dentry = simple_start_creating(parent, name);516if (IS_ERR(dentry)) {517iput(new_inode);518return dentry;519}520521inc_nlink(parent->d_inode);522set_nlink(new_inode, 2);523d_make_persistent(dentry, new_inode);524fsnotify_mkdir(parent->d_inode, dentry);525simple_done_creating(dentry);526return dentry;527}528529static int binder_features_show(struct seq_file *m, void *unused)530{531bool *feature = m->private;532533seq_printf(m, "%d\n", *feature);534535return 0;536}537DEFINE_SHOW_ATTRIBUTE(binder_features);538539static int init_binder_features(struct super_block *sb)540{541struct dentry *dentry, *dir;542543dir = binderfs_create_dir(sb->s_root, "features");544if (IS_ERR(dir))545return PTR_ERR(dir);546547dentry = rust_binderfs_create_file(dir, "oneway_spam_detection",548&binder_features_fops,549&binder_features.oneway_spam_detection);550if (IS_ERR(dentry))551return PTR_ERR(dentry);552553dentry = rust_binderfs_create_file(dir, "extended_error",554&binder_features_fops,555&binder_features.extended_error);556if (IS_ERR(dentry))557return PTR_ERR(dentry);558559dentry = rust_binderfs_create_file(dir, "freeze_notification",560&binder_features_fops,561&binder_features.freeze_notification);562if (IS_ERR(dentry))563return PTR_ERR(dentry);564565return 0;566}567568static int init_binder_logs(struct super_block *sb)569{570struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;571struct binderfs_info *info;572int ret = 0;573574binder_logs_root_dir = binderfs_create_dir(sb->s_root,575"binder_logs");576if (IS_ERR(binder_logs_root_dir)) {577ret = PTR_ERR(binder_logs_root_dir);578goto out;579}580581dentry = rust_binderfs_create_file(binder_logs_root_dir, "stats",582&rust_binder_stats_fops, NULL);583if (IS_ERR(dentry)) {584ret = PTR_ERR(dentry);585goto out;586}587588dentry = rust_binderfs_create_file(binder_logs_root_dir, "state",589&rust_binder_state_fops, NULL);590if (IS_ERR(dentry)) {591ret = PTR_ERR(dentry);592goto out;593}594595dentry = rust_binderfs_create_file(binder_logs_root_dir, "transactions",596&rust_binder_transactions_fops, NULL);597if (IS_ERR(dentry)) {598ret = PTR_ERR(dentry);599goto out;600}601602proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");603if (IS_ERR(proc_log_dir)) {604ret = PTR_ERR(proc_log_dir);605goto out;606}607info = sb->s_fs_info;608info->proc_log_dir = proc_log_dir;609610out:611return ret;612}613614static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)615{616int ret;617struct binderfs_info *info;618struct binderfs_mount_opts *ctx = fc->fs_private;619struct inode *inode = NULL;620struct binderfs_device device_info = {};621const char *name;622size_t len;623624sb->s_blocksize = PAGE_SIZE;625sb->s_blocksize_bits = PAGE_SHIFT;626627/*628* The binderfs filesystem can be mounted by userns root in a629* non-initial userns. By default such mounts have the SB_I_NODEV flag630* set in s_iflags to prevent security issues where userns root can631* just create random device nodes via mknod() since it owns the632* filesystem mount. But binderfs does not allow to create any files633* including devices nodes. The only way to create binder devices nodes634* is through the binder-control device which userns root is explicitly635* allowed to do. So removing the SB_I_NODEV flag from s_iflags is both636* necessary and safe.637*/638sb->s_iflags &= ~SB_I_NODEV;639sb->s_iflags |= SB_I_NOEXEC;640sb->s_magic = RUST_BINDERFS_SUPER_MAGIC;641sb->s_op = &binderfs_super_ops;642sb->s_time_gran = 1;643644sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);645if (!sb->s_fs_info)646return -ENOMEM;647info = sb->s_fs_info;648649info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);650651info->root_gid = make_kgid(sb->s_user_ns, 0);652if (!gid_valid(info->root_gid))653info->root_gid = GLOBAL_ROOT_GID;654info->root_uid = make_kuid(sb->s_user_ns, 0);655if (!uid_valid(info->root_uid))656info->root_uid = GLOBAL_ROOT_UID;657info->mount_opts.max = ctx->max;658info->mount_opts.stats_mode = ctx->stats_mode;659660inode = new_inode(sb);661if (!inode)662return -ENOMEM;663664inode->i_ino = FIRST_INODE;665inode->i_fop = &simple_dir_operations;666inode->i_mode = S_IFDIR | 0755;667simple_inode_init_ts(inode);668inode->i_op = &binderfs_dir_inode_operations;669set_nlink(inode, 2);670671sb->s_root = d_make_root(inode);672if (!sb->s_root)673return -ENOMEM;674675ret = binderfs_binder_ctl_create(sb);676if (ret)677return ret;678679name = rust_binder_devices_param;680for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {681strscpy(device_info.name, name, len + 1);682ret = binderfs_binder_device_create(inode, NULL, &device_info);683if (ret)684return ret;685name += len;686if (*name == ',')687name++;688}689690ret = init_binder_features(sb);691if (ret)692return ret;693694if (info->mount_opts.stats_mode == binderfs_stats_mode_global)695return init_binder_logs(sb);696697return 0;698}699700static int binderfs_fs_context_get_tree(struct fs_context *fc)701{702return get_tree_nodev(fc, binderfs_fill_super);703}704705static void binderfs_fs_context_free(struct fs_context *fc)706{707struct binderfs_mount_opts *ctx = fc->fs_private;708709kfree(ctx);710}711712static const struct fs_context_operations binderfs_fs_context_ops = {713.free = binderfs_fs_context_free,714.get_tree = binderfs_fs_context_get_tree,715.parse_param = binderfs_fs_context_parse_param,716.reconfigure = binderfs_fs_context_reconfigure,717};718719static int binderfs_init_fs_context(struct fs_context *fc)720{721struct binderfs_mount_opts *ctx;722723ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);724if (!ctx)725return -ENOMEM;726727ctx->max = BINDERFS_MAX_MINOR;728ctx->stats_mode = binderfs_stats_mode_unset;729730fc->fs_private = ctx;731fc->ops = &binderfs_fs_context_ops;732733return 0;734}735736static void binderfs_kill_super(struct super_block *sb)737{738struct binderfs_info *info = sb->s_fs_info;739740/*741* During inode eviction struct binderfs_info is needed.742* So first wipe the super_block then free struct binderfs_info.743*/744kill_anon_super(sb);745746if (info && info->ipc_ns)747put_ipc_ns(info->ipc_ns);748749kfree(info);750}751752static struct file_system_type binder_fs_type = {753.name = "binder",754.init_fs_context = binderfs_init_fs_context,755.parameters = binderfs_fs_parameters,756.kill_sb = binderfs_kill_super,757.fs_flags = FS_USERNS_MOUNT,758};759760int init_rust_binderfs(void)761{762int ret;763const char *name;764size_t len;765766/* Verify that the default binderfs device names are valid. */767name = rust_binder_devices_param;768for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {769if (len > BINDERFS_MAX_NAME)770return -E2BIG;771name += len;772if (*name == ',')773name++;774}775776/* Allocate new major number for binderfs. */777ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,778"rust_binder");779if (ret)780return ret;781782ret = register_filesystem(&binder_fs_type);783if (ret) {784unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);785return ret;786}787788return ret;789}790791792