leds: gpio: Utilise PTR_ERR_OR_ZERO()
[linux-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;
1da177e4
LT
63} Node;
64
65static DEFINE_RWLOCK(entries_lock);
1f5ce9e9 66static struct file_system_type bm_fs_type;
1da177e4
LT
67static struct vfsmount *bm_mnt;
68static int entry_count;
69
bbaecc08
MF
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/*
1da177e4
LT
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 */
90static Node *check_file(struct linux_binprm *bprm)
91{
92 char *p = strrchr(bprm->interp, '.');
93 struct list_head *l;
94
6b899c4e 95 /* Walk all the registered handlers. */
1da177e4
LT
96 list_for_each(l, &entries) {
97 Node *e = list_entry(l, Node, list);
98 char *s;
99 int j;
100
6b899c4e 101 /* Make sure this one is currently enabled. */
1da177e4
LT
102 if (!test_bit(Enabled, &e->flags))
103 continue;
104
6b899c4e 105 /* Do matching based on extension if applicable. */
1da177e4
LT
106 if (!test_bit(Magic, &e->flags)) {
107 if (p && !strcmp(e->magic, p + 1))
108 return e;
109 continue;
110 }
111
6b899c4e 112 /* Do matching based on magic & mask. */
1da177e4
LT
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 */
71613c3b 132static int load_misc_binary(struct linux_binprm *bprm)
1da177e4
LT
133{
134 Node *fmt;
e6084d4a 135 struct file *interp_file = NULL;
1da177e4 136 int retval;
1da177e4
LT
137
138 retval = -ENOEXEC;
139 if (!enabled)
43a4f261 140 return retval;
1da177e4
LT
141
142 /* to keep locking time low, we copy the interpreter string */
143 read_lock(&entries_lock);
144 fmt = check_file(bprm);
50097f74 145 if (fmt)
43a4f261 146 dget(fmt->dentry);
1da177e4
LT
147 read_unlock(&entries_lock);
148 if (!fmt)
43a4f261 149 return retval;
1da177e4 150
51f39a1f 151 /* Need to be able to load the file after exec */
43a4f261 152 retval = -ENOENT;
51f39a1f 153 if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
43a4f261 154 goto ret;
51f39a1f 155
2347961b
LV
156 if (fmt->flags & MISC_FMT_PRESERVE_ARGV0) {
157 bprm->interp_flags |= BINPRM_FLAGS_PRESERVE_ARGV0;
158 } else {
b6a2fea3
OW
159 retval = remove_arg_zero(bprm);
160 if (retval)
e6084d4a 161 goto ret;
1da177e4
LT
162 }
163
bc2bf338 164 if (fmt->flags & MISC_FMT_OPEN_BINARY)
b8a61c9e 165 bprm->have_execfd = 1;
1da177e4 166
1da177e4 167 /* make argv[1] be the path to the binary */
986db2d1 168 retval = copy_string_kernel(bprm->interp, bprm);
1da177e4 169 if (retval < 0)
b8a61c9e 170 goto ret;
1da177e4
LT
171 bprm->argc++;
172
173 /* add the interp as argv[0] */
986db2d1 174 retval = copy_string_kernel(fmt->interpreter, bprm);
1da177e4 175 if (retval < 0)
b8a61c9e 176 goto ret;
e6084d4a 177 bprm->argc++;
1da177e4 178
b66c5984 179 /* Update interp in case binfmt_script needs it. */
50097f74 180 retval = bprm_change_interp(fmt->interpreter, bprm);
b66c5984 181 if (retval < 0)
b8a61c9e 182 goto ret;
1da177e4 183
eb23aa03 184 if (fmt->flags & MISC_FMT_OPEN_FILE) {
19f391eb 185 interp_file = file_clone_open(fmt->interp_file);
948b701a
JB
186 if (!IS_ERR(interp_file))
187 deny_write_access(interp_file);
188 } else {
50097f74 189 interp_file = open_exec(fmt->interpreter);
948b701a 190 }
e6084d4a
MF
191 retval = PTR_ERR(interp_file);
192 if (IS_ERR(interp_file))
b8a61c9e 193 goto ret;
1da177e4 194
bc2bf338 195 bprm->interpreter = interp_file;
a16b3357 196 if (fmt->flags & MISC_FMT_CREDENTIALS)
56305aa9 197 bprm->execfd_creds = 1;
1da177e4 198
bc2bf338 199 retval = 0;
e6084d4a 200ret:
43a4f261 201 dput(fmt->dentry);
1da177e4 202 return retval;
1da177e4
LT
203}
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 }
7d65cf10 226 s[-1] ='\0';
1da177e4
LT
227 return s;
228}
229
e6084d4a 230static char *check_special_flags(char *sfs, Node *e)
1da177e4 231{
e6084d4a 232 char *p = sfs;
1da177e4
LT
233 int cont = 1;
234
235 /* special flags */
236 while (cont) {
237 switch (*p) {
e6084d4a
MF
238 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;
948b701a
JB
256 case 'F':
257 pr_debug("register: flag: F: open interpreter file now\n");
258 p++;
259 e->flags |= MISC_FMT_OPEN_FILE;
260 break;
e6084d4a
MF
261 default:
262 cont = 0;
1da177e4
LT
263 }
264 }
265
266 return p;
267}
e6084d4a 268
1da177e4
LT
269/*
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
6b899c4e
MF
281 pr_debug("register: received %zu bytes\n", count);
282
1da177e4
LT
283 /* some sanity checks */
284 err = -EINVAL;
bbaecc08 285 if ((count < 11) || (count > MAX_REGISTER_LENGTH))
1da177e4
LT
286 goto out;
287
288 err = -ENOMEM;
289 memsize = sizeof(Node) + count + 8;
f7e1ad1a 290 e = kmalloc(memsize, GFP_KERNEL);
1da177e4
LT
291 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))
e6084d4a 298 goto efault;
1da177e4
LT
299
300 del = *p++; /* delimeter */
301
6b899c4e
MF
302 pr_debug("register: delim: %#x {%c}\n", del, del);
303
304 /* Pad the buffer with the delim to simplify parsing below. */
e6084d4a 305 memset(buf + count, del, 8);
1da177e4 306
6b899c4e 307 /* Parse the 'name' field. */
1da177e4
LT
308 e->name = p;
309 p = strchr(p, del);
310 if (!p)
e6084d4a 311 goto einval;
1da177e4
LT
312 *p++ = '\0';
313 if (!e->name[0] ||
314 !strcmp(e->name, ".") ||
315 !strcmp(e->name, "..") ||
316 strchr(e->name, '/'))
e6084d4a 317 goto einval;
6b899c4e
MF
318
319 pr_debug("register: name: {%s}\n", e->name);
320
321 /* Parse the 'type' field. */
1da177e4 322 switch (*p++) {
6b899c4e
MF
323 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:
e6084d4a 332 goto einval;
1da177e4
LT
333 }
334 if (*p++ != del)
e6084d4a 335 goto einval;
6b899c4e 336
1da177e4 337 if (test_bit(Magic, &e->flags)) {
6b899c4e
MF
338 /* Handle the 'M' (magic) format. */
339 char *s;
340
341 /* Parse the 'offset' field. */
342 s = strchr(p, del);
1da177e4 343 if (!s)
e6084d4a 344 goto einval;
5cc41e09
TLSC
345 *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;
1da177e4 352 if (*p++)
e6084d4a 353 goto einval;
6b899c4e
MF
354 pr_debug("register: offset: %#x\n", e->offset);
355
356 /* Parse the 'magic' field. */
1da177e4
LT
357 e->magic = p;
358 p = scanarg(p, del);
359 if (!p)
e6084d4a 360 goto einval;
7d65cf10 361 if (!e->magic[0])
e6084d4a 362 goto einval;
6b899c4e
MF
363 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. */
1da177e4
LT
369 e->mask = p;
370 p = scanarg(p, del);
371 if (!p)
e6084d4a 372 goto einval;
7d65cf10 373 if (!e->mask[0]) {
1da177e4 374 e->mask = NULL;
6b899c4e
MF
375 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 */
8d82e180
AS
387 e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
388 if (e->mask &&
389 string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
e6084d4a 390 goto einval;
5cc41e09
TLSC
391 if (e->size > BINPRM_BUF_SIZE ||
392 BINPRM_BUF_SIZE - e->size < e->offset)
e6084d4a 393 goto einval;
6b899c4e
MF
394 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;
f7e1ad1a 402 char *masked = kmalloc(e->size, GFP_KERNEL);
6b899c4e
MF
403
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 }
1da177e4 419 } else {
6b899c4e
MF
420 /* Handle the 'E' (extension) format. */
421
422 /* Skip the 'offset' field. */
1da177e4
LT
423 p = strchr(p, del);
424 if (!p)
e6084d4a 425 goto einval;
1da177e4 426 *p++ = '\0';
6b899c4e
MF
427
428 /* Parse the 'magic' field. */
1da177e4
LT
429 e->magic = p;
430 p = strchr(p, del);
431 if (!p)
e6084d4a 432 goto einval;
1da177e4
LT
433 *p++ = '\0';
434 if (!e->magic[0] || strchr(e->magic, '/'))
e6084d4a 435 goto einval;
6b899c4e
MF
436 pr_debug("register: extension: {%s}\n", e->magic);
437
438 /* Skip the 'mask' field. */
1da177e4
LT
439 p = strchr(p, del);
440 if (!p)
e6084d4a 441 goto einval;
1da177e4
LT
442 *p++ = '\0';
443 }
6b899c4e
MF
444
445 /* Parse the 'interpreter' field. */
1da177e4
LT
446 e->interpreter = p;
447 p = strchr(p, del);
448 if (!p)
e6084d4a 449 goto einval;
1da177e4
LT
450 *p++ = '\0';
451 if (!e->interpreter[0])
e6084d4a 452 goto einval;
6b899c4e 453 pr_debug("register: interpreter: {%s}\n", e->interpreter);
1da177e4 454
6b899c4e 455 /* Parse the 'flags' field. */
e6084d4a 456 p = check_special_flags(p, e);
1da177e4
LT
457 if (*p == '\n')
458 p++;
459 if (p != buf + count)
e6084d4a
MF
460 goto einval;
461
1da177e4
LT
462 return e;
463
464out:
465 return ERR_PTR(err);
466
e6084d4a 467efault:
1da177e4
LT
468 kfree(e);
469 return ERR_PTR(-EFAULT);
e6084d4a 470einval:
1da177e4
LT
471 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
1da177e4
LT
483 if (count > 3)
484 return -EINVAL;
485 if (copy_from_user(s, buffer, count))
486 return -EFAULT;
de8288b1
AB
487 if (!count)
488 return 0;
e6084d4a 489 if (s[count - 1] == '\n')
1da177e4
LT
490 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{
6ceafb88
RV
504 char *dp = page;
505 const char *status = "disabled";
1da177e4
LT
506
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
6ceafb88 515 dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter);
1da177e4
LT
516
517 /* print the special flags */
6ceafb88 518 dp += sprintf(dp, "flags: ");
e6084d4a
MF
519 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';
948b701a
JB
525 if (e->flags & MISC_FMT_OPEN_FILE)
526 *dp++ = 'F';
e6084d4a 527 *dp++ = '\n';
1da177e4
LT
528
529 if (!test_bit(Magic, &e->flags)) {
530 sprintf(dp, "extension .%s\n", e->magic);
531 } else {
6ceafb88
RV
532 dp += sprintf(dp, "offset %i\nmagic ", e->offset);
533 dp = bin2hex(dp, e->magic, e->size);
1da177e4 534 if (e->mask) {
6ceafb88
RV
535 dp += sprintf(dp, "\nmask ");
536 dp = bin2hex(dp, e->mask, e->size);
1da177e4
LT
537 }
538 *dp++ = '\n';
539 *dp = '\0';
540 }
541}
542
543static struct inode *bm_get_inode(struct super_block *sb, int mode)
544{
e6084d4a 545 struct inode *inode = new_inode(sb);
1da177e4
LT
546
547 if (inode) {
85fe4025 548 inode->i_ino = get_next_ino();
1da177e4 549 inode->i_mode = mode;
2276e5ba 550 inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
1da177e4
LT
551 }
552 return inode;
553}
554
b57922d9 555static void bm_evict_inode(struct inode *inode)
1da177e4 556{
83f91827
ON
557 Node *e = inode->i_private;
558
7e866006 559 if (e && e->flags & MISC_FMT_OPEN_FILE)
83f91827
ON
560 filp_close(e->interp_file, NULL);
561
dbd5768f 562 clear_inode(inode);
83f91827 563 kfree(e);
1da177e4
LT
564}
565
566static void kill_node(Node *e)
567{
568 struct dentry *dentry;
569
570 write_lock(&entries_lock);
baba1b29 571 list_del_init(&e->list);
1da177e4
LT
572 write_unlock(&entries_lock);
573
baba1b29
ON
574 dentry = e->dentry;
575 drop_nlink(d_inode(dentry));
576 d_drop(dentry);
577 dput(dentry);
578 simple_release_fs(&bm_mnt, &entry_count);
1da177e4
LT
579}
580
581/* /<entry> */
582
583static ssize_t
e6084d4a 584bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1da177e4 585{
496ad9aa 586 Node *e = file_inode(file)->i_private;
1da177e4
LT
587 ssize_t res;
588 char *page;
1da177e4 589
e6084d4a
MF
590 page = (char *) __get_free_page(GFP_KERNEL);
591 if (!page)
1da177e4
LT
592 return -ENOMEM;
593
594 entry_status(e, page);
1da177e4 595
6e2c10a1
AM
596 res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
597
1da177e4
LT
598 free_page((unsigned long) page);
599 return res;
600}
601
602static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
603 size_t count, loff_t *ppos)
604{
605 struct dentry *root;
496ad9aa 606 Node *e = file_inode(file)->i_private;
1da177e4
LT
607 int res = parse_command(buffer, count);
608
609 switch (res) {
e6084d4a
MF
610 case 1:
611 /* Disable this handler. */
612 clear_bit(Enabled, &e->flags);
613 break;
614 case 2:
615 /* Enable this handler. */
616 set_bit(Enabled, &e->flags);
617 break;
618 case 3:
619 /* Delete this handler. */
ea7d4c04 620 root = file_inode(file)->i_sb->s_root;
5955102c 621 inode_lock(d_inode(root));
1da177e4 622
baba1b29
ON
623 if (!list_empty(&e->list))
624 kill_node(e);
1da177e4 625
5955102c 626 inode_unlock(d_inode(root));
e6084d4a
MF
627 break;
628 default:
629 return res;
1da177e4 630 }
e6084d4a 631
1da177e4
LT
632 return count;
633}
634
4b6f5d20 635static const struct file_operations bm_entry_operations = {
1da177e4
LT
636 .read = bm_entry_read,
637 .write = bm_entry_write,
6038f373 638 .llseek = default_llseek,
1da177e4
LT
639};
640
641/* /register */
642
643static ssize_t bm_register_write(struct file *file, const char __user *buffer,
644 size_t count, loff_t *ppos)
645{
646 Node *e;
647 struct inode *inode;
ea7d4c04
AV
648 struct super_block *sb = file_inode(file)->i_sb;
649 struct dentry *root = sb->s_root, *dentry;
1da177e4 650 int err = 0;
e7850f4d 651 struct file *f = NULL;
1da177e4
LT
652
653 e = create_entry(buffer, count);
654
655 if (IS_ERR(e))
656 return PTR_ERR(e);
657
e7850f4d
LR
658 if (e->flags & MISC_FMT_OPEN_FILE) {
659 f = open_exec(e->interpreter);
660 if (IS_ERR(f)) {
661 pr_notice("register: failed to install interpreter file %s\n",
662 e->interpreter);
663 kfree(e);
664 return PTR_ERR(f);
665 }
666 e->interp_file = f;
667 }
668
5955102c 669 inode_lock(d_inode(root));
1da177e4
LT
670 dentry = lookup_one_len(e->name, root, strlen(e->name));
671 err = PTR_ERR(dentry);
672 if (IS_ERR(dentry))
673 goto out;
674
675 err = -EEXIST;
75c3cfa8 676 if (d_really_is_positive(dentry))
1da177e4
LT
677 goto out2;
678
679 inode = bm_get_inode(sb, S_IFREG | 0644);
680
681 err = -ENOMEM;
682 if (!inode)
683 goto out2;
684
1f5ce9e9 685 err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
1da177e4
LT
686 if (err) {
687 iput(inode);
688 inode = NULL;
689 goto out2;
690 }
691
692 e->dentry = dget(dentry);
8e18e294 693 inode->i_private = e;
1da177e4
LT
694 inode->i_fop = &bm_entry_operations;
695
696 d_instantiate(dentry, inode);
697 write_lock(&entries_lock);
698 list_add(&e->list, &entries);
699 write_unlock(&entries_lock);
700
701 err = 0;
702out2:
703 dput(dentry);
704out:
5955102c 705 inode_unlock(d_inode(root));
1da177e4
LT
706
707 if (err) {
e7850f4d
LR
708 if (f)
709 filp_close(f, NULL);
1da177e4 710 kfree(e);
948b701a 711 return err;
1da177e4
LT
712 }
713 return count;
714}
715
4b6f5d20 716static const struct file_operations bm_register_operations = {
1da177e4 717 .write = bm_register_write,
6038f373 718 .llseek = noop_llseek,
1da177e4
LT
719};
720
721/* /status */
722
723static ssize_t
724bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
725{
87113e80 726 char *s = enabled ? "enabled\n" : "disabled\n";
1da177e4 727
92f4c701 728 return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
1da177e4
LT
729}
730
e6084d4a 731static ssize_t bm_status_write(struct file *file, const char __user *buffer,
1da177e4
LT
732 size_t count, loff_t *ppos)
733{
734 int res = parse_command(buffer, count);
735 struct dentry *root;
736
737 switch (res) {
e6084d4a
MF
738 case 1:
739 /* Disable all handlers. */
740 enabled = 0;
741 break;
742 case 2:
743 /* Enable all handlers. */
744 enabled = 1;
745 break;
746 case 3:
747 /* Delete all handlers. */
ea7d4c04 748 root = file_inode(file)->i_sb->s_root;
5955102c 749 inode_lock(d_inode(root));
1da177e4 750
e6084d4a 751 while (!list_empty(&entries))
baba1b29 752 kill_node(list_first_entry(&entries, Node, list));
1da177e4 753
5955102c 754 inode_unlock(d_inode(root));
e6084d4a
MF
755 break;
756 default:
757 return res;
1da177e4 758 }
e6084d4a 759
1da177e4
LT
760 return count;
761}
762
4b6f5d20 763static const struct file_operations bm_status_operations = {
1da177e4
LT
764 .read = bm_status_read,
765 .write = bm_status_write,
6038f373 766 .llseek = default_llseek,
1da177e4
LT
767};
768
769/* Superblock handling */
770
ee9b6d61 771static const struct super_operations s_ops = {
1da177e4 772 .statfs = simple_statfs,
b57922d9 773 .evict_inode = bm_evict_inode,
1da177e4
LT
774};
775
bc99a664 776static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
1da177e4 777{
e6084d4a 778 int err;
cda37124 779 static const struct tree_descr bm_files[] = {
1a1c9bb4
JL
780 [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
781 [3] = {"register", &bm_register_operations, S_IWUSR},
1da177e4
LT
782 /* last one */ {""}
783 };
e6084d4a
MF
784
785 err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
1da177e4
LT
786 if (!err)
787 sb->s_op = &s_ops;
788 return err;
789}
790
bc99a664 791static int bm_get_tree(struct fs_context *fc)
1da177e4 792{
bc99a664
DH
793 return get_tree_single(fc, bm_fill_super);
794}
795
796static const struct fs_context_operations bm_context_ops = {
797 .get_tree = bm_get_tree,
798};
799
800static int bm_init_fs_context(struct fs_context *fc)
801{
802 fc->ops = &bm_context_ops;
803 return 0;
1da177e4
LT
804}
805
806static struct linux_binfmt misc_format = {
807 .module = THIS_MODULE,
808 .load_binary = load_misc_binary,
809};
810
811static struct file_system_type bm_fs_type = {
812 .owner = THIS_MODULE,
813 .name = "binfmt_misc",
bc99a664 814 .init_fs_context = bm_init_fs_context,
1da177e4
LT
815 .kill_sb = kill_litter_super,
816};
7f78e035 817MODULE_ALIAS_FS("binfmt_misc");
1da177e4
LT
818
819static int __init init_misc_binfmt(void)
820{
821 int err = register_filesystem(&bm_fs_type);
8fc3dc5a
AV
822 if (!err)
823 insert_binfmt(&misc_format);
b42bc9a3 824 return err;
1da177e4
LT
825}
826
827static void __exit exit_misc_binfmt(void)
828{
829 unregister_binfmt(&misc_format);
830 unregister_filesystem(&bm_fs_type);
831}
832
833core_initcall(init_misc_binfmt);
834module_exit(exit_misc_binfmt);
835MODULE_LICENSE("GPL");