Path: blob/master/drivers/android/binder/rust_binderfs.c
29519 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 :135BINDERFS_MAX_MINOR_CAPPED,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;180inode_lock(d_inode(root));181182/* look it up */183dentry = lookup_noperm(&QSTR(req->name), root);184if (IS_ERR(dentry)) {185inode_unlock(d_inode(root));186ret = PTR_ERR(dentry);187goto err;188}189190if (d_really_is_positive(dentry)) {191/* already exists */192dput(dentry);193inode_unlock(d_inode(root));194ret = -EEXIST;195goto err;196}197198inode->i_private = device;199d_instantiate(dentry, inode);200fsnotify_create(root->d_inode, dentry);201inode_unlock(d_inode(root));202203return 0;204205err:206kfree(device);207rust_binder_remove_context(ctx);208mutex_lock(&binderfs_minors_mutex);209--info->device_count;210ida_free(&binderfs_minors, minor);211mutex_unlock(&binderfs_minors_mutex);212iput(inode);213214return ret;215}216217/**218* binder_ctl_ioctl - handle binder device node allocation requests219*220* The request handler for the binder-control device. All requests operate on221* the binderfs mount the binder-control device resides in:222* - BINDER_CTL_ADD223* Allocate a new binder device.224*225* Return: %0 on success, negative errno on failure.226*/227static long binder_ctl_ioctl(struct file *file, unsigned int cmd,228unsigned long arg)229{230int ret = -EINVAL;231struct inode *inode = file_inode(file);232struct binderfs_device __user *device = (struct binderfs_device __user *)arg;233struct binderfs_device device_req;234235switch (cmd) {236case BINDER_CTL_ADD:237ret = copy_from_user(&device_req, device, sizeof(device_req));238if (ret) {239ret = -EFAULT;240break;241}242243ret = binderfs_binder_device_create(inode, device, &device_req);244break;245default:246break;247}248249return ret;250}251252static void binderfs_evict_inode(struct inode *inode)253{254struct binder_device *device = inode->i_private;255struct binderfs_info *info = BINDERFS_SB(inode->i_sb);256257clear_inode(inode);258259if (!S_ISCHR(inode->i_mode) || !device)260return;261262mutex_lock(&binderfs_minors_mutex);263--info->device_count;264ida_free(&binderfs_minors, device->minor);265mutex_unlock(&binderfs_minors_mutex);266267/* ctx is null for binder-control, but this function ignores null pointers */268rust_binder_remove_context(device->ctx);269270kfree(device);271}272273static int binderfs_fs_context_parse_param(struct fs_context *fc,274struct fs_parameter *param)275{276int opt;277struct binderfs_mount_opts *ctx = fc->fs_private;278struct fs_parse_result result;279280opt = fs_parse(fc, binderfs_fs_parameters, param, &result);281if (opt < 0)282return opt;283284switch (opt) {285case Opt_max:286if (result.uint_32 > BINDERFS_MAX_MINOR)287return invalfc(fc, "Bad value for '%s'", param->key);288289ctx->max = result.uint_32;290break;291case Opt_stats_mode:292if (!capable(CAP_SYS_ADMIN))293return -EPERM;294295ctx->stats_mode = result.uint_32;296break;297default:298return invalfc(fc, "Unsupported parameter '%s'", param->key);299}300301return 0;302}303304static int binderfs_fs_context_reconfigure(struct fs_context *fc)305{306struct binderfs_mount_opts *ctx = fc->fs_private;307struct binderfs_info *info = BINDERFS_SB(fc->root->d_sb);308309if (info->mount_opts.stats_mode != ctx->stats_mode)310return invalfc(fc, "Binderfs stats mode cannot be changed during a remount");311312info->mount_opts.stats_mode = ctx->stats_mode;313info->mount_opts.max = ctx->max;314return 0;315}316317static int binderfs_show_options(struct seq_file *seq, struct dentry *root)318{319struct binderfs_info *info = BINDERFS_SB(root->d_sb);320321if (info->mount_opts.max <= BINDERFS_MAX_MINOR)322seq_printf(seq, ",max=%d", info->mount_opts.max);323324switch (info->mount_opts.stats_mode) {325case binderfs_stats_mode_unset:326break;327case binderfs_stats_mode_global:328seq_puts(seq, ",stats=global");329break;330}331332return 0;333}334335static const struct super_operations binderfs_super_ops = {336.evict_inode = binderfs_evict_inode,337.show_options = binderfs_show_options,338.statfs = simple_statfs,339};340341static inline bool is_binderfs_control_device(const struct dentry *dentry)342{343struct binderfs_info *info = dentry->d_sb->s_fs_info;344345return info->control_dentry == dentry;346}347348static int binderfs_rename(struct mnt_idmap *idmap,349struct inode *old_dir, struct dentry *old_dentry,350struct inode *new_dir, struct dentry *new_dentry,351unsigned int flags)352{353if (is_binderfs_control_device(old_dentry) ||354is_binderfs_control_device(new_dentry))355return -EPERM;356357return simple_rename(idmap, old_dir, old_dentry, new_dir,358new_dentry, flags);359}360361static int binderfs_unlink(struct inode *dir, struct dentry *dentry)362{363if (is_binderfs_control_device(dentry))364return -EPERM;365366return simple_unlink(dir, dentry);367}368369static const struct file_operations binder_ctl_fops = {370.owner = THIS_MODULE,371.open = nonseekable_open,372.unlocked_ioctl = binder_ctl_ioctl,373.compat_ioctl = binder_ctl_ioctl,374.llseek = noop_llseek,375};376377/**378* binderfs_binder_ctl_create - create a new binder-control device379* @sb: super block of the binderfs mount380*381* This function creates a new binder-control device node in the binderfs mount382* referred to by @sb.383*384* Return: 0 on success, negative errno on failure385*/386static int binderfs_binder_ctl_create(struct super_block *sb)387{388int minor, ret;389struct dentry *dentry;390struct binder_device *device;391struct inode *inode = NULL;392struct dentry *root = sb->s_root;393struct binderfs_info *info = sb->s_fs_info;394#if defined(CONFIG_IPC_NS)395bool use_reserve = (info->ipc_ns == &init_ipc_ns);396#else397bool use_reserve = true;398#endif399400device = kzalloc(sizeof(*device), GFP_KERNEL);401if (!device)402return -ENOMEM;403404/* If we have already created a binder-control node, return. */405if (info->control_dentry) {406ret = 0;407goto out;408}409410ret = -ENOMEM;411inode = new_inode(sb);412if (!inode)413goto out;414415/* Reserve a new minor number for the new device. */416mutex_lock(&binderfs_minors_mutex);417minor = ida_alloc_max(&binderfs_minors,418use_reserve ? BINDERFS_MAX_MINOR :419BINDERFS_MAX_MINOR_CAPPED,420GFP_KERNEL);421mutex_unlock(&binderfs_minors_mutex);422if (minor < 0) {423ret = minor;424goto out;425}426427inode->i_ino = SECOND_INODE;428simple_inode_init_ts(inode);429init_special_inode(inode, S_IFCHR | 0600,430MKDEV(MAJOR(binderfs_dev), minor));431inode->i_fop = &binder_ctl_fops;432inode->i_uid = info->root_uid;433inode->i_gid = info->root_gid;434435device->minor = minor;436device->ctx = NULL;437438dentry = d_alloc_name(root, "binder-control");439if (!dentry)440goto out;441442inode->i_private = device;443info->control_dentry = dentry;444d_add(dentry, inode);445446return 0;447448out:449kfree(device);450iput(inode);451452return ret;453}454455static const struct inode_operations binderfs_dir_inode_operations = {456.lookup = simple_lookup,457.rename = binderfs_rename,458.unlink = binderfs_unlink,459};460461static struct inode *binderfs_make_inode(struct super_block *sb, int mode)462{463struct inode *ret;464465ret = new_inode(sb);466if (ret) {467ret->i_ino = iunique(sb, BINDERFS_MAX_MINOR + INODE_OFFSET);468ret->i_mode = mode;469simple_inode_init_ts(ret);470}471return ret;472}473474static struct dentry *binderfs_create_dentry(struct dentry *parent,475const char *name)476{477struct dentry *dentry;478479dentry = lookup_noperm(&QSTR(name), parent);480if (IS_ERR(dentry))481return dentry;482483/* Return error if the file/dir already exists. */484if (d_really_is_positive(dentry)) {485dput(dentry);486return ERR_PTR(-EEXIST);487}488489return dentry;490}491492void rust_binderfs_remove_file(struct dentry *dentry)493{494struct inode *parent_inode;495496parent_inode = d_inode(dentry->d_parent);497inode_lock(parent_inode);498if (simple_positive(dentry)) {499dget(dentry);500simple_unlink(parent_inode, dentry);501d_delete(dentry);502dput(dentry);503}504inode_unlock(parent_inode);505}506507static struct dentry *rust_binderfs_create_file(struct dentry *parent, const char *name,508const struct file_operations *fops,509void *data)510{511struct dentry *dentry;512struct inode *new_inode, *parent_inode;513struct super_block *sb;514515parent_inode = d_inode(parent);516inode_lock(parent_inode);517518dentry = binderfs_create_dentry(parent, name);519if (IS_ERR(dentry))520goto out;521522sb = parent_inode->i_sb;523new_inode = binderfs_make_inode(sb, S_IFREG | 0444);524if (!new_inode) {525dput(dentry);526dentry = ERR_PTR(-ENOMEM);527goto out;528}529530new_inode->i_fop = fops;531new_inode->i_private = data;532d_instantiate(dentry, new_inode);533fsnotify_create(parent_inode, dentry);534535out:536inode_unlock(parent_inode);537return dentry;538}539540struct dentry *rust_binderfs_create_proc_file(struct inode *nodp, int pid)541{542struct binderfs_info *info = nodp->i_sb->s_fs_info;543struct dentry *dir = info->proc_log_dir;544char strbuf[20 + 1];545void *data = (void *)(unsigned long) pid;546547if (!dir)548return NULL;549550snprintf(strbuf, sizeof(strbuf), "%u", pid);551return rust_binderfs_create_file(dir, strbuf, &rust_binder_proc_fops, data);552}553554static struct dentry *binderfs_create_dir(struct dentry *parent,555const char *name)556{557struct dentry *dentry;558struct inode *new_inode, *parent_inode;559struct super_block *sb;560561parent_inode = d_inode(parent);562inode_lock(parent_inode);563564dentry = binderfs_create_dentry(parent, name);565if (IS_ERR(dentry))566goto out;567568sb = parent_inode->i_sb;569new_inode = binderfs_make_inode(sb, S_IFDIR | 0755);570if (!new_inode) {571dput(dentry);572dentry = ERR_PTR(-ENOMEM);573goto out;574}575576new_inode->i_fop = &simple_dir_operations;577new_inode->i_op = &simple_dir_inode_operations;578579set_nlink(new_inode, 2);580d_instantiate(dentry, new_inode);581inc_nlink(parent_inode);582fsnotify_mkdir(parent_inode, dentry);583584out:585inode_unlock(parent_inode);586return dentry;587}588589static int binder_features_show(struct seq_file *m, void *unused)590{591bool *feature = m->private;592593seq_printf(m, "%d\n", *feature);594595return 0;596}597DEFINE_SHOW_ATTRIBUTE(binder_features);598599static int init_binder_features(struct super_block *sb)600{601struct dentry *dentry, *dir;602603dir = binderfs_create_dir(sb->s_root, "features");604if (IS_ERR(dir))605return PTR_ERR(dir);606607dentry = rust_binderfs_create_file(dir, "oneway_spam_detection",608&binder_features_fops,609&binder_features.oneway_spam_detection);610if (IS_ERR(dentry))611return PTR_ERR(dentry);612613dentry = rust_binderfs_create_file(dir, "extended_error",614&binder_features_fops,615&binder_features.extended_error);616if (IS_ERR(dentry))617return PTR_ERR(dentry);618619dentry = rust_binderfs_create_file(dir, "freeze_notification",620&binder_features_fops,621&binder_features.freeze_notification);622if (IS_ERR(dentry))623return PTR_ERR(dentry);624625return 0;626}627628static int init_binder_logs(struct super_block *sb)629{630struct dentry *binder_logs_root_dir, *dentry, *proc_log_dir;631struct binderfs_info *info;632int ret = 0;633634binder_logs_root_dir = binderfs_create_dir(sb->s_root,635"binder_logs");636if (IS_ERR(binder_logs_root_dir)) {637ret = PTR_ERR(binder_logs_root_dir);638goto out;639}640641dentry = rust_binderfs_create_file(binder_logs_root_dir, "stats",642&rust_binder_stats_fops, NULL);643if (IS_ERR(dentry)) {644ret = PTR_ERR(dentry);645goto out;646}647648dentry = rust_binderfs_create_file(binder_logs_root_dir, "state",649&rust_binder_state_fops, NULL);650if (IS_ERR(dentry)) {651ret = PTR_ERR(dentry);652goto out;653}654655dentry = rust_binderfs_create_file(binder_logs_root_dir, "transactions",656&rust_binder_transactions_fops, NULL);657if (IS_ERR(dentry)) {658ret = PTR_ERR(dentry);659goto out;660}661662proc_log_dir = binderfs_create_dir(binder_logs_root_dir, "proc");663if (IS_ERR(proc_log_dir)) {664ret = PTR_ERR(proc_log_dir);665goto out;666}667info = sb->s_fs_info;668info->proc_log_dir = proc_log_dir;669670out:671return ret;672}673674static int binderfs_fill_super(struct super_block *sb, struct fs_context *fc)675{676int ret;677struct binderfs_info *info;678struct binderfs_mount_opts *ctx = fc->fs_private;679struct inode *inode = NULL;680struct binderfs_device device_info = {};681const char *name;682size_t len;683684sb->s_blocksize = PAGE_SIZE;685sb->s_blocksize_bits = PAGE_SHIFT;686687/*688* The binderfs filesystem can be mounted by userns root in a689* non-initial userns. By default such mounts have the SB_I_NODEV flag690* set in s_iflags to prevent security issues where userns root can691* just create random device nodes via mknod() since it owns the692* filesystem mount. But binderfs does not allow to create any files693* including devices nodes. The only way to create binder devices nodes694* is through the binder-control device which userns root is explicitly695* allowed to do. So removing the SB_I_NODEV flag from s_iflags is both696* necessary and safe.697*/698sb->s_iflags &= ~SB_I_NODEV;699sb->s_iflags |= SB_I_NOEXEC;700sb->s_magic = RUST_BINDERFS_SUPER_MAGIC;701sb->s_op = &binderfs_super_ops;702sb->s_time_gran = 1;703704sb->s_fs_info = kzalloc(sizeof(struct binderfs_info), GFP_KERNEL);705if (!sb->s_fs_info)706return -ENOMEM;707info = sb->s_fs_info;708709info->ipc_ns = get_ipc_ns(current->nsproxy->ipc_ns);710711info->root_gid = make_kgid(sb->s_user_ns, 0);712if (!gid_valid(info->root_gid))713info->root_gid = GLOBAL_ROOT_GID;714info->root_uid = make_kuid(sb->s_user_ns, 0);715if (!uid_valid(info->root_uid))716info->root_uid = GLOBAL_ROOT_UID;717info->mount_opts.max = ctx->max;718info->mount_opts.stats_mode = ctx->stats_mode;719720inode = new_inode(sb);721if (!inode)722return -ENOMEM;723724inode->i_ino = FIRST_INODE;725inode->i_fop = &simple_dir_operations;726inode->i_mode = S_IFDIR | 0755;727simple_inode_init_ts(inode);728inode->i_op = &binderfs_dir_inode_operations;729set_nlink(inode, 2);730731sb->s_root = d_make_root(inode);732if (!sb->s_root)733return -ENOMEM;734735ret = binderfs_binder_ctl_create(sb);736if (ret)737return ret;738739name = rust_binder_devices_param;740for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {741strscpy(device_info.name, name, len + 1);742ret = binderfs_binder_device_create(inode, NULL, &device_info);743if (ret)744return ret;745name += len;746if (*name == ',')747name++;748}749750ret = init_binder_features(sb);751if (ret)752return ret;753754if (info->mount_opts.stats_mode == binderfs_stats_mode_global)755return init_binder_logs(sb);756757return 0;758}759760static int binderfs_fs_context_get_tree(struct fs_context *fc)761{762return get_tree_nodev(fc, binderfs_fill_super);763}764765static void binderfs_fs_context_free(struct fs_context *fc)766{767struct binderfs_mount_opts *ctx = fc->fs_private;768769kfree(ctx);770}771772static const struct fs_context_operations binderfs_fs_context_ops = {773.free = binderfs_fs_context_free,774.get_tree = binderfs_fs_context_get_tree,775.parse_param = binderfs_fs_context_parse_param,776.reconfigure = binderfs_fs_context_reconfigure,777};778779static int binderfs_init_fs_context(struct fs_context *fc)780{781struct binderfs_mount_opts *ctx;782783ctx = kzalloc(sizeof(struct binderfs_mount_opts), GFP_KERNEL);784if (!ctx)785return -ENOMEM;786787ctx->max = BINDERFS_MAX_MINOR;788ctx->stats_mode = binderfs_stats_mode_unset;789790fc->fs_private = ctx;791fc->ops = &binderfs_fs_context_ops;792793return 0;794}795796static void binderfs_kill_super(struct super_block *sb)797{798struct binderfs_info *info = sb->s_fs_info;799800/*801* During inode eviction struct binderfs_info is needed.802* So first wipe the super_block then free struct binderfs_info.803*/804kill_litter_super(sb);805806if (info && info->ipc_ns)807put_ipc_ns(info->ipc_ns);808809kfree(info);810}811812static struct file_system_type binder_fs_type = {813.name = "binder",814.init_fs_context = binderfs_init_fs_context,815.parameters = binderfs_fs_parameters,816.kill_sb = binderfs_kill_super,817.fs_flags = FS_USERNS_MOUNT,818};819820int init_rust_binderfs(void)821{822int ret;823const char *name;824size_t len;825826/* Verify that the default binderfs device names are valid. */827name = rust_binder_devices_param;828for (len = strcspn(name, ","); len > 0; len = strcspn(name, ",")) {829if (len > BINDERFS_MAX_NAME)830return -E2BIG;831name += len;832if (*name == ',')833name++;834}835836/* Allocate new major number for binderfs. */837ret = alloc_chrdev_region(&binderfs_dev, 0, BINDERFS_MAX_MINOR,838"rust_binder");839if (ret)840return ret;841842ret = register_filesystem(&binder_fs_type);843if (ret) {844unregister_chrdev_region(binderfs_dev, BINDERFS_MAX_MINOR);845return ret;846}847848return ret;849}850851852