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>
30 #include <asm/uaccess.h>
33 VERBOSE_STATUS
= 1 /* make it zero to save 400 bytes kernel memory */
36 static LIST_HEAD(entries
);
37 static int enabled
= 1;
39 enum {Enabled
, Magic
};
40 #define MISC_FMT_PRESERVE_ARGV0 (1<<31)
43 struct list_head list
;
44 unsigned long flags
; /* type, status, etc. */
45 int offset
; /* offset of magic */
46 int size
; /* size of magic/mask */
47 char *magic
; /* magic or filename extension */
48 char *mask
; /* mask, NULL for exact match */
49 char *interpreter
; /* filename of interpreter */
51 struct dentry
*dentry
;
54 static rwlock_t entries_lock
__attribute__((unused
)) = RW_LOCK_UNLOCKED
;
55 static struct vfsmount
*bm_mnt
;
56 static int entry_count
= 0;
59 * Check if we support the binfmt
60 * if we do, return the node, else NULL
61 * locking is done in load_misc_binary
63 static Node
*check_file(struct linux_binprm
*bprm
)
65 char *p
= strrchr(bprm
->filename
, '.');
68 list_for_each(l
, &entries
) {
69 Node
*e
= list_entry(l
, Node
, list
);
73 if (!test_bit(Enabled
, &e
->flags
))
76 if (!test_bit(Magic
, &e
->flags
)) {
77 if (p
&& !strcmp(e
->magic
, p
+ 1))
82 s
= bprm
->buf
+ e
->offset
;
84 for (j
= 0; j
< e
->size
; j
++)
85 if ((*s
++ ^ e
->magic
[j
]) & e
->mask
[j
])
88 for (j
= 0; j
< e
->size
; j
++)
89 if ((*s
++ ^ e
->magic
[j
]))
101 static int load_misc_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
)
105 char iname
[BINPRM_BUF_SIZE
];
106 char *iname_addr
= iname
;
113 /* to keep locking time low, we copy the interpreter string */
114 read_lock(&entries_lock
);
115 fmt
= check_file(bprm
);
117 strncpy(iname
, fmt
->interpreter
, BINPRM_BUF_SIZE
- 1);
118 iname
[BINPRM_BUF_SIZE
- 1] = '\0';
120 read_unlock(&entries_lock
);
124 allow_write_access(bprm
->file
);
128 /* Build args for interpreter */
129 if (!(fmt
->flags
& MISC_FMT_PRESERVE_ARGV0
)) {
130 remove_arg_zero(bprm
);
132 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
133 if (retval
< 0) goto _ret
;
135 retval
= copy_strings_kernel(1, &iname_addr
, bprm
);
136 if (retval
< 0) goto _ret
;
138 bprm
->filename
= iname
; /* for binfmt_script */
140 file
= open_exec(iname
);
141 retval
= PTR_ERR(file
);
146 retval
= prepare_binprm(bprm
);
148 retval
= search_binary_handler(bprm
, regs
);
153 /* Command parsers */
156 * parses and copies one argument enclosed in del from *sp to *dp,
157 * recognising the \x special.
158 * returns pointer to the copied argument or NULL in case of an
159 * error (and sets err) or null argument length.
161 static char *scanarg(char *s
, char del
)
165 while ((c
= *s
++) != del
) {
166 if (c
== '\\' && *s
== 'x') {
177 static int unquote(char *from
)
179 char c
= 0, *s
= from
, *p
= from
;
181 while ((c
= *s
++) != '\0') {
182 if (c
== '\\' && *s
== 'x') {
185 *p
= (c
- (isdigit(c
) ? '0' : 'A' - 10)) << 4;
187 *p
++ |= c
- (isdigit(c
) ? '0' : 'A' - 10);
196 * This registers a new binary format, it recognises the syntax
197 * ':name:type:offset:magic:mask:interpreter:'
198 * where the ':' is the IFS, that can be chosen with the first char
200 static Node
*create_entry(const char *buffer
, size_t count
)
207 /* some sanity checks */
209 if ((count
< 11) || (count
> 256))
213 memsize
= sizeof(Node
) + count
+ 8;
214 e
= (Node
*) kmalloc(memsize
, GFP_USER
);
218 p
= buf
= (char *)e
+ sizeof(Node
);
220 memset(e
, 0, sizeof(Node
));
221 if (copy_from_user(buf
, buffer
, count
))
224 del
= *p
++; /* delimeter */
226 memset(buf
+count
, del
, 8);
234 !strcmp(e
->name
, ".") ||
235 !strcmp(e
->name
, "..") ||
236 strchr(e
->name
, '/'))
239 case 'E': e
->flags
= 1<<Enabled
; break;
240 case 'M': e
->flags
= (1<<Enabled
) | (1<<Magic
); break;
241 default: goto Einval
;
245 if (test_bit(Magic
, &e
->flags
)) {
246 char *s
= strchr(p
, del
);
250 e
->offset
= simple_strtoul(p
, &p
, 10);
267 e
->size
= unquote(e
->magic
);
268 if (e
->mask
&& unquote(e
->mask
) != e
->size
)
270 if (e
->size
+ e
->offset
> BINPRM_BUF_SIZE
)
282 if (!e
->magic
[0] || strchr(e
->magic
, '/'))
294 if (!e
->interpreter
[0])
299 e
->flags
|= MISC_FMT_PRESERVE_ARGV0
;
304 if (p
!= buf
+ count
)
313 return ERR_PTR(-EFAULT
);
316 return ERR_PTR(-EINVAL
);
320 * Set status of entry/binfmt_misc:
321 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
323 static int parse_command(const char *buffer
, size_t count
)
331 if (copy_from_user(s
, buffer
, count
))
333 if (s
[count
-1] == '\n')
335 if (count
== 1 && s
[0] == '0')
337 if (count
== 1 && s
[0] == '1')
339 if (count
== 2 && s
[0] == '-' && s
[1] == '1')
346 static void entry_status(Node
*e
, char *page
)
349 char *status
= "disabled";
351 if (test_bit(Enabled
, &e
->flags
))
354 if (!VERBOSE_STATUS
) {
355 sprintf(page
, "%s\n", status
);
359 sprintf(page
, "%s\ninterpreter %s\n", status
, e
->interpreter
);
360 dp
= page
+ strlen(page
);
361 if (!test_bit(Magic
, &e
->flags
)) {
362 sprintf(dp
, "extension .%s\n", e
->magic
);
366 sprintf(dp
, "offset %i\nmagic ", e
->offset
);
367 dp
= page
+ strlen(page
);
368 for (i
= 0; i
< e
->size
; i
++) {
369 sprintf(dp
, "%02x", 0xff & (int) (e
->magic
[i
]));
373 sprintf(dp
, "\nmask ");
375 for (i
= 0; i
< e
->size
; i
++) {
376 sprintf(dp
, "%02x", 0xff & (int) (e
->mask
[i
]));
385 static struct inode
*bm_get_inode(struct super_block
*sb
, int mode
)
387 struct inode
* inode
= new_inode(sb
);
390 inode
->i_mode
= mode
;
393 inode
->i_blksize
= PAGE_CACHE_SIZE
;
395 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
400 static void bm_clear_inode(struct inode
*inode
)
402 Node
*e
= inode
->u
.generic_ip
;
405 struct vfsmount
*mnt
;
406 write_lock(&entries_lock
);
411 write_unlock(&entries_lock
);
417 static void kill_node(Node
*e
)
419 struct dentry
*dentry
;
421 write_lock(&entries_lock
);
424 list_del_init(&e
->list
);
427 write_unlock(&entries_lock
);
430 dentry
->d_inode
->i_nlink
--;
439 bm_entry_read(struct file
* file
, char * buf
, size_t nbytes
, loff_t
*ppos
)
441 Node
*e
= file
->f_dentry
->d_inode
->u
.generic_ip
;
447 if (!(page
= (char*) __get_free_page(GFP_KERNEL
)))
450 entry_status(e
, page
);
459 if (len
< pos
+ nbytes
)
462 if (copy_to_user(buf
, page
+ pos
, nbytes
))
464 *ppos
= pos
+ nbytes
;
467 free_page((unsigned long) page
);
471 static ssize_t
bm_entry_write(struct file
*file
, const char *buffer
,
472 size_t count
, loff_t
*ppos
)
475 Node
*e
= file
->f_dentry
->d_inode
->u
.generic_ip
;
476 int res
= parse_command(buffer
, count
);
479 case 1: clear_bit(Enabled
, &e
->flags
);
481 case 2: set_bit(Enabled
, &e
->flags
);
483 case 3: root
= dget(file
->f_vfsmnt
->mnt_sb
->s_root
);
484 down(&root
->d_inode
->i_sem
);
488 up(&root
->d_inode
->i_sem
);
496 static struct file_operations bm_entry_operations
= {
497 .read
= bm_entry_read
,
498 .write
= bm_entry_write
,
501 static struct file_system_type bm_fs_type
;
505 static ssize_t
bm_register_write(struct file
*file
, const char *buffer
,
506 size_t count
, loff_t
*ppos
)
510 struct vfsmount
*mnt
= NULL
;
511 struct dentry
*root
, *dentry
;
512 struct super_block
*sb
= file
->f_vfsmnt
->mnt_sb
;
515 e
= create_entry(buffer
, count
);
520 root
= dget(sb
->s_root
);
521 down(&root
->d_inode
->i_sem
);
522 dentry
= lookup_one_len(e
->name
, root
, strlen(e
->name
));
523 err
= PTR_ERR(dentry
);
531 inode
= bm_get_inode(sb
, S_IFREG
| 0644);
537 write_lock(&entries_lock
);
539 write_unlock(&entries_lock
);
540 mnt
= kern_mount(&bm_fs_type
);
547 write_lock(&entries_lock
);
554 e
->dentry
= dget(dentry
);
555 inode
->u
.generic_ip
= e
;
556 inode
->i_fop
= &bm_entry_operations
;
557 d_instantiate(dentry
, inode
);
559 list_add(&e
->list
, &entries
);
560 write_unlock(&entries_lock
);
567 up(&root
->d_inode
->i_sem
);
577 static struct file_operations bm_register_operations
= {
578 .write
= bm_register_write
,
584 bm_status_read(struct file
* file
, char * buf
, size_t nbytes
, loff_t
*ppos
)
586 char *s
= enabled
? "enabled" : "disabled";
594 if (len
< pos
+ nbytes
)
596 if (copy_to_user(buf
, s
+ pos
, nbytes
))
598 *ppos
= pos
+ nbytes
;
602 static ssize_t
bm_status_write(struct file
* file
, const char * buffer
,
603 size_t count
, loff_t
*ppos
)
605 int res
= parse_command(buffer
, count
);
609 case 1: enabled
= 0; break;
610 case 2: enabled
= 1; break;
611 case 3: root
= dget(file
->f_vfsmnt
->mnt_sb
->s_root
);
612 down(&root
->d_inode
->i_sem
);
614 while (!list_empty(&entries
))
615 kill_node(list_entry(entries
.next
, Node
, list
));
617 up(&root
->d_inode
->i_sem
);
624 static struct file_operations bm_status_operations
= {
625 .read
= bm_status_read
,
626 .write
= bm_status_write
,
629 /* Superblock handling */
631 static struct super_operations s_ops
= {
632 .statfs
= simple_statfs
,
633 .drop_inode
= generic_delete_inode
,
634 .clear_inode
= bm_clear_inode
,
637 static int bm_fill_super(struct super_block
* sb
, void * data
, int silent
)
639 struct qstr names
[2] = {{.name
= "status"}, {.name
= "register"}};
640 struct inode
* inode
;
641 struct dentry
* dentry
[3];
644 for (i
=0; i
<sizeof(names
)/sizeof(names
[0]); i
++) {
645 names
[i
].len
= strlen(names
[i
].name
);
646 names
[i
].hash
= full_name_hash(names
[i
].name
, names
[i
].len
);
649 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
650 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
651 sb
->s_magic
= 0x42494e4d;
654 inode
= bm_get_inode(sb
, S_IFDIR
| 0755);
657 inode
->i_op
= &simple_dir_inode_operations
;
658 inode
->i_fop
= &simple_dir_operations
;
659 dentry
[0] = d_alloc_root(inode
);
664 dentry
[1] = d_alloc(dentry
[0], &names
[0]);
667 dentry
[2] = d_alloc(dentry
[0], &names
[1]);
670 inode
= bm_get_inode(sb
, S_IFREG
| 0644);
673 inode
->i_fop
= &bm_status_operations
;
674 d_add(dentry
[1], inode
);
675 inode
= bm_get_inode(sb
, S_IFREG
| 0400);
678 inode
->i_fop
= &bm_register_operations
;
679 d_add(dentry
[2], inode
);
681 sb
->s_root
= dentry
[0];
693 static struct super_block
*bm_get_sb(struct file_system_type
*fs_type
,
694 int flags
, char *dev_name
, void *data
)
696 return get_sb_single(fs_type
, flags
, data
, bm_fill_super
);
699 static struct linux_binfmt misc_format
= {
700 .module
= THIS_MODULE
,
701 .load_binary
= load_misc_binary
,
704 static struct file_system_type bm_fs_type
= {
705 .owner
= THIS_MODULE
,
706 .name
= "binfmt_misc",
708 .kill_sb
= kill_litter_super
,
711 static int __init
init_misc_binfmt(void)
713 int err
= register_filesystem(&bm_fs_type
);
715 err
= register_binfmt(&misc_format
);
717 unregister_filesystem(&bm_fs_type
);
722 static void __exit
exit_misc_binfmt(void)
724 unregister_binfmt(&misc_format
);
725 unregister_filesystem(&bm_fs_type
);
728 module_init(init_misc_binfmt
);
729 module_exit(exit_misc_binfmt
);
730 MODULE_LICENSE("GPL");