Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 3 | * binfmt_misc.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 5 | * Copyright (C) 1997 Richard Günther |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 7 | * binfmt_misc detects binaries via a magic or filename extension and invokes |
Mauro Carvalho Chehab | 34962fb | 2018-05-08 15:14:57 -0300 | [diff] [blame] | 8 | * a specified wrapper. See Documentation/admin-guide/binfmt-misc.rst for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
Rasmus Villemoes | 6ceafb8 | 2015-04-16 12:44:56 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
Ingo Molnar | 589ee62 | 2017-02-04 00:16:44 +0100 | [diff] [blame] | 16 | #include <linux/sched/mm.h> |
Muthu Kumar | b502bd1 | 2012-03-23 15:01:50 -0700 | [diff] [blame] | 17 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/binfmts.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/ctype.h> |
Andy Shevchenko | 8d82e18 | 2013-04-30 15:27:33 -0700 | [diff] [blame] | 21 | #include <linux/string_helpers.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/file.h> |
| 23 | #include <linux/pagemap.h> |
| 24 | #include <linux/namei.h> |
| 25 | #include <linux/mount.h> |
David Howells | bc99a66 | 2019-03-25 16:38:28 +0000 | [diff] [blame] | 26 | #include <linux/fs_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/syscalls.h> |
Akinobu Mita | 6e2c10a | 2008-07-23 21:29:15 -0700 | [diff] [blame] | 28 | #include <linux/fs.h> |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 29 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
James Bottomley | 948b701 | 2016-02-17 16:51:16 -0800 | [diff] [blame] | 31 | #include "internal.h" |
| 32 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 33 | #ifdef DEBUG |
| 34 | # define USE_DEBUG 1 |
| 35 | #else |
| 36 | # define USE_DEBUG 0 |
| 37 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | enum { |
| 40 | VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */ |
| 41 | }; |
| 42 | |
| 43 | static LIST_HEAD(entries); |
| 44 | static int enabled = 1; |
| 45 | |
| 46 | enum {Enabled, Magic}; |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 47 | #define MISC_FMT_PRESERVE_ARGV0 (1 << 31) |
| 48 | #define MISC_FMT_OPEN_BINARY (1 << 30) |
| 49 | #define MISC_FMT_CREDENTIALS (1 << 29) |
James Bottomley | 948b701 | 2016-02-17 16:51:16 -0800 | [diff] [blame] | 50 | #define MISC_FMT_OPEN_FILE (1 << 28) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | typedef struct { |
| 53 | struct list_head list; |
| 54 | unsigned long flags; /* type, status, etc. */ |
| 55 | int offset; /* offset of magic */ |
| 56 | int size; /* size of magic/mask */ |
| 57 | char *magic; /* magic or filename extension */ |
| 58 | char *mask; /* mask, NULL for exact match */ |
Oleg Nesterov | 50097f7 | 2017-10-03 16:15:58 -0700 | [diff] [blame] | 59 | const char *interpreter; /* filename of interpreter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | char *name; |
| 61 | struct dentry *dentry; |
James Bottomley | 948b701 | 2016-02-17 16:51:16 -0800 | [diff] [blame] | 62 | struct file *interp_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } Node; |
| 64 | |
| 65 | static DEFINE_RWLOCK(entries_lock); |
Trond Myklebust | 1f5ce9e | 2006-06-09 09:34:16 -0400 | [diff] [blame] | 66 | static struct file_system_type bm_fs_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static struct vfsmount *bm_mnt; |
| 68 | static int entry_count; |
| 69 | |
Mike Frysinger | bbaecc0 | 2014-10-13 15:52:03 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Max length of the register string. Determined by: |
| 72 | * - 7 delimiters |
| 73 | * - name: ~50 bytes |
| 74 | * - type: 1 byte |
| 75 | * - offset: 3 bytes (has to be smaller than BINPRM_BUF_SIZE) |
| 76 | * - magic: 128 bytes (512 in escaped form) |
| 77 | * - mask: 128 bytes (512 in escaped form) |
| 78 | * - interp: ~50 bytes |
| 79 | * - flags: 5 bytes |
| 80 | * Round that up a bit, and then back off to hold the internal data |
| 81 | * (like struct Node). |
| 82 | */ |
| 83 | #define MAX_REGISTER_LENGTH 1920 |
| 84 | |
| 85 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * Check if we support the binfmt |
| 87 | * if we do, return the node, else NULL |
| 88 | * locking is done in load_misc_binary |
| 89 | */ |
| 90 | static Node *check_file(struct linux_binprm *bprm) |
| 91 | { |
| 92 | char *p = strrchr(bprm->interp, '.'); |
| 93 | struct list_head *l; |
| 94 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 95 | /* Walk all the registered handlers. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | list_for_each(l, &entries) { |
| 97 | Node *e = list_entry(l, Node, list); |
| 98 | char *s; |
| 99 | int j; |
| 100 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 101 | /* Make sure this one is currently enabled. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (!test_bit(Enabled, &e->flags)) |
| 103 | continue; |
| 104 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 105 | /* Do matching based on extension if applicable. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (!test_bit(Magic, &e->flags)) { |
| 107 | if (p && !strcmp(e->magic, p + 1)) |
| 108 | return e; |
| 109 | continue; |
| 110 | } |
| 111 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 112 | /* Do matching based on magic & mask. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | s = bprm->buf + e->offset; |
| 114 | if (e->mask) { |
| 115 | for (j = 0; j < e->size; j++) |
| 116 | if ((*s++ ^ e->magic[j]) & e->mask[j]) |
| 117 | break; |
| 118 | } else { |
| 119 | for (j = 0; j < e->size; j++) |
| 120 | if ((*s++ ^ e->magic[j])) |
| 121 | break; |
| 122 | } |
| 123 | if (j == e->size) |
| 124 | return e; |
| 125 | } |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * the loader itself |
| 131 | */ |
Al Viro | 71613c3 | 2012-10-20 22:00:48 -0400 | [diff] [blame] | 132 | static int load_misc_binary(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | Node *fmt; |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 135 | struct file *interp_file = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | retval = -ENOEXEC; |
| 139 | if (!enabled) |
Oleg Nesterov | 43a4f26 | 2017-10-03 16:15:55 -0700 | [diff] [blame] | 140 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | /* to keep locking time low, we copy the interpreter string */ |
| 143 | read_lock(&entries_lock); |
| 144 | fmt = check_file(bprm); |
Oleg Nesterov | 50097f7 | 2017-10-03 16:15:58 -0700 | [diff] [blame] | 145 | if (fmt) |
Oleg Nesterov | 43a4f26 | 2017-10-03 16:15:55 -0700 | [diff] [blame] | 146 | dget(fmt->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | read_unlock(&entries_lock); |
| 148 | if (!fmt) |
Oleg Nesterov | 43a4f26 | 2017-10-03 16:15:55 -0700 | [diff] [blame] | 149 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
David Drysdale | 51f39a1 | 2014-12-12 16:57:29 -0800 | [diff] [blame] | 151 | /* Need to be able to load the file after exec */ |
Oleg Nesterov | 43a4f26 | 2017-10-03 16:15:55 -0700 | [diff] [blame] | 152 | retval = -ENOENT; |
David Drysdale | 51f39a1 | 2014-12-12 16:57:29 -0800 | [diff] [blame] | 153 | if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE) |
Oleg Nesterov | 43a4f26 | 2017-10-03 16:15:55 -0700 | [diff] [blame] | 154 | goto ret; |
David Drysdale | 51f39a1 | 2014-12-12 16:57:29 -0800 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) { |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 157 | retval = remove_arg_zero(bprm); |
| 158 | if (retval) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 159 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Eric W. Biederman | bc2bf33 | 2020-05-18 18:43:20 -0500 | [diff] [blame] | 162 | if (fmt->flags & MISC_FMT_OPEN_BINARY) |
Eric W. Biederman | b8a61c9 | 2020-05-14 15:17:40 -0500 | [diff] [blame] | 163 | bprm->have_execfd = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | /* make argv[1] be the path to the binary */ |
Christoph Hellwig | 986db2d | 2020-06-04 16:51:14 -0700 | [diff] [blame] | 166 | retval = copy_string_kernel(bprm->interp, bprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | if (retval < 0) |
Eric W. Biederman | b8a61c9 | 2020-05-14 15:17:40 -0500 | [diff] [blame] | 168 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | bprm->argc++; |
| 170 | |
| 171 | /* add the interp as argv[0] */ |
Christoph Hellwig | 986db2d | 2020-06-04 16:51:14 -0700 | [diff] [blame] | 172 | retval = copy_string_kernel(fmt->interpreter, bprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (retval < 0) |
Eric W. Biederman | b8a61c9 | 2020-05-14 15:17:40 -0500 | [diff] [blame] | 174 | goto ret; |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 175 | bprm->argc++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Kees Cook | b66c598 | 2012-12-20 15:05:16 -0800 | [diff] [blame] | 177 | /* Update interp in case binfmt_script needs it. */ |
Oleg Nesterov | 50097f7 | 2017-10-03 16:15:58 -0700 | [diff] [blame] | 178 | retval = bprm_change_interp(fmt->interpreter, bprm); |
Kees Cook | b66c598 | 2012-12-20 15:05:16 -0800 | [diff] [blame] | 179 | if (retval < 0) |
Eric W. Biederman | b8a61c9 | 2020-05-14 15:17:40 -0500 | [diff] [blame] | 180 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Oleg Nesterov | eb23aa0 | 2017-10-03 16:15:51 -0700 | [diff] [blame] | 182 | if (fmt->flags & MISC_FMT_OPEN_FILE) { |
Al Viro | 19f391e | 2018-06-08 11:19:32 -0400 | [diff] [blame] | 183 | interp_file = file_clone_open(fmt->interp_file); |
James Bottomley | 948b701 | 2016-02-17 16:51:16 -0800 | [diff] [blame] | 184 | if (!IS_ERR(interp_file)) |
| 185 | deny_write_access(interp_file); |
| 186 | } else { |
Oleg Nesterov | 50097f7 | 2017-10-03 16:15:58 -0700 | [diff] [blame] | 187 | interp_file = open_exec(fmt->interpreter); |
James Bottomley | 948b701 | 2016-02-17 16:51:16 -0800 | [diff] [blame] | 188 | } |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 189 | retval = PTR_ERR(interp_file); |
| 190 | if (IS_ERR(interp_file)) |
Eric W. Biederman | b8a61c9 | 2020-05-14 15:17:40 -0500 | [diff] [blame] | 191 | goto ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Eric W. Biederman | bc2bf33 | 2020-05-18 18:43:20 -0500 | [diff] [blame] | 193 | bprm->interpreter = interp_file; |
Eric W. Biederman | a16b3357 | 2020-05-16 06:02:54 -0500 | [diff] [blame] | 194 | if (fmt->flags & MISC_FMT_CREDENTIALS) |
Eric W. Biederman | 56305aa | 2020-05-29 22:00:54 -0500 | [diff] [blame] | 195 | bprm->execfd_creds = 1; |
Christoph Hellwig | bdd1d2d | 2017-09-01 17:39:13 +0200 | [diff] [blame] | 196 | |
Eric W. Biederman | bc2bf33 | 2020-05-18 18:43:20 -0500 | [diff] [blame] | 197 | retval = 0; |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 198 | ret: |
Oleg Nesterov | 43a4f26 | 2017-10-03 16:15:55 -0700 | [diff] [blame] | 199 | dput(fmt->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* Command parsers */ |
| 204 | |
| 205 | /* |
| 206 | * parses and copies one argument enclosed in del from *sp to *dp, |
| 207 | * recognising the \x special. |
| 208 | * returns pointer to the copied argument or NULL in case of an |
| 209 | * error (and sets err) or null argument length. |
| 210 | */ |
| 211 | static char *scanarg(char *s, char del) |
| 212 | { |
| 213 | char c; |
| 214 | |
| 215 | while ((c = *s++) != del) { |
| 216 | if (c == '\\' && *s == 'x') { |
| 217 | s++; |
| 218 | if (!isxdigit(*s++)) |
| 219 | return NULL; |
| 220 | if (!isxdigit(*s++)) |
| 221 | return NULL; |
| 222 | } |
| 223 | } |
Al Viro | 7d65cf1 | 2014-12-17 05:29:16 -0500 | [diff] [blame] | 224 | s[-1] ='\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return s; |
| 226 | } |
| 227 | |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 228 | static char *check_special_flags(char *sfs, Node *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 230 | char *p = sfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | int cont = 1; |
| 232 | |
| 233 | /* special flags */ |
| 234 | while (cont) { |
| 235 | switch (*p) { |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 236 | case 'P': |
| 237 | pr_debug("register: flag: P (preserve argv0)\n"); |
| 238 | p++; |
| 239 | e->flags |= MISC_FMT_PRESERVE_ARGV0; |
| 240 | break; |
| 241 | case 'O': |
| 242 | pr_debug("register: flag: O (open binary)\n"); |
| 243 | p++; |
| 244 | e->flags |= MISC_FMT_OPEN_BINARY; |
| 245 | break; |
| 246 | case 'C': |
| 247 | pr_debug("register: flag: C (preserve creds)\n"); |
| 248 | p++; |
| 249 | /* this flags also implies the |
| 250 | open-binary flag */ |
| 251 | e->flags |= (MISC_FMT_CREDENTIALS | |
| 252 | MISC_FMT_OPEN_BINARY); |
| 253 | break; |
James Bottomley | 948b701 | 2016-02-17 16:51:16 -0800 | [diff] [blame] | 254 | case 'F': |
| 255 | pr_debug("register: flag: F: open interpreter file now\n"); |
| 256 | p++; |
| 257 | e->flags |= MISC_FMT_OPEN_FILE; |
| 258 | break; |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 259 | default: |
| 260 | cont = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | return p; |
| 265 | } |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* |
| 268 | * This registers a new binary format, it recognises the syntax |
| 269 | * ':name:type:offset:magic:mask:interpreter:flags' |
| 270 | * where the ':' is the IFS, that can be chosen with the first char |
| 271 | */ |
| 272 | static Node *create_entry(const char __user *buffer, size_t count) |
| 273 | { |
| 274 | Node *e; |
| 275 | int memsize, err; |
| 276 | char *buf, *p; |
| 277 | char del; |
| 278 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 279 | pr_debug("register: received %zu bytes\n", count); |
| 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | /* some sanity checks */ |
| 282 | err = -EINVAL; |
Mike Frysinger | bbaecc0 | 2014-10-13 15:52:03 -0700 | [diff] [blame] | 283 | if ((count < 11) || (count > MAX_REGISTER_LENGTH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | goto out; |
| 285 | |
| 286 | err = -ENOMEM; |
| 287 | memsize = sizeof(Node) + count + 8; |
Andrew Morton | f7e1ad1 | 2014-12-10 15:52:13 -0800 | [diff] [blame] | 288 | e = kmalloc(memsize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | if (!e) |
| 290 | goto out; |
| 291 | |
| 292 | p = buf = (char *)e + sizeof(Node); |
| 293 | |
| 294 | memset(e, 0, sizeof(Node)); |
| 295 | if (copy_from_user(buf, buffer, count)) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 296 | goto efault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | del = *p++; /* delimeter */ |
| 299 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 300 | pr_debug("register: delim: %#x {%c}\n", del, del); |
| 301 | |
| 302 | /* Pad the buffer with the delim to simplify parsing below. */ |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 303 | memset(buf + count, del, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 305 | /* Parse the 'name' field. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | e->name = p; |
| 307 | p = strchr(p, del); |
| 308 | if (!p) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 309 | goto einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | *p++ = '\0'; |
| 311 | if (!e->name[0] || |
| 312 | !strcmp(e->name, ".") || |
| 313 | !strcmp(e->name, "..") || |
| 314 | strchr(e->name, '/')) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 315 | goto einval; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 316 | |
| 317 | pr_debug("register: name: {%s}\n", e->name); |
| 318 | |
| 319 | /* Parse the 'type' field. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | switch (*p++) { |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 321 | case 'E': |
| 322 | pr_debug("register: type: E (extension)\n"); |
| 323 | e->flags = 1 << Enabled; |
| 324 | break; |
| 325 | case 'M': |
| 326 | pr_debug("register: type: M (magic)\n"); |
| 327 | e->flags = (1 << Enabled) | (1 << Magic); |
| 328 | break; |
| 329 | default: |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 330 | goto einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | if (*p++ != del) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 333 | goto einval; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | if (test_bit(Magic, &e->flags)) { |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 336 | /* Handle the 'M' (magic) format. */ |
| 337 | char *s; |
| 338 | |
| 339 | /* Parse the 'offset' field. */ |
| 340 | s = strchr(p, del); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (!s) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 342 | goto einval; |
Thadeu Lima de Souza Cascardo | 5cc41e0 | 2018-06-07 17:11:01 -0700 | [diff] [blame] | 343 | *s = '\0'; |
| 344 | if (p != s) { |
| 345 | int r = kstrtoint(p, 10, &e->offset); |
| 346 | if (r != 0 || e->offset < 0) |
| 347 | goto einval; |
| 348 | } |
| 349 | p = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (*p++) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 351 | goto einval; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 352 | pr_debug("register: offset: %#x\n", e->offset); |
| 353 | |
| 354 | /* Parse the 'magic' field. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | e->magic = p; |
| 356 | p = scanarg(p, del); |
| 357 | if (!p) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 358 | goto einval; |
Al Viro | 7d65cf1 | 2014-12-17 05:29:16 -0500 | [diff] [blame] | 359 | if (!e->magic[0]) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 360 | goto einval; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 361 | if (USE_DEBUG) |
| 362 | print_hex_dump_bytes( |
| 363 | KBUILD_MODNAME ": register: magic[raw]: ", |
| 364 | DUMP_PREFIX_NONE, e->magic, p - e->magic); |
| 365 | |
| 366 | /* Parse the 'mask' field. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | e->mask = p; |
| 368 | p = scanarg(p, del); |
| 369 | if (!p) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 370 | goto einval; |
Al Viro | 7d65cf1 | 2014-12-17 05:29:16 -0500 | [diff] [blame] | 371 | if (!e->mask[0]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | e->mask = NULL; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 373 | pr_debug("register: mask[raw]: none\n"); |
| 374 | } else if (USE_DEBUG) |
| 375 | print_hex_dump_bytes( |
| 376 | KBUILD_MODNAME ": register: mask[raw]: ", |
| 377 | DUMP_PREFIX_NONE, e->mask, p - e->mask); |
| 378 | |
| 379 | /* |
| 380 | * Decode the magic & mask fields. |
| 381 | * Note: while we might have accepted embedded NUL bytes from |
| 382 | * above, the unescape helpers here will stop at the first one |
| 383 | * it encounters. |
| 384 | */ |
Andy Shevchenko | 8d82e18 | 2013-04-30 15:27:33 -0700 | [diff] [blame] | 385 | e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX); |
| 386 | if (e->mask && |
| 387 | string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 388 | goto einval; |
Thadeu Lima de Souza Cascardo | 5cc41e0 | 2018-06-07 17:11:01 -0700 | [diff] [blame] | 389 | if (e->size > BINPRM_BUF_SIZE || |
| 390 | BINPRM_BUF_SIZE - e->size < e->offset) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 391 | goto einval; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 392 | pr_debug("register: magic/mask length: %i\n", e->size); |
| 393 | if (USE_DEBUG) { |
| 394 | print_hex_dump_bytes( |
| 395 | KBUILD_MODNAME ": register: magic[decoded]: ", |
| 396 | DUMP_PREFIX_NONE, e->magic, e->size); |
| 397 | |
| 398 | if (e->mask) { |
| 399 | int i; |
Andrew Morton | f7e1ad1 | 2014-12-10 15:52:13 -0800 | [diff] [blame] | 400 | char *masked = kmalloc(e->size, GFP_KERNEL); |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 401 | |
| 402 | print_hex_dump_bytes( |
| 403 | KBUILD_MODNAME ": register: mask[decoded]: ", |
| 404 | DUMP_PREFIX_NONE, e->mask, e->size); |
| 405 | |
| 406 | if (masked) { |
| 407 | for (i = 0; i < e->size; ++i) |
| 408 | masked[i] = e->magic[i] & e->mask[i]; |
| 409 | print_hex_dump_bytes( |
| 410 | KBUILD_MODNAME ": register: magic[masked]: ", |
| 411 | DUMP_PREFIX_NONE, masked, e->size); |
| 412 | |
| 413 | kfree(masked); |
| 414 | } |
| 415 | } |
| 416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } else { |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 418 | /* Handle the 'E' (extension) format. */ |
| 419 | |
| 420 | /* Skip the 'offset' field. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | p = strchr(p, del); |
| 422 | if (!p) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 423 | goto einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | *p++ = '\0'; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 425 | |
| 426 | /* Parse the 'magic' field. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | e->magic = p; |
| 428 | p = strchr(p, del); |
| 429 | if (!p) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 430 | goto einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | *p++ = '\0'; |
| 432 | if (!e->magic[0] || strchr(e->magic, '/')) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 433 | goto einval; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 434 | pr_debug("register: extension: {%s}\n", e->magic); |
| 435 | |
| 436 | /* Skip the 'mask' field. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | p = strchr(p, del); |
| 438 | if (!p) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 439 | goto einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | *p++ = '\0'; |
| 441 | } |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 442 | |
| 443 | /* Parse the 'interpreter' field. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | e->interpreter = p; |
| 445 | p = strchr(p, del); |
| 446 | if (!p) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 447 | goto einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | *p++ = '\0'; |
| 449 | if (!e->interpreter[0]) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 450 | goto einval; |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 451 | pr_debug("register: interpreter: {%s}\n", e->interpreter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Mike Frysinger | 6b899c4 | 2014-12-10 15:52:08 -0800 | [diff] [blame] | 453 | /* Parse the 'flags' field. */ |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 454 | p = check_special_flags(p, e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | if (*p == '\n') |
| 456 | p++; |
| 457 | if (p != buf + count) |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 458 | goto einval; |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return e; |
| 461 | |
| 462 | out: |
| 463 | return ERR_PTR(err); |
| 464 | |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 465 | efault: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | kfree(e); |
| 467 | return ERR_PTR(-EFAULT); |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 468 | einval: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | kfree(e); |
| 470 | return ERR_PTR(-EINVAL); |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Set status of entry/binfmt_misc: |
| 475 | * '1' enables, '0' disables and '-1' clears entry/binfmt_misc |
| 476 | */ |
| 477 | static int parse_command(const char __user *buffer, size_t count) |
| 478 | { |
| 479 | char s[4]; |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (count > 3) |
| 482 | return -EINVAL; |
| 483 | if (copy_from_user(s, buffer, count)) |
| 484 | return -EFAULT; |
Arnd Bergmann | de8288b | 2014-10-13 15:52:08 -0700 | [diff] [blame] | 485 | if (!count) |
| 486 | return 0; |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 487 | if (s[count - 1] == '\n') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | count--; |
| 489 | if (count == 1 && s[0] == '0') |
| 490 | return 1; |
| 491 | if (count == 1 && s[0] == '1') |
| 492 | return 2; |
| 493 | if (count == 2 && s[0] == '-' && s[1] == '1') |
| 494 | return 3; |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
| 498 | /* generic stuff */ |
| 499 | |
| 500 | static void entry_status(Node *e, char *page) |
| 501 | { |
Rasmus Villemoes | 6ceafb8 | 2015-04-16 12:44:56 -0700 | [diff] [blame] | 502 | char *dp = page; |
| 503 | const char *status = "disabled"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | if (test_bit(Enabled, &e->flags)) |
| 506 | status = "enabled"; |
| 507 | |
| 508 | if (!VERBOSE_STATUS) { |
| 509 | sprintf(page, "%s\n", status); |
| 510 | return; |
| 511 | } |
| 512 | |
Rasmus Villemoes | 6ceafb8 | 2015-04-16 12:44:56 -0700 | [diff] [blame] | 513 | dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | /* print the special flags */ |
Rasmus Villemoes | 6ceafb8 | 2015-04-16 12:44:56 -0700 | [diff] [blame] | 516 | dp += sprintf(dp, "flags: "); |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 517 | if (e->flags & MISC_FMT_PRESERVE_ARGV0) |
| 518 | *dp++ = 'P'; |
| 519 | if (e->flags & MISC_FMT_OPEN_BINARY) |
| 520 | *dp++ = 'O'; |
| 521 | if (e->flags & MISC_FMT_CREDENTIALS) |
| 522 | *dp++ = 'C'; |
James Bottomley | 948b701 | 2016-02-17 16:51:16 -0800 | [diff] [blame] | 523 | if (e->flags & MISC_FMT_OPEN_FILE) |
| 524 | *dp++ = 'F'; |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 525 | *dp++ = '\n'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | if (!test_bit(Magic, &e->flags)) { |
| 528 | sprintf(dp, "extension .%s\n", e->magic); |
| 529 | } else { |
Rasmus Villemoes | 6ceafb8 | 2015-04-16 12:44:56 -0700 | [diff] [blame] | 530 | dp += sprintf(dp, "offset %i\nmagic ", e->offset); |
| 531 | dp = bin2hex(dp, e->magic, e->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (e->mask) { |
Rasmus Villemoes | 6ceafb8 | 2015-04-16 12:44:56 -0700 | [diff] [blame] | 533 | dp += sprintf(dp, "\nmask "); |
| 534 | dp = bin2hex(dp, e->mask, e->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | *dp++ = '\n'; |
| 537 | *dp = '\0'; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | static struct inode *bm_get_inode(struct super_block *sb, int mode) |
| 542 | { |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 543 | struct inode *inode = new_inode(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
| 545 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 546 | inode->i_ino = get_next_ino(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | inode->i_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | inode->i_atime = inode->i_mtime = inode->i_ctime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 549 | current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | return inode; |
| 552 | } |
| 553 | |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 554 | static void bm_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
Oleg Nesterov | 83f9182 | 2017-10-03 16:15:48 -0700 | [diff] [blame] | 556 | Node *e = inode->i_private; |
| 557 | |
Eryu Guan | 7e86600 | 2017-10-13 15:58:18 -0700 | [diff] [blame] | 558 | if (e && e->flags & MISC_FMT_OPEN_FILE) |
Oleg Nesterov | 83f9182 | 2017-10-03 16:15:48 -0700 | [diff] [blame] | 559 | filp_close(e->interp_file, NULL); |
| 560 | |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 561 | clear_inode(inode); |
Oleg Nesterov | 83f9182 | 2017-10-03 16:15:48 -0700 | [diff] [blame] | 562 | kfree(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static void kill_node(Node *e) |
| 566 | { |
| 567 | struct dentry *dentry; |
| 568 | |
| 569 | write_lock(&entries_lock); |
Oleg Nesterov | baba1b2 | 2017-10-03 16:15:45 -0700 | [diff] [blame] | 570 | list_del_init(&e->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | write_unlock(&entries_lock); |
| 572 | |
Oleg Nesterov | baba1b2 | 2017-10-03 16:15:45 -0700 | [diff] [blame] | 573 | dentry = e->dentry; |
| 574 | drop_nlink(d_inode(dentry)); |
| 575 | d_drop(dentry); |
| 576 | dput(dentry); |
| 577 | simple_release_fs(&bm_mnt, &entry_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* /<entry> */ |
| 581 | |
| 582 | static ssize_t |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 583 | bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 585 | Node *e = file_inode(file)->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | ssize_t res; |
| 587 | char *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 589 | page = (char *) __get_free_page(GFP_KERNEL); |
| 590 | if (!page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | return -ENOMEM; |
| 592 | |
| 593 | entry_status(e, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Akinobu Mita | 6e2c10a | 2008-07-23 21:29:15 -0700 | [diff] [blame] | 595 | res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page)); |
| 596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | free_page((unsigned long) page); |
| 598 | return res; |
| 599 | } |
| 600 | |
| 601 | static ssize_t bm_entry_write(struct file *file, const char __user *buffer, |
| 602 | size_t count, loff_t *ppos) |
| 603 | { |
| 604 | struct dentry *root; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 605 | Node *e = file_inode(file)->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | int res = parse_command(buffer, count); |
| 607 | |
| 608 | switch (res) { |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 609 | case 1: |
| 610 | /* Disable this handler. */ |
| 611 | clear_bit(Enabled, &e->flags); |
| 612 | break; |
| 613 | case 2: |
| 614 | /* Enable this handler. */ |
| 615 | set_bit(Enabled, &e->flags); |
| 616 | break; |
| 617 | case 3: |
| 618 | /* Delete this handler. */ |
Al Viro | ea7d4c0 | 2016-05-29 19:14:03 -0400 | [diff] [blame] | 619 | root = file_inode(file)->i_sb->s_root; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 620 | inode_lock(d_inode(root)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Oleg Nesterov | baba1b2 | 2017-10-03 16:15:45 -0700 | [diff] [blame] | 622 | if (!list_empty(&e->list)) |
| 623 | kill_node(e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 625 | inode_unlock(d_inode(root)); |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 626 | break; |
| 627 | default: |
| 628 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | return count; |
| 632 | } |
| 633 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 634 | static const struct file_operations bm_entry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | .read = bm_entry_read, |
| 636 | .write = bm_entry_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 637 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | }; |
| 639 | |
| 640 | /* /register */ |
| 641 | |
| 642 | static ssize_t bm_register_write(struct file *file, const char __user *buffer, |
| 643 | size_t count, loff_t *ppos) |
| 644 | { |
| 645 | Node *e; |
| 646 | struct inode *inode; |
Al Viro | ea7d4c0 | 2016-05-29 19:14:03 -0400 | [diff] [blame] | 647 | struct super_block *sb = file_inode(file)->i_sb; |
| 648 | struct dentry *root = sb->s_root, *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | int err = 0; |
Lior Ribak | 5ab9464 | 2021-03-12 21:07:41 -0800 | [diff] [blame] | 650 | struct file *f = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
| 652 | e = create_entry(buffer, count); |
| 653 | |
| 654 | if (IS_ERR(e)) |
| 655 | return PTR_ERR(e); |
| 656 | |
Lior Ribak | 5ab9464 | 2021-03-12 21:07:41 -0800 | [diff] [blame] | 657 | if (e->flags & MISC_FMT_OPEN_FILE) { |
| 658 | f = open_exec(e->interpreter); |
| 659 | if (IS_ERR(f)) { |
| 660 | pr_notice("register: failed to install interpreter file %s\n", |
| 661 | e->interpreter); |
| 662 | kfree(e); |
| 663 | return PTR_ERR(f); |
| 664 | } |
| 665 | e->interp_file = f; |
| 666 | } |
| 667 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 668 | inode_lock(d_inode(root)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | dentry = lookup_one_len(e->name, root, strlen(e->name)); |
| 670 | err = PTR_ERR(dentry); |
| 671 | if (IS_ERR(dentry)) |
| 672 | goto out; |
| 673 | |
| 674 | err = -EEXIST; |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 675 | if (d_really_is_positive(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | goto out2; |
| 677 | |
| 678 | inode = bm_get_inode(sb, S_IFREG | 0644); |
| 679 | |
| 680 | err = -ENOMEM; |
| 681 | if (!inode) |
| 682 | goto out2; |
| 683 | |
Trond Myklebust | 1f5ce9e | 2006-06-09 09:34:16 -0400 | [diff] [blame] | 684 | err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (err) { |
| 686 | iput(inode); |
| 687 | inode = NULL; |
| 688 | goto out2; |
| 689 | } |
| 690 | |
| 691 | e->dentry = dget(dentry); |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 692 | inode->i_private = e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | inode->i_fop = &bm_entry_operations; |
| 694 | |
| 695 | d_instantiate(dentry, inode); |
| 696 | write_lock(&entries_lock); |
| 697 | list_add(&e->list, &entries); |
| 698 | write_unlock(&entries_lock); |
| 699 | |
| 700 | err = 0; |
| 701 | out2: |
| 702 | dput(dentry); |
| 703 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 704 | inode_unlock(d_inode(root)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | if (err) { |
Lior Ribak | 5ab9464 | 2021-03-12 21:07:41 -0800 | [diff] [blame] | 707 | if (f) |
| 708 | filp_close(f, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | kfree(e); |
James Bottomley | 948b701 | 2016-02-17 16:51:16 -0800 | [diff] [blame] | 710 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | return count; |
| 713 | } |
| 714 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 715 | static const struct file_operations bm_register_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | .write = bm_register_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 717 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | }; |
| 719 | |
| 720 | /* /status */ |
| 721 | |
| 722 | static ssize_t |
| 723 | bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) |
| 724 | { |
Qinghuang Feng | 87113e8 | 2009-01-06 14:41:38 -0800 | [diff] [blame] | 725 | char *s = enabled ? "enabled\n" : "disabled\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Akinobu Mita | 92f4c70 | 2007-05-09 02:33:32 -0700 | [diff] [blame] | 727 | return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 730 | static ssize_t bm_status_write(struct file *file, const char __user *buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | size_t count, loff_t *ppos) |
| 732 | { |
| 733 | int res = parse_command(buffer, count); |
| 734 | struct dentry *root; |
| 735 | |
| 736 | switch (res) { |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 737 | case 1: |
| 738 | /* Disable all handlers. */ |
| 739 | enabled = 0; |
| 740 | break; |
| 741 | case 2: |
| 742 | /* Enable all handlers. */ |
| 743 | enabled = 1; |
| 744 | break; |
| 745 | case 3: |
| 746 | /* Delete all handlers. */ |
Al Viro | ea7d4c0 | 2016-05-29 19:14:03 -0400 | [diff] [blame] | 747 | root = file_inode(file)->i_sb->s_root; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 748 | inode_lock(d_inode(root)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 750 | while (!list_empty(&entries)) |
Oleg Nesterov | baba1b2 | 2017-10-03 16:15:45 -0700 | [diff] [blame] | 751 | kill_node(list_first_entry(&entries, Node, list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 753 | inode_unlock(d_inode(root)); |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 754 | break; |
| 755 | default: |
| 756 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | } |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | return count; |
| 760 | } |
| 761 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 762 | static const struct file_operations bm_status_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | .read = bm_status_read, |
| 764 | .write = bm_status_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 765 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | }; |
| 767 | |
| 768 | /* Superblock handling */ |
| 769 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 770 | static const struct super_operations s_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | .statfs = simple_statfs, |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 772 | .evict_inode = bm_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | }; |
| 774 | |
David Howells | bc99a66 | 2019-03-25 16:38:28 +0000 | [diff] [blame] | 775 | static int bm_fill_super(struct super_block *sb, struct fs_context *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 777 | int err; |
Eric Biggers | cda3712 | 2017-03-25 21:15:37 -0700 | [diff] [blame] | 778 | static const struct tree_descr bm_files[] = { |
Jeff Layton | 1a1c9bb | 2007-05-08 00:32:31 -0700 | [diff] [blame] | 779 | [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO}, |
| 780 | [3] = {"register", &bm_register_operations, S_IWUSR}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | /* last one */ {""} |
| 782 | }; |
Mike Frysinger | e6084d4 | 2014-12-10 15:52:10 -0800 | [diff] [blame] | 783 | |
| 784 | err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (!err) |
| 786 | sb->s_op = &s_ops; |
| 787 | return err; |
| 788 | } |
| 789 | |
David Howells | bc99a66 | 2019-03-25 16:38:28 +0000 | [diff] [blame] | 790 | static int bm_get_tree(struct fs_context *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
David Howells | bc99a66 | 2019-03-25 16:38:28 +0000 | [diff] [blame] | 792 | return get_tree_single(fc, bm_fill_super); |
| 793 | } |
| 794 | |
| 795 | static const struct fs_context_operations bm_context_ops = { |
| 796 | .get_tree = bm_get_tree, |
| 797 | }; |
| 798 | |
| 799 | static int bm_init_fs_context(struct fs_context *fc) |
| 800 | { |
| 801 | fc->ops = &bm_context_ops; |
| 802 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static struct linux_binfmt misc_format = { |
| 806 | .module = THIS_MODULE, |
| 807 | .load_binary = load_misc_binary, |
| 808 | }; |
| 809 | |
| 810 | static struct file_system_type bm_fs_type = { |
| 811 | .owner = THIS_MODULE, |
| 812 | .name = "binfmt_misc", |
David Howells | bc99a66 | 2019-03-25 16:38:28 +0000 | [diff] [blame] | 813 | .init_fs_context = bm_init_fs_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | .kill_sb = kill_litter_super, |
| 815 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 816 | MODULE_ALIAS_FS("binfmt_misc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | static int __init init_misc_binfmt(void) |
| 819 | { |
| 820 | int err = register_filesystem(&bm_fs_type); |
Al Viro | 8fc3dc5 | 2012-03-17 03:05:16 -0400 | [diff] [blame] | 821 | if (!err) |
| 822 | insert_binfmt(&misc_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | return err; |
| 824 | } |
| 825 | |
| 826 | static void __exit exit_misc_binfmt(void) |
| 827 | { |
| 828 | unregister_binfmt(&misc_format); |
| 829 | unregister_filesystem(&bm_fs_type); |
| 830 | } |
| 831 | |
| 832 | core_initcall(init_misc_binfmt); |
| 833 | module_exit(exit_misc_binfmt); |
| 834 | MODULE_LICENSE("GPL"); |