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>
21 #include <linux/sched.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)
42 #define MISC_FMT_OPEN_BINARY (1<<30)
43 #define MISC_FMT_CREDENTIALS (1<<29)
46 struct list_head list
;
47 unsigned long flags
; /* type, status, etc. */
48 int offset
; /* offset of magic */
49 int size
; /* size of magic/mask */
50 char *magic
; /* magic or filename extension */
51 char *mask
; /* mask, NULL for exact match */
52 char *interpreter
; /* filename of interpreter */
54 struct dentry
*dentry
;
57 static DEFINE_RWLOCK(entries_lock
);
58 static struct file_system_type bm_fs_type
;
59 static struct vfsmount
*bm_mnt
;
60 static int entry_count
;
63 * Check if we support the binfmt
64 * if we do, return the node, else NULL
65 * locking is done in load_misc_binary
67 static Node
*check_file(struct linux_binprm
*bprm
)
69 char *p
= strrchr(bprm
->interp
, '.');
72 list_for_each(l
, &entries
) {
73 Node
*e
= list_entry(l
, Node
, list
);
77 if (!test_bit(Enabled
, &e
->flags
))
80 if (!test_bit(Magic
, &e
->flags
)) {
81 if (p
&& !strcmp(e
->magic
, p
+ 1))
86 s
= bprm
->buf
+ e
->offset
;
88 for (j
= 0; j
< e
->size
; j
++)
89 if ((*s
++ ^ e
->magic
[j
]) & e
->mask
[j
])
92 for (j
= 0; j
< e
->size
; j
++)
93 if ((*s
++ ^ e
->magic
[j
]))
105 static int load_misc_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
)
108 struct file
* interp_file
= NULL
;
109 char iname
[BINPRM_BUF_SIZE
];
110 char *iname_addr
= iname
;
118 /* to keep locking time low, we copy the interpreter string */
119 read_lock(&entries_lock
);
120 fmt
= check_file(bprm
);
122 strlcpy(iname
, fmt
->interpreter
, BINPRM_BUF_SIZE
);
123 read_unlock(&entries_lock
);
127 if (!(fmt
->flags
& MISC_FMT_PRESERVE_ARGV0
)) {
128 retval
= remove_arg_zero(bprm
);
133 if (fmt
->flags
& MISC_FMT_OPEN_BINARY
) {
135 /* if the binary should be opened on behalf of the
136 * interpreter than keep it open and assign descriptor
138 fd_binary
= get_unused_fd();
143 fd_install(fd_binary
, bprm
->file
);
145 /* if the binary is not readable than enforce mm->dumpable=0
146 regardless of the interpreter's permissions */
147 if (file_permission(bprm
->file
, MAY_READ
))
148 bprm
->interp_flags
|= BINPRM_FLAGS_ENFORCE_NONDUMP
;
150 allow_write_access(bprm
->file
);
153 /* mark the bprm that fd should be passed to interp */
154 bprm
->interp_flags
|= BINPRM_FLAGS_EXECFD
;
155 bprm
->interp_data
= fd_binary
;
158 allow_write_access(bprm
->file
);
162 /* make argv[1] be the path to the binary */
163 retval
= copy_strings_kernel (1, &bprm
->interp
, bprm
);
168 /* add the interp as argv[0] */
169 retval
= copy_strings_kernel (1, &iname_addr
, bprm
);
174 bprm
->interp
= iname
; /* for binfmt_script */
176 interp_file
= open_exec (iname
);
177 retval
= PTR_ERR (interp_file
);
178 if (IS_ERR (interp_file
))
181 bprm
->file
= interp_file
;
182 if (fmt
->flags
& MISC_FMT_CREDENTIALS
) {
184 * No need to call prepare_binprm(), it's already been
185 * done. bprm->buf is stale, update from interp_file.
187 memset(bprm
->buf
, 0, BINPRM_BUF_SIZE
);
188 retval
= kernel_read(bprm
->file
, 0, bprm
->buf
, BINPRM_BUF_SIZE
);
190 retval
= prepare_binprm (bprm
);
195 retval
= search_binary_handler (bprm
, regs
);
203 sys_close(fd_binary
);
204 bprm
->interp_flags
= 0;
205 bprm
->interp_data
= 0;
209 /* Command parsers */
212 * parses and copies one argument enclosed in del from *sp to *dp,
213 * recognising the \x special.
214 * returns pointer to the copied argument or NULL in case of an
215 * error (and sets err) or null argument length.
217 static char *scanarg(char *s
, char del
)
221 while ((c
= *s
++) != del
) {
222 if (c
== '\\' && *s
== 'x') {
233 static int unquote(char *from
)
235 char c
= 0, *s
= from
, *p
= from
;
237 while ((c
= *s
++) != '\0') {
238 if (c
== '\\' && *s
== 'x') {
241 *p
= (c
- (isdigit(c
) ? '0' : 'A' - 10)) << 4;
243 *p
++ |= c
- (isdigit(c
) ? '0' : 'A' - 10);
251 static char * check_special_flags (char * sfs
, Node
* e
)
261 e
->flags
|= MISC_FMT_PRESERVE_ARGV0
;
265 e
->flags
|= MISC_FMT_OPEN_BINARY
;
269 /* this flags also implies the
271 e
->flags
|= (MISC_FMT_CREDENTIALS
|
272 MISC_FMT_OPEN_BINARY
);
282 * This registers a new binary format, it recognises the syntax
283 * ':name:type:offset:magic:mask:interpreter:flags'
284 * where the ':' is the IFS, that can be chosen with the first char
286 static Node
*create_entry(const char __user
*buffer
, size_t count
)
293 /* some sanity checks */
295 if ((count
< 11) || (count
> 256))
299 memsize
= sizeof(Node
) + count
+ 8;
300 e
= kmalloc(memsize
, GFP_USER
);
304 p
= buf
= (char *)e
+ sizeof(Node
);
306 memset(e
, 0, sizeof(Node
));
307 if (copy_from_user(buf
, buffer
, count
))
310 del
= *p
++; /* delimeter */
312 memset(buf
+count
, del
, 8);
320 !strcmp(e
->name
, ".") ||
321 !strcmp(e
->name
, "..") ||
322 strchr(e
->name
, '/'))
325 case 'E': e
->flags
= 1<<Enabled
; break;
326 case 'M': e
->flags
= (1<<Enabled
) | (1<<Magic
); break;
327 default: goto Einval
;
331 if (test_bit(Magic
, &e
->flags
)) {
332 char *s
= strchr(p
, del
);
336 e
->offset
= simple_strtoul(p
, &p
, 10);
353 e
->size
= unquote(e
->magic
);
354 if (e
->mask
&& unquote(e
->mask
) != e
->size
)
356 if (e
->size
+ e
->offset
> BINPRM_BUF_SIZE
)
368 if (!e
->magic
[0] || strchr(e
->magic
, '/'))
380 if (!e
->interpreter
[0])
384 p
= check_special_flags (p
, e
);
388 if (p
!= buf
+ count
)
397 return ERR_PTR(-EFAULT
);
400 return ERR_PTR(-EINVAL
);
404 * Set status of entry/binfmt_misc:
405 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
407 static int parse_command(const char __user
*buffer
, size_t count
)
415 if (copy_from_user(s
, buffer
, count
))
417 if (s
[count
-1] == '\n')
419 if (count
== 1 && s
[0] == '0')
421 if (count
== 1 && s
[0] == '1')
423 if (count
== 2 && s
[0] == '-' && s
[1] == '1')
430 static void entry_status(Node
*e
, char *page
)
433 char *status
= "disabled";
434 const char * flags
= "flags: ";
436 if (test_bit(Enabled
, &e
->flags
))
439 if (!VERBOSE_STATUS
) {
440 sprintf(page
, "%s\n", status
);
444 sprintf(page
, "%s\ninterpreter %s\n", status
, e
->interpreter
);
445 dp
= page
+ strlen(page
);
447 /* print the special flags */
448 sprintf (dp
, "%s", flags
);
449 dp
+= strlen (flags
);
450 if (e
->flags
& MISC_FMT_PRESERVE_ARGV0
) {
453 if (e
->flags
& MISC_FMT_OPEN_BINARY
) {
456 if (e
->flags
& MISC_FMT_CREDENTIALS
) {
462 if (!test_bit(Magic
, &e
->flags
)) {
463 sprintf(dp
, "extension .%s\n", e
->magic
);
467 sprintf(dp
, "offset %i\nmagic ", e
->offset
);
468 dp
= page
+ strlen(page
);
469 for (i
= 0; i
< e
->size
; i
++) {
470 sprintf(dp
, "%02x", 0xff & (int) (e
->magic
[i
]));
474 sprintf(dp
, "\nmask ");
476 for (i
= 0; i
< e
->size
; i
++) {
477 sprintf(dp
, "%02x", 0xff & (int) (e
->mask
[i
]));
486 static struct inode
*bm_get_inode(struct super_block
*sb
, int mode
)
488 struct inode
* inode
= new_inode(sb
);
491 inode
->i_mode
= mode
;
495 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
=
496 current_fs_time(inode
->i_sb
);
501 static void bm_clear_inode(struct inode
*inode
)
503 kfree(inode
->i_private
);
506 static void kill_node(Node
*e
)
508 struct dentry
*dentry
;
510 write_lock(&entries_lock
);
513 list_del_init(&e
->list
);
516 write_unlock(&entries_lock
);
519 dentry
->d_inode
->i_nlink
--;
522 simple_release_fs(&bm_mnt
, &entry_count
);
529 bm_entry_read(struct file
* file
, char __user
* buf
, size_t nbytes
, loff_t
*ppos
)
531 Node
*e
= file
->f_path
.dentry
->d_inode
->i_private
;
537 if (!(page
= (char*) __get_free_page(GFP_KERNEL
)))
540 entry_status(e
, page
);
549 if (len
< pos
+ nbytes
)
552 if (copy_to_user(buf
, page
+ pos
, nbytes
))
554 *ppos
= pos
+ nbytes
;
557 free_page((unsigned long) page
);
561 static ssize_t
bm_entry_write(struct file
*file
, const char __user
*buffer
,
562 size_t count
, loff_t
*ppos
)
565 Node
*e
= file
->f_path
.dentry
->d_inode
->i_private
;
566 int res
= parse_command(buffer
, count
);
569 case 1: clear_bit(Enabled
, &e
->flags
);
571 case 2: set_bit(Enabled
, &e
->flags
);
573 case 3: root
= dget(file
->f_path
.mnt
->mnt_sb
->s_root
);
574 mutex_lock(&root
->d_inode
->i_mutex
);
578 mutex_unlock(&root
->d_inode
->i_mutex
);
586 static const struct file_operations bm_entry_operations
= {
587 .read
= bm_entry_read
,
588 .write
= bm_entry_write
,
593 static ssize_t
bm_register_write(struct file
*file
, const char __user
*buffer
,
594 size_t count
, loff_t
*ppos
)
598 struct dentry
*root
, *dentry
;
599 struct super_block
*sb
= file
->f_path
.mnt
->mnt_sb
;
602 e
= create_entry(buffer
, count
);
607 root
= dget(sb
->s_root
);
608 mutex_lock(&root
->d_inode
->i_mutex
);
609 dentry
= lookup_one_len(e
->name
, root
, strlen(e
->name
));
610 err
= PTR_ERR(dentry
);
618 inode
= bm_get_inode(sb
, S_IFREG
| 0644);
624 err
= simple_pin_fs(&bm_fs_type
, &bm_mnt
, &entry_count
);
631 e
->dentry
= dget(dentry
);
632 inode
->i_private
= e
;
633 inode
->i_fop
= &bm_entry_operations
;
635 d_instantiate(dentry
, inode
);
636 write_lock(&entries_lock
);
637 list_add(&e
->list
, &entries
);
638 write_unlock(&entries_lock
);
644 mutex_unlock(&root
->d_inode
->i_mutex
);
654 static const struct file_operations bm_register_operations
= {
655 .write
= bm_register_write
,
661 bm_status_read(struct file
*file
, char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
663 char *s
= enabled
? "enabled" : "disabled";
665 return simple_read_from_buffer(buf
, nbytes
, ppos
, s
, strlen(s
));
668 static ssize_t
bm_status_write(struct file
* file
, const char __user
* buffer
,
669 size_t count
, loff_t
*ppos
)
671 int res
= parse_command(buffer
, count
);
675 case 1: enabled
= 0; break;
676 case 2: enabled
= 1; break;
677 case 3: root
= dget(file
->f_path
.mnt
->mnt_sb
->s_root
);
678 mutex_lock(&root
->d_inode
->i_mutex
);
680 while (!list_empty(&entries
))
681 kill_node(list_entry(entries
.next
, Node
, list
));
683 mutex_unlock(&root
->d_inode
->i_mutex
);
690 static const struct file_operations bm_status_operations
= {
691 .read
= bm_status_read
,
692 .write
= bm_status_write
,
695 /* Superblock handling */
697 static const struct super_operations s_ops
= {
698 .statfs
= simple_statfs
,
699 .clear_inode
= bm_clear_inode
,
702 static int bm_fill_super(struct super_block
* sb
, void * data
, int silent
)
704 static struct tree_descr bm_files
[] = {
705 [2] = {"status", &bm_status_operations
, S_IWUSR
|S_IRUGO
},
706 [3] = {"register", &bm_register_operations
, S_IWUSR
},
709 int err
= simple_fill_super(sb
, 0x42494e4d, bm_files
);
715 static int bm_get_sb(struct file_system_type
*fs_type
,
716 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
718 return get_sb_single(fs_type
, flags
, data
, bm_fill_super
, mnt
);
721 static struct linux_binfmt misc_format
= {
722 .module
= THIS_MODULE
,
723 .load_binary
= load_misc_binary
,
726 static struct file_system_type bm_fs_type
= {
727 .owner
= THIS_MODULE
,
728 .name
= "binfmt_misc",
730 .kill_sb
= kill_litter_super
,
733 static int __init
init_misc_binfmt(void)
735 int err
= register_filesystem(&bm_fs_type
);
737 err
= register_binfmt(&misc_format
);
739 unregister_filesystem(&bm_fs_type
);
744 static void __exit
exit_misc_binfmt(void)
746 unregister_binfmt(&misc_format
);
747 unregister_filesystem(&bm_fs_type
);
750 core_initcall(init_misc_binfmt
);
751 module_exit(exit_misc_binfmt
);
752 MODULE_LICENSE("GPL");