binfmt_misc: cleanup on filesystem umount
[linux-2.6-block.git] / fs / binfmt_misc.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
e6084d4a 3 * binfmt_misc.c
1da177e4 4 *
e6084d4a 5 * Copyright (C) 1997 Richard Günther
1da177e4 6 *
e6084d4a 7 * binfmt_misc detects binaries via a magic or filename extension and invokes
34962fb8 8 * a specified wrapper. See Documentation/admin-guide/binfmt-misc.rst for more details.
1da177e4
LT
9 */
10
6b899c4e
MF
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
6ceafb88 13#include <linux/kernel.h>
1da177e4
LT
14#include <linux/module.h>
15#include <linux/init.h>
589ee628 16#include <linux/sched/mm.h>
b502bd11 17#include <linux/magic.h>
1da177e4
LT
18#include <linux/binfmts.h>
19#include <linux/slab.h>
20#include <linux/ctype.h>
8d82e180 21#include <linux/string_helpers.h>
1da177e4
LT
22#include <linux/file.h>
23#include <linux/pagemap.h>
24#include <linux/namei.h>
25#include <linux/mount.h>
bc99a664 26#include <linux/fs_context.h>
1da177e4 27#include <linux/syscalls.h>
6e2c10a1 28#include <linux/fs.h>
6b899c4e 29#include <linux/uaccess.h>
1da177e4 30
948b701a
JB
31#include "internal.h"
32
6b899c4e
MF
33#ifdef DEBUG
34# define USE_DEBUG 1
35#else
36# define USE_DEBUG 0
37#endif
1da177e4
LT
38
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};
6a46bf55
LS
47#define MISC_FMT_PRESERVE_ARGV0 (1UL << 31)
48#define MISC_FMT_OPEN_BINARY (1UL << 30)
49#define MISC_FMT_CREDENTIALS (1UL << 29)
50#define MISC_FMT_OPEN_FILE (1UL << 28)
1da177e4
LT
51
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 */
50097f74 59 const char *interpreter; /* filename of interpreter */
1da177e4
LT
60 char *name;
61 struct dentry *dentry;
948b701a 62 struct file *interp_file;
1c5976ef 63 refcount_t users; /* sync removal with load_misc_binary() */
1da177e4
LT
64} Node;
65
66static DEFINE_RWLOCK(entries_lock);
1f5ce9e9 67static struct file_system_type bm_fs_type;
1da177e4 68
bbaecc08
MF
69/*
70 * Max length of the register string. Determined by:
71 * - 7 delimiters
72 * - name: ~50 bytes
73 * - type: 1 byte
74 * - offset: 3 bytes (has to be smaller than BINPRM_BUF_SIZE)
75 * - magic: 128 bytes (512 in escaped form)
76 * - mask: 128 bytes (512 in escaped form)
77 * - interp: ~50 bytes
78 * - flags: 5 bytes
79 * Round that up a bit, and then back off to hold the internal data
80 * (like struct Node).
81 */
82#define MAX_REGISTER_LENGTH 1920
83
1c5976ef
CB
84/**
85 * search_binfmt_handler - search for a binary handler for @bprm
86 * @misc: handle to binfmt_misc instance
87 * @bprm: binary for which we are looking for a handler
88 *
89 * Search for a binary type handler for @bprm in the list of registered binary
90 * type handlers.
91 *
92 * Return: binary type list entry on success, NULL on failure
1da177e4 93 */
1c5976ef 94static Node *search_binfmt_handler(struct linux_binprm *bprm)
1da177e4
LT
95{
96 char *p = strrchr(bprm->interp, '.');
1c5976ef 97 Node *e;
1da177e4 98
6b899c4e 99 /* Walk all the registered handlers. */
1c5976ef 100 list_for_each_entry(e, &entries, list) {
1da177e4
LT
101 char *s;
102 int j;
103
6b899c4e 104 /* Make sure this one is currently enabled. */
1da177e4
LT
105 if (!test_bit(Enabled, &e->flags))
106 continue;
107
6b899c4e 108 /* Do matching based on extension if applicable. */
1da177e4
LT
109 if (!test_bit(Magic, &e->flags)) {
110 if (p && !strcmp(e->magic, p + 1))
111 return e;
112 continue;
113 }
114
6b899c4e 115 /* Do matching based on magic & mask. */
1da177e4
LT
116 s = bprm->buf + e->offset;
117 if (e->mask) {
118 for (j = 0; j < e->size; j++)
119 if ((*s++ ^ e->magic[j]) & e->mask[j])
120 break;
121 } else {
122 for (j = 0; j < e->size; j++)
123 if ((*s++ ^ e->magic[j]))
124 break;
125 }
126 if (j == e->size)
127 return e;
128 }
1c5976ef 129
1da177e4
LT
130 return NULL;
131}
132
1c5976ef
CB
133/**
134 * get_binfmt_handler - try to find a binary type handler
135 * @misc: handle to binfmt_misc instance
136 * @bprm: binary for which we are looking for a handler
137 *
138 * Try to find a binfmt handler for the binary type. If one is found take a
139 * reference to protect against removal via bm_{entry,status}_write().
140 *
141 * Return: binary type list entry on success, NULL on failure
142 */
143static Node *get_binfmt_handler(struct linux_binprm *bprm)
144{
145 Node *e;
146
147 read_lock(&entries_lock);
148 e = search_binfmt_handler(bprm);
149 if (e)
150 refcount_inc(&e->users);
151 read_unlock(&entries_lock);
152 return e;
153}
154
155/**
156 * put_binfmt_handler - put binary handler node
157 * @e: node to put
158 *
159 * Free node syncing with load_misc_binary() and defer final free to
160 * load_misc_binary() in case it is using the binary type handler we were
161 * requested to remove.
162 */
163static void put_binfmt_handler(Node *e)
164{
165 if (refcount_dec_and_test(&e->users)) {
166 if (e->flags & MISC_FMT_OPEN_FILE)
167 filp_close(e->interp_file, NULL);
168 kfree(e);
169 }
170}
171
1da177e4
LT
172/*
173 * the loader itself
174 */
71613c3b 175static int load_misc_binary(struct linux_binprm *bprm)
1da177e4
LT
176{
177 Node *fmt;
e6084d4a 178 struct file *interp_file = NULL;
1da177e4 179 int retval;
1da177e4
LT
180
181 retval = -ENOEXEC;
182 if (!enabled)
43a4f261 183 return retval;
1da177e4 184
1c5976ef 185 fmt = get_binfmt_handler(bprm);
1da177e4 186 if (!fmt)
43a4f261 187 return retval;
1da177e4 188
51f39a1f 189 /* Need to be able to load the file after exec */
43a4f261 190 retval = -ENOENT;
51f39a1f 191 if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
43a4f261 192 goto ret;
51f39a1f 193
2347961b
LV
194 if (fmt->flags & MISC_FMT_PRESERVE_ARGV0) {
195 bprm->interp_flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
196 } else {
b6a2fea3
OW
197 retval = remove_arg_zero(bprm);
198 if (retval)
e6084d4a 199 goto ret;
1da177e4
LT
200 }
201
bc2bf338 202 if (fmt->flags & MISC_FMT_OPEN_BINARY)
b8a61c9e 203 bprm->have_execfd = 1;
1da177e4 204
1da177e4 205 /* make argv[1] be the path to the binary */
986db2d1 206 retval = copy_string_kernel(bprm->interp, bprm);
1da177e4 207 if (retval < 0)
b8a61c9e 208 goto ret;
1da177e4
LT
209 bprm->argc++;
210
211 /* add the interp as argv[0] */
986db2d1 212 retval = copy_string_kernel(fmt->interpreter, bprm);
1da177e4 213 if (retval < 0)
b8a61c9e 214 goto ret;
e6084d4a 215 bprm->argc++;
1da177e4 216
b66c5984 217 /* Update interp in case binfmt_script needs it. */
50097f74 218 retval = bprm_change_interp(fmt->interpreter, bprm);
b66c5984 219 if (retval < 0)
b8a61c9e 220 goto ret;
1da177e4 221
eb23aa03 222 if (fmt->flags & MISC_FMT_OPEN_FILE) {
19f391eb 223 interp_file = file_clone_open(fmt->interp_file);
948b701a
JB
224 if (!IS_ERR(interp_file))
225 deny_write_access(interp_file);
226 } else {
50097f74 227 interp_file = open_exec(fmt->interpreter);
948b701a 228 }
e6084d4a
MF
229 retval = PTR_ERR(interp_file);
230 if (IS_ERR(interp_file))
b8a61c9e 231 goto ret;
1da177e4 232
bc2bf338 233 bprm->interpreter = interp_file;
a16b3357 234 if (fmt->flags & MISC_FMT_CREDENTIALS)
56305aa9 235 bprm->execfd_creds = 1;
1da177e4 236
bc2bf338 237 retval = 0;
e6084d4a 238ret:
1c5976ef
CB
239
240 /*
241 * If we actually put the node here all concurrent calls to
242 * load_misc_binary() will have finished. We also know
243 * that for the refcount to be zero ->evict_inode() must have removed
244 * the node to be deleted from the list. All that is left for us is to
245 * close and free.
246 */
247 put_binfmt_handler(fmt);
248
1da177e4 249 return retval;
1da177e4
LT
250}
251
252/* Command parsers */
253
254/*
255 * parses and copies one argument enclosed in del from *sp to *dp,
256 * recognising the \x special.
257 * returns pointer to the copied argument or NULL in case of an
258 * error (and sets err) or null argument length.
259 */
260static char *scanarg(char *s, char del)
261{
262 char c;
263
264 while ((c = *s++) != del) {
265 if (c == '\\' && *s == 'x') {
266 s++;
267 if (!isxdigit(*s++))
268 return NULL;
269 if (!isxdigit(*s++))
270 return NULL;
271 }
272 }
7d65cf10 273 s[-1] ='\0';
1da177e4
LT
274 return s;
275}
276
e6084d4a 277static char *check_special_flags(char *sfs, Node *e)
1da177e4 278{
e6084d4a 279 char *p = sfs;
1da177e4
LT
280 int cont = 1;
281
282 /* special flags */
283 while (cont) {
284 switch (*p) {
e6084d4a
MF
285 case 'P':
286 pr_debug("register: flag: P (preserve argv0)\n");
287 p++;
288 e->flags |= MISC_FMT_PRESERVE_ARGV0;
289 break;
290 case 'O':
291 pr_debug("register: flag: O (open binary)\n");
292 p++;
293 e->flags |= MISC_FMT_OPEN_BINARY;
294 break;
295 case 'C':
296 pr_debug("register: flag: C (preserve creds)\n");
297 p++;
298 /* this flags also implies the
299 open-binary flag */
300 e->flags |= (MISC_FMT_CREDENTIALS |
301 MISC_FMT_OPEN_BINARY);
302 break;
948b701a
JB
303 case 'F':
304 pr_debug("register: flag: F: open interpreter file now\n");
305 p++;
306 e->flags |= MISC_FMT_OPEN_FILE;
307 break;
e6084d4a
MF
308 default:
309 cont = 0;
1da177e4
LT
310 }
311 }
312
313 return p;
314}
e6084d4a 315
1da177e4
LT
316/*
317 * This registers a new binary format, it recognises the syntax
318 * ':name:type:offset:magic:mask:interpreter:flags'
319 * where the ':' is the IFS, that can be chosen with the first char
320 */
321static Node *create_entry(const char __user *buffer, size_t count)
322{
323 Node *e;
324 int memsize, err;
325 char *buf, *p;
326 char del;
327
6b899c4e
MF
328 pr_debug("register: received %zu bytes\n", count);
329
1da177e4
LT
330 /* some sanity checks */
331 err = -EINVAL;
bbaecc08 332 if ((count < 11) || (count > MAX_REGISTER_LENGTH))
1da177e4
LT
333 goto out;
334
335 err = -ENOMEM;
336 memsize = sizeof(Node) + count + 8;
f7e1ad1a 337 e = kmalloc(memsize, GFP_KERNEL);
1da177e4
LT
338 if (!e)
339 goto out;
340
341 p = buf = (char *)e + sizeof(Node);
342
343 memset(e, 0, sizeof(Node));
344 if (copy_from_user(buf, buffer, count))
e6084d4a 345 goto efault;
1da177e4
LT
346
347 del = *p++; /* delimeter */
348
6b899c4e
MF
349 pr_debug("register: delim: %#x {%c}\n", del, del);
350
351 /* Pad the buffer with the delim to simplify parsing below. */
e6084d4a 352 memset(buf + count, del, 8);
1da177e4 353
6b899c4e 354 /* Parse the 'name' field. */
1da177e4
LT
355 e->name = p;
356 p = strchr(p, del);
357 if (!p)
e6084d4a 358 goto einval;
1da177e4
LT
359 *p++ = '\0';
360 if (!e->name[0] ||
361 !strcmp(e->name, ".") ||
362 !strcmp(e->name, "..") ||
363 strchr(e->name, '/'))
e6084d4a 364 goto einval;
6b899c4e
MF
365
366 pr_debug("register: name: {%s}\n", e->name);
367
368 /* Parse the 'type' field. */
1da177e4 369 switch (*p++) {
6b899c4e
MF
370 case 'E':
371 pr_debug("register: type: E (extension)\n");
372 e->flags = 1 << Enabled;
373 break;
374 case 'M':
375 pr_debug("register: type: M (magic)\n");
376 e->flags = (1 << Enabled) | (1 << Magic);
377 break;
378 default:
e6084d4a 379 goto einval;
1da177e4
LT
380 }
381 if (*p++ != del)
e6084d4a 382 goto einval;
6b899c4e 383
1da177e4 384 if (test_bit(Magic, &e->flags)) {
6b899c4e
MF
385 /* Handle the 'M' (magic) format. */
386 char *s;
387
388 /* Parse the 'offset' field. */
389 s = strchr(p, del);
1da177e4 390 if (!s)
e6084d4a 391 goto einval;
5cc41e09
TLSC
392 *s = '\0';
393 if (p != s) {
394 int r = kstrtoint(p, 10, &e->offset);
395 if (r != 0 || e->offset < 0)
396 goto einval;
397 }
398 p = s;
1da177e4 399 if (*p++)
e6084d4a 400 goto einval;
6b899c4e
MF
401 pr_debug("register: offset: %#x\n", e->offset);
402
403 /* Parse the 'magic' field. */
1da177e4
LT
404 e->magic = p;
405 p = scanarg(p, del);
406 if (!p)
e6084d4a 407 goto einval;
7d65cf10 408 if (!e->magic[0])
e6084d4a 409 goto einval;
6b899c4e
MF
410 if (USE_DEBUG)
411 print_hex_dump_bytes(
412 KBUILD_MODNAME ": register: magic[raw]: ",
413 DUMP_PREFIX_NONE, e->magic, p - e->magic);
414
415 /* Parse the 'mask' field. */
1da177e4
LT
416 e->mask = p;
417 p = scanarg(p, del);
418 if (!p)
e6084d4a 419 goto einval;
7d65cf10 420 if (!e->mask[0]) {
1da177e4 421 e->mask = NULL;
6b899c4e
MF
422 pr_debug("register: mask[raw]: none\n");
423 } else if (USE_DEBUG)
424 print_hex_dump_bytes(
425 KBUILD_MODNAME ": register: mask[raw]: ",
426 DUMP_PREFIX_NONE, e->mask, p - e->mask);
427
428 /*
429 * Decode the magic & mask fields.
430 * Note: while we might have accepted embedded NUL bytes from
431 * above, the unescape helpers here will stop at the first one
432 * it encounters.
433 */
8d82e180
AS
434 e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
435 if (e->mask &&
436 string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
e6084d4a 437 goto einval;
5cc41e09
TLSC
438 if (e->size > BINPRM_BUF_SIZE ||
439 BINPRM_BUF_SIZE - e->size < e->offset)
e6084d4a 440 goto einval;
6b899c4e
MF
441 pr_debug("register: magic/mask length: %i\n", e->size);
442 if (USE_DEBUG) {
443 print_hex_dump_bytes(
444 KBUILD_MODNAME ": register: magic[decoded]: ",
445 DUMP_PREFIX_NONE, e->magic, e->size);
446
447 if (e->mask) {
448 int i;
f7e1ad1a 449 char *masked = kmalloc(e->size, GFP_KERNEL);
6b899c4e
MF
450
451 print_hex_dump_bytes(
452 KBUILD_MODNAME ": register: mask[decoded]: ",
453 DUMP_PREFIX_NONE, e->mask, e->size);
454
455 if (masked) {
456 for (i = 0; i < e->size; ++i)
457 masked[i] = e->magic[i] & e->mask[i];
458 print_hex_dump_bytes(
459 KBUILD_MODNAME ": register: magic[masked]: ",
460 DUMP_PREFIX_NONE, masked, e->size);
461
462 kfree(masked);
463 }
464 }
465 }
1da177e4 466 } else {
6b899c4e
MF
467 /* Handle the 'E' (extension) format. */
468
469 /* Skip the 'offset' field. */
1da177e4
LT
470 p = strchr(p, del);
471 if (!p)
e6084d4a 472 goto einval;
1da177e4 473 *p++ = '\0';
6b899c4e
MF
474
475 /* Parse the 'magic' field. */
1da177e4
LT
476 e->magic = p;
477 p = strchr(p, del);
478 if (!p)
e6084d4a 479 goto einval;
1da177e4
LT
480 *p++ = '\0';
481 if (!e->magic[0] || strchr(e->magic, '/'))
e6084d4a 482 goto einval;
6b899c4e
MF
483 pr_debug("register: extension: {%s}\n", e->magic);
484
485 /* Skip the 'mask' field. */
1da177e4
LT
486 p = strchr(p, del);
487 if (!p)
e6084d4a 488 goto einval;
1da177e4
LT
489 *p++ = '\0';
490 }
6b899c4e
MF
491
492 /* Parse the 'interpreter' field. */
1da177e4
LT
493 e->interpreter = p;
494 p = strchr(p, del);
495 if (!p)
e6084d4a 496 goto einval;
1da177e4
LT
497 *p++ = '\0';
498 if (!e->interpreter[0])
e6084d4a 499 goto einval;
6b899c4e 500 pr_debug("register: interpreter: {%s}\n", e->interpreter);
1da177e4 501
6b899c4e 502 /* Parse the 'flags' field. */
e6084d4a 503 p = check_special_flags(p, e);
1da177e4
LT
504 if (*p == '\n')
505 p++;
506 if (p != buf + count)
e6084d4a
MF
507 goto einval;
508
1da177e4
LT
509 return e;
510
511out:
512 return ERR_PTR(err);
513
e6084d4a 514efault:
1da177e4
LT
515 kfree(e);
516 return ERR_PTR(-EFAULT);
e6084d4a 517einval:
1da177e4
LT
518 kfree(e);
519 return ERR_PTR(-EINVAL);
520}
521
522/*
523 * Set status of entry/binfmt_misc:
524 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
525 */
526static int parse_command(const char __user *buffer, size_t count)
527{
528 char s[4];
529
1da177e4
LT
530 if (count > 3)
531 return -EINVAL;
532 if (copy_from_user(s, buffer, count))
533 return -EFAULT;
de8288b1
AB
534 if (!count)
535 return 0;
e6084d4a 536 if (s[count - 1] == '\n')
1da177e4
LT
537 count--;
538 if (count == 1 && s[0] == '0')
539 return 1;
540 if (count == 1 && s[0] == '1')
541 return 2;
542 if (count == 2 && s[0] == '-' && s[1] == '1')
543 return 3;
544 return -EINVAL;
545}
546
547/* generic stuff */
548
549static void entry_status(Node *e, char *page)
550{
6ceafb88
RV
551 char *dp = page;
552 const char *status = "disabled";
1da177e4
LT
553
554 if (test_bit(Enabled, &e->flags))
555 status = "enabled";
556
557 if (!VERBOSE_STATUS) {
558 sprintf(page, "%s\n", status);
559 return;
560 }
561
6ceafb88 562 dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter);
1da177e4
LT
563
564 /* print the special flags */
6ceafb88 565 dp += sprintf(dp, "flags: ");
e6084d4a
MF
566 if (e->flags & MISC_FMT_PRESERVE_ARGV0)
567 *dp++ = 'P';
568 if (e->flags & MISC_FMT_OPEN_BINARY)
569 *dp++ = 'O';
570 if (e->flags & MISC_FMT_CREDENTIALS)
571 *dp++ = 'C';
948b701a
JB
572 if (e->flags & MISC_FMT_OPEN_FILE)
573 *dp++ = 'F';
e6084d4a 574 *dp++ = '\n';
1da177e4
LT
575
576 if (!test_bit(Magic, &e->flags)) {
577 sprintf(dp, "extension .%s\n", e->magic);
578 } else {
6ceafb88
RV
579 dp += sprintf(dp, "offset %i\nmagic ", e->offset);
580 dp = bin2hex(dp, e->magic, e->size);
1da177e4 581 if (e->mask) {
6ceafb88
RV
582 dp += sprintf(dp, "\nmask ");
583 dp = bin2hex(dp, e->mask, e->size);
1da177e4
LT
584 }
585 *dp++ = '\n';
586 *dp = '\0';
587 }
588}
589
590static struct inode *bm_get_inode(struct super_block *sb, int mode)
591{
e6084d4a 592 struct inode *inode = new_inode(sb);
1da177e4
LT
593
594 if (inode) {
85fe4025 595 inode->i_ino = get_next_ino();
1da177e4 596 inode->i_mode = mode;
2276e5ba 597 inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
1da177e4
LT
598 }
599 return inode;
600}
601
1c5976ef
CB
602/**
603 * bm_evict_inode - cleanup data associated with @inode
604 * @inode: inode to which the data is attached
605 *
606 * Cleanup the binary type handler data associated with @inode if a binary type
607 * entry is removed or the filesystem is unmounted and the super block is
608 * shutdown.
609 *
610 * If the ->evict call was not caused by a super block shutdown but by a write
611 * to remove the entry or all entries via bm_{entry,status}_write() the entry
612 * will have already been removed from the list. We keep the list_empty() check
613 * to make that explicit.
614*/
b57922d9 615static void bm_evict_inode(struct inode *inode)
1da177e4 616{
83f91827
ON
617 Node *e = inode->i_private;
618
dbd5768f 619 clear_inode(inode);
1c5976ef
CB
620
621 if (e) {
622 write_lock(&entries_lock);
623 if (!list_empty(&e->list))
624 list_del_init(&e->list);
625 write_unlock(&entries_lock);
626 put_binfmt_handler(e);
627 }
1da177e4
LT
628}
629
1c5976ef
CB
630/**
631 * unlink_binfmt_dentry - remove the dentry for the binary type handler
632 * @dentry: dentry associated with the binary type handler
633 *
634 * Do the actual filesystem work to remove a dentry for a registered binary
635 * type handler. Since binfmt_misc only allows simple files to be created
636 * directly under the root dentry of the filesystem we ensure that we are
637 * indeed passed a dentry directly beneath the root dentry, that the inode
638 * associated with the root dentry is locked, and that it is a regular file we
639 * are asked to remove.
640 */
641static void unlink_binfmt_dentry(struct dentry *dentry)
1da177e4 642{
1c5976ef
CB
643 struct dentry *parent = dentry->d_parent;
644 struct inode *inode, *parent_inode;
645
646 /* All entries are immediate descendants of the root dentry. */
647 if (WARN_ON_ONCE(dentry->d_sb->s_root != parent))
648 return;
1da177e4 649
1c5976ef
CB
650 /* We only expect to be called on regular files. */
651 inode = d_inode(dentry);
652 if (WARN_ON_ONCE(!S_ISREG(inode->i_mode)))
653 return;
654
655 /* The parent inode must be locked. */
656 parent_inode = d_inode(parent);
657 if (WARN_ON_ONCE(!inode_is_locked(parent_inode)))
658 return;
659
660 if (simple_positive(dentry)) {
661 dget(dentry);
662 simple_unlink(parent_inode, dentry);
663 d_delete(dentry);
664 dput(dentry);
665 }
666}
667
668/**
669 * remove_binfmt_handler - remove a binary type handler
670 * @misc: handle to binfmt_misc instance
671 * @e: binary type handler to remove
672 *
673 * Remove a binary type handler from the list of binary type handlers and
674 * remove its associated dentry. This is called from
675 * binfmt_{entry,status}_write(). In the future, we might want to think about
676 * adding a proper ->unlink() method to binfmt_misc instead of forcing caller's
677 * to use writes to files in order to delete binary type handlers. But it has
678 * worked for so long that it's not a pressing issue.
679 */
680static void remove_binfmt_handler(Node *e)
681{
1da177e4 682 write_lock(&entries_lock);
baba1b29 683 list_del_init(&e->list);
1da177e4 684 write_unlock(&entries_lock);
1c5976ef 685 unlink_binfmt_dentry(e->dentry);
1da177e4
LT
686}
687
688/* /<entry> */
689
690static ssize_t
e6084d4a 691bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1da177e4 692{
496ad9aa 693 Node *e = file_inode(file)->i_private;
1da177e4
LT
694 ssize_t res;
695 char *page;
1da177e4 696
e6084d4a
MF
697 page = (char *) __get_free_page(GFP_KERNEL);
698 if (!page)
1da177e4
LT
699 return -ENOMEM;
700
701 entry_status(e, page);
1da177e4 702
6e2c10a1
AM
703 res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
704
1da177e4
LT
705 free_page((unsigned long) page);
706 return res;
707}
708
709static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
710 size_t count, loff_t *ppos)
711{
1c5976ef
CB
712 struct inode *inode = file_inode(file);
713 Node *e = inode->i_private;
1da177e4
LT
714 int res = parse_command(buffer, count);
715
716 switch (res) {
e6084d4a
MF
717 case 1:
718 /* Disable this handler. */
719 clear_bit(Enabled, &e->flags);
720 break;
721 case 2:
722 /* Enable this handler. */
723 set_bit(Enabled, &e->flags);
724 break;
725 case 3:
726 /* Delete this handler. */
1c5976ef
CB
727 inode = d_inode(inode->i_sb->s_root);
728 inode_lock(inode);
1da177e4 729
1c5976ef
CB
730 /*
731 * In order to add new element or remove elements from the list
732 * via bm_{entry,register,status}_write() inode_lock() on the
733 * root inode must be held.
734 * The lock is exclusive ensuring that the list can't be
735 * modified. Only load_misc_binary() can access but does so
736 * read-only. So we only need to take the write lock when we
737 * actually remove the entry from the list.
738 */
baba1b29 739 if (!list_empty(&e->list))
1c5976ef 740 remove_binfmt_handler(e);
1da177e4 741
1c5976ef 742 inode_unlock(inode);
e6084d4a
MF
743 break;
744 default:
745 return res;
1da177e4 746 }
e6084d4a 747
1da177e4
LT
748 return count;
749}
750
4b6f5d20 751static const struct file_operations bm_entry_operations = {
1da177e4
LT
752 .read = bm_entry_read,
753 .write = bm_entry_write,
6038f373 754 .llseek = default_llseek,
1da177e4
LT
755};
756
757/* /register */
758
759static ssize_t bm_register_write(struct file *file, const char __user *buffer,
760 size_t count, loff_t *ppos)
761{
762 Node *e;
763 struct inode *inode;
ea7d4c04
AV
764 struct super_block *sb = file_inode(file)->i_sb;
765 struct dentry *root = sb->s_root, *dentry;
1da177e4 766 int err = 0;
e7850f4d 767 struct file *f = NULL;
1da177e4
LT
768
769 e = create_entry(buffer, count);
770
771 if (IS_ERR(e))
772 return PTR_ERR(e);
773
e7850f4d
LR
774 if (e->flags & MISC_FMT_OPEN_FILE) {
775 f = open_exec(e->interpreter);
776 if (IS_ERR(f)) {
777 pr_notice("register: failed to install interpreter file %s\n",
778 e->interpreter);
779 kfree(e);
780 return PTR_ERR(f);
781 }
782 e->interp_file = f;
783 }
784
5955102c 785 inode_lock(d_inode(root));
1da177e4
LT
786 dentry = lookup_one_len(e->name, root, strlen(e->name));
787 err = PTR_ERR(dentry);
788 if (IS_ERR(dentry))
789 goto out;
790
791 err = -EEXIST;
75c3cfa8 792 if (d_really_is_positive(dentry))
1da177e4
LT
793 goto out2;
794
795 inode = bm_get_inode(sb, S_IFREG | 0644);
796
797 err = -ENOMEM;
798 if (!inode)
799 goto out2;
800
1c5976ef 801 refcount_set(&e->users, 1);
1da177e4 802 e->dentry = dget(dentry);
8e18e294 803 inode->i_private = e;
1da177e4
LT
804 inode->i_fop = &bm_entry_operations;
805
806 d_instantiate(dentry, inode);
807 write_lock(&entries_lock);
808 list_add(&e->list, &entries);
809 write_unlock(&entries_lock);
810
811 err = 0;
812out2:
813 dput(dentry);
814out:
5955102c 815 inode_unlock(d_inode(root));
1da177e4
LT
816
817 if (err) {
e7850f4d
LR
818 if (f)
819 filp_close(f, NULL);
1da177e4 820 kfree(e);
948b701a 821 return err;
1da177e4
LT
822 }
823 return count;
824}
825
4b6f5d20 826static const struct file_operations bm_register_operations = {
1da177e4 827 .write = bm_register_write,
6038f373 828 .llseek = noop_llseek,
1da177e4
LT
829};
830
831/* /status */
832
833static ssize_t
834bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
835{
87113e80 836 char *s = enabled ? "enabled\n" : "disabled\n";
1da177e4 837
92f4c701 838 return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
1da177e4
LT
839}
840
e6084d4a 841static ssize_t bm_status_write(struct file *file, const char __user *buffer,
1da177e4
LT
842 size_t count, loff_t *ppos)
843{
844 int res = parse_command(buffer, count);
1c5976ef
CB
845 Node *e, *next;
846 struct inode *inode;
1da177e4
LT
847
848 switch (res) {
e6084d4a
MF
849 case 1:
850 /* Disable all handlers. */
851 enabled = 0;
852 break;
853 case 2:
854 /* Enable all handlers. */
855 enabled = 1;
856 break;
857 case 3:
858 /* Delete all handlers. */
1c5976ef
CB
859 inode = d_inode(file_inode(file)->i_sb->s_root);
860 inode_lock(inode);
1da177e4 861
1c5976ef
CB
862 /*
863 * In order to add new element or remove elements from the list
864 * via bm_{entry,register,status}_write() inode_lock() on the
865 * root inode must be held.
866 * The lock is exclusive ensuring that the list can't be
867 * modified. Only load_misc_binary() can access but does so
868 * read-only. So we only need to take the write lock when we
869 * actually remove the entry from the list.
870 */
871 list_for_each_entry_safe(e, next, &entries, list)
872 remove_binfmt_handler(e);
1da177e4 873
1c5976ef 874 inode_unlock(inode);
e6084d4a
MF
875 break;
876 default:
877 return res;
1da177e4 878 }
e6084d4a 879
1da177e4
LT
880 return count;
881}
882
4b6f5d20 883static const struct file_operations bm_status_operations = {
1da177e4
LT
884 .read = bm_status_read,
885 .write = bm_status_write,
6038f373 886 .llseek = default_llseek,
1da177e4
LT
887};
888
889/* Superblock handling */
890
ee9b6d61 891static const struct super_operations s_ops = {
1da177e4 892 .statfs = simple_statfs,
b57922d9 893 .evict_inode = bm_evict_inode,
1da177e4
LT
894};
895
bc99a664 896static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
1da177e4 897{
e6084d4a 898 int err;
cda37124 899 static const struct tree_descr bm_files[] = {
1a1c9bb4
JL
900 [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
901 [3] = {"register", &bm_register_operations, S_IWUSR},
1da177e4
LT
902 /* last one */ {""}
903 };
e6084d4a
MF
904
905 err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
1da177e4
LT
906 if (!err)
907 sb->s_op = &s_ops;
908 return err;
909}
910
bc99a664 911static int bm_get_tree(struct fs_context *fc)
1da177e4 912{
bc99a664
DH
913 return get_tree_single(fc, bm_fill_super);
914}
915
916static const struct fs_context_operations bm_context_ops = {
917 .get_tree = bm_get_tree,
918};
919
920static int bm_init_fs_context(struct fs_context *fc)
921{
922 fc->ops = &bm_context_ops;
923 return 0;
1da177e4
LT
924}
925
926static struct linux_binfmt misc_format = {
927 .module = THIS_MODULE,
928 .load_binary = load_misc_binary,
929};
930
931static struct file_system_type bm_fs_type = {
932 .owner = THIS_MODULE,
933 .name = "binfmt_misc",
bc99a664 934 .init_fs_context = bm_init_fs_context,
1da177e4
LT
935 .kill_sb = kill_litter_super,
936};
7f78e035 937MODULE_ALIAS_FS("binfmt_misc");
1da177e4
LT
938
939static int __init init_misc_binfmt(void)
940{
941 int err = register_filesystem(&bm_fs_type);
8fc3dc5a
AV
942 if (!err)
943 insert_binfmt(&misc_format);
b42bc9a3 944 return err;
1da177e4
LT
945}
946
947static void __exit exit_misc_binfmt(void)
948{
949 unregister_binfmt(&misc_format);
950 unregister_filesystem(&bm_fs_type);
951}
952
953core_initcall(init_misc_binfmt);
954module_exit(exit_misc_binfmt);
955MODULE_LICENSE("GPL");