#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/array_size.h>
#include <linux/binfmt_misc.h>
#include <linux/binfmts.h>
#include <linux/bitops.h>
#include <linux/bits.h>
#include <linux/bug.h>
#include <linux/cleanup.h>
#include <linux/cred.h>
#include <linux/ctype.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fs_context.h>
#include <linux/init.h>
#include <linux/kstrtox.h>
#include <linux/limits.h>
#include <linux/list.h>
#include <linux/magic.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/rculist.h>
#include <linux/refcount.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/srcu.h>
#include <linux/string.h>
#include <linux/string_helpers.h>
#include <linux/uaccess.h>
#include <linux/user_namespace.h>
#include "internal.h"
enum binfmt_misc_entry_bits {
MISC_FMT_ENABLED_BIT = 0,
MISC_FMT_MAGIC_BIT = 1,
MISC_FMT_BPF_BIT = 2,
};
enum binfmt_misc_entry_flags {
MISC_FMT_PRESERVE_ARGV0 = (1U << 31),
MISC_FMT_OPEN_BINARY = (1U << 30),
MISC_FMT_CREDENTIALS = (1U << 29),
MISC_FMT_OPEN_FILE = (1U << 28),
MISC_FMT_TRANSPARENT = (1U << 27),
MISC_FMT_LOADER = (1U << 26),
MISC_FMT_DISABLED = (1U << 25),
};
#define MISC_FMT_INVOCATION_FLAGS (MISC_FMT_PRESERVE_ARGV0 | \
MISC_FMT_OPEN_BINARY | \
MISC_FMT_CREDENTIALS | \
MISC_FMT_OPEN_FILE | \
MISC_FMT_TRANSPARENT | \
MISC_FMT_LOADER)
struct binfmt_misc_flag {
char c;
unsigned long flag;
unsigned long implies;
const char *desc;
};
static const struct binfmt_misc_flag misc_flags[] = {
{ 'P', MISC_FMT_PRESERVE_ARGV0, 0, "preserve argv0" },
{ 'O', MISC_FMT_OPEN_BINARY, 0, "open binary" },
{ 'C', MISC_FMT_CREDENTIALS, MISC_FMT_OPEN_BINARY, "credentials from the binary" },
{ 'F', MISC_FMT_OPEN_FILE, 0, "open interpreter file now" },
{ 'T', MISC_FMT_TRANSPARENT, MISC_FMT_OPEN_BINARY, "transparent" },
{ 'L', MISC_FMT_LOADER, 0, "loader substitution" },
{ 'D', MISC_FMT_DISABLED, 0, "register disabled" },
};
static const struct binfmt_misc_flag *misc_flag_by_char(const char c)
{
for (int i = 0; i < ARRAY_SIZE(misc_flags); i++)
if (misc_flags[i].c == c)
return &misc_flags[i];
return NULL;
}
static bool misc_valid_delim(const char c)
{
if (!isascii(c) || !ispunct(c))
return false;
return c != '\\';
}
struct binfmt_misc_entry {
struct hlist_node node;
unsigned long flags;
int offset;
int size;
char *magic;
char *mask;
const char *interpreter;
char *name;
struct dentry *dentry;
const struct binfmt_misc_ops *bpf_ops;
const char *bpf_ops_name;
struct list_head interps;
refcount_t users;
struct rcu_head rcu;
char buf[];
};
#define MAX_REGISTER_LENGTH 1920
#define MISC_DELIM_PAD 8
DEFINE_STATIC_SRCU_FAST(bm_entries_srcu);
static bool entry_matches_magic(const struct binfmt_misc_entry *e,
const struct linux_binprm *bprm)
{
const char *s = bprm->buf + e->offset;
int i;
if (!e->mask)
return !memcmp(s, e->magic, e->size);
for (i = 0; i < e->size; i++)
if ((s[i] ^ e->magic[i]) & e->mask[i])
return false;
return true;
}
static bool entry_matches_extension(const struct binfmt_misc_entry *e,
const char *ext)
{
return ext && !strcmp(e->magic, ext);
}
static struct binfmt_misc_entry *
search_binfmt_handler(struct binfmt_misc *misc, struct linux_binprm *bprm)
{
char *dot = strrchr(bprm->interp, '.');
const char *ext = dot ? dot + 1 : NULL;
struct binfmt_misc_entry *e;
hlist_for_each_entry_rcu(e, &misc->entries, node,
srcu_read_lock_held(&bm_entries_srcu)) {
if (!test_bit(MISC_FMT_ENABLED_BIT, &e->flags))
continue;
if (test_bit(MISC_FMT_BPF_BIT, &e->flags)) {
if (!e->bpf_ops->match(bprm))
continue;
} else if (test_bit(MISC_FMT_MAGIC_BIT, &e->flags)) {
if (!entry_matches_magic(e, bprm))
continue;
} else {
if (!entry_matches_extension(e, ext))
continue;
}
if (refcount_inc_not_zero(&e->users))
return e;
}
return NULL;
}
static struct binfmt_misc_entry *get_binfmt_handler(struct binfmt_misc *misc,
struct linux_binprm *bprm)
{
guard(srcu_fast)(&bm_entries_srcu);
return search_binfmt_handler(misc, bprm);
}
const struct binfmt_misc_interp *
binfmt_misc_find_interp(const struct list_head *interps, const char *name)
{
struct binfmt_misc_interp *interp;
list_for_each_entry(interp, interps, list)
if (!strcmp(interp->name, name))
return interp;
return NULL;
}
static void close_interp_file(struct file *f)
{
if (IS_ERR_OR_NULL(f))
return;
exe_file_allow_write_access(f);
filp_close(f, NULL);
}
DEFINE_FREE(close_interp_file, struct file *, close_interp_file(_T))
static struct file *open_interp_file(const struct cred *cred, const char *path)
{
struct file *f;
scoped_with_creds(cred)
f = open_exec(path);
if (IS_ERR(f))
pr_notice("register: failed to install interpreter %s\n", path);
return f;
}
static void entry_put_interpreters(struct binfmt_misc_entry *e)
{
struct binfmt_misc_interp *interp, *tmp;
list_for_each_entry_safe(interp, tmp, &e->interps, list) {
list_del(&interp->list);
close_interp_file(interp->file);
dec_ucount(interp->ucounts, UCOUNT_BINFMT_MISC_INTERPRETERS);
kfree(interp);
}
}
static int entry_attach_interpreter(struct binfmt_misc_entry *e,
const char *name, const char *path,
struct file *f)
{
size_t nlen = strlen(name), plen = strlen(path);
struct binfmt_misc_interp *interp;
struct ucounts *ucounts;
if (binfmt_misc_find_interp(&e->interps, name))
return -EEXIST;
if (list_count_nodes(&e->interps) >= BINFMT_MISC_INTERP_MAX)
return -ENOSPC;
ucounts = inc_ucount(current_user_ns(), current_euid(),
UCOUNT_BINFMT_MISC_INTERPRETERS);
if (!ucounts)
return -ENOSPC;
interp = kmalloc_flex(*interp, name, nlen + plen + 2,
GFP_KERNEL_ACCOUNT);
if (!interp) {
dec_ucount(ucounts, UCOUNT_BINFMT_MISC_INTERPRETERS);
return -ENOMEM;
}
interp->path = interp->name + nlen + 1;
strscpy(interp->name, name, nlen + 1);
strscpy(interp->name + nlen + 1, path, plen + 1);
interp->file = f;
interp->ucounts = ucounts;
list_add_tail_rcu(&interp->list, &e->interps);
pr_debug("register: interpreter: %s {%s}\n", name, path);
return 0;
}
static void bm_entry_free_rcu(struct rcu_head *rcu)
{
struct binfmt_misc_entry *e = container_of(rcu, struct binfmt_misc_entry, rcu);
if (e->bpf_ops)
binfmt_misc_put_ops(e->bpf_ops);
kfree(e);
}
static void put_binfmt_handler(struct binfmt_misc_entry *e)
{
if (IS_ERR_OR_NULL(e))
return;
if (refcount_dec_and_test(&e->users)) {
entry_put_interpreters(e);
call_srcu(&bm_entries_srcu, &e->rcu, bm_entry_free_rcu);
}
}
DEFINE_FREE(put_binfmt_handler, struct binfmt_misc_entry *, put_binfmt_handler(_T))
static void drop_staged_selection(struct linux_binprm *bprm)
{
kfree(bprm->bpf_interp);
bprm->bpf_interp = NULL;
kfree(bprm->bpf_interp_arg);
bprm->bpf_interp_arg = NULL;
if (bprm->bpf_interp_file) {
fput(bprm->bpf_interp_file);
bprm->bpf_interp_file = NULL;
}
bprm->bpf_flags = 0;
}
static struct binfmt_misc *current_binfmt_misc(void)
{
const struct user_namespace *user_ns;
struct binfmt_misc *misc;
for (user_ns = current_user_ns(); user_ns; user_ns = user_ns->parent) {
misc = smp_load_acquire(&user_ns->binfmt_misc);
if (misc)
return misc;
}
return &init_binfmt_misc;
}
static const char *entry_select_interpreter(const struct binfmt_misc_entry *e,
struct linux_binprm *bprm)
{
int retval;
drop_staged_selection(bprm);
if (!test_bit(MISC_FMT_BPF_BIT, &e->flags))
return e->interpreter;
bprm->bpf_interps = &e->interps;
retval = e->bpf_ops->load(bprm);
bprm->bpf_interps = NULL;
if (retval) {
if (retval > 0 || retval < -MAX_ERRNO)
retval = -ENOEXEC;
goto drop_staged;
}
if (!bprm->bpf_interp) {
retval = -ENOEXEC;
goto drop_staged;
}
return bprm->bpf_interp;
drop_staged:
drop_staged_selection(bprm);
return ERR_PTR(retval);
}
static unsigned long entry_invocation_flags(const struct binfmt_misc_entry *e,
struct linux_binprm *bprm)
{
unsigned long flags = 0;
u64 bpf_flags;
if (!test_bit(MISC_FMT_BPF_BIT, &e->flags))
return e->flags;
bpf_flags = bprm->bpf_flags;
bprm->bpf_flags = 0;
if (bpf_flags & BPF_BINPRM_PRESERVE_ARGV0)
flags |= MISC_FMT_PRESERVE_ARGV0;
if (bpf_flags & BPF_BINPRM_EXECFD)
flags |= MISC_FMT_OPEN_BINARY;
if (bpf_flags & BPF_BINPRM_CREDENTIALS)
flags |= MISC_FMT_CREDENTIALS | MISC_FMT_OPEN_BINARY;
if (bpf_flags & BPF_BINPRM_TRANSPARENT)
flags |= MISC_FMT_TRANSPARENT | MISC_FMT_OPEN_BINARY;
if (bpf_flags & BPF_BINPRM_LOADER)
flags |= MISC_FMT_LOADER;
return flags;
}
static struct file *entry_open_interpreter(const struct binfmt_misc_entry *e,
struct linux_binprm *bprm,
const char *interpreter)
{
struct file *interp_file __free(fput) = NULL;
struct binfmt_misc_interp *interp;
struct file *bound;
int retval;
if (bprm->bpf_interp_file) {
bound = bprm->bpf_interp_file;
} else if (e->flags & MISC_FMT_OPEN_FILE) {
interp = list_first_entry(&e->interps,
struct binfmt_misc_interp, list);
bound = interp->file;
} else {
return open_exec(interpreter);
}
interp_file = file_clone_open(bound);
if (IS_ERR(interp_file))
return interp_file;
retval = exe_file_deny_write_access(interp_file);
if (retval)
return ERR_PTR(retval);
return no_free_ptr(interp_file);
}
static int build_interp_argv(struct linux_binprm *bprm, const char *interpreter,
unsigned long flags)
{
int retval;
if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
return -ENOENT;
if (flags & MISC_FMT_PRESERVE_ARGV0) {
bprm->interp_flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
} else {
retval = remove_arg_zero(bprm);
if (retval)
return retval;
}
retval = copy_string_kernel(bprm->interp, bprm);
if (retval < 0)
return retval;
bprm->argc++;
if (bprm->bpf_interp_arg) {
retval = copy_string_kernel(bprm->bpf_interp_arg, bprm);
if (retval < 0)
return retval;
bprm->argc++;
kfree(bprm->bpf_interp_arg);
bprm->bpf_interp_arg = NULL;
}
retval = copy_string_kernel(interpreter, bprm);
if (retval < 0)
return retval;
bprm->argc++;
return 0;
}
static int load_misc_binary(struct linux_binprm *bprm)
{
struct binfmt_misc_entry *fmt __free(put_binfmt_handler) = NULL;
const char *interpreter;
struct file *interp_file;
struct binfmt_misc *misc;
unsigned long flags;
int retval;
WARN_ON_ONCE(bprm->loader);
misc = current_binfmt_misc();
if (!READ_ONCE(misc->enabled))
return -ENOEXEC;
fmt = get_binfmt_handler(misc, bprm);
if (!fmt)
return -ENOEXEC;
interpreter = entry_select_interpreter(fmt, bprm);
if (IS_ERR(interpreter))
return PTR_ERR(interpreter);
flags = entry_invocation_flags(fmt, bprm);
if ((flags & (MISC_FMT_LOADER | MISC_FMT_TRANSPARENT)) &&
bprm->bpf_interp_arg)
return -EINVAL;
if (flags & MISC_FMT_LOADER) {
interp_file = entry_open_interpreter(fmt, bprm, interpreter);
if (IS_ERR(interp_file)) {
retval = PTR_ERR(interp_file);
return retval == -ENOEXEC ? -EACCES : retval;
}
bprm->loader = interp_file;
return -ENOEXEC;
}
if (!(flags & MISC_FMT_TRANSPARENT)) {
retval = build_interp_argv(bprm, interpreter, flags);
if (retval)
return retval;
}
retval = bprm_change_interp(interpreter, bprm);
if (retval < 0)
return retval;
interp_file = entry_open_interpreter(fmt, bprm, interpreter);
if (IS_ERR(interp_file))
return PTR_ERR(interp_file);
if (flags & MISC_FMT_TRANSPARENT)
bprm->interp_flags |= BINPRM_FLAGS_TRANSPARENT_INTERP;
bprm->interpreter = interp_file;
if (flags & MISC_FMT_OPEN_BINARY)
bprm->have_execfd = 1;
if (flags & MISC_FMT_CREDENTIALS)
bprm->execfd_creds = 1;
return 0;
}
static char *scanarg(char *s, char del)
{
char c;
while ((c = *s++) != del) {
if (c == '\\' && *s == 'x') {
s++;
if (!isxdigit(*s++))
return NULL;
if (!isxdigit(*s++))
return NULL;
}
}
s[-1] = '\0';
return s;
}
static char *check_special_flags(char *p, struct binfmt_misc_entry *e)
{
for (;; p++) {
const struct binfmt_misc_flag *f = misc_flag_by_char(*p);
if (!f)
return p;
pr_debug("register: flag: %c (%s)\n", f->c, f->desc);
e->flags |= f->flag | f->implies;
}
}
static char *parse_magic_fields(struct binfmt_misc_entry *e, char *p, char del)
{
char *s;
s = strchr(p, del);
if (!s)
return NULL;
*s = '\0';
if (p != s) {
if (kstrtoint(p, 10, &e->offset) || e->offset < 0)
return NULL;
}
p = s + 1;
pr_debug("register: offset: %#x\n", e->offset);
e->magic = p;
p = scanarg(p, del);
if (!p || !e->magic[0])
return NULL;
print_hex_dump_debug(
KBUILD_MODNAME ": register: magic[raw]: ",
DUMP_PREFIX_NONE, 16, 1, e->magic, p - e->magic, true);
e->mask = p;
p = scanarg(p, del);
if (!p)
return NULL;
if (!e->mask[0]) {
e->mask = NULL;
pr_debug("register: mask[raw]: none\n");
} else {
print_hex_dump_debug(
KBUILD_MODNAME ": register: mask[raw]: ",
DUMP_PREFIX_NONE, 16, 1, e->mask, p - e->mask, true);
}
e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
if (e->mask && string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
return NULL;
if (e->size > BINPRM_BUF_SIZE || BINPRM_BUF_SIZE - e->size < e->offset)
return NULL;
pr_debug("register: magic/mask length: %i\n", e->size);
print_hex_dump_debug(
KBUILD_MODNAME ": register: magic[decoded]: ",
DUMP_PREFIX_NONE, 16, 1, e->magic, e->size, true);
if (e->mask)
print_hex_dump_debug(
KBUILD_MODNAME ": register: mask[decoded]: ",
DUMP_PREFIX_NONE, 16, 1, e->mask, e->size, true);
return p;
}
static char *parse_extension_fields(struct binfmt_misc_entry *e, char *p,
char del)
{
p = strchr(p, del);
if (!p)
return NULL;
*p++ = '\0';
e->magic = p;
p = strchr(p, del);
if (!p)
return NULL;
*p++ = '\0';
if (!e->magic[0] || strchr(e->magic, '/'))
return NULL;
pr_debug("register: extension: {%s}\n", e->magic);
p = strchr(p, del);
if (!p)
return NULL;
*p++ = '\0';
return p;
}
static char *parse_bpf_fields(struct binfmt_misc_entry *e, char *p, char del)
{
if (*p++ != del)
return NULL;
if (*p++ != del)
return NULL;
if (*p++ != del)
return NULL;
return p;
}
static struct binfmt_misc_entry *create_entry(const char __user *buffer,
size_t count)
{
struct binfmt_misc_entry *e __free(kfree) = NULL;
char *buf, *p;
char del;
pr_debug("register: received %zu bytes\n", count);
if ((count < 11) || (count > MAX_REGISTER_LENGTH))
return ERR_PTR(-EINVAL);
e = kmalloc_flex(*e, buf, count + MISC_DELIM_PAD, GFP_KERNEL_ACCOUNT);
if (!e)
return ERR_PTR(-ENOMEM);
p = buf = e->buf;
memset(e, 0, sizeof(*e));
INIT_LIST_HEAD(&e->interps);
if (copy_from_user(buf, buffer, count))
return ERR_PTR(-EFAULT);
del = *p++;
pr_debug("register: delim: %#x\n", del);
if (!misc_valid_delim(del))
return ERR_PTR(-EINVAL);
memset(buf + count, del, MISC_DELIM_PAD);
e->name = p;
p = strchr(p, del);
if (!p)
return ERR_PTR(-EINVAL);
*p++ = '\0';
if (!e->name[0] ||
!strcmp(e->name, ".") ||
!strcmp(e->name, "..") ||
strchr(e->name, '/'))
return ERR_PTR(-EINVAL);
pr_debug("register: name: {%s}\n", e->name);
switch (*p++) {
case 'E':
pr_debug("register: type: E (extension)\n");
e->flags = BIT(MISC_FMT_ENABLED_BIT);
break;
case 'M':
pr_debug("register: type: M (magic)\n");
e->flags = BIT(MISC_FMT_ENABLED_BIT) | BIT(MISC_FMT_MAGIC_BIT);
break;
case 'B':
pr_debug("register: type: B (bpf)\n");
if (!IS_ENABLED(CONFIG_BINFMT_MISC_BPF))
return ERR_PTR(-EINVAL);
e->flags = BIT(MISC_FMT_ENABLED_BIT) | BIT(MISC_FMT_BPF_BIT);
break;
default:
return ERR_PTR(-EINVAL);
}
if (*p++ != del)
return ERR_PTR(-EINVAL);
if (test_bit(MISC_FMT_BPF_BIT, &e->flags))
p = parse_bpf_fields(e, p, del);
else if (test_bit(MISC_FMT_MAGIC_BIT, &e->flags))
p = parse_magic_fields(e, p, del);
else
p = parse_extension_fields(e, p, del);
if (!p)
return ERR_PTR(-EINVAL);
e->interpreter = p;
p = strchr(p, del);
if (!p)
return ERR_PTR(-EINVAL);
*p++ = '\0';
if (test_bit(MISC_FMT_BPF_BIT, &e->flags)) {
e->bpf_ops_name = e->interpreter;
e->interpreter = NULL;
if (!e->bpf_ops_name[0])
return ERR_PTR(-EINVAL);
pr_debug("register: bpf handler: {%s}\n", e->bpf_ops_name);
} else if (!e->interpreter[0]) {
return ERR_PTR(-EINVAL);
} else {
pr_debug("register: interpreter: {%s}\n", e->interpreter);
}
p = check_special_flags(p, e);
if (test_bit(MISC_FMT_BPF_BIT, &e->flags) &&
(e->flags & MISC_FMT_INVOCATION_FLAGS))
return ERR_PTR(-EINVAL);
if (e->flags & MISC_FMT_DISABLED) {
e->flags &= ~MISC_FMT_DISABLED;
clear_bit(MISC_FMT_ENABLED_BIT, &e->flags);
}
if ((e->flags & MISC_FMT_TRANSPARENT) &&
(e->flags & MISC_FMT_PRESERVE_ARGV0))
return ERR_PTR(-EINVAL);
if ((e->flags & MISC_FMT_LOADER) &&
(e->flags & (MISC_FMT_TRANSPARENT | MISC_FMT_PRESERVE_ARGV0 |
MISC_FMT_CREDENTIALS | MISC_FMT_OPEN_BINARY)))
return ERR_PTR(-EINVAL);
if (*p == '\n')
p++;
if (p != buf + count)
return ERR_PTR(-EINVAL);
if ((e->flags & (MISC_FMT_LOADER | MISC_FMT_CREDENTIALS)) &&
!(e->flags & MISC_FMT_OPEN_FILE) &&
e->interpreter[0] != '/')
return ERR_PTR(-EINVAL);
refcount_set(&e->users, 1);
return no_free_ptr(e);
}
enum bm_command {
BM_CMD_IGNORE,
BM_CMD_DISABLE,
BM_CMD_ENABLE,
BM_CMD_REMOVE,
};
#define MAX_COMMAND_LENGTH 3
static int parse_command(const char *s, size_t count)
{
if (count > MAX_COMMAND_LENGTH)
return -EINVAL;
if (!count)
return BM_CMD_IGNORE;
if (s[count - 1] == '\n')
count--;
if (count == 1 && s[0] == '0')
return BM_CMD_DISABLE;
if (count == 1 && s[0] == '1')
return BM_CMD_ENABLE;
if (count == 2 && s[0] == '-' && s[1] == '1')
return BM_CMD_REMOVE;
return -EINVAL;
}
static int read_command(const char __user *buffer, size_t count)
{
char s[MAX_COMMAND_LENGTH + 1];
if (count > sizeof(s) - 1)
return -EINVAL;
if (copy_from_user(s, buffer, count))
return -EFAULT;
return parse_command(s, count);
}
static struct inode *bm_root_inode(struct super_block *sb)
{
return d_inode(sb->s_root);
}
static void bm_seq_hex(struct seq_file *m, const u8 *data, int size)
{
for (int i = 0; i < size; i++)
seq_printf(m, "%02x", data[i]);
}
static int bm_entry_show(struct seq_file *m, void *unused)
{
struct binfmt_misc_entry *e = m->private;
if (test_bit(MISC_FMT_ENABLED_BIT, &e->flags))
seq_puts(m, "enabled\n");
else
seq_puts(m, "disabled\n");
if (test_bit(MISC_FMT_BPF_BIT, &e->flags)) {
struct binfmt_misc_interp *interp;
seq_printf(m, "bpf %s\n", e->bpf_ops->name);
rcu_read_lock();
list_for_each_entry_rcu(interp, &e->interps, list)
seq_printf(m, "bpf-interpreter %s %s\n",
interp->name, interp->path);
rcu_read_unlock();
} else {
seq_printf(m, "interpreter %s\n", e->interpreter);
}
seq_puts(m, "flags: ");
for (int i = 0; i < ARRAY_SIZE(misc_flags); i++)
if (e->flags & misc_flags[i].flag)
seq_putc(m, misc_flags[i].c);
seq_putc(m, '\n');
if (test_bit(MISC_FMT_BPF_BIT, &e->flags)) {
} else if (!test_bit(MISC_FMT_MAGIC_BIT, &e->flags)) {
seq_printf(m, "extension .%s\n", e->magic);
} else {
seq_printf(m, "offset %i\nmagic ", e->offset);
bm_seq_hex(m, e->magic, e->size);
if (e->mask) {
seq_puts(m, "\nmask ");
bm_seq_hex(m, e->mask, e->size);
}
seq_putc(m, '\n');
}
return 0;
}
static struct inode *bm_get_inode(struct super_block *sb, umode_t mode)
{
struct inode *inode = new_inode(sb);
if (inode) {
inode->i_ino = get_next_ino();
inode->i_mode = mode;
simple_inode_init_ts(inode);
}
return inode;
}
static struct binfmt_misc *i_binfmt_misc(struct inode *inode)
{
return inode->i_sb->s_user_ns->binfmt_misc;
}
static void bm_evict_inode(struct inode *inode)
{
struct binfmt_misc_entry *e = inode->i_private;
clear_inode(inode);
if (e) {
struct binfmt_misc *misc;
misc = i_binfmt_misc(inode);
spin_lock(&misc->entries_lock);
if (!hlist_unhashed(&e->node))
hlist_del_init_rcu(&e->node);
spin_unlock(&misc->entries_lock);
put_binfmt_handler(e);
}
}
static void unlink_binfmt_handler(struct binfmt_misc *misc,
struct binfmt_misc_entry *e)
{
spin_lock(&misc->entries_lock);
hlist_del_init_rcu(&e->node);
spin_unlock(&misc->entries_lock);
}
static void remove_binfmt_handler(struct binfmt_misc *misc,
struct binfmt_misc_entry *e)
{
unlink_binfmt_handler(misc, e);
locked_recursive_removal(e->dentry, NULL);
}
static void bm_remove_entry(struct binfmt_misc_entry *e, struct super_block *sb)
{
struct inode *root = bm_root_inode(sb);
inode_lock_nested(root, I_MUTEX_PARENT);
if (!d_unhashed(e->dentry))
remove_binfmt_handler(i_binfmt_misc(root), e);
inode_unlock(root);
}
static void bm_remove_all_entries(struct binfmt_misc *misc,
struct super_block *sb)
{
struct inode *root = bm_root_inode(sb);
struct dentry *child = NULL;
inode_lock_nested(root, I_MUTEX_PARENT);
while ((child = find_next_child(sb->s_root, child))) {
struct binfmt_misc_entry *e = d_inode(child)->i_private;
if (e)
remove_binfmt_handler(misc, e);
}
inode_unlock(root);
}
static int bm_unlink(struct inode *dir, struct dentry *dentry)
{
struct binfmt_misc_entry *e = d_inode(dentry)->i_private;
if (!e)
return -EPERM;
unlink_binfmt_handler(i_binfmt_misc(dir), e);
return simple_unlink(dir, dentry);
}
static const struct inode_operations bm_dir_inode_operations = {
.lookup = simple_lookup,
.unlink = bm_unlink,
};
static int bm_entry_open(struct inode *inode, struct file *file)
{
int ret;
ret = single_open(file, bm_entry_show, inode->i_private);
if (ret)
return ret;
if (file->f_mode & FMODE_WRITE)
file->f_mode |= FMODE_PWRITE;
return 0;
}
#define MAX_BINDING_LENGTH (BINFMT_MISC_INTERP_NAME_MAX + PATH_MAX + 3)
static ssize_t bm_entry_add_interp(struct binfmt_misc_entry *e,
struct file *file, char *buf, size_t count)
{
struct file *f __free(close_interp_file) = NULL;
struct inode *root = bm_root_inode(file_inode(file)->i_sb);
size_t nlen, plen;
char *name, *path;
int retval;
if (!test_bit(MISC_FMT_BPF_BIT, &e->flags))
return -EINVAL;
if (!hlist_unhashed_lockless(&e->node))
return -EBUSY;
name = buf + 1;
path = strchr(name, ' ');
if (!path)
return -EINVAL;
*path++ = '\0';
plen = strlen(path);
if (path + plen != buf + count)
return -EINVAL;
if (plen && path[plen - 1] == '\n')
path[--plen] = '\0';
if (path[0] != '/')
return -EINVAL;
nlen = path - name - 1;
if (!nlen || nlen > BINFMT_MISC_INTERP_NAME_MAX)
return -EINVAL;
for (const char *p = name; *p; p++)
if (!isascii(*p) || !isgraph(*p))
return -EINVAL;
f = open_interp_file(file->f_cred, path);
if (IS_ERR(f))
return PTR_ERR(f);
inode_lock(root);
if (d_unhashed(e->dentry))
retval = -ENOENT;
else if (!hlist_unhashed(&e->node))
retval = -EBUSY;
else
retval = entry_attach_interpreter(e, name, path, f);
inode_unlock(root);
if (retval)
return retval;
retain_and_null_ptr(f);
return count;
}
static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos)
{
struct inode *inode = file_inode(file);
struct binfmt_misc_entry *e = inode->i_private;
char *buf __free(kfree) = NULL;
int res;
if (count > MAX_BINDING_LENGTH)
return -E2BIG;
buf = memdup_user_nul(buffer, count);
if (IS_ERR(buf))
return PTR_ERR(buf);
if (buf[0] == '+')
return bm_entry_add_interp(e, file, buf, count);
res = parse_command(buf, count);
switch (res) {
case BM_CMD_DISABLE:
clear_bit(MISC_FMT_ENABLED_BIT, &e->flags);
break;
case BM_CMD_ENABLE: {
struct inode *root = bm_root_inode(inode->i_sb);
inode_lock(root);
set_bit(MISC_FMT_ENABLED_BIT, &e->flags);
if (hlist_unhashed(&e->node) && !d_unhashed(e->dentry)) {
struct binfmt_misc *misc = i_binfmt_misc(inode);
spin_lock(&misc->entries_lock);
hlist_add_head_rcu(&e->node, &misc->entries);
spin_unlock(&misc->entries_lock);
}
inode_unlock(root);
break;
}
case BM_CMD_REMOVE:
bm_remove_entry(e, inode->i_sb);
break;
default:
return res;
}
return count;
}
static const struct file_operations bm_entry_operations = {
.open = bm_entry_open,
.read = seq_read,
.write = bm_entry_write,
.llseek = seq_lseek,
.release = single_release,
};
static int add_entry(struct binfmt_misc_entry *e, struct super_block *sb)
{
struct dentry *dentry = simple_start_creating(sb->s_root, e->name);
struct inode *inode;
struct binfmt_misc *misc;
if (IS_ERR(dentry))
return PTR_ERR(dentry);
inode = bm_get_inode(sb, S_IFREG | 0644);
if (unlikely(!inode)) {
simple_done_creating(dentry);
return -ENOMEM;
}
e->dentry = dentry;
inode->i_private = e;
inode->i_fop = &bm_entry_operations;
d_make_persistent(dentry, inode);
if (test_bit(MISC_FMT_ENABLED_BIT, &e->flags)) {
misc = i_binfmt_misc(inode);
spin_lock(&misc->entries_lock);
hlist_add_head_rcu(&e->node, &misc->entries);
spin_unlock(&misc->entries_lock);
}
simple_done_creating(dentry);
return 0;
}
static ssize_t bm_register_write(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos)
{
struct binfmt_misc_entry *e __free(put_binfmt_handler) = NULL;
struct super_block *sb = file_inode(file)->i_sb;
int err;
e = create_entry(buffer, count);
if (IS_ERR(e))
return PTR_ERR(e);
if (test_bit(MISC_FMT_BPF_BIT, &e->flags)) {
e->bpf_ops = binfmt_misc_get_ops(sb->s_user_ns, e->bpf_ops_name);
if (!e->bpf_ops) {
pr_notice("register: no bpf handler named %s\n",
e->bpf_ops_name);
return -ENOENT;
}
}
if (e->flags & MISC_FMT_OPEN_FILE) {
struct file *f = open_interp_file(file->f_cred, e->interpreter);
if (IS_ERR(f))
return PTR_ERR(f);
err = entry_attach_interpreter(e, "", e->interpreter, f);
if (err) {
close_interp_file(f);
return err;
}
}
err = add_entry(e, sb);
if (err)
return err;
retain_and_null_ptr(e);
return count;
}
static const struct file_operations bm_register_operations = {
.write = bm_register_write,
.llseek = noop_llseek,
};
static ssize_t
bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
struct binfmt_misc *misc;
const char *s;
misc = i_binfmt_misc(file_inode(file));
s = READ_ONCE(misc->enabled) ? "enabled\n" : "disabled\n";
return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
}
static ssize_t bm_status_write(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos)
{
struct binfmt_misc *misc;
int res = read_command(buffer, count);
misc = i_binfmt_misc(file_inode(file));
switch (res) {
case BM_CMD_DISABLE:
WRITE_ONCE(misc->enabled, false);
break;
case BM_CMD_ENABLE:
WRITE_ONCE(misc->enabled, true);
break;
case BM_CMD_REMOVE:
bm_remove_all_entries(misc, file_inode(file)->i_sb);
break;
default:
return res;
}
return count;
}
static const struct file_operations bm_status_operations = {
.read = bm_status_read,
.write = bm_status_write,
.llseek = default_llseek,
};
static const struct super_operations bm_super_ops = {
.statfs = simple_statfs,
.evict_inode = bm_evict_inode,
};
static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
{
int err;
struct user_namespace *user_ns = sb->s_user_ns;
struct binfmt_misc *misc;
static const struct tree_descr bm_files[] = {
[2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
[3] = {"register", &bm_register_operations, S_IWUSR},
{""}
};
if (user_ns != current_user_ns())
return -EINVAL;
sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
misc = user_ns->binfmt_misc;
if (!misc) {
misc = kzalloc_obj(struct binfmt_misc);
if (!misc)
return -ENOMEM;
INIT_HLIST_HEAD(&misc->entries);
spin_lock_init(&misc->entries_lock);
smp_store_release(&user_ns->binfmt_misc, misc);
}
WRITE_ONCE(misc->enabled, true);
err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
if (err)
return err;
sb->s_op = &bm_super_ops;
d_inode(sb->s_root)->i_op = &bm_dir_inode_operations;
return 0;
}
static void bm_free(struct fs_context *fc)
{
if (fc->s_fs_info)
put_user_ns(fc->s_fs_info);
}
static int bm_get_tree(struct fs_context *fc)
{
return get_tree_keyed(fc, bm_fill_super, get_user_ns(fc->user_ns));
}
static const struct fs_context_operations bm_context_ops = {
.free = bm_free,
.get_tree = bm_get_tree,
};
static void bm_kill_sb(struct super_block *sb)
{
struct user_namespace *user_ns = sb->s_fs_info;
kill_anon_super(sb);
put_user_ns(user_ns);
}
static int bm_init_fs_context(struct fs_context *fc)
{
fc->ops = &bm_context_ops;
return 0;
}
static struct linux_binfmt misc_format = {
.module = THIS_MODULE,
.load_binary = load_misc_binary,
};
static struct file_system_type bm_fs_type = {
.owner = THIS_MODULE,
.name = "binfmt_misc",
.init_fs_context = bm_init_fs_context,
.fs_flags = FS_USERNS_MOUNT,
.kill_sb = bm_kill_sb,
};
MODULE_ALIAS_FS("binfmt_misc");
static int __init init_misc_binfmt(void)
{
int err = register_filesystem(&bm_fs_type);
if (!err)
insert_binfmt(&misc_format);
return err;
}
static void __exit exit_misc_binfmt(void)
{
unregister_binfmt(&misc_format);
unregister_filesystem(&bm_fs_type);
srcu_barrier(&bm_entries_srcu);
}
core_initcall(init_misc_binfmt);
module_exit(exit_misc_binfmt);
MODULE_DESCRIPTION("Kernel support for miscellaneous binaries");
MODULE_LICENSE("GPL"