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
;
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 strlcpy(iname
, fmt
->interpreter
, BINPRM_BUF_SIZE
);
118 read_unlock(&entries_lock
);
122 allow_write_access(bprm
->file
);
126 /* Build args for interpreter */
127 if (!(fmt
->flags
& MISC_FMT_PRESERVE_ARGV0
)) {
128 remove_arg_zero(bprm
);
130 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
131 if (retval
< 0) goto _ret
;
133 retval
= copy_strings_kernel(1, &iname_addr
, bprm
);
134 if (retval
< 0) goto _ret
;
136 bprm
->filename
= iname
; /* for binfmt_script */
138 file
= open_exec(iname
);
139 retval
= PTR_ERR(file
);
144 retval
= prepare_binprm(bprm
);
146 retval
= search_binary_handler(bprm
, regs
);
151 /* Command parsers */
154 * parses and copies one argument enclosed in del from *sp to *dp,
155 * recognising the \x special.
156 * returns pointer to the copied argument or NULL in case of an
157 * error (and sets err) or null argument length.
159 static char *scanarg(char *s
, char del
)
163 while ((c
= *s
++) != del
) {
164 if (c
== '\\' && *s
== 'x') {
175 static int unquote(char *from
)
177 char c
= 0, *s
= from
, *p
= from
;
179 while ((c
= *s
++) != '\0') {
180 if (c
== '\\' && *s
== 'x') {
183 *p
= (c
- (isdigit(c
) ? '0' : 'A' - 10)) << 4;
185 *p
++ |= c
- (isdigit(c
) ? '0' : 'A' - 10);
194 * This registers a new binary format, it recognises the syntax
195 * ':name:type:offset:magic:mask:interpreter:'
196 * where the ':' is the IFS, that can be chosen with the first char
198 static Node
*create_entry(const char *buffer
, size_t count
)
205 /* some sanity checks */
207 if ((count
< 11) || (count
> 256))
211 memsize
= sizeof(Node
) + count
+ 8;
212 e
= (Node
*) kmalloc(memsize
, GFP_USER
);
216 p
= buf
= (char *)e
+ sizeof(Node
);
218 memset(e
, 0, sizeof(Node
));
219 if (copy_from_user(buf
, buffer
, count
))
222 del
= *p
++; /* delimeter */
224 memset(buf
+count
, del
, 8);
232 !strcmp(e
->name
, ".") ||
233 !strcmp(e
->name
, "..") ||
234 strchr(e
->name
, '/'))
237 case 'E': e
->flags
= 1<<Enabled
; break;
238 case 'M': e
->flags
= (1<<Enabled
) | (1<<Magic
); break;
239 default: goto Einval
;
243 if (test_bit(Magic
, &e
->flags
)) {
244 char *s
= strchr(p
, del
);
248 e
->offset
= simple_strtoul(p
, &p
, 10);
265 e
->size
= unquote(e
->magic
);
266 if (e
->mask
&& unquote(e
->mask
) != e
->size
)
268 if (e
->size
+ e
->offset
> BINPRM_BUF_SIZE
)
280 if (!e
->magic
[0] || strchr(e
->magic
, '/'))
292 if (!e
->interpreter
[0])
297 e
->flags
|= MISC_FMT_PRESERVE_ARGV0
;
302 if (p
!= buf
+ count
)
311 return ERR_PTR(-EFAULT
);
314 return ERR_PTR(-EINVAL
);
318 * Set status of entry/binfmt_misc:
319 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
321 static int parse_command(const char *buffer
, size_t count
)
329 if (copy_from_user(s
, buffer
, count
))
331 if (s
[count
-1] == '\n')
333 if (count
== 1 && s
[0] == '0')
335 if (count
== 1 && s
[0] == '1')
337 if (count
== 2 && s
[0] == '-' && s
[1] == '1')
344 static void entry_status(Node
*e
, char *page
)
347 char *status
= "disabled";
349 if (test_bit(Enabled
, &e
->flags
))
352 if (!VERBOSE_STATUS
) {
353 sprintf(page
, "%s\n", status
);
357 sprintf(page
, "%s\ninterpreter %s\n", status
, e
->interpreter
);
358 dp
= page
+ strlen(page
);
359 if (!test_bit(Magic
, &e
->flags
)) {
360 sprintf(dp
, "extension .%s\n", e
->magic
);
364 sprintf(dp
, "offset %i\nmagic ", e
->offset
);
365 dp
= page
+ strlen(page
);
366 for (i
= 0; i
< e
->size
; i
++) {
367 sprintf(dp
, "%02x", 0xff & (int) (e
->magic
[i
]));
371 sprintf(dp
, "\nmask ");
373 for (i
= 0; i
< e
->size
; i
++) {
374 sprintf(dp
, "%02x", 0xff & (int) (e
->mask
[i
]));
383 static struct inode
*bm_get_inode(struct super_block
*sb
, int mode
)
385 struct inode
* inode
= new_inode(sb
);
388 inode
->i_mode
= mode
;
391 inode
->i_blksize
= PAGE_CACHE_SIZE
;
393 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
398 static void bm_clear_inode(struct inode
*inode
)
400 kfree(inode
->u
.generic_ip
);
403 static void kill_node(Node
*e
)
405 struct dentry
*dentry
;
407 write_lock(&entries_lock
);
410 list_del_init(&e
->list
);
413 write_unlock(&entries_lock
);
416 dentry
->d_inode
->i_nlink
--;
419 simple_release_fs(&bm_mnt
, &entry_count
);
426 bm_entry_read(struct file
* file
, char * buf
, size_t nbytes
, loff_t
*ppos
)
428 Node
*e
= file
->f_dentry
->d_inode
->u
.generic_ip
;
434 if (!(page
= (char*) __get_free_page(GFP_KERNEL
)))
437 entry_status(e
, page
);
446 if (len
< pos
+ nbytes
)
449 if (copy_to_user(buf
, page
+ pos
, nbytes
))
451 *ppos
= pos
+ nbytes
;
454 free_page((unsigned long) page
);
458 static ssize_t
bm_entry_write(struct file
*file
, const char *buffer
,
459 size_t count
, loff_t
*ppos
)
462 Node
*e
= file
->f_dentry
->d_inode
->u
.generic_ip
;
463 int res
= parse_command(buffer
, count
);
466 case 1: clear_bit(Enabled
, &e
->flags
);
468 case 2: set_bit(Enabled
, &e
->flags
);
470 case 3: root
= dget(file
->f_vfsmnt
->mnt_sb
->s_root
);
471 down(&root
->d_inode
->i_sem
);
475 up(&root
->d_inode
->i_sem
);
483 static struct file_operations bm_entry_operations
= {
484 .read
= bm_entry_read
,
485 .write
= bm_entry_write
,
490 static ssize_t
bm_register_write(struct file
*file
, const char *buffer
,
491 size_t count
, loff_t
*ppos
)
495 struct dentry
*root
, *dentry
;
496 struct super_block
*sb
= file
->f_vfsmnt
->mnt_sb
;
499 e
= create_entry(buffer
, count
);
504 root
= dget(sb
->s_root
);
505 down(&root
->d_inode
->i_sem
);
506 dentry
= lookup_one_len(e
->name
, root
, strlen(e
->name
));
507 err
= PTR_ERR(dentry
);
515 inode
= bm_get_inode(sb
, S_IFREG
| 0644);
521 err
= simple_pin_fs("binfmt_misc", &bm_mnt
, &entry_count
);
528 e
->dentry
= dget(dentry
);
529 inode
->u
.generic_ip
= e
;
530 inode
->i_fop
= &bm_entry_operations
;
532 write_lock(&entries_lock
);
533 d_instantiate(dentry
, inode
);
534 list_add(&e
->list
, &entries
);
535 write_unlock(&entries_lock
);
541 up(&root
->d_inode
->i_sem
);
551 static struct file_operations bm_register_operations
= {
552 .write
= bm_register_write
,
558 bm_status_read(struct file
* file
, char * buf
, size_t nbytes
, loff_t
*ppos
)
560 char *s
= enabled
? "enabled" : "disabled";
568 if (len
< pos
+ nbytes
)
570 if (copy_to_user(buf
, s
+ pos
, nbytes
))
572 *ppos
= pos
+ nbytes
;
576 static ssize_t
bm_status_write(struct file
* file
, const char * buffer
,
577 size_t count
, loff_t
*ppos
)
579 int res
= parse_command(buffer
, count
);
583 case 1: enabled
= 0; break;
584 case 2: enabled
= 1; break;
585 case 3: root
= dget(file
->f_vfsmnt
->mnt_sb
->s_root
);
586 down(&root
->d_inode
->i_sem
);
588 while (!list_empty(&entries
))
589 kill_node(list_entry(entries
.next
, Node
, list
));
591 up(&root
->d_inode
->i_sem
);
598 static struct file_operations bm_status_operations
= {
599 .read
= bm_status_read
,
600 .write
= bm_status_write
,
603 /* Superblock handling */
605 static struct super_operations s_ops
= {
606 .statfs
= simple_statfs
,
607 .clear_inode
= bm_clear_inode
,
610 static int bm_fill_super(struct super_block
* sb
, void * data
, int silent
)
612 static struct tree_descr bm_files
[] = {
613 [1] = {"status", &bm_status_operations
, S_IWUSR
|S_IRUGO
},
614 [2] = {"register", &bm_register_operations
, S_IWUSR
},
617 int err
= simple_fill_super(sb
, 0x42494e4d, bm_files
);
623 static struct super_block
*bm_get_sb(struct file_system_type
*fs_type
,
624 int flags
, const char *dev_name
, void *data
)
626 return get_sb_single(fs_type
, flags
, data
, bm_fill_super
);
629 static struct linux_binfmt misc_format
= {
630 .module
= THIS_MODULE
,
631 .load_binary
= load_misc_binary
,
634 static struct file_system_type bm_fs_type
= {
635 .owner
= THIS_MODULE
,
636 .name
= "binfmt_misc",
638 .kill_sb
= kill_litter_super
,
641 static int __init
init_misc_binfmt(void)
643 int err
= register_filesystem(&bm_fs_type
);
645 err
= register_binfmt(&misc_format
);
647 unregister_filesystem(&bm_fs_type
);
652 static void __exit
exit_misc_binfmt(void)
654 unregister_binfmt(&misc_format
);
655 unregister_filesystem(&bm_fs_type
);
658 module_init(init_misc_binfmt
);
659 module_exit(exit_misc_binfmt
);
660 MODULE_LICENSE("GPL");