blob: e1eae7ea823aeb2d5d0b245511658fcdb3a3ed09 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Mike Frysingere6084d42014-12-10 15:52:10 -08003 * binfmt_misc.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Mike Frysingere6084d42014-12-10 15:52:10 -08005 * Copyright (C) 1997 Richard Günther
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Mike Frysingere6084d42014-12-10 15:52:10 -08007 * binfmt_misc detects binaries via a magic or filename extension and invokes
Mauro Carvalho Chehab34962fb2018-05-08 15:14:57 -03008 * a specified wrapper. See Documentation/admin-guide/binfmt-misc.rst for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Mike Frysinger6b899c42014-12-10 15:52:08 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Rasmus Villemoes6ceafb82015-04-16 12:44:56 -070013#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/init.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010016#include <linux/sched/mm.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070017#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/binfmts.h>
19#include <linux/slab.h>
20#include <linux/ctype.h>
Andy Shevchenko8d82e182013-04-30 15:27:33 -070021#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/file.h>
23#include <linux/pagemap.h>
24#include <linux/namei.h>
25#include <linux/mount.h>
David Howellsbc99a662019-03-25 16:38:28 +000026#include <linux/fs_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/syscalls.h>
Akinobu Mita6e2c10a2008-07-23 21:29:15 -070028#include <linux/fs.h>
Mike Frysinger6b899c42014-12-10 15:52:08 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
James Bottomley948b7012016-02-17 16:51:16 -080031#include "internal.h"
32
Mike Frysinger6b899c42014-12-10 15:52:08 -080033#ifdef DEBUG
34# define USE_DEBUG 1
35#else
36# define USE_DEBUG 0
37#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39enum {
40 VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
41};
42
43static LIST_HEAD(entries);
44static int enabled = 1;
45
46enum {Enabled, Magic};
Mike Frysingere6084d42014-12-10 15:52:10 -080047#define MISC_FMT_PRESERVE_ARGV0 (1 << 31)
48#define MISC_FMT_OPEN_BINARY (1 << 30)
49#define MISC_FMT_CREDENTIALS (1 << 29)
James Bottomley948b7012016-02-17 16:51:16 -080050#define MISC_FMT_OPEN_FILE (1 << 28)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52typedef 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 Nesterov50097f72017-10-03 16:15:58 -070059 const char *interpreter; /* filename of interpreter */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 char *name;
61 struct dentry *dentry;
James Bottomley948b7012016-02-17 16:51:16 -080062 struct file *interp_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063} Node;
64
65static DEFINE_RWLOCK(entries_lock);
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -040066static struct file_system_type bm_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static struct vfsmount *bm_mnt;
68static int entry_count;
69
Mike Frysingerbbaecc02014-10-13 15:52:03 -070070/*
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 Torvalds1da177e2005-04-16 15:20:36 -070086 * Check if we support the binfmt
87 * if we do, return the node, else NULL
88 * locking is done in load_misc_binary
89 */
90static Node *check_file(struct linux_binprm *bprm)
91{
92 char *p = strrchr(bprm->interp, '.');
93 struct list_head *l;
94
Mike Frysinger6b899c42014-12-10 15:52:08 -080095 /* Walk all the registered handlers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 list_for_each(l, &entries) {
97 Node *e = list_entry(l, Node, list);
98 char *s;
99 int j;
100
Mike Frysinger6b899c42014-12-10 15:52:08 -0800101 /* Make sure this one is currently enabled. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!test_bit(Enabled, &e->flags))
103 continue;
104
Mike Frysinger6b899c42014-12-10 15:52:08 -0800105 /* Do matching based on extension if applicable. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (!test_bit(Magic, &e->flags)) {
107 if (p && !strcmp(e->magic, p + 1))
108 return e;
109 continue;
110 }
111
Mike Frysinger6b899c42014-12-10 15:52:08 -0800112 /* Do matching based on magic & mask. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 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 Viro71613c32012-10-20 22:00:48 -0400132static int load_misc_binary(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 Node *fmt;
Mike Frysingere6084d42014-12-10 15:52:10 -0800135 struct file *interp_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 retval = -ENOEXEC;
139 if (!enabled)
Oleg Nesterov43a4f262017-10-03 16:15:55 -0700140 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /* to keep locking time low, we copy the interpreter string */
143 read_lock(&entries_lock);
144 fmt = check_file(bprm);
Oleg Nesterov50097f72017-10-03 16:15:58 -0700145 if (fmt)
Oleg Nesterov43a4f262017-10-03 16:15:55 -0700146 dget(fmt->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 read_unlock(&entries_lock);
148 if (!fmt)
Oleg Nesterov43a4f262017-10-03 16:15:55 -0700149 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
David Drysdale51f39a12014-12-12 16:57:29 -0800151 /* Need to be able to load the file after exec */
Oleg Nesterov43a4f262017-10-03 16:15:55 -0700152 retval = -ENOENT;
David Drysdale51f39a12014-12-12 16:57:29 -0800153 if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
Oleg Nesterov43a4f262017-10-03 16:15:55 -0700154 goto ret;
David Drysdale51f39a12014-12-12 16:57:29 -0800155
Laurent Vivier23479612020-01-28 14:25:39 +0100156 if (fmt->flags & MISC_FMT_PRESERVE_ARGV0) {
157 bprm->interp_flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
158 } else {
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700159 retval = remove_arg_zero(bprm);
160 if (retval)
Mike Frysingere6084d42014-12-10 15:52:10 -0800161 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163
Eric W. Biedermanbc2bf332020-05-18 18:43:20 -0500164 if (fmt->flags & MISC_FMT_OPEN_BINARY)
Eric W. Biedermanb8a61c92020-05-14 15:17:40 -0500165 bprm->have_execfd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* make argv[1] be the path to the binary */
Christoph Hellwig986db2d2020-06-04 16:51:14 -0700168 retval = copy_string_kernel(bprm->interp, bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (retval < 0)
Eric W. Biedermanb8a61c92020-05-14 15:17:40 -0500170 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 bprm->argc++;
172
173 /* add the interp as argv[0] */
Christoph Hellwig986db2d2020-06-04 16:51:14 -0700174 retval = copy_string_kernel(fmt->interpreter, bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (retval < 0)
Eric W. Biedermanb8a61c92020-05-14 15:17:40 -0500176 goto ret;
Mike Frysingere6084d42014-12-10 15:52:10 -0800177 bprm->argc++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Kees Cookb66c5982012-12-20 15:05:16 -0800179 /* Update interp in case binfmt_script needs it. */
Oleg Nesterov50097f72017-10-03 16:15:58 -0700180 retval = bprm_change_interp(fmt->interpreter, bprm);
Kees Cookb66c5982012-12-20 15:05:16 -0800181 if (retval < 0)
Eric W. Biedermanb8a61c92020-05-14 15:17:40 -0500182 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Oleg Nesteroveb23aa02017-10-03 16:15:51 -0700184 if (fmt->flags & MISC_FMT_OPEN_FILE) {
Al Viro19f391e2018-06-08 11:19:32 -0400185 interp_file = file_clone_open(fmt->interp_file);
James Bottomley948b7012016-02-17 16:51:16 -0800186 if (!IS_ERR(interp_file))
187 deny_write_access(interp_file);
188 } else {
Oleg Nesterov50097f72017-10-03 16:15:58 -0700189 interp_file = open_exec(fmt->interpreter);
James Bottomley948b7012016-02-17 16:51:16 -0800190 }
Mike Frysingere6084d42014-12-10 15:52:10 -0800191 retval = PTR_ERR(interp_file);
192 if (IS_ERR(interp_file))
Eric W. Biedermanb8a61c92020-05-14 15:17:40 -0500193 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Eric W. Biedermanbc2bf332020-05-18 18:43:20 -0500195 bprm->interpreter = interp_file;
Eric W. Biedermana16b33572020-05-16 06:02:54 -0500196 if (fmt->flags & MISC_FMT_CREDENTIALS)
Eric W. Biederman56305aa2020-05-29 22:00:54 -0500197 bprm->execfd_creds = 1;
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200198
Eric W. Biedermanbc2bf332020-05-18 18:43:20 -0500199 retval = 0;
Mike Frysingere6084d42014-12-10 15:52:10 -0800200ret:
Oleg Nesterov43a4f262017-10-03 16:15:55 -0700201 dput(fmt->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/* Command parsers */
206
207/*
208 * parses and copies one argument enclosed in del from *sp to *dp,
209 * recognising the \x special.
210 * returns pointer to the copied argument or NULL in case of an
211 * error (and sets err) or null argument length.
212 */
213static char *scanarg(char *s, char del)
214{
215 char c;
216
217 while ((c = *s++) != del) {
218 if (c == '\\' && *s == 'x') {
219 s++;
220 if (!isxdigit(*s++))
221 return NULL;
222 if (!isxdigit(*s++))
223 return NULL;
224 }
225 }
Al Viro7d65cf12014-12-17 05:29:16 -0500226 s[-1] ='\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return s;
228}
229
Mike Frysingere6084d42014-12-10 15:52:10 -0800230static char *check_special_flags(char *sfs, Node *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Mike Frysingere6084d42014-12-10 15:52:10 -0800232 char *p = sfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int cont = 1;
234
235 /* special flags */
236 while (cont) {
237 switch (*p) {
Mike Frysingere6084d42014-12-10 15:52:10 -0800238 case 'P':
239 pr_debug("register: flag: P (preserve argv0)\n");
240 p++;
241 e->flags |= MISC_FMT_PRESERVE_ARGV0;
242 break;
243 case 'O':
244 pr_debug("register: flag: O (open binary)\n");
245 p++;
246 e->flags |= MISC_FMT_OPEN_BINARY;
247 break;
248 case 'C':
249 pr_debug("register: flag: C (preserve creds)\n");
250 p++;
251 /* this flags also implies the
252 open-binary flag */
253 e->flags |= (MISC_FMT_CREDENTIALS |
254 MISC_FMT_OPEN_BINARY);
255 break;
James Bottomley948b7012016-02-17 16:51:16 -0800256 case 'F':
257 pr_debug("register: flag: F: open interpreter file now\n");
258 p++;
259 e->flags |= MISC_FMT_OPEN_FILE;
260 break;
Mike Frysingere6084d42014-12-10 15:52:10 -0800261 default:
262 cont = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 }
265
266 return p;
267}
Mike Frysingere6084d42014-12-10 15:52:10 -0800268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
270 * This registers a new binary format, it recognises the syntax
271 * ':name:type:offset:magic:mask:interpreter:flags'
272 * where the ':' is the IFS, that can be chosen with the first char
273 */
274static Node *create_entry(const char __user *buffer, size_t count)
275{
276 Node *e;
277 int memsize, err;
278 char *buf, *p;
279 char del;
280
Mike Frysinger6b899c42014-12-10 15:52:08 -0800281 pr_debug("register: received %zu bytes\n", count);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* some sanity checks */
284 err = -EINVAL;
Mike Frysingerbbaecc02014-10-13 15:52:03 -0700285 if ((count < 11) || (count > MAX_REGISTER_LENGTH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto out;
287
288 err = -ENOMEM;
289 memsize = sizeof(Node) + count + 8;
Andrew Mortonf7e1ad12014-12-10 15:52:13 -0800290 e = kmalloc(memsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!e)
292 goto out;
293
294 p = buf = (char *)e + sizeof(Node);
295
296 memset(e, 0, sizeof(Node));
297 if (copy_from_user(buf, buffer, count))
Mike Frysingere6084d42014-12-10 15:52:10 -0800298 goto efault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 del = *p++; /* delimeter */
301
Mike Frysinger6b899c42014-12-10 15:52:08 -0800302 pr_debug("register: delim: %#x {%c}\n", del, del);
303
304 /* Pad the buffer with the delim to simplify parsing below. */
Mike Frysingere6084d42014-12-10 15:52:10 -0800305 memset(buf + count, del, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Mike Frysinger6b899c42014-12-10 15:52:08 -0800307 /* Parse the 'name' field. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 e->name = p;
309 p = strchr(p, del);
310 if (!p)
Mike Frysingere6084d42014-12-10 15:52:10 -0800311 goto einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 *p++ = '\0';
313 if (!e->name[0] ||
314 !strcmp(e->name, ".") ||
315 !strcmp(e->name, "..") ||
316 strchr(e->name, '/'))
Mike Frysingere6084d42014-12-10 15:52:10 -0800317 goto einval;
Mike Frysinger6b899c42014-12-10 15:52:08 -0800318
319 pr_debug("register: name: {%s}\n", e->name);
320
321 /* Parse the 'type' field. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 switch (*p++) {
Mike Frysinger6b899c42014-12-10 15:52:08 -0800323 case 'E':
324 pr_debug("register: type: E (extension)\n");
325 e->flags = 1 << Enabled;
326 break;
327 case 'M':
328 pr_debug("register: type: M (magic)\n");
329 e->flags = (1 << Enabled) | (1 << Magic);
330 break;
331 default:
Mike Frysingere6084d42014-12-10 15:52:10 -0800332 goto einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 if (*p++ != del)
Mike Frysingere6084d42014-12-10 15:52:10 -0800335 goto einval;
Mike Frysinger6b899c42014-12-10 15:52:08 -0800336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (test_bit(Magic, &e->flags)) {
Mike Frysinger6b899c42014-12-10 15:52:08 -0800338 /* Handle the 'M' (magic) format. */
339 char *s;
340
341 /* Parse the 'offset' field. */
342 s = strchr(p, del);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!s)
Mike Frysingere6084d42014-12-10 15:52:10 -0800344 goto einval;
Thadeu Lima de Souza Cascardo5cc41e02018-06-07 17:11:01 -0700345 *s = '\0';
346 if (p != s) {
347 int r = kstrtoint(p, 10, &e->offset);
348 if (r != 0 || e->offset < 0)
349 goto einval;
350 }
351 p = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (*p++)
Mike Frysingere6084d42014-12-10 15:52:10 -0800353 goto einval;
Mike Frysinger6b899c42014-12-10 15:52:08 -0800354 pr_debug("register: offset: %#x\n", e->offset);
355
356 /* Parse the 'magic' field. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 e->magic = p;
358 p = scanarg(p, del);
359 if (!p)
Mike Frysingere6084d42014-12-10 15:52:10 -0800360 goto einval;
Al Viro7d65cf12014-12-17 05:29:16 -0500361 if (!e->magic[0])
Mike Frysingere6084d42014-12-10 15:52:10 -0800362 goto einval;
Mike Frysinger6b899c42014-12-10 15:52:08 -0800363 if (USE_DEBUG)
364 print_hex_dump_bytes(
365 KBUILD_MODNAME ": register: magic[raw]: ",
366 DUMP_PREFIX_NONE, e->magic, p - e->magic);
367
368 /* Parse the 'mask' field. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 e->mask = p;
370 p = scanarg(p, del);
371 if (!p)
Mike Frysingere6084d42014-12-10 15:52:10 -0800372 goto einval;
Al Viro7d65cf12014-12-17 05:29:16 -0500373 if (!e->mask[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 e->mask = NULL;
Mike Frysinger6b899c42014-12-10 15:52:08 -0800375 pr_debug("register: mask[raw]: none\n");
376 } else if (USE_DEBUG)
377 print_hex_dump_bytes(
378 KBUILD_MODNAME ": register: mask[raw]: ",
379 DUMP_PREFIX_NONE, e->mask, p - e->mask);
380
381 /*
382 * Decode the magic & mask fields.
383 * Note: while we might have accepted embedded NUL bytes from
384 * above, the unescape helpers here will stop at the first one
385 * it encounters.
386 */
Andy Shevchenko8d82e182013-04-30 15:27:33 -0700387 e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
388 if (e->mask &&
389 string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
Mike Frysingere6084d42014-12-10 15:52:10 -0800390 goto einval;
Thadeu Lima de Souza Cascardo5cc41e02018-06-07 17:11:01 -0700391 if (e->size > BINPRM_BUF_SIZE ||
392 BINPRM_BUF_SIZE - e->size < e->offset)
Mike Frysingere6084d42014-12-10 15:52:10 -0800393 goto einval;
Mike Frysinger6b899c42014-12-10 15:52:08 -0800394 pr_debug("register: magic/mask length: %i\n", e->size);
395 if (USE_DEBUG) {
396 print_hex_dump_bytes(
397 KBUILD_MODNAME ": register: magic[decoded]: ",
398 DUMP_PREFIX_NONE, e->magic, e->size);
399
400 if (e->mask) {
401 int i;
Andrew Mortonf7e1ad12014-12-10 15:52:13 -0800402 char *masked = kmalloc(e->size, GFP_KERNEL);
Mike Frysinger6b899c42014-12-10 15:52:08 -0800403
404 print_hex_dump_bytes(
405 KBUILD_MODNAME ": register: mask[decoded]: ",
406 DUMP_PREFIX_NONE, e->mask, e->size);
407
408 if (masked) {
409 for (i = 0; i < e->size; ++i)
410 masked[i] = e->magic[i] & e->mask[i];
411 print_hex_dump_bytes(
412 KBUILD_MODNAME ": register: magic[masked]: ",
413 DUMP_PREFIX_NONE, masked, e->size);
414
415 kfree(masked);
416 }
417 }
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 } else {
Mike Frysinger6b899c42014-12-10 15:52:08 -0800420 /* Handle the 'E' (extension) format. */
421
422 /* Skip the 'offset' field. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 p = strchr(p, del);
424 if (!p)
Mike Frysingere6084d42014-12-10 15:52:10 -0800425 goto einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *p++ = '\0';
Mike Frysinger6b899c42014-12-10 15:52:08 -0800427
428 /* Parse the 'magic' field. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 e->magic = p;
430 p = strchr(p, del);
431 if (!p)
Mike Frysingere6084d42014-12-10 15:52:10 -0800432 goto einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 *p++ = '\0';
434 if (!e->magic[0] || strchr(e->magic, '/'))
Mike Frysingere6084d42014-12-10 15:52:10 -0800435 goto einval;
Mike Frysinger6b899c42014-12-10 15:52:08 -0800436 pr_debug("register: extension: {%s}\n", e->magic);
437
438 /* Skip the 'mask' field. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 p = strchr(p, del);
440 if (!p)
Mike Frysingere6084d42014-12-10 15:52:10 -0800441 goto einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 *p++ = '\0';
443 }
Mike Frysinger6b899c42014-12-10 15:52:08 -0800444
445 /* Parse the 'interpreter' field. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 e->interpreter = p;
447 p = strchr(p, del);
448 if (!p)
Mike Frysingere6084d42014-12-10 15:52:10 -0800449 goto einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *p++ = '\0';
451 if (!e->interpreter[0])
Mike Frysingere6084d42014-12-10 15:52:10 -0800452 goto einval;
Mike Frysinger6b899c42014-12-10 15:52:08 -0800453 pr_debug("register: interpreter: {%s}\n", e->interpreter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Mike Frysinger6b899c42014-12-10 15:52:08 -0800455 /* Parse the 'flags' field. */
Mike Frysingere6084d42014-12-10 15:52:10 -0800456 p = check_special_flags(p, e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (*p == '\n')
458 p++;
459 if (p != buf + count)
Mike Frysingere6084d42014-12-10 15:52:10 -0800460 goto einval;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return e;
463
464out:
465 return ERR_PTR(err);
466
Mike Frysingere6084d42014-12-10 15:52:10 -0800467efault:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 kfree(e);
469 return ERR_PTR(-EFAULT);
Mike Frysingere6084d42014-12-10 15:52:10 -0800470einval:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 kfree(e);
472 return ERR_PTR(-EINVAL);
473}
474
475/*
476 * Set status of entry/binfmt_misc:
477 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
478 */
479static int parse_command(const char __user *buffer, size_t count)
480{
481 char s[4];
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (count > 3)
484 return -EINVAL;
485 if (copy_from_user(s, buffer, count))
486 return -EFAULT;
Arnd Bergmannde8288b2014-10-13 15:52:08 -0700487 if (!count)
488 return 0;
Mike Frysingere6084d42014-12-10 15:52:10 -0800489 if (s[count - 1] == '\n')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 count--;
491 if (count == 1 && s[0] == '0')
492 return 1;
493 if (count == 1 && s[0] == '1')
494 return 2;
495 if (count == 2 && s[0] == '-' && s[1] == '1')
496 return 3;
497 return -EINVAL;
498}
499
500/* generic stuff */
501
502static void entry_status(Node *e, char *page)
503{
Rasmus Villemoes6ceafb82015-04-16 12:44:56 -0700504 char *dp = page;
505 const char *status = "disabled";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (test_bit(Enabled, &e->flags))
508 status = "enabled";
509
510 if (!VERBOSE_STATUS) {
511 sprintf(page, "%s\n", status);
512 return;
513 }
514
Rasmus Villemoes6ceafb82015-04-16 12:44:56 -0700515 dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /* print the special flags */
Rasmus Villemoes6ceafb82015-04-16 12:44:56 -0700518 dp += sprintf(dp, "flags: ");
Mike Frysingere6084d42014-12-10 15:52:10 -0800519 if (e->flags & MISC_FMT_PRESERVE_ARGV0)
520 *dp++ = 'P';
521 if (e->flags & MISC_FMT_OPEN_BINARY)
522 *dp++ = 'O';
523 if (e->flags & MISC_FMT_CREDENTIALS)
524 *dp++ = 'C';
James Bottomley948b7012016-02-17 16:51:16 -0800525 if (e->flags & MISC_FMT_OPEN_FILE)
526 *dp++ = 'F';
Mike Frysingere6084d42014-12-10 15:52:10 -0800527 *dp++ = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (!test_bit(Magic, &e->flags)) {
530 sprintf(dp, "extension .%s\n", e->magic);
531 } else {
Rasmus Villemoes6ceafb82015-04-16 12:44:56 -0700532 dp += sprintf(dp, "offset %i\nmagic ", e->offset);
533 dp = bin2hex(dp, e->magic, e->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (e->mask) {
Rasmus Villemoes6ceafb82015-04-16 12:44:56 -0700535 dp += sprintf(dp, "\nmask ");
536 dp = bin2hex(dp, e->mask, e->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 *dp++ = '\n';
539 *dp = '\0';
540 }
541}
542
543static struct inode *bm_get_inode(struct super_block *sb, int mode)
544{
Mike Frysingere6084d42014-12-10 15:52:10 -0800545 struct inode *inode = new_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400548 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 inode->i_atime = inode->i_mtime = inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700551 current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 return inode;
554}
555
Al Virob57922d2010-06-07 14:34:48 -0400556static void bm_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Oleg Nesterov83f91822017-10-03 16:15:48 -0700558 Node *e = inode->i_private;
559
Eryu Guan7e866002017-10-13 15:58:18 -0700560 if (e && e->flags & MISC_FMT_OPEN_FILE)
Oleg Nesterov83f91822017-10-03 16:15:48 -0700561 filp_close(e->interp_file, NULL);
562
Jan Karadbd57682012-05-03 14:48:02 +0200563 clear_inode(inode);
Oleg Nesterov83f91822017-10-03 16:15:48 -0700564 kfree(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567static void kill_node(Node *e)
568{
569 struct dentry *dentry;
570
571 write_lock(&entries_lock);
Oleg Nesterovbaba1b22017-10-03 16:15:45 -0700572 list_del_init(&e->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 write_unlock(&entries_lock);
574
Oleg Nesterovbaba1b22017-10-03 16:15:45 -0700575 dentry = e->dentry;
576 drop_nlink(d_inode(dentry));
577 d_drop(dentry);
578 dput(dentry);
579 simple_release_fs(&bm_mnt, &entry_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582/* /<entry> */
583
584static ssize_t
Mike Frysingere6084d42014-12-10 15:52:10 -0800585bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Al Viro496ad9a2013-01-23 17:07:38 -0500587 Node *e = file_inode(file)->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ssize_t res;
589 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Mike Frysingere6084d42014-12-10 15:52:10 -0800591 page = (char *) __get_free_page(GFP_KERNEL);
592 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return -ENOMEM;
594
595 entry_status(e, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Akinobu Mita6e2c10a2008-07-23 21:29:15 -0700597 res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 free_page((unsigned long) page);
600 return res;
601}
602
603static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
604 size_t count, loff_t *ppos)
605{
606 struct dentry *root;
Al Viro496ad9a2013-01-23 17:07:38 -0500607 Node *e = file_inode(file)->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int res = parse_command(buffer, count);
609
610 switch (res) {
Mike Frysingere6084d42014-12-10 15:52:10 -0800611 case 1:
612 /* Disable this handler. */
613 clear_bit(Enabled, &e->flags);
614 break;
615 case 2:
616 /* Enable this handler. */
617 set_bit(Enabled, &e->flags);
618 break;
619 case 3:
620 /* Delete this handler. */
Al Viroea7d4c02016-05-29 19:14:03 -0400621 root = file_inode(file)->i_sb->s_root;
Al Viro59551022016-01-22 15:40:57 -0500622 inode_lock(d_inode(root));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Oleg Nesterovbaba1b22017-10-03 16:15:45 -0700624 if (!list_empty(&e->list))
625 kill_node(e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Al Viro59551022016-01-22 15:40:57 -0500627 inode_unlock(d_inode(root));
Mike Frysingere6084d42014-12-10 15:52:10 -0800628 break;
629 default:
630 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Mike Frysingere6084d42014-12-10 15:52:10 -0800632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return count;
634}
635
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800636static const struct file_operations bm_entry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .read = bm_entry_read,
638 .write = bm_entry_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200639 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
642/* /register */
643
644static ssize_t bm_register_write(struct file *file, const char __user *buffer,
645 size_t count, loff_t *ppos)
646{
647 Node *e;
648 struct inode *inode;
Al Viroea7d4c02016-05-29 19:14:03 -0400649 struct super_block *sb = file_inode(file)->i_sb;
650 struct dentry *root = sb->s_root, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int err = 0;
Lior Ribake7850f42021-03-12 21:07:41 -0800652 struct file *f = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 e = create_entry(buffer, count);
655
656 if (IS_ERR(e))
657 return PTR_ERR(e);
658
Lior Ribake7850f42021-03-12 21:07:41 -0800659 if (e->flags & MISC_FMT_OPEN_FILE) {
660 f = open_exec(e->interpreter);
661 if (IS_ERR(f)) {
662 pr_notice("register: failed to install interpreter file %s\n",
663 e->interpreter);
664 kfree(e);
665 return PTR_ERR(f);
666 }
667 e->interp_file = f;
668 }
669
Al Viro59551022016-01-22 15:40:57 -0500670 inode_lock(d_inode(root));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 dentry = lookup_one_len(e->name, root, strlen(e->name));
672 err = PTR_ERR(dentry);
673 if (IS_ERR(dentry))
674 goto out;
675
676 err = -EEXIST;
David Howells75c3cfa2015-03-17 22:26:12 +0000677 if (d_really_is_positive(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto out2;
679
680 inode = bm_get_inode(sb, S_IFREG | 0644);
681
682 err = -ENOMEM;
683 if (!inode)
684 goto out2;
685
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400686 err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (err) {
688 iput(inode);
689 inode = NULL;
690 goto out2;
691 }
692
693 e->dentry = dget(dentry);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700694 inode->i_private = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 inode->i_fop = &bm_entry_operations;
696
697 d_instantiate(dentry, inode);
698 write_lock(&entries_lock);
699 list_add(&e->list, &entries);
700 write_unlock(&entries_lock);
701
702 err = 0;
703out2:
704 dput(dentry);
705out:
Al Viro59551022016-01-22 15:40:57 -0500706 inode_unlock(d_inode(root));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (err) {
Lior Ribake7850f42021-03-12 21:07:41 -0800709 if (f)
710 filp_close(f, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 kfree(e);
James Bottomley948b7012016-02-17 16:51:16 -0800712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 return count;
715}
716
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800717static const struct file_operations bm_register_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .write = bm_register_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200719 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720};
721
722/* /status */
723
724static ssize_t
725bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
726{
Qinghuang Feng87113e82009-01-06 14:41:38 -0800727 char *s = enabled ? "enabled\n" : "disabled\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Akinobu Mita92f4c702007-05-09 02:33:32 -0700729 return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Mike Frysingere6084d42014-12-10 15:52:10 -0800732static ssize_t bm_status_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 size_t count, loff_t *ppos)
734{
735 int res = parse_command(buffer, count);
736 struct dentry *root;
737
738 switch (res) {
Mike Frysingere6084d42014-12-10 15:52:10 -0800739 case 1:
740 /* Disable all handlers. */
741 enabled = 0;
742 break;
743 case 2:
744 /* Enable all handlers. */
745 enabled = 1;
746 break;
747 case 3:
748 /* Delete all handlers. */
Al Viroea7d4c02016-05-29 19:14:03 -0400749 root = file_inode(file)->i_sb->s_root;
Al Viro59551022016-01-22 15:40:57 -0500750 inode_lock(d_inode(root));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Mike Frysingere6084d42014-12-10 15:52:10 -0800752 while (!list_empty(&entries))
Oleg Nesterovbaba1b22017-10-03 16:15:45 -0700753 kill_node(list_first_entry(&entries, Node, list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Al Viro59551022016-01-22 15:40:57 -0500755 inode_unlock(d_inode(root));
Mike Frysingere6084d42014-12-10 15:52:10 -0800756 break;
757 default:
758 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Mike Frysingere6084d42014-12-10 15:52:10 -0800760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return count;
762}
763
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800764static const struct file_operations bm_status_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .read = bm_status_read,
766 .write = bm_status_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200767 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768};
769
770/* Superblock handling */
771
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800772static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 .statfs = simple_statfs,
Al Virob57922d2010-06-07 14:34:48 -0400774 .evict_inode = bm_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775};
776
David Howellsbc99a662019-03-25 16:38:28 +0000777static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Mike Frysingere6084d42014-12-10 15:52:10 -0800779 int err;
Eric Biggerscda37122017-03-25 21:15:37 -0700780 static const struct tree_descr bm_files[] = {
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700781 [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
782 [3] = {"register", &bm_register_operations, S_IWUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* last one */ {""}
784 };
Mike Frysingere6084d42014-12-10 15:52:10 -0800785
786 err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!err)
788 sb->s_op = &s_ops;
789 return err;
790}
791
David Howellsbc99a662019-03-25 16:38:28 +0000792static int bm_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
David Howellsbc99a662019-03-25 16:38:28 +0000794 return get_tree_single(fc, bm_fill_super);
795}
796
797static const struct fs_context_operations bm_context_ops = {
798 .get_tree = bm_get_tree,
799};
800
801static int bm_init_fs_context(struct fs_context *fc)
802{
803 fc->ops = &bm_context_ops;
804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807static struct linux_binfmt misc_format = {
808 .module = THIS_MODULE,
809 .load_binary = load_misc_binary,
810};
811
812static struct file_system_type bm_fs_type = {
813 .owner = THIS_MODULE,
814 .name = "binfmt_misc",
David Howellsbc99a662019-03-25 16:38:28 +0000815 .init_fs_context = bm_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .kill_sb = kill_litter_super,
817};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800818MODULE_ALIAS_FS("binfmt_misc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820static int __init init_misc_binfmt(void)
821{
822 int err = register_filesystem(&bm_fs_type);
Al Viro8fc3dc52012-03-17 03:05:16 -0400823 if (!err)
824 insert_binfmt(&misc_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return err;
826}
827
828static void __exit exit_misc_binfmt(void)
829{
830 unregister_binfmt(&misc_format);
831 unregister_filesystem(&bm_fs_type);
832}
833
834core_initcall(init_misc_binfmt);
835module_exit(exit_misc_binfmt);
836MODULE_LICENSE("GPL");