binfmt_misc.c 17.4 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

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

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

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

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

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

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

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

199
	retval = 0;
200
ret:
201
	dput(fmt->dentry);
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	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;
		}
	}
226
	s[-1] ='\0';
L
Linus Torvalds 已提交
227 228 229
	return s;
}

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

	/* special flags */
	while (cont) {
		switch (*p) {
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		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;
256 257 258 259 260
		case 'F':
			pr_debug("register: flag: F: open interpreter file now\n");
			p++;
			e->flags |= MISC_FMT_OPEN_FILE;
			break;
261 262
		default:
			cont = 0;
L
Linus Torvalds 已提交
263 264 265 266 267
		}
	}

	return p;
}
268

L
Linus Torvalds 已提交
269 270 271 272 273 274 275 276 277 278 279 280
/*
 * 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;

281 282
	pr_debug("register: received %zu bytes\n", count);

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

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

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

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

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

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

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

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

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

	/* Parse the 'type' field. */
L
Linus Torvalds 已提交
322
	switch (*p++) {
323 324 325 326 327 328 329 330 331
	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:
332
		goto einval;
L
Linus Torvalds 已提交
333 334
	}
	if (*p++ != del)
335
		goto einval;
336

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

		/* Parse the 'offset' field. */
		s = strchr(p, del);
L
Linus Torvalds 已提交
343
		if (!s)
344
			goto einval;
345 346 347 348 349 350 351
		*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 已提交
352
		if (*p++)
353
			goto einval;
354 355 356
		pr_debug("register: offset: %#x\n", e->offset);

		/* Parse the 'magic' field. */
L
Linus Torvalds 已提交
357 358 359
		e->magic = p;
		p = scanarg(p, del);
		if (!p)
360
			goto einval;
361
		if (!e->magic[0])
362
			goto einval;
363 364 365 366 367 368
		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 已提交
369 370 371
		e->mask = p;
		p = scanarg(p, del);
		if (!p)
372
			goto einval;
373
		if (!e->mask[0]) {
L
Linus Torvalds 已提交
374
			e->mask = NULL;
375 376 377 378 379 380 381 382 383 384 385 386
			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.
		 */
387 388 389
		e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
		if (e->mask &&
		    string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
390
			goto einval;
391 392
		if (e->size > BINPRM_BUF_SIZE ||
		    BINPRM_BUF_SIZE - e->size < e->offset)
393
			goto einval;
394 395 396 397 398 399 400 401
		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;
402
				char *masked = kmalloc(e->size, GFP_KERNEL);
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

				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 已提交
419
	} else {
420 421 422
		/* Handle the 'E' (extension) format. */

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

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

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

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

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

L
Linus Torvalds 已提交
462 463 464 465 466
	return e;

out:
	return ERR_PTR(err);

467
efault:
L
Linus Torvalds 已提交
468 469
	kfree(e);
	return ERR_PTR(-EFAULT);
470
einval:
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	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;
487 488
	if (!count)
		return 0;
489
	if (s[count - 1] == '\n')
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503
		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)
{
504 505
	char *dp = page;
	const char *status = "disabled";
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514

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

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

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

	/* print the special flags */
518
	dp += sprintf(dp, "flags: ");
519 520 521 522 523 524
	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';
525 526
	if (e->flags & MISC_FMT_OPEN_FILE)
		*dp++ = 'F';
527
	*dp++ = '\n';
L
Linus Torvalds 已提交
528 529 530 531

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

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

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

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

560
	if (e && e->flags & MISC_FMT_OPEN_FILE)
561 562
		filp_close(e->interp_file, NULL);

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

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

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

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

/* /<entry> */

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

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

	entry_status(e, page);

597 598
	res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));

L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606
	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 已提交
607
	Node *e = file_inode(file)->i_private;
L
Linus Torvalds 已提交
608 609 610
	int res = parse_command(buffer, count);

	switch (res) {
611 612 613 614 615 616 617 618 619 620
	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. */
621
		root = file_inode(file)->i_sb->s_root;
A
Al Viro 已提交
622
		inode_lock(d_inode(root));
L
Linus Torvalds 已提交
623

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

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

L
Linus Torvalds 已提交
633 634 635
	return count;
}

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

/* /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;
649 650
	struct super_block *sb = file_inode(file)->i_sb;
	struct dentry *root = sb->s_root, *dentry;
L
Linus Torvalds 已提交
651
	int err = 0;
652
	struct file *f = NULL;
L
Linus Torvalds 已提交
653 654 655 656 657 658

	e = create_entry(buffer, count);

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

659 660 661 662 663 664 665 666 667 668 669
	if (e->flags & MISC_FMT_OPEN_FILE) {
		f = open_exec(e->interpreter);
		if (IS_ERR(f)) {
			pr_notice("register: failed to install interpreter file %s\n",
				 e->interpreter);
			kfree(e);
			return PTR_ERR(f);
		}
		e->interp_file = f;
	}

A
Al Viro 已提交
670
	inode_lock(d_inode(root));
L
Linus Torvalds 已提交
671 672 673 674 675 676
	dentry = lookup_one_len(e->name, root, strlen(e->name));
	err = PTR_ERR(dentry);
	if (IS_ERR(dentry))
		goto out;

	err = -EEXIST;
677
	if (d_really_is_positive(dentry))
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685
		goto out2;

	inode = bm_get_inode(sb, S_IFREG | 0644);

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

686
	err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
L
Linus Torvalds 已提交
687 688 689 690 691 692 693
	if (err) {
		iput(inode);
		inode = NULL;
		goto out2;
	}

	e->dentry = dget(dentry);
694
	inode->i_private = e;
L
Linus Torvalds 已提交
695 696 697 698 699 700 701 702 703 704 705
	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 已提交
706
	inode_unlock(d_inode(root));
L
Linus Torvalds 已提交
707 708

	if (err) {
709 710
		if (f)
			filp_close(f, NULL);
L
Linus Torvalds 已提交
711
		kfree(e);
712
		return err;
L
Linus Torvalds 已提交
713 714 715 716
	}
	return count;
}

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

/* /status */

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

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

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

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

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

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

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

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

/* Superblock handling */

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

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

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

792
static int bm_get_tree(struct fs_context *fc)
L
Linus Torvalds 已提交
793
{
794 795 796 797 798 799 800 801 802 803 804
	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 已提交
805 806 807 808 809 810 811 812 813 814
}

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",
815
	.init_fs_context = bm_init_fs_context,
L
Linus Torvalds 已提交
816 817
	.kill_sb	= kill_litter_super,
};
818
MODULE_ALIAS_FS("binfmt_misc");
L
Linus Torvalds 已提交
819 820 821 822

static int __init init_misc_binfmt(void)
{
	int err = register_filesystem(&bm_fs_type);
A
Al Viro 已提交
823 824
	if (!err)
		insert_binfmt(&misc_format);
L
Linus Torvalds 已提交
825 826 827 828 829 830 831 832 833 834 835 836
	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");