blob: b60500300dd7803e20655d50437aec7dfefeedec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * binfmt_misc.c
3 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +02004 * Copyright (C) 1997 Richard Günther
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * binfmt_misc detects binaries via a magic or filename extension and invokes
7 * a specified wrapper. This should obsolete binfmt_java, binfmt_em86 and
8 * binfmt_mz.
9 *
10 * 1997-04-25 first version
11 * [...]
12 * 1997-05-19 cleanup
13 * 1997-06-26 hpa: pass the real filename rather than argv[0]
14 * 1997-06-30 minor cleanup
15 * 1997-08-09 removed extension stripping, locking cleanup
16 * 2001-02-28 AV: rewritten into something that resembles C. Original didn't.
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040021#include <linux/sched.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070022#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/binfmts.h>
24#include <linux/slab.h>
25#include <linux/ctype.h>
Andy Shevchenko8d82e182013-04-30 15:27:33 -070026#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/file.h>
28#include <linux/pagemap.h>
29#include <linux/namei.h>
30#include <linux/mount.h>
31#include <linux/syscalls.h>
Akinobu Mita6e2c10a2008-07-23 21:29:15 -070032#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/uaccess.h>
35
36enum {
37 VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
38};
39
40static LIST_HEAD(entries);
41static int enabled = 1;
42
43enum {Enabled, Magic};
44#define MISC_FMT_PRESERVE_ARGV0 (1<<31)
45#define MISC_FMT_OPEN_BINARY (1<<30)
46#define MISC_FMT_CREDENTIALS (1<<29)
47
48typedef struct {
49 struct list_head list;
50 unsigned long flags; /* type, status, etc. */
51 int offset; /* offset of magic */
52 int size; /* size of magic/mask */
53 char *magic; /* magic or filename extension */
54 char *mask; /* mask, NULL for exact match */
55 char *interpreter; /* filename of interpreter */
56 char *name;
57 struct dentry *dentry;
58} Node;
59
60static DEFINE_RWLOCK(entries_lock);
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -040061static struct file_system_type bm_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct vfsmount *bm_mnt;
63static int entry_count;
64
65/*
66 * Check if we support the binfmt
67 * if we do, return the node, else NULL
68 * locking is done in load_misc_binary
69 */
70static Node *check_file(struct linux_binprm *bprm)
71{
72 char *p = strrchr(bprm->interp, '.');
73 struct list_head *l;
74
75 list_for_each(l, &entries) {
76 Node *e = list_entry(l, Node, list);
77 char *s;
78 int j;
79
80 if (!test_bit(Enabled, &e->flags))
81 continue;
82
83 if (!test_bit(Magic, &e->flags)) {
84 if (p && !strcmp(e->magic, p + 1))
85 return e;
86 continue;
87 }
88
89 s = bprm->buf + e->offset;
90 if (e->mask) {
91 for (j = 0; j < e->size; j++)
92 if ((*s++ ^ e->magic[j]) & e->mask[j])
93 break;
94 } else {
95 for (j = 0; j < e->size; j++)
96 if ((*s++ ^ e->magic[j]))
97 break;
98 }
99 if (j == e->size)
100 return e;
101 }
102 return NULL;
103}
104
105/*
106 * the loader itself
107 */
Al Viro71613c32012-10-20 22:00:48 -0400108static int load_misc_binary(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 Node *fmt;
111 struct file * interp_file = NULL;
112 char iname[BINPRM_BUF_SIZE];
David Howellsd7627462010-08-17 23:52:56 +0100113 const char *iname_addr = iname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int retval;
115 int fd_binary = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 retval = -ENOEXEC;
118 if (!enabled)
119 goto _ret;
120
121 /* to keep locking time low, we copy the interpreter string */
122 read_lock(&entries_lock);
123 fmt = check_file(bprm);
124 if (fmt)
125 strlcpy(iname, fmt->interpreter, BINPRM_BUF_SIZE);
126 read_unlock(&entries_lock);
127 if (!fmt)
128 goto _ret;
129
130 if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700131 retval = remove_arg_zero(bprm);
132 if (retval)
133 goto _ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
136 if (fmt->flags & MISC_FMT_OPEN_BINARY) {
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* if the binary should be opened on behalf of the
139 * interpreter than keep it open and assign descriptor
140 * to it */
141 fd_binary = get_unused_fd();
142 if (fd_binary < 0) {
143 retval = fd_binary;
Al Virofd8328b2008-04-22 05:11:59 -0400144 goto _ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 fd_install(fd_binary, bprm->file);
147
148 /* if the binary is not readable than enforce mm->dumpable=0
149 regardless of the interpreter's permissions */
Al Viro1b5d7832011-06-19 12:49:47 -0400150 would_dump(bprm, bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 allow_write_access(bprm->file);
153 bprm->file = NULL;
154
155 /* mark the bprm that fd should be passed to interp */
156 bprm->interp_flags |= BINPRM_FLAGS_EXECFD;
157 bprm->interp_data = fd_binary;
158
159 } else {
160 allow_write_access(bprm->file);
161 fput(bprm->file);
162 bprm->file = NULL;
163 }
164 /* make argv[1] be the path to the binary */
165 retval = copy_strings_kernel (1, &bprm->interp, bprm);
166 if (retval < 0)
167 goto _error;
168 bprm->argc++;
169
170 /* add the interp as argv[0] */
171 retval = copy_strings_kernel (1, &iname_addr, bprm);
172 if (retval < 0)
173 goto _error;
174 bprm->argc ++;
175
Kees Cookb66c5982012-12-20 15:05:16 -0800176 /* Update interp in case binfmt_script needs it. */
177 retval = bprm_change_interp(iname, bprm);
178 if (retval < 0)
179 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 interp_file = open_exec (iname);
182 retval = PTR_ERR (interp_file);
183 if (IS_ERR (interp_file))
184 goto _error;
185
186 bprm->file = interp_file;
187 if (fmt->flags & MISC_FMT_CREDENTIALS) {
188 /*
189 * No need to call prepare_binprm(), it's already been
190 * done. bprm->buf is stale, update from interp_file.
191 */
192 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
193 retval = kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
194 } else
195 retval = prepare_binprm (bprm);
196
197 if (retval < 0)
198 goto _error;
199
Al Viro3c456bf2012-10-20 21:53:31 -0400200 retval = search_binary_handler(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (retval < 0)
202 goto _error;
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204_ret:
205 return retval;
206_error:
207 if (fd_binary > 0)
208 sys_close(fd_binary);
209 bprm->interp_flags = 0;
210 bprm->interp_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto _ret;
212}
213
214/* Command parsers */
215
216/*
217 * parses and copies one argument enclosed in del from *sp to *dp,
218 * recognising the \x special.
219 * returns pointer to the copied argument or NULL in case of an
220 * error (and sets err) or null argument length.
221 */
222static char *scanarg(char *s, char del)
223{
224 char c;
225
226 while ((c = *s++) != del) {
227 if (c == '\\' && *s == 'x') {
228 s++;
229 if (!isxdigit(*s++))
230 return NULL;
231 if (!isxdigit(*s++))
232 return NULL;
233 }
234 }
235 return s;
236}
237
Arjan van de Ven858119e2006-01-14 13:20:43 -0800238static char * check_special_flags (char * sfs, Node * e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 char * p = sfs;
241 int cont = 1;
242
243 /* special flags */
244 while (cont) {
245 switch (*p) {
246 case 'P':
247 p++;
248 e->flags |= MISC_FMT_PRESERVE_ARGV0;
249 break;
250 case 'O':
251 p++;
252 e->flags |= MISC_FMT_OPEN_BINARY;
253 break;
254 case 'C':
255 p++;
256 /* this flags also implies the
257 open-binary flag */
258 e->flags |= (MISC_FMT_CREDENTIALS |
259 MISC_FMT_OPEN_BINARY);
260 break;
261 default:
262 cont = 0;
263 }
264 }
265
266 return p;
267}
268/*
269 * This registers a new binary format, it recognises the syntax
270 * ':name:type:offset:magic:mask:interpreter:flags'
271 * where the ':' is the IFS, that can be chosen with the first char
272 */
273static Node *create_entry(const char __user *buffer, size_t count)
274{
275 Node *e;
276 int memsize, err;
277 char *buf, *p;
278 char del;
279
280 /* some sanity checks */
281 err = -EINVAL;
282 if ((count < 11) || (count > 256))
283 goto out;
284
285 err = -ENOMEM;
286 memsize = sizeof(Node) + count + 8;
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800287 e = kmalloc(memsize, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!e)
289 goto out;
290
291 p = buf = (char *)e + sizeof(Node);
292
293 memset(e, 0, sizeof(Node));
294 if (copy_from_user(buf, buffer, count))
295 goto Efault;
296
297 del = *p++; /* delimeter */
298
299 memset(buf+count, del, 8);
300
301 e->name = p;
302 p = strchr(p, del);
303 if (!p)
304 goto Einval;
305 *p++ = '\0';
306 if (!e->name[0] ||
307 !strcmp(e->name, ".") ||
308 !strcmp(e->name, "..") ||
309 strchr(e->name, '/'))
310 goto Einval;
311 switch (*p++) {
312 case 'E': e->flags = 1<<Enabled; break;
313 case 'M': e->flags = (1<<Enabled) | (1<<Magic); break;
314 default: goto Einval;
315 }
316 if (*p++ != del)
317 goto Einval;
318 if (test_bit(Magic, &e->flags)) {
319 char *s = strchr(p, del);
320 if (!s)
321 goto Einval;
322 *s++ = '\0';
323 e->offset = simple_strtoul(p, &p, 10);
324 if (*p++)
325 goto Einval;
326 e->magic = p;
327 p = scanarg(p, del);
328 if (!p)
329 goto Einval;
330 p[-1] = '\0';
331 if (!e->magic[0])
332 goto Einval;
333 e->mask = p;
334 p = scanarg(p, del);
335 if (!p)
336 goto Einval;
337 p[-1] = '\0';
338 if (!e->mask[0])
339 e->mask = NULL;
Andy Shevchenko8d82e182013-04-30 15:27:33 -0700340 e->size = string_unescape_inplace(e->magic, UNESCAPE_HEX);
341 if (e->mask &&
342 string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto Einval;
344 if (e->size + e->offset > BINPRM_BUF_SIZE)
345 goto Einval;
346 } else {
347 p = strchr(p, del);
348 if (!p)
349 goto Einval;
350 *p++ = '\0';
351 e->magic = p;
352 p = strchr(p, del);
353 if (!p)
354 goto Einval;
355 *p++ = '\0';
356 if (!e->magic[0] || strchr(e->magic, '/'))
357 goto Einval;
358 p = strchr(p, del);
359 if (!p)
360 goto Einval;
361 *p++ = '\0';
362 }
363 e->interpreter = p;
364 p = strchr(p, del);
365 if (!p)
366 goto Einval;
367 *p++ = '\0';
368 if (!e->interpreter[0])
369 goto Einval;
370
371
372 p = check_special_flags (p, e);
373
374 if (*p == '\n')
375 p++;
376 if (p != buf + count)
377 goto Einval;
378 return e;
379
380out:
381 return ERR_PTR(err);
382
383Efault:
384 kfree(e);
385 return ERR_PTR(-EFAULT);
386Einval:
387 kfree(e);
388 return ERR_PTR(-EINVAL);
389}
390
391/*
392 * Set status of entry/binfmt_misc:
393 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
394 */
395static int parse_command(const char __user *buffer, size_t count)
396{
397 char s[4];
398
399 if (!count)
400 return 0;
401 if (count > 3)
402 return -EINVAL;
403 if (copy_from_user(s, buffer, count))
404 return -EFAULT;
405 if (s[count-1] == '\n')
406 count--;
407 if (count == 1 && s[0] == '0')
408 return 1;
409 if (count == 1 && s[0] == '1')
410 return 2;
411 if (count == 2 && s[0] == '-' && s[1] == '1')
412 return 3;
413 return -EINVAL;
414}
415
416/* generic stuff */
417
418static void entry_status(Node *e, char *page)
419{
420 char *dp;
421 char *status = "disabled";
422 const char * flags = "flags: ";
423
424 if (test_bit(Enabled, &e->flags))
425 status = "enabled";
426
427 if (!VERBOSE_STATUS) {
428 sprintf(page, "%s\n", status);
429 return;
430 }
431
432 sprintf(page, "%s\ninterpreter %s\n", status, e->interpreter);
433 dp = page + strlen(page);
434
435 /* print the special flags */
436 sprintf (dp, "%s", flags);
437 dp += strlen (flags);
438 if (e->flags & MISC_FMT_PRESERVE_ARGV0) {
439 *dp ++ = 'P';
440 }
441 if (e->flags & MISC_FMT_OPEN_BINARY) {
442 *dp ++ = 'O';
443 }
444 if (e->flags & MISC_FMT_CREDENTIALS) {
445 *dp ++ = 'C';
446 }
447 *dp ++ = '\n';
448
449
450 if (!test_bit(Magic, &e->flags)) {
451 sprintf(dp, "extension .%s\n", e->magic);
452 } else {
453 int i;
454
455 sprintf(dp, "offset %i\nmagic ", e->offset);
456 dp = page + strlen(page);
457 for (i = 0; i < e->size; i++) {
458 sprintf(dp, "%02x", 0xff & (int) (e->magic[i]));
459 dp += 2;
460 }
461 if (e->mask) {
462 sprintf(dp, "\nmask ");
463 dp += 6;
464 for (i = 0; i < e->size; i++) {
465 sprintf(dp, "%02x", 0xff & (int) (e->mask[i]));
466 dp += 2;
467 }
468 }
469 *dp++ = '\n';
470 *dp = '\0';
471 }
472}
473
474static struct inode *bm_get_inode(struct super_block *sb, int mode)
475{
476 struct inode * inode = new_inode(sb);
477
478 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400479 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 inode->i_atime = inode->i_mtime = inode->i_ctime =
482 current_fs_time(inode->i_sb);
483 }
484 return inode;
485}
486
Al Virob57922d2010-06-07 14:34:48 -0400487static void bm_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jan Karadbd57682012-05-03 14:48:02 +0200489 clear_inode(inode);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700490 kfree(inode->i_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static void kill_node(Node *e)
494{
495 struct dentry *dentry;
496
497 write_lock(&entries_lock);
498 dentry = e->dentry;
499 if (dentry) {
500 list_del_init(&e->list);
501 e->dentry = NULL;
502 }
503 write_unlock(&entries_lock);
504
505 if (dentry) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200506 drop_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 d_drop(dentry);
508 dput(dentry);
509 simple_release_fs(&bm_mnt, &entry_count);
510 }
511}
512
513/* /<entry> */
514
515static ssize_t
516bm_entry_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos)
517{
Al Viro496ad9a2013-01-23 17:07:38 -0500518 Node *e = file_inode(file)->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ssize_t res;
520 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
523 return -ENOMEM;
524
525 entry_status(e, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Akinobu Mita6e2c10a2008-07-23 21:29:15 -0700527 res = simple_read_from_buffer(buf, nbytes, ppos, page, strlen(page));
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 free_page((unsigned long) page);
530 return res;
531}
532
533static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
534 size_t count, loff_t *ppos)
535{
536 struct dentry *root;
Al Viro496ad9a2013-01-23 17:07:38 -0500537 Node *e = file_inode(file)->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int res = parse_command(buffer, count);
539
540 switch (res) {
541 case 1: clear_bit(Enabled, &e->flags);
542 break;
543 case 2: set_bit(Enabled, &e->flags);
544 break;
Al Virod8c95842011-12-07 18:16:57 -0500545 case 3: root = dget(file->f_path.dentry->d_sb->s_root);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800546 mutex_lock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 kill_node(e);
549
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800550 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dput(root);
552 break;
553 default: return res;
554 }
555 return count;
556}
557
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800558static const struct file_operations bm_entry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 .read = bm_entry_read,
560 .write = bm_entry_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200561 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562};
563
564/* /register */
565
566static ssize_t bm_register_write(struct file *file, const char __user *buffer,
567 size_t count, loff_t *ppos)
568{
569 Node *e;
570 struct inode *inode;
571 struct dentry *root, *dentry;
Al Virod8c95842011-12-07 18:16:57 -0500572 struct super_block *sb = file->f_path.dentry->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int err = 0;
574
575 e = create_entry(buffer, count);
576
577 if (IS_ERR(e))
578 return PTR_ERR(e);
579
580 root = dget(sb->s_root);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800581 mutex_lock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 dentry = lookup_one_len(e->name, root, strlen(e->name));
583 err = PTR_ERR(dentry);
584 if (IS_ERR(dentry))
585 goto out;
586
587 err = -EEXIST;
588 if (dentry->d_inode)
589 goto out2;
590
591 inode = bm_get_inode(sb, S_IFREG | 0644);
592
593 err = -ENOMEM;
594 if (!inode)
595 goto out2;
596
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400597 err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (err) {
599 iput(inode);
600 inode = NULL;
601 goto out2;
602 }
603
604 e->dentry = dget(dentry);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700605 inode->i_private = e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 inode->i_fop = &bm_entry_operations;
607
608 d_instantiate(dentry, inode);
609 write_lock(&entries_lock);
610 list_add(&e->list, &entries);
611 write_unlock(&entries_lock);
612
613 err = 0;
614out2:
615 dput(dentry);
616out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800617 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 dput(root);
619
620 if (err) {
621 kfree(e);
622 return -EINVAL;
623 }
624 return count;
625}
626
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800627static const struct file_operations bm_register_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 .write = bm_register_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200629 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630};
631
632/* /status */
633
634static ssize_t
635bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
636{
Qinghuang Feng87113e802009-01-06 14:41:38 -0800637 char *s = enabled ? "enabled\n" : "disabled\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Akinobu Mita92f4c702007-05-09 02:33:32 -0700639 return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static ssize_t bm_status_write(struct file * file, const char __user * buffer,
643 size_t count, loff_t *ppos)
644{
645 int res = parse_command(buffer, count);
646 struct dentry *root;
647
648 switch (res) {
649 case 1: enabled = 0; break;
650 case 2: enabled = 1; break;
Al Virod8c95842011-12-07 18:16:57 -0500651 case 3: root = dget(file->f_path.dentry->d_sb->s_root);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800652 mutex_lock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 while (!list_empty(&entries))
655 kill_node(list_entry(entries.next, Node, list));
656
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800657 mutex_unlock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dput(root);
Luis Henriquesb003f962014-04-03 14:49:34 -0700659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 default: return res;
661 }
662 return count;
663}
664
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800665static const struct file_operations bm_status_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .read = bm_status_read,
667 .write = bm_status_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200668 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669};
670
671/* Superblock handling */
672
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800673static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .statfs = simple_statfs,
Al Virob57922d2010-06-07 14:34:48 -0400675 .evict_inode = bm_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676};
677
678static int bm_fill_super(struct super_block * sb, void * data, int silent)
679{
680 static struct tree_descr bm_files[] = {
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700681 [2] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
682 [3] = {"register", &bm_register_operations, S_IWUSR},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* last one */ {""}
684 };
Muthu Kumarb502bd12012-03-23 15:01:50 -0700685 int err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!err)
687 sb->s_op = &s_ops;
688 return err;
689}
690
Al Virofc14f2f2010-07-25 01:48:30 +0400691static struct dentry *bm_mount(struct file_system_type *fs_type,
692 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Al Virofc14f2f2010-07-25 01:48:30 +0400694 return mount_single(fs_type, flags, data, bm_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697static struct linux_binfmt misc_format = {
698 .module = THIS_MODULE,
699 .load_binary = load_misc_binary,
700};
701
702static struct file_system_type bm_fs_type = {
703 .owner = THIS_MODULE,
704 .name = "binfmt_misc",
Al Virofc14f2f2010-07-25 01:48:30 +0400705 .mount = bm_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 .kill_sb = kill_litter_super,
707};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800708MODULE_ALIAS_FS("binfmt_misc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710static int __init init_misc_binfmt(void)
711{
712 int err = register_filesystem(&bm_fs_type);
Al Viro8fc3dc52012-03-17 03:05:16 -0400713 if (!err)
714 insert_binfmt(&misc_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return err;
716}
717
718static void __exit exit_misc_binfmt(void)
719{
720 unregister_binfmt(&misc_format);
721 unregister_filesystem(&bm_fs_type);
722}
723
724core_initcall(init_misc_binfmt);
725module_exit(exit_misc_binfmt);
726MODULE_LICENSE("GPL");