4 * Copyright (C) 1997 Richard Günther
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
10 * 1997-04-25 first version
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.
19 #include <linux/module.h>
20 #include <linux/init.h>
22 #include <linux/binfmts.h>
23 #include <linux/slab.h>
24 #include <linux/ctype.h>
25 #include <linux/file.h>
26 #include <linux/pagemap.h>
28 #include <asm/uaccess.h>
31 VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
34 static LIST_HEAD(entries);
35 static int enabled = 1;
37 enum {Enabled, Magic};
40 struct list_head list;
41 unsigned long flags; /* type, status, etc. */
42 int offset; /* offset of magic */
43 int size; /* size of magic/mask */
44 char *magic; /* magic or filename extension */
45 char *mask; /* mask, NULL for exact match */
46 char *interpreter; /* filename of interpreter */
48 struct dentry *dentry;
51 static rwlock_t entries_lock __attribute__((unused)) = RW_LOCK_UNLOCKED;
54 * Check if we support the binfmt
55 * if we do, return the node, else NULL
56 * locking is done in load_misc_binary
58 static Node *check_file(struct linux_binprm *bprm)
60 char *p = strrchr(bprm->filename, '.');
63 for (l = entries.next; l != &entries; l = l->next) {
64 Node *e = list_entry(l, Node, list);
68 if (!test_bit(Enabled, &e->flags))
71 if (!test_bit(Magic, &e->flags)) {
72 if (p && !strcmp(e->magic, p + 1))
77 s = bprm->buf + e->offset;
79 for (j = 0; j < e->size; j++)
80 if ((*s++ ^ e->magic[j]) & e->mask[j])
83 for (j = 0; j < e->size; j++)
84 if ((*s++ ^ e->magic[j]))
96 static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
100 char iname[BINPRM_BUF_SIZE];
101 char *iname_addr = iname;
108 /* to keep locking time low, we copy the interpreter string */
109 read_lock(&entries_lock);
110 fmt = check_file(bprm);
112 strncpy(iname, fmt->interpreter, BINPRM_BUF_SIZE - 1);
113 iname[BINPRM_BUF_SIZE - 1] = '\0';
115 read_unlock(&entries_lock);
119 allow_write_access(bprm->file);
123 /* Build args for interpreter */
124 remove_arg_zero(bprm);
125 retval = copy_strings_kernel(1, &bprm->filename, bprm);
126 if (retval < 0) goto _ret;
128 retval = copy_strings_kernel(1, &iname_addr, bprm);
129 if (retval < 0) goto _ret;
131 bprm->filename = iname; /* for binfmt_script */
133 file = open_exec(iname);
134 retval = PTR_ERR(file);
139 retval = prepare_binprm(bprm);
141 retval = search_binary_handler(bprm, regs);
146 /* Command parsers */
149 * parses and copies one argument enclosed in del from *sp to *dp,
150 * recognising the \x special.
151 * returns pointer to the copied argument or NULL in case of an
152 * error (and sets err) or null argument length.
154 static char *scanarg(char *s, char del)
158 while ((c = *s++) != del) {
159 if (c == '\\' && *s == 'x') {
170 static int unquote(char *from)
172 char c = 0, *s = from, *p = from;
174 while ((c = *s++) != '\0') {
175 if (c == '\\' && *s == 'x') {
178 *p = (c - (isdigit(c) ? '0' : 'A' - 10)) << 4;
180 *p++ |= c - (isdigit(c) ? '0' : 'A' - 10);
189 * This registers a new binary format, it recognises the syntax
190 * ':name:type:offset:magic:mask:interpreter:'
191 * where the ':' is the IFS, that can be chosen with the first char
193 static Node *create_entry(const char *buffer, size_t count)
200 /* some sanity checks */
202 if ((count < 11) || (count > 256))
206 memsize = sizeof(Node) + count + 8;
207 e = (Node *) kmalloc(memsize, GFP_USER);
211 p = buf = (char *)e + sizeof(Node);
213 memset(e, 0, sizeof(Node));
214 if (copy_from_user(buf, buffer, count))
217 del = *p++; /* delimeter */
219 memset(buf+count, del, 8);
227 !strcmp(e->name, ".") ||
228 !strcmp(e->name, "..") ||
229 strchr(e->name, '/'))
232 case 'E': e->flags = 1<<Enabled; break;
233 case 'M': e->flags = (1<<Enabled) | (1<<Magic); break;
234 default: goto Einval;
238 if (test_bit(Magic, &e->flags)) {
239 char *s = strchr(p, del);
243 e->offset = simple_strtoul(p, &p, 10);
260 e->size = unquote(e->magic);
261 if (e->mask && unquote(e->mask) != e->size)
263 if (e->size + e->offset > BINPRM_BUF_SIZE)
275 if (!e->magic[0] || strchr(e->magic, '/'))
287 if (!e->interpreter[0])
292 if (p != buf + count)
301 return ERR_PTR(-EFAULT);
304 return ERR_PTR(-EINVAL);
308 * Set status of entry/binfmt_misc:
309 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
311 static int parse_command(const char *buffer, size_t count)
319 if (copy_from_user(s, buffer, count))
321 if (s[count-1] == '\n')
323 if (count == 1 && s[0] == '0')
325 if (count == 1 && s[0] == '1')
327 if (count == 2 && s[0] == '-' && s[1] == '1')
334 static void entry_status(Node *e, char *page)
337 char *status = "disabled";
339 if (test_bit(Enabled, &e->flags))
342 if (!VERBOSE_STATUS) {
343 sprintf(page, "%s\n", status);
347 sprintf(page, "%s\ninterpreter %s\n", status, e->interpreter);
348 dp = page + strlen(page);
349 if (!test_bit(Magic, &e->flags)) {
350 sprintf(dp, "extension .%s\n", e->magic);
354 sprintf(dp, "offset %i\nmagic ", e->offset);
355 dp = page + strlen(page);
356 for (i = 0; i < e->size; i++) {
357 sprintf(dp, "%02x", 0xff & (int) (e->magic[i]));
361 sprintf(dp, "\nmask ");
363 for (i = 0; i < e->size; i++) {
364 sprintf(dp, "%02x", 0xff & (int) (e->mask[i]));
373 static struct inode *bm_get_inode(struct super_block *sb, int mode)
375 struct inode * inode = new_inode(sb);
378 inode->i_mode = mode;
381 inode->i_blksize = PAGE_CACHE_SIZE;
383 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
388 static void bm_clear_inode(struct inode *inode)
390 Node *e = inode->u.generic_ip;
393 write_lock(&entries_lock);
395 write_unlock(&entries_lock);
400 static void kill_node(Node *e)
402 struct dentry *dentry;
404 write_lock(&entries_lock);
408 INIT_LIST_HEAD(&e->list);
411 write_unlock(&entries_lock);
414 dentry->d_inode->i_nlink--;
423 bm_entry_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
425 Node *e = file->f_dentry->d_inode->u.generic_ip;
431 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
434 entry_status(e, page);
443 if (len < pos + nbytes)
446 if (copy_to_user(buf, page + pos, nbytes))
448 *ppos = pos + nbytes;
451 free_page((unsigned long) page);
455 static ssize_t bm_entry_write(struct file *file, const char *buffer,
456 size_t count, loff_t *ppos)
459 Node *e = file->f_dentry->d_inode->u.generic_ip;
460 int res = parse_command(buffer, count);
463 case 1: clear_bit(Enabled, &e->flags);
465 case 2: set_bit(Enabled, &e->flags);
467 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
468 down(&root->d_inode->i_sem);
469 down(&root->d_inode->i_zombie);
473 up(&root->d_inode->i_zombie);
474 up(&root->d_inode->i_sem);
482 static struct file_operations bm_entry_operations = {
484 write: bm_entry_write,
489 static ssize_t bm_register_write(struct file *file, const char *buffer,
490 size_t count, loff_t *ppos)
493 struct dentry *root, *dentry;
494 struct super_block *sb = file->f_vfsmnt->mnt_sb;
497 e = create_entry(buffer, count);
502 root = dget(sb->s_root);
503 down(&root->d_inode->i_sem);
504 dentry = lookup_one_len(e->name, root, strlen(e->name));
505 err = PTR_ERR(dentry);
506 if (!IS_ERR(dentry)) {
507 down(&root->d_inode->i_zombie);
508 if (dentry->d_inode) {
511 struct inode * inode = bm_get_inode(sb, S_IFREG | 0644);
515 write_lock(&entries_lock);
517 e->dentry = dget(dentry);
518 inode->u.generic_ip = e;
519 inode->i_fop = &bm_entry_operations;
520 d_instantiate(dentry, inode);
522 list_add(&e->list, &entries);
523 write_unlock(&entries_lock);
528 up(&root->d_inode->i_zombie);
531 up(&root->d_inode->i_sem);
541 static struct file_operations bm_register_operations = {
542 write: bm_register_write,
548 bm_status_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
550 char *s = enabled ? "enabled" : "disabled";
558 if (len < pos + nbytes)
560 if (copy_to_user(buf, s + pos, nbytes))
562 *ppos = pos + nbytes;
566 static ssize_t bm_status_write(struct file * file, const char * buffer,
567 size_t count, loff_t *ppos)
569 int res = parse_command(buffer, count);
573 case 1: enabled = 0; break;
574 case 2: enabled = 1; break;
575 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
576 down(&root->d_inode->i_sem);
577 down(&root->d_inode->i_zombie);
579 while (!list_empty(&entries))
580 kill_node(list_entry(entries.next, Node, list));
582 up(&root->d_inode->i_zombie);
583 up(&root->d_inode->i_sem);
590 static struct file_operations bm_status_operations = {
591 read: bm_status_read,
592 write: bm_status_write,
597 static struct dentry * bm_lookup(struct inode *dir, struct dentry *dentry)
603 static struct inode_operations bm_dir_inode_operations = {
607 /* Superblock handling */
609 static int bm_statfs(struct super_block *sb, struct statfs *buf)
611 buf->f_type = sb->s_magic;
612 buf->f_bsize = PAGE_CACHE_SIZE;
613 buf->f_namelen = 255;
617 static struct super_operations s_ops = {
619 put_inode: force_delete,
620 clear_inode: bm_clear_inode,
623 static struct super_block *bm_read_super(struct super_block * sb, void * data, int silent)
625 struct qstr names[2] = {{name:"status"}, {name:"register"}};
626 struct inode * inode;
627 struct dentry * dentry[3];
630 for (i=0; i<sizeof(names)/sizeof(names[0]); i++) {
631 names[i].len = strlen(names[i].name);
632 names[i].hash = full_name_hash(names[i].name, names[i].len);
635 sb->s_blocksize = PAGE_CACHE_SIZE;
636 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
637 sb->s_magic = 0x42494e4d;
640 inode = bm_get_inode(sb, S_IFDIR | 0755);
643 inode->i_op = &bm_dir_inode_operations;
644 inode->i_fop = &dcache_dir_ops;
645 dentry[0] = d_alloc_root(inode);
650 dentry[1] = d_alloc(dentry[0], &names[0]);
653 dentry[2] = d_alloc(dentry[0], &names[1]);
656 inode = bm_get_inode(sb, S_IFREG | 0644);
659 inode->i_fop = &bm_status_operations;
660 d_add(dentry[1], inode);
661 inode = bm_get_inode(sb, S_IFREG | 0400);
664 inode->i_fop = &bm_register_operations;
665 d_add(dentry[2], inode);
667 sb->s_root = dentry[0];
679 static struct linux_binfmt misc_format = {
680 NULL, THIS_MODULE, load_misc_binary, NULL, NULL, 0
683 static DECLARE_FSTYPE(bm_fs_type, "binfmt_misc", bm_read_super, FS_SINGLE|FS_LITTER);
685 static struct vfsmount *bm_mnt;
687 static int __init init_misc_binfmt(void)
689 int err = register_filesystem(&bm_fs_type);
691 bm_mnt = kern_mount(&bm_fs_type);
692 err = PTR_ERR(bm_mnt);
694 unregister_filesystem(&bm_fs_type);
696 err = register_binfmt(&misc_format);
698 unregister_filesystem(&bm_fs_type);
706 static void __exit exit_misc_binfmt(void)
708 unregister_binfmt(&misc_format);
709 unregister_filesystem(&bm_fs_type);
715 module_init(init_misc_binfmt);
716 module_exit(exit_misc_binfmt);
717 MODULE_LICENSE("GPL");