binfmt_misc.c 17.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
L
Linus Torvalds 已提交
2
/*
3
 * binfmt_misc.c
L
Linus Torvalds 已提交
4
 *
5
 * Copyright (C) 1997 Richard Günther
L
Linus Torvalds 已提交
6
 *
7
 * binfmt_misc detects binaries via a magic or filename extension and invokes
8
 * a specified wrapper. See Documentation/admin-guide/binfmt-misc.rst for more details.
L
Linus Torvalds 已提交
9 10
 */

11 12
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

13
#include <linux/kernel.h>
L
Linus Torvalds 已提交
14 15
#include <linux/module.h>
#include <linux/init.h>
16
#include <linux/sched/mm.h>
17
#include <linux/magic.h>
L
Linus Torvalds 已提交
18 19 20
#include <linux/binfmts.h>
#include <linux/slab.h>
#include <linux/ctype.h>
21
#include <linux/string_helpers.h>
L
Linus Torvalds 已提交
22 23 24 25
#include <linux/file.h>
#include <linux/pagemap.h>
#include <linux/namei.h>
#include <linux/mount.h>
26
#include <linux/fs_context.h>
L
Linus Torvalds 已提交
27
#include <linux/syscalls.h>
28
#include <linux/fs.h>
29
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
30

31 32
#include "internal.h"

33 34 35 36 37
#ifdef DEBUG
# define USE_DEBUG 1
#else
# define USE_DEBUG 0
#endif
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46

enum {
	VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
};

static LIST_HEAD(entries);
static int enabled = 1;

enum {Enabled, Magic};
47 48 49
#define MISC_FMT_PRESERVE_ARGV0 (1 << 31)
#define MISC_FMT_OPEN_BINARY (1 << 30)
#define MISC_FMT_CREDENTIALS (1 << 29)
50
#define MISC_FMT_OPEN_FILE (1 << 28)
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58

typedef struct {
	struct list_head list;
	unsigned long flags;		/* type, status, etc. */
	int offset;			/* offset of magic */
	int size;			/* size of magic/mask */
	char *magic;			/* magic or filename extension */
	char *mask;			/* mask, NULL for exact match */
59
	const char *interpreter;	/* filename of interpreter */
L
Linus Torvalds 已提交
60 61
	char *name;
	struct dentry *dentry;
62
	struct file *interp_file;
L
Linus Torvalds 已提交
63 64 65
} Node;

static DEFINE_RWLOCK(entries_lock);
66
static struct file_system_type bm_fs_type;
L
Linus Torvalds 已提交
67 68 69
static struct vfsmount *bm_mnt;
static int entry_count;

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
 * Max length of the register string.  Determined by:
 *  - 7 delimiters
 *  - name:   ~50 bytes
 *  - type:   1 byte
 *  - offset: 3 bytes (has to be smaller than BINPRM_BUF_SIZE)
 *  - magic:  128 bytes (512 in escaped form)
 *  - mask:   128 bytes (512 in escaped form)
 *  - interp: ~50 bytes
 *  - flags:  5 bytes
 * Round that up a bit, and then back off to hold the internal data
 * (like struct Node).
 */
#define MAX_REGISTER_LENGTH 1920

/*
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94
 * Check if we support the binfmt
 * if we do, return the node, else NULL
 * locking is done in load_misc_binary
 */
static Node *check_file(struct linux_binprm *bprm)
{
	char *p = strrchr(bprm->interp, '.');
	struct list_head *l;

95
	/* Walk all the registered handlers. */
L
Linus Torvalds 已提交
96 97 98 99 100
	list_for_each(l, &entries) {
		Node *e = list_entry(l, Node, list);
		char *s;
		int j;

101
		/* Make sure this one is currently enabled. */
L
Linus Torvalds 已提交
102 103 104
		if (!test_bit(Enabled, &e->flags))
			continue;

105
		/* Do matching based on extension if applicable. */
L
Linus Torvalds 已提交
106 107 108 109 110 111
		if (!test_bit(Magic, &e->flags)) {
			if (p && !strcmp(e->magic, p + 1))
				return e;
			continue;
		}

112
		/* Do matching based on magic & mask. */
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		s = bprm->buf + e->offset;
		if (e->mask) {
			for (j = 0; j < e->size; j++)
				if ((*s++ ^ e->magic[j]) & e->mask[j])
					break;
		} else {
			for (j = 0; j < e->size; j++)
				if ((*s++ ^ e->magic[j]))
					break;
		}
		if (j == e->size)
			return e;
	}
	return NULL;
}

/*
 * the loader itself
 */
132
static int load_misc_binary(struct linux_binprm *bprm)
L
Linus Torvalds 已提交
133 134
{
	Node *fmt;
135
	struct file *interp_file = NULL;
L
Linus Torvalds 已提交
136 137 138 139
	int retval;

	retval = -ENOEXEC;
	if (!enabled)
140
		return retval;
L
Linus Torvalds 已提交
141 142 143 144

	/* to keep locking time low, we copy the interpreter string */
	read_lock(&entries_lock);
	fmt = check_file(bprm);
145
	if (fmt)
146
		dget(fmt->dentry);
L
Linus Torvalds 已提交
147 148
	read_unlock(&entries_lock);
	if (!fmt)
149
		return retval;
L
Linus Torvalds 已提交
150

151
	/* Need to be able to load the file after exec */
152
	retval = -ENOENT;
153
	if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
154
		goto ret;
155

L
Linus Torvalds 已提交
156
	if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
157 158
		retval = remove_arg_zero(bprm);
		if (retval)
159
			goto ret;
L
Linus Torvalds 已提交
160 161
	}

162
	if (fmt->flags & MISC_FMT_OPEN_BINARY)
E
Eric W. Biederman 已提交
163
		bprm->have_execfd = 1;
L
Linus Torvalds 已提交
164 165

	/* make argv[1] be the path to the binary */
166
	retval = copy_string_kernel(bprm->interp, bprm);
L
Linus Torvalds 已提交
167
	if (retval < 0)
E
Eric W. Biederman 已提交
168
		goto ret;
L
Linus Torvalds 已提交
169 170 171
	bprm->argc++;

	/* add the interp as argv[0] */
172
	retval = copy_string_kernel(fmt->interpreter, bprm);
L
Linus Torvalds 已提交
173
	if (retval < 0)
E
Eric W. Biederman 已提交
174
		goto ret;
175
	bprm->argc++;
L
Linus Torvalds 已提交
176

177
	/* Update interp in case binfmt_script needs it. */
178
	retval = bprm_change_interp(fmt->interpreter, bprm);
179
	if (retval < 0)
E
Eric W. Biederman 已提交
180
		goto ret;
L
Linus Torvalds 已提交
181

182
	if (fmt->flags & MISC_FMT_OPEN_FILE) {
183
		interp_file = file_clone_open(fmt->interp_file);
184 185 186
		if (!IS_ERR(interp_file))
			deny_write_access(interp_file);
	} else {
187
		interp_file = open_exec(fmt->interpreter);
188
	}
189 190
	retval = PTR_ERR(interp_file);
	if (IS_ERR(interp_file))
E
Eric W. Biederman 已提交
191
		goto ret;
L
Linus Torvalds 已提交
192

193
	bprm->interpreter = interp_file;
194
	if (fmt->flags & MISC_FMT_CREDENTIALS)
195
		bprm->execfd_creds = 1;
L
Linus Torvalds 已提交
196

197
	retval = 0;
198
ret:
199
	dput(fmt->dentry);
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	return retval;
}

/* Command parsers */

/*
 * parses and copies one argument enclosed in del from *sp to *dp,
 * recognising the \x special.
 * returns pointer to the copied argument or NULL in case of an
 * error (and sets err) or null argument length.
 */
static char *scanarg(char *s, char del)
{
	char c;

	while ((c = *s++) != del) {
		if (c == '\\' && *s == 'x') {
			s++;
			if (!isxdigit(*s++))
				return NULL;
			if (!isxdigit(*s++))
				return NULL;
		}
	}
224
	s[-1] ='\0';
L
Linus Torvalds 已提交
225 226 227
	return s;
}

228
static char *check_special_flags(char *sfs, Node *e)
L
Linus Torvalds 已提交
229
{
230
	char *p = sfs;
L
Linus Torvalds 已提交
231 232 233 234 235
	int cont = 1;

	/* special flags */
	while (cont) {
		switch (*p) {
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
		case 'P':
			pr_debug("register: flag: P (preserve argv0)\n");
			p++;
			e->flags |= MISC_FMT_PRESERVE_ARGV0;
			break;
		case 'O':
			pr_debug("register: flag: O (open binary)\n");
			p++;
			e->flags |= MISC_FMT_OPEN_BINARY;
			break;
		case 'C':
			pr_debug("register: flag: C (preserve creds)\n");
			p++;
			/* this flags also implies the
			   open-binary flag */
			e->flags |= (MISC_FMT_CREDENTIALS |
					MISC_FMT_OPEN_BINARY);
			break;
254 255 256 257 258
		case 'F':
			pr_debug("register: flag: F: open interpreter file now\n");
			p++;
			e->flags |= MISC_FMT_OPEN_FILE;
			break;
259 260
		default:
			cont = 0;
L
Linus Torvalds 已提交
261 262 263 264 265
		}
	}

	return p;
}
266

L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278
/*
 * This registers a new binary format, it recognises the syntax
 * ':name:type:offset:magic:mask:interpreter:flags'
 * where the ':' is the IFS, that can be chosen with the first char
 */
static Node *create_entry(const char __user *buffer, size_t count)
{
	Node *e;
	int memsize, err;
	char *buf, *p;
	char del;

279 280
	pr_debug("register: received %zu bytes\n", count);

L
Linus Torvalds 已提交
281 282
	/* some sanity checks */
	err = -EINVAL;
283
	if ((count < 11) || (count > MAX_REGISTER_LENGTH))
L
Linus Torvalds 已提交
284 285 286 287
		goto out;

	err = -ENOMEM;
	memsize = sizeof(Node) + count + 8;
288
	e = kmalloc(memsize, GFP_KERNEL);
L
Linus Torvalds 已提交
289 290 291 292 293 294 295
	if (!e)
		goto out;

	p = buf = (char *)e + sizeof(Node);

	memset(e, 0, sizeof(Node));
	if (copy_from_user(buf, buffer, count))
296
		goto efault;
L
Linus Torvalds 已提交
297 298 299

	del = *p++;	/* delimeter */

300 301 302
	pr_debug("register: delim: %#x {%c}\n", del, del);

	/* Pad the buffer with the delim to simplify parsing below. */
303
	memset(buf + count, del, 8);
L
Linus Torvalds 已提交
304

305
	/* Parse the 'name' field. */
L
Linus Torvalds 已提交
306 307 308
	e->name = p;
	p = strchr(p, del);
	if (!p)
309
		goto einval;
L
Linus Torvalds 已提交
310 311 312 313 314
	*p++ = '\0';
	if (!e->name[0] ||
	    !strcmp(e->name, ".") ||
	    !strcmp(e->name, "..") ||
	    strchr(e->name, '/'))
315
		goto einval;
316 317 318 319

	pr_debug("register: name: {%s}\n", e->name);

	/* Parse the 'type' field. */
L
Linus Torvalds 已提交
320
	switch (*p++) {
321 322 323 324 325 326 327 328 329
	case 'E':
		pr_debug("register: type: E (extension)\n");
		e->flags = 1 << Enabled;
		break;
	case 'M':
		pr_debug("register: type: M (magic)\n");
		e->flags = (1 << Enabled) | (1 << Magic);
		break;
	default:
330
		goto einval;
L
Linus Torvalds 已提交
331 332
	}
	if (*p++ != del)
333
		goto einval;
334

L
Linus Torvalds 已提交
335
	if (test_bit(Magic, &e->flags)) {
336 337 338 339 340
		/* Handle the 'M' (magic) format. */
		char *s;

		/* Parse the 'offset' field. */
		s = strchr(p, del);
L
Linus Torvalds 已提交
341
		if (!s)
342
			goto einval;
343 344 345 346 347 348 349
		*s = '\0';
		if (p != s) {
			int r = kstrtoint(p, 10, &e->offset);
			if (r != 0 || e->offset < 0)
				goto einval;
		}
		p = s;
L
Linus Torvalds 已提交
350
		if (*p++)
351
			goto einval;
352 353 354
		pr_debug("register: offset: %#x\n", e->offset);

		/* Parse the 'magic' field. */
L
Linus Torvalds 已提交
355 356 357
		e->magic = p;
		p = scanarg(p, del);
		if (!p)
358
			goto einval;
359
		if (!e->magic[0])
360
			goto einval;
361 362 363 364 365 366
		if (USE_DEBUG)
			print_hex_dump_bytes(
				KBUILD_MODNAME ": register: magic[raw]: ",
				DUMP_PREFIX_NONE, e->magic, p - e->magic);

		/* Parse the 'mask' field. */
L
Linus Torvalds 已提交
367 368 369
		e->mask = p;
		p = scanarg(p, del);
		if (!p)
370
			goto einval;
371
		if (!e->mask[0]) {
L
Linus Torvalds 已提交
372
			e->mask = NULL;
373 374 375 376 377 378 379 380 381 382 383 384
			pr_debug("register:  mask[raw]: none\n");
		} else if (USE_DEBUG)
			print_hex_dump_bytes(
				KBUILD_MODNAME ": register:  mask[raw]: ",
				DUMP_PREFIX_NONE, e->mask, p - e->mask);

		/*
		 * Decode the magic & mask fields.
		 * Note: while we might have accepted embedded NUL bytes from
		 * above, the unescape helpers here will stop at the first one
		 * it encounters.
		 */
385 386 387
		e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
		if (e->mask &&
		    string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
388
			goto einval;
389 390
		if (e->size > BINPRM_BUF_SIZE ||
		    BINPRM_BUF_SIZE - e->size < e->offset)
391
			goto einval;
392 393 394 395 396 397 398 399
		pr_debug("register: magic/mask length: %i\n", e->size);
		if (USE_DEBUG) {
			print_hex_dump_bytes(
				KBUILD_MODNAME ": register: magic[decoded]: ",
				DUMP_PREFIX_NONE, e->magic, e->size);

			if (e->mask) {
				int i;
400
				char *masked = kmalloc(e->size, GFP_KERNEL);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

				print_hex_dump_bytes(
					KBUILD_MODNAME ": register:  mask[decoded]: ",
					DUMP_PREFIX_NONE, e->mask, e->size);

				if (masked) {
					for (i = 0; i < e->size; ++i)
						masked[i] = e->magic[i] & e->mask[i];
					print_hex_dump_bytes(
						KBUILD_MODNAME ": register:  magic[masked]: ",
						DUMP_PREFIX_NONE, masked, e->size);

					kfree(masked);
				}
			}
		}
L
Linus Torvalds 已提交
417
	} else {
418 419 420
		/* Handle the 'E' (extension) format. */

		/* Skip the 'offset' field. */
L
Linus Torvalds 已提交
421 422
		p = strchr(p, del);
		if (!p)
423
			goto einval;
L
Linus Torvalds 已提交
424
		*p++ = '\0';
425 426

		/* Parse the 'magic' field. */
L
Linus Torvalds 已提交
427 428 429
		e->magic = p;
		p = strchr(p, del);
		if (!p)
430
			goto einval;
L
Linus Torvalds 已提交
431 432
		*p++ = '\0';
		if (!e->magic[0] || strchr(e->magic, '/'))
433
			goto einval;
434 435 436
		pr_debug("register: extension: {%s}\n", e->magic);

		/* Skip the 'mask' field. */
L
Linus Torvalds 已提交
437 438
		p = strchr(p, del);
		if (!p)
439
			goto einval;
L
Linus Torvalds 已提交
440 441
		*p++ = '\0';
	}
442 443

	/* Parse the 'interpreter' field. */
L
Linus Torvalds 已提交
444 445 446
	e->interpreter = p;
	p = strchr(p, del);
	if (!p)
447
		goto einval;
L
Linus Torvalds 已提交
448 449
	*p++ = '\0';
	if (!e->interpreter[0])
450
		goto einval;
451
	pr_debug("register: interpreter: {%s}\n", e->interpreter);
L
Linus Torvalds 已提交
452

453
	/* Parse the 'flags' field. */
454
	p = check_special_flags(p, e);
L
Linus Torvalds 已提交
455 456 457
	if (*p == '\n')
		p++;
	if (p != buf + count)
458 459
		goto einval;

L
Linus Torvalds 已提交
460 461 462 463 464
	return e;

out:
	return ERR_PTR(err);

465
efault:
L
Linus Torvalds 已提交
466 467
	kfree(e);
	return ERR_PTR(-EFAULT);
468
einval:
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	kfree(e);
	return ERR_PTR(-EINVAL);
}

/*
 * Set status of entry/binfmt_misc:
 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
 */
static int parse_command(const char __user *buffer, size_t count)
{
	char s[4];

	if (count > 3)
		return -EINVAL;
	if (copy_from_user(s, buffer, count))
		return -EFAULT;
485 486
	if (!count)
		return 0;
487
	if (s[count - 1] == '\n')
L
Linus Torvalds 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501
		count--;
	if (count == 1 && s[0] == '0')
		return 1;
	if (count == 1 && s[0] == '1')
		return 2;
	if (count == 2 && s[0] == '-' && s[1] == '1')
		return 3;
	return -EINVAL;
}

/* generic stuff */

static void entry_status(Node *e, char *page)
{
502 503
	char *dp = page;
	const char *status = "disabled";
L
Linus Torvalds 已提交
504 505 506 507 508 509 510 511 512

	if (test_bit(Enabled, &e->flags))
		status = "enabled";

	if (!VERBOSE_STATUS) {
		sprintf(page, "%s\n", status);
		return;
	}

513
	dp += sprintf(dp, "%s\ninterpreter %s\n", status, e->interpreter);
L
Linus Torvalds 已提交
514 515

	/* print the special flags */
516
	dp += sprintf(dp, "flags: ");
517 518 519 520 521 522
	if (e->flags & MISC_FMT_PRESERVE_ARGV0)
		*dp++ = 'P';
	if (e->flags & MISC_FMT_OPEN_BINARY)
		*dp++ = 'O';
	if (e->flags & MISC_FMT_CREDENTIALS)
		*dp++ = 'C';
523 524
	if (e->flags & MISC_FMT_OPEN_FILE)
		*dp++ = 'F';
525
	*dp++ = '\n';
L
Linus Torvalds 已提交
526 527 528 529

	if (!test_bit(Magic, &e->flags)) {
		sprintf(dp, "extension .%s\n", e->magic);
	} else {
530 531
		dp += sprintf(dp, "offset %i\nmagic ", e->offset);
		dp = bin2hex(dp, e->magic, e->size);
L
Linus Torvalds 已提交
532
		if (e->mask) {
533 534
			dp += sprintf(dp, "\nmask ");
			dp = bin2hex(dp, e->mask, e->size);
L
Linus Torvalds 已提交
535 536 537 538 539 540 541 542
		}
		*dp++ = '\n';
		*dp = '\0';
	}
}

static struct inode *bm_get_inode(struct super_block *sb, int mode)
{
543
	struct inode *inode = new_inode(sb);
L
Linus Torvalds 已提交
544 545

	if (inode) {
546
		inode->i_ino = get_next_ino();
L
Linus Torvalds 已提交
547 548
		inode->i_mode = mode;
		inode->i_atime = inode->i_mtime = inode->i_ctime =
549
			current_time(inode);
L
Linus Torvalds 已提交
550 551 552 553
	}
	return inode;
}

554
static void bm_evict_inode(struct inode *inode)
L
Linus Torvalds 已提交
555
{
556 557
	Node *e = inode->i_private;

558
	if (e && e->flags & MISC_FMT_OPEN_FILE)
559 560
		filp_close(e->interp_file, NULL);

561
	clear_inode(inode);
562
	kfree(e);
L
Linus Torvalds 已提交
563 564 565 566 567 568 569
}

static void kill_node(Node *e)
{
	struct dentry *dentry;

	write_lock(&entries_lock);
570
	list_del_init(&e->list);
L
Linus Torvalds 已提交
571 572
	write_unlock(&entries_lock);

573 574 575 576 577
	dentry = e->dentry;
	drop_nlink(d_inode(dentry));
	d_drop(dentry);
	dput(dentry);
	simple_release_fs(&bm_mnt, &entry_count);
L
Linus Torvalds 已提交
578 579 580 581 582
}

/* /<entry> */

static ssize_t
583
bm_entry_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
L
Linus Torvalds 已提交
584
{
A
Al Viro 已提交
585
	Node *e = file_inode(file)->i_private;
L
Linus Torvalds 已提交
586 587 588
	ssize_t res;
	char *page;

589 590
	page = (char *) __get_free_page(GFP_KERNEL);
	if (!page)
L
Linus Torvalds 已提交
591 592 593 594
		return -ENOMEM;

	entry_status(e, page);

595 596
	res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));

L
Linus Torvalds 已提交
597 598 599 600 601 602 603 604
	free_page((unsigned long) page);
	return res;
}

static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
				size_t count, loff_t *ppos)
{
	struct dentry *root;
A
Al Viro 已提交
605
	Node *e = file_inode(file)->i_private;
L
Linus Torvalds 已提交
606 607 608
	int res = parse_command(buffer, count);

	switch (res) {
609 610 611 612 613 614 615 616 617 618
	case 1:
		/* Disable this handler. */
		clear_bit(Enabled, &e->flags);
		break;
	case 2:
		/* Enable this handler. */
		set_bit(Enabled, &e->flags);
		break;
	case 3:
		/* Delete this handler. */
619
		root = file_inode(file)->i_sb->s_root;
A
Al Viro 已提交
620
		inode_lock(d_inode(root));
L
Linus Torvalds 已提交
621

622 623
		if (!list_empty(&e->list))
			kill_node(e);
L
Linus Torvalds 已提交
624

A
Al Viro 已提交
625
		inode_unlock(d_inode(root));
626 627 628
		break;
	default:
		return res;
L
Linus Torvalds 已提交
629
	}
630

L
Linus Torvalds 已提交
631 632 633
	return count;
}

634
static const struct file_operations bm_entry_operations = {
L
Linus Torvalds 已提交
635 636
	.read		= bm_entry_read,
	.write		= bm_entry_write,
637
	.llseek		= default_llseek,
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646
};

/* /register */

static ssize_t bm_register_write(struct file *file, const char __user *buffer,
			       size_t count, loff_t *ppos)
{
	Node *e;
	struct inode *inode;
647 648
	struct super_block *sb = file_inode(file)->i_sb;
	struct dentry *root = sb->s_root, *dentry;
L
Linus Torvalds 已提交
649 650 651 652 653 654 655
	int err = 0;

	e = create_entry(buffer, count);

	if (IS_ERR(e))
		return PTR_ERR(e);

A
Al Viro 已提交
656
	inode_lock(d_inode(root));
L
Linus Torvalds 已提交
657 658 659 660 661 662
	dentry = lookup_one_len(e->name, root, strlen(e->name));
	err = PTR_ERR(dentry);
	if (IS_ERR(dentry))
		goto out;

	err = -EEXIST;
663
	if (d_really_is_positive(dentry))
L
Linus Torvalds 已提交
664 665 666 667 668 669 670 671
		goto out2;

	inode = bm_get_inode(sb, S_IFREG | 0644);

	err = -ENOMEM;
	if (!inode)
		goto out2;

672
	err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
L
Linus Torvalds 已提交
673 674 675 676 677 678
	if (err) {
		iput(inode);
		inode = NULL;
		goto out2;
	}

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	if (e->flags & MISC_FMT_OPEN_FILE) {
		struct file *f;

		f = open_exec(e->interpreter);
		if (IS_ERR(f)) {
			err = PTR_ERR(f);
			pr_notice("register: failed to install interpreter file %s\n", e->interpreter);
			simple_release_fs(&bm_mnt, &entry_count);
			iput(inode);
			inode = NULL;
			goto out2;
		}
		e->interp_file = f;
	}

L
Linus Torvalds 已提交
694
	e->dentry = dget(dentry);
695
	inode->i_private = e;
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704 705 706
	inode->i_fop = &bm_entry_operations;

	d_instantiate(dentry, inode);
	write_lock(&entries_lock);
	list_add(&e->list, &entries);
	write_unlock(&entries_lock);

	err = 0;
out2:
	dput(dentry);
out:
A
Al Viro 已提交
707
	inode_unlock(d_inode(root));
L
Linus Torvalds 已提交
708 709 710

	if (err) {
		kfree(e);
711
		return err;
L
Linus Torvalds 已提交
712 713 714 715
	}
	return count;
}

716
static const struct file_operations bm_register_operations = {
L
Linus Torvalds 已提交
717
	.write		= bm_register_write,
718
	.llseek		= noop_llseek,
L
Linus Torvalds 已提交
719 720 721 722 723 724 725
};

/* /status */

static ssize_t
bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
726
	char *s = enabled ? "enabled\n" : "disabled\n";
L
Linus Torvalds 已提交
727

728
	return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
L
Linus Torvalds 已提交
729 730
}

731
static ssize_t bm_status_write(struct file *file, const char __user *buffer,
L
Linus Torvalds 已提交
732 733 734 735 736 737
		size_t count, loff_t *ppos)
{
	int res = parse_command(buffer, count);
	struct dentry *root;

	switch (res) {
738 739 740 741 742 743 744 745 746 747
	case 1:
		/* Disable all handlers. */
		enabled = 0;
		break;
	case 2:
		/* Enable all handlers. */
		enabled = 1;
		break;
	case 3:
		/* Delete all handlers. */
748
		root = file_inode(file)->i_sb->s_root;
A
Al Viro 已提交
749
		inode_lock(d_inode(root));
L
Linus Torvalds 已提交
750

751
		while (!list_empty(&entries))
752
			kill_node(list_first_entry(&entries, Node, list));
L
Linus Torvalds 已提交
753

A
Al Viro 已提交
754
		inode_unlock(d_inode(root));
755 756 757
		break;
	default:
		return res;
L
Linus Torvalds 已提交
758
	}
759

L
Linus Torvalds 已提交
760 761 762
	return count;
}

763
static const struct file_operations bm_status_operations = {
L
Linus Torvalds 已提交
764 765
	.read		= bm_status_read,
	.write		= bm_status_write,
766
	.llseek		= default_llseek,
L
Linus Torvalds 已提交
767 768 769 770
};

/* Superblock handling */

771
static const struct super_operations s_ops = {
L
Linus Torvalds 已提交
772
	.statfs		= simple_statfs,
773
	.evict_inode	= bm_evict_inode,
L
Linus Torvalds 已提交
774 775
};

776
static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
L
Linus Torvalds 已提交
777
{
778
	int err;
779
	static const struct tree_descr bm_files[] = {
780 781
		[2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
		[3] = {"register", &bm_register_operations, S_IWUSR},
L
Linus Torvalds 已提交
782 783
		/* last one */ {""}
	};
784 785

	err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
L
Linus Torvalds 已提交
786 787 788 789 790
	if (!err)
		sb->s_op = &s_ops;
	return err;
}

791
static int bm_get_tree(struct fs_context *fc)
L
Linus Torvalds 已提交
792
{
793 794 795 796 797 798 799 800 801 802 803
	return get_tree_single(fc, bm_fill_super);
}

static const struct fs_context_operations bm_context_ops = {
	.get_tree	= bm_get_tree,
};

static int bm_init_fs_context(struct fs_context *fc)
{
	fc->ops = &bm_context_ops;
	return 0;
L
Linus Torvalds 已提交
804 805 806 807 808 809 810 811 812 813
}

static struct linux_binfmt misc_format = {
	.module = THIS_MODULE,
	.load_binary = load_misc_binary,
};

static struct file_system_type bm_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "binfmt_misc",
814
	.init_fs_context = bm_init_fs_context,
L
Linus Torvalds 已提交
815 816
	.kill_sb	= kill_litter_super,
};
817
MODULE_ALIAS_FS("binfmt_misc");
L
Linus Torvalds 已提交
818 819 820 821

static int __init init_misc_binfmt(void)
{
	int err = register_filesystem(&bm_fs_type);
A
Al Viro 已提交
822 823
	if (!err)
		insert_binfmt(&misc_format);
L
Linus Torvalds 已提交
824 825 826 827 828 829 830 831 832 833 834 835
	return err;
}

static void __exit exit_misc_binfmt(void)
{
	unregister_binfmt(&misc_format);
	unregister_filesystem(&bm_fs_type);
}

core_initcall(init_misc_binfmt);
module_exit(exit_misc_binfmt);
MODULE_LICENSE("GPL");