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
18 #include <linux/config.h>
19 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
24 #include <linux/malloc.h>
25 #include <linux/binfmts.h>
26 #include <linux/init.h>
27 #include <linux/proc_fs.h>
28 #include <linux/string.h>
29 #include <linux/ctype.h>
30 #include <asm/uaccess.h>
31 #include <asm/spinlock.h>
34 * We should make this work with a "stub-only" /proc,
35 * which would just not be able to be configured.
36 * Right now the /proc-fs support is too black and white,
37 * though, so just remind people that this should be
40 #ifndef CONFIG_PROC_FS
41 #error You really need /proc support for binfmt_misc. Please reconfigure!
44 #define VERBOSE_STATUS /* undef this to save 400 bytes kernel memory */
47 struct binfmt_entry
*next
;
49 int flags
; /* type, status, etc. */
50 int offset
; /* offset of magic */
51 int size
; /* size of magic/mask */
52 char *magic
; /* magic or filename extension */
53 char *mask
; /* mask, NULL for exact match */
54 char *interpreter
; /* filename of interpreter */
56 struct proc_dir_entry
*proc_dir
;
59 #define ENTRY_ENABLED 1 /* the old binfmt_entry.enabled */
60 #define ENTRY_MAGIC 8 /* not filename detection */
62 static int load_misc_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
);
63 static void entry_proc_cleanup(struct binfmt_entry
*e
);
64 static int entry_proc_setup(struct binfmt_entry
*e
);
66 static struct linux_binfmt misc_format
= {
73 load_misc_binary
, NULL
, NULL
, 0
76 static struct proc_dir_entry
*bm_dir
= NULL
;
78 static struct binfmt_entry
*entries
= NULL
;
79 static int free_id
= 1;
80 static int enabled
= 1;
82 static rwlock_t entries_lock
__attribute__((unused
)) = RW_LOCK_UNLOCKED
;
86 * Unregister one entry
88 static void clear_entry(int id
)
90 struct binfmt_entry
**ep
, *e
;
92 write_lock(&entries_lock
);
94 while (*ep
&& ((*ep
)->id
!= id
))
98 write_unlock(&entries_lock
);
101 entry_proc_cleanup(e
);
107 * Clear all registered binary formats
109 static void clear_entries(void)
111 struct binfmt_entry
*e
, *n
;
113 write_lock(&entries_lock
);
116 write_unlock(&entries_lock
);
120 entry_proc_cleanup(e
);
126 * Find entry through id and lock it
128 static struct binfmt_entry
*get_entry(int id
)
130 struct binfmt_entry
*e
;
132 read_lock(&entries_lock
);
134 while (e
&& (e
->id
!= id
))
137 read_unlock(&entries_lock
);
144 static inline void put_entry(struct binfmt_entry
*e
)
147 read_unlock(&entries_lock
);
152 * Check if we support the binfmt
153 * if we do, return the binfmt_entry, else NULL
154 * locking is done in load_misc_binary
156 static struct binfmt_entry
*check_file(struct linux_binprm
*bprm
)
158 struct binfmt_entry
*e
;
159 char *p
= strrchr(bprm
->filename
, '.');
164 if (e
->flags
& ENTRY_ENABLED
) {
165 if (!(e
->flags
& ENTRY_MAGIC
)) {
166 if (p
&& !strcmp(e
->magic
, p
+ 1))
170 while ((j
< e
->size
) &&
171 !((bprm
->buf
[e
->offset
+ j
] ^ e
->magic
[j
])
172 & (e
->mask
? e
->mask
[j
] : 0xff)))
186 static int load_misc_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
)
188 struct binfmt_entry
*fmt
;
189 struct dentry
* dentry
;
191 char *iname_addr
= iname
;
199 /* to keep locking time low, we copy the interpreter string */
200 read_lock(&entries_lock
);
201 fmt
= check_file(bprm
);
203 strncpy(iname
, fmt
->interpreter
, 127);
206 read_unlock(&entries_lock
);
213 /* Build args for interpreter */
214 remove_arg_zero(bprm
);
215 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
216 if (retval
< 0) goto _ret
;
218 retval
= copy_strings_kernel(1, &iname_addr
, bprm
);
219 if (retval
< 0) goto _ret
;
221 bprm
->filename
= iname
; /* for binfmt_script */
223 dentry
= open_namei(iname
, 0, 0);
224 retval
= PTR_ERR(dentry
);
227 bprm
->dentry
= dentry
;
229 retval
= prepare_binprm(bprm
);
231 retval
= search_binary_handler(bprm
, regs
);
240 * /proc handling routines
244 * parses and copies one argument enclosed in del from *sp to *dp,
245 * recognising the \x special.
246 * returns pointer to the copied argument or NULL in case of an
247 * error (and sets err) or null argument length.
249 static char *copyarg(char **dp
, const char **sp
, int *count
,
250 char del
, int special
, int *err
)
252 char c
= 0, *res
= *dp
;
254 while (!*err
&& ((c
= *((*sp
)++)), (*count
)--) && (c
!= del
)) {
257 if (special
&& (**sp
== 'x')) {
258 if (!isxdigit(c
= toupper(*(++*sp
))))
260 **dp
= (c
- (isdigit(c
) ? '0' : 'A' - 10)) * 16;
261 if (!isxdigit(c
= toupper(*(++*sp
))))
263 *((*dp
)++) += c
- (isdigit(c
) ? '0' : 'A' - 10);
272 if (*err
|| (c
!= del
) || (res
== *dp
))
280 * This registers a new binary format, it recognises the syntax
281 * ':name:type:offset:magic:mask:interpreter:'
282 * where the ':' is the IFS, that can be chosen with the first char
284 static int proc_write_register(struct file
*file
, const char *buffer
,
285 unsigned long count
, void *data
)
289 struct binfmt_entry
*e
;
290 int memsize
, cnt
= count
- 1, err
;
292 /* some sanity checks */
294 if ((count
< 11) || (count
> 256))
298 memsize
= sizeof(struct binfmt_entry
) + count
;
299 if (!(e
= (struct binfmt_entry
*) kmalloc(memsize
, GFP_USER
)))
305 dp
= (char *)e
+ sizeof(struct binfmt_entry
);
307 e
->proc_name
= copyarg(&dp
, &sp
, &cnt
, del
, 0, &err
);
309 /* we can use bit 3 of type for ext/magic
310 flag due to the nice encoding of E and M */
311 if ((*sp
& ~('E' | 'M')) || (sp
[1] != del
))
314 e
->flags
= (*sp
++ & (ENTRY_MAGIC
| ENTRY_ENABLED
));
318 while (cnt
-- && isdigit(*sp
))
319 e
->offset
= e
->offset
* 10 + *sp
++ - '0';
323 e
->magic
= copyarg(&dp
, &sp
, &cnt
, del
, (e
->flags
& ENTRY_MAGIC
), &err
);
324 e
->size
= dp
- e
->magic
;
325 e
->mask
= copyarg(&dp
, &sp
, &cnt
, del
, 1, &err
);
326 if (e
->mask
&& ((dp
- e
->mask
) != e
->size
))
328 e
->interpreter
= copyarg(&dp
, &sp
, &cnt
, del
, 0, &err
);
331 /* more sanity checks */
332 if (err
|| !(!cnt
|| (!(--cnt
) && (*sp
== '\n'))) ||
333 (e
->size
< 1) || ((e
->size
+ e
->offset
) > 127) ||
334 !(e
->proc_name
) || !(e
->interpreter
) || entry_proc_setup(e
))
337 write_lock(&entries_lock
);
340 write_unlock(&entries_lock
);
352 * Get status of entry/binfmt_misc
353 * FIXME? should an entry be marked disabled if binfmt_misc is disabled though
356 static int proc_read_status(char *page
, char **start
, off_t off
,
357 int count
, int *eof
, void *data
)
359 struct binfmt_entry
*e
;
363 #ifndef VERBOSE_STATUS
365 if (!(e
= get_entry((int) data
))) {
369 i
= e
->flags
& ENTRY_ENABLED
;
374 sprintf(page
, "%s\n", (i
? "enabled" : "disabled"));
377 sprintf(page
, "%s\n", (enabled
? "enabled" : "disabled"));
379 if (!(e
= get_entry((long) data
))) {
383 sprintf(page
, "%s\ninterpreter %s\n",
384 (e
->flags
& ENTRY_ENABLED
? "enabled" : "disabled"),
386 dp
= page
+ strlen(page
);
387 if (!(e
->flags
& ENTRY_MAGIC
)) {
388 sprintf(dp
, "extension .%s\n", e
->magic
);
389 dp
= page
+ strlen(page
);
391 sprintf(dp
, "offset %i\nmagic ", e
->offset
);
392 dp
= page
+ strlen(page
);
393 for (i
= 0; i
< e
->size
; i
++) {
394 sprintf(dp
, "%02x", 0xff & (int) (e
->magic
[i
]));
398 sprintf(dp
, "\nmask ");
400 for (i
= 0; i
< e
->size
; i
++) {
401 sprintf(dp
, "%02x", 0xff & (int) (e
->mask
[i
]));
412 elen
= strlen(page
) - off
;
415 *eof
= (elen
<= count
) ? 1 : 0;
424 * Set status of entry/binfmt_misc:
425 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
427 static int proc_write_status(struct file
*file
, const char *buffer
,
428 unsigned long count
, void *data
)
430 struct binfmt_entry
*e
;
433 if (buffer
[count
-1] == '\n')
435 if ((count
== 1) && !(buffer
[0] & ~('0' | '1'))) {
437 if ((e
= get_entry((long) data
)))
438 e
->flags
= (e
->flags
& ~ENTRY_ENABLED
)
439 | (int)(buffer
[0] - '0');
442 enabled
= buffer
[0] - '0';
444 } else if ((count
== 2) && (buffer
[0] == '-') && (buffer
[1] == '1')) {
446 clear_entry((long) data
);
456 * Remove the /proc-dir entries of one binfmt
458 static void entry_proc_cleanup(struct binfmt_entry
*e
)
460 remove_proc_entry(e
->proc_name
, bm_dir
);
464 * Create the /proc-dir entry for binfmt
466 static int entry_proc_setup(struct binfmt_entry
*e
)
468 if (!(e
->proc_dir
= create_proc_entry(e
->proc_name
,
469 S_IFREG
| S_IRUGO
| S_IWUSR
, bm_dir
)))
471 printk(KERN_WARNING
"Unable to create /proc entry.\n");
474 e
->proc_dir
->data
= (void *) (e
->id
);
475 e
->proc_dir
->read_proc
= proc_read_status
;
476 e
->proc_dir
->write_proc
= proc_write_status
;
482 * This is called as the fill_inode function when an inode
483 * is going into (fill = 1) or out of service (fill = 0).
484 * We use it here to manage the module use counts.
486 * Note: only the top-level directory needs to do this; if
487 * a lower level is referenced, the parent will be as well.
489 static void bm_modcount(struct inode
*inode
, int fill
)
498 int __init
init_misc_binfmt(void)
501 struct proc_dir_entry
*status
= NULL
, *reg
;
503 bm_dir
= create_proc_entry("sys/fs/binfmt_misc", S_IFDIR
, NULL
);
507 bm_dir
->fill_inode
= bm_modcount
;
510 status
= create_proc_entry("status", S_IFREG
| S_IRUGO
| S_IWUSR
,
514 status
->read_proc
= proc_read_status
;
515 status
->write_proc
= proc_write_status
;
517 reg
= create_proc_entry("register", S_IFREG
| S_IWUSR
, bm_dir
);
520 reg
->write_proc
= proc_write_register
;
522 error
= register_binfmt(&misc_format
);
527 remove_proc_entry("status", bm_dir
);
529 remove_proc_entry("sys/fs/binfmt_misc", NULL
);
535 int init_module(void)
537 return init_misc_binfmt();
540 void cleanup_module(void)
542 unregister_binfmt(&misc_format
);
543 remove_proc_entry("register", bm_dir
);
544 remove_proc_entry("status", bm_dir
);
546 remove_proc_entry("sys/fs/binfmt_misc", NULL
);
549 #undef VERBOSE_STATUS