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>
27 #include <linux/namei.h>
28 #include <linux/mount.h>
29 #include <linux/syscalls.h>
31 #include <asm/uaccess.h>
34 VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
37 static LIST_HEAD(entries);
38 static int enabled = 1;
40 enum {Enabled, Magic};
41 #define MISC_FMT_PRESERVE_ARGV0 (1<<31)
44 struct list_head list;
45 unsigned long flags; /* type, status, etc. */
46 int offset; /* offset of magic */
47 int size; /* size of magic/mask */
48 char *magic; /* magic or filename extension */
49 char *mask; /* mask, NULL for exact match */
50 char *interpreter; /* filename of interpreter */
52 struct dentry *dentry;
55 static rwlock_t entries_lock = RW_LOCK_UNLOCKED;
56 static struct vfsmount *bm_mnt;
57 static int entry_count;
60 * Check if we support the binfmt
61 * if we do, return the node, else NULL
62 * locking is done in load_misc_binary
64 static Node *check_file(struct linux_binprm *bprm)
66 char *p = strrchr(bprm->interp, '.');
69 list_for_each(l, &entries) {
70 Node *e = list_entry(l, Node, list);
74 if (!test_bit(Enabled, &e->flags))
77 if (!test_bit(Magic, &e->flags)) {
78 if (p && !strcmp(e->magic, p + 1))
83 s = bprm->buf + e->offset;
85 for (j = 0; j < e->size; j++)
86 if ((*s++ ^ e->magic[j]) & e->mask[j])
89 for (j = 0; j < e->size; j++)
90 if ((*s++ ^ e->magic[j]))
102 static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
106 char iname[BINPRM_BUF_SIZE];
107 char *iname_addr = iname;
114 /* to keep locking time low, we copy the interpreter string */
115 read_lock(&entries_lock);
116 fmt = check_file(bprm);
118 strlcpy(iname, fmt->interpreter, BINPRM_BUF_SIZE);
119 read_unlock(&entries_lock);
123 allow_write_access(bprm->file);
127 /* Build args for interpreter */
128 if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
129 remove_arg_zero(bprm);
131 retval = copy_strings_kernel(1, &bprm->interp, bprm);
132 if (retval < 0) goto _ret;
134 retval = copy_strings_kernel(1, &iname_addr, bprm);
135 if (retval < 0) goto _ret;
137 bprm->interp = iname; /* for binfmt_script */
139 file = open_exec(iname);
140 retval = PTR_ERR(file);
145 retval = prepare_binprm(bprm);
147 retval = search_binary_handler(bprm, regs);
152 /* Command parsers */
155 * parses and copies one argument enclosed in del from *sp to *dp,
156 * recognising the \x special.
157 * returns pointer to the copied argument or NULL in case of an
158 * error (and sets err) or null argument length.
160 static char *scanarg(char *s, char del)
164 while ((c = *s++) != del) {
165 if (c == '\\' && *s == 'x') {
176 static int unquote(char *from)
178 char c = 0, *s = from, *p = from;
180 while ((c = *s++) != '\0') {
181 if (c == '\\' && *s == 'x') {
184 *p = (c - (isdigit(c) ? '0' : 'A' - 10)) << 4;
186 *p++ |= c - (isdigit(c) ? '0' : 'A' - 10);
195 * This registers a new binary format, it recognises the syntax
196 * ':name:type:offset:magic:mask:interpreter:'
197 * where the ':' is the IFS, that can be chosen with the first char
199 static Node *create_entry(const char *buffer, size_t count)
206 /* some sanity checks */
208 if ((count < 11) || (count > 256))
212 memsize = sizeof(Node) + count + 8;
213 e = (Node *) kmalloc(memsize, GFP_USER);
217 p = buf = (char *)e + sizeof(Node);
219 memset(e, 0, sizeof(Node));
220 if (copy_from_user(buf, buffer, count))
223 del = *p++; /* delimeter */
225 memset(buf+count, del, 8);
233 !strcmp(e->name, ".") ||
234 !strcmp(e->name, "..") ||
235 strchr(e->name, '/'))
238 case 'E': e->flags = 1<<Enabled; break;
239 case 'M': e->flags = (1<<Enabled) | (1<<Magic); break;
240 default: goto Einval;
244 if (test_bit(Magic, &e->flags)) {
245 char *s = strchr(p, del);
249 e->offset = simple_strtoul(p, &p, 10);
266 e->size = unquote(e->magic);
267 if (e->mask && unquote(e->mask) != e->size)
269 if (e->size + e->offset > BINPRM_BUF_SIZE)
281 if (!e->magic[0] || strchr(e->magic, '/'))
293 if (!e->interpreter[0])
298 e->flags |= MISC_FMT_PRESERVE_ARGV0;
303 if (p != buf + count)
312 return ERR_PTR(-EFAULT);
315 return ERR_PTR(-EINVAL);
319 * Set status of entry/binfmt_misc:
320 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
322 static int parse_command(const char *buffer, size_t count)
330 if (copy_from_user(s, buffer, count))
332 if (s[count-1] == '\n')
334 if (count == 1 && s[0] == '0')
336 if (count == 1 && s[0] == '1')
338 if (count == 2 && s[0] == '-' && s[1] == '1')
345 static void entry_status(Node *e, char *page)
348 char *status = "disabled";
350 if (test_bit(Enabled, &e->flags))
353 if (!VERBOSE_STATUS) {
354 sprintf(page, "%s\n", status);
358 sprintf(page, "%s\ninterpreter %s\n", status, e->interpreter);
359 dp = page + strlen(page);
360 if (!test_bit(Magic, &e->flags)) {
361 sprintf(dp, "extension .%s\n", e->magic);
365 sprintf(dp, "offset %i\nmagic ", e->offset);
366 dp = page + strlen(page);
367 for (i = 0; i < e->size; i++) {
368 sprintf(dp, "%02x", 0xff & (int) (e->magic[i]));
372 sprintf(dp, "\nmask ");
374 for (i = 0; i < e->size; i++) {
375 sprintf(dp, "%02x", 0xff & (int) (e->mask[i]));
384 static struct inode *bm_get_inode(struct super_block *sb, int mode)
386 struct inode * inode = new_inode(sb);
389 inode->i_mode = mode;
392 inode->i_blksize = PAGE_CACHE_SIZE;
394 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
399 static void bm_clear_inode(struct inode *inode)
401 kfree(inode->u.generic_ip);
404 static void kill_node(Node *e)
406 struct dentry *dentry;
408 write_lock(&entries_lock);
411 list_del_init(&e->list);
414 write_unlock(&entries_lock);
417 dentry->d_inode->i_nlink--;
420 simple_release_fs(&bm_mnt, &entry_count);
427 bm_entry_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
429 Node *e = file->f_dentry->d_inode->u.generic_ip;
435 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
438 entry_status(e, page);
447 if (len < pos + nbytes)
450 if (copy_to_user(buf, page + pos, nbytes))
452 *ppos = pos + nbytes;
455 free_page((unsigned long) page);
459 static ssize_t bm_entry_write(struct file *file, const char *buffer,
460 size_t count, loff_t *ppos)
463 Node *e = file->f_dentry->d_inode->u.generic_ip;
464 int res = parse_command(buffer, count);
467 case 1: clear_bit(Enabled, &e->flags);
469 case 2: set_bit(Enabled, &e->flags);
471 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
472 down(&root->d_inode->i_sem);
476 up(&root->d_inode->i_sem);
484 static struct file_operations bm_entry_operations = {
485 .read = bm_entry_read,
486 .write = bm_entry_write,
491 static ssize_t bm_register_write(struct file *file, const char *buffer,
492 size_t count, loff_t *ppos)
496 struct dentry *root, *dentry;
497 struct super_block *sb = file->f_vfsmnt->mnt_sb;
500 e = create_entry(buffer, count);
505 root = dget(sb->s_root);
506 down(&root->d_inode->i_sem);
507 dentry = lookup_one_len(e->name, root, strlen(e->name));
508 err = PTR_ERR(dentry);
516 inode = bm_get_inode(sb, S_IFREG | 0644);
522 err = simple_pin_fs("binfmt_misc", &bm_mnt, &entry_count);
529 e->dentry = dget(dentry);
530 inode->u.generic_ip = e;
531 inode->i_fop = &bm_entry_operations;
533 d_instantiate(dentry, inode);
534 write_lock(&entries_lock);
535 list_add(&e->list, &entries);
536 write_unlock(&entries_lock);
542 up(&root->d_inode->i_sem);
552 static struct file_operations bm_register_operations = {
553 .write = bm_register_write,
559 bm_status_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
561 char *s = enabled ? "enabled" : "disabled";
569 if (len < pos + nbytes)
571 if (copy_to_user(buf, s + pos, nbytes))
573 *ppos = pos + nbytes;
577 static ssize_t bm_status_write(struct file * file, const char * buffer,
578 size_t count, loff_t *ppos)
580 int res = parse_command(buffer, count);
584 case 1: enabled = 0; break;
585 case 2: enabled = 1; break;
586 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
587 down(&root->d_inode->i_sem);
589 while (!list_empty(&entries))
590 kill_node(list_entry(entries.next, Node, list));
592 up(&root->d_inode->i_sem);
599 static struct file_operations bm_status_operations = {
600 .read = bm_status_read,
601 .write = bm_status_write,
604 /* Superblock handling */
606 static struct super_operations s_ops = {
607 .statfs = simple_statfs,
608 .clear_inode = bm_clear_inode,
611 static int bm_fill_super(struct super_block * sb, void * data, int silent)
613 static struct tree_descr bm_files[] = {
614 [1] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
615 [2] = {"register", &bm_register_operations, S_IWUSR},
618 int err = simple_fill_super(sb, 0x42494e4d, bm_files);
624 static struct super_block *bm_get_sb(struct file_system_type *fs_type,
625 int flags, const char *dev_name, void *data)
627 return get_sb_single(fs_type, flags, data, bm_fill_super);
630 static struct linux_binfmt misc_format = {
631 .module = THIS_MODULE,
632 .load_binary = load_misc_binary,
635 static struct file_system_type bm_fs_type = {
636 .owner = THIS_MODULE,
637 .name = "binfmt_misc",
639 .kill_sb = kill_litter_super,
642 static int __init init_misc_binfmt(void)
644 int err = register_filesystem(&bm_fs_type);
646 err = register_binfmt(&misc_format);
648 unregister_filesystem(&bm_fs_type);
653 static void __exit exit_misc_binfmt(void)
655 unregister_binfmt(&misc_format);
656 unregister_filesystem(&bm_fs_type);
659 core_initcall(init_misc_binfmt);
660 module_exit(exit_misc_binfmt);
661 MODULE_LICENSE("GPL");