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/module.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
23 #include <linux/malloc.h>
24 #include <linux/binfmts.h>
25 #include <linux/init.h>
26 #include <linux/proc_fs.h>
27 #include <linux/string.h>
28 #include <linux/ctype.h>
29 #include <asm/uaccess.h>
30 #include <asm/spinlock.h>
33 #define VERBOSE_STATUS /* undef this to save 400 bytes kernel memory */
36 struct binfmt_entry
*next
;
38 int flags
; /* type, status, etc. */
39 int offset
; /* offset of magic */
40 int size
; /* size of magic/mask */
41 char *magic
; /* magic or filename extension */
42 char *mask
; /* mask, NULL for exact match */
43 char *interpreter
; /* filename of interpreter */
45 struct proc_dir_entry
*proc_dir
;
48 #define ENTRY_ENABLED 1 /* the old binfmt_entry.enabled */
49 #define ENTRY_MAGIC 8 /* not filename detection */
51 static int load_misc_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
);
52 static void entry_proc_cleanup(struct binfmt_entry
*e
);
53 static int entry_proc_setup(struct binfmt_entry
*e
);
55 static struct linux_binfmt misc_format
= {
57 NULL
, 0, load_misc_binary
, NULL
, NULL
59 NULL
, &__this_module
, load_misc_binary
, NULL
, NULL
63 static struct proc_dir_entry
*bm_dir
= NULL
;
65 static struct binfmt_entry
*entries
= NULL
;
66 static int free_id
= 1;
67 static int enabled
= 1;
70 static rwlock_t entries_lock
= RW_LOCK_UNLOCKED
;
75 * Unregister one entry
77 static void clear_entry(int id
)
79 struct binfmt_entry
**ep
, *e
;
81 write_lock(&entries_lock
);
83 while (*ep
&& ((*ep
)->id
!= id
))
87 write_unlock(&entries_lock
);
90 entry_proc_cleanup(e
);
96 * Clear all registered binary formats
98 static void clear_entries(void)
100 struct binfmt_entry
*e
, *n
;
102 write_lock(&entries_lock
);
105 write_unlock(&entries_lock
);
109 entry_proc_cleanup(e
);
115 * Find entry through id and lock it
117 static struct binfmt_entry
*get_entry(int id
)
119 struct binfmt_entry
*e
;
121 read_lock(&entries_lock
);
123 while (e
&& (e
->id
!= id
))
126 read_unlock(&entries_lock
);
133 static inline void put_entry(struct binfmt_entry
*e
)
136 read_unlock(&entries_lock
);
141 * Check if we support the binfmt
142 * if we do, return the binfmt_entry, else NULL
143 * locking is done in load_misc_binary
145 static struct binfmt_entry
*check_file(struct linux_binprm
*bprm
)
147 struct binfmt_entry
*e
;
148 char *p
= strrchr(bprm
->filename
, '.');
153 if (e
->flags
& ENTRY_ENABLED
) {
154 if (!(e
->flags
& ENTRY_MAGIC
)) {
155 if (p
&& !strcmp(e
->magic
, p
+ 1))
159 while ((j
< e
->size
) &&
160 !((bprm
->buf
[e
->offset
+ j
] ^ e
->magic
[j
])
161 & (e
->mask
? e
->mask
[j
] : 0xff)))
175 static int load_misc_binary(struct linux_binprm
*bprm
, struct pt_regs
*regs
)
177 struct binfmt_entry
*fmt
;
178 struct dentry
* dentry
;
180 char *iname_addr
= iname
;
181 int retval
, fmt_flags
= 0;
189 /* to keep locking time low, we copy the interpreter string */
190 read_lock(&entries_lock
);
191 if ((fmt
= check_file(bprm
))) {
192 strncpy(iname
, fmt
->interpreter
, 127);
194 fmt_flags
= fmt
->flags
;
196 read_unlock(&entries_lock
);
205 /* Build args for interpreter */
206 remove_arg_zero(bprm
);
207 bprm
->p
= copy_strings(1, &bprm
->filename
, bprm
->page
, bprm
->p
, 2);
209 bprm
->p
= copy_strings(1, &iname_addr
, bprm
->page
, bprm
->p
, 2);
215 bprm
->filename
= iname
; /* for binfmt_script */
217 dentry
= open_namei(iname
, 0, 0);
218 retval
= PTR_ERR(dentry
);
221 bprm
->dentry
= dentry
;
223 retval
= prepare_binprm(bprm
);
225 retval
= search_binary_handler(bprm
, regs
);
234 * /proc handling routines
238 * parses and copies one argument enclosed in del from *sp to *dp,
239 * recognising the \x special.
240 * returns pointer to the copied argument or NULL in case of an
241 * error (and sets err) or null argument length.
243 static char *copyarg(char **dp
, const char **sp
, int *count
,
244 char del
, int special
, int *err
)
246 char c
= 0, *res
= *dp
;
248 while (!*err
&& ((c
= *((*sp
)++)), (*count
)--) && (c
!= del
)) {
251 if (special
&& (**sp
== 'x')) {
252 if (!isxdigit(c
= toupper(*(++*sp
))))
254 **dp
= (c
- (isdigit(c
) ? '0' : 'A' - 10)) * 16;
255 if (!isxdigit(c
= toupper(*(++*sp
))))
257 *((*dp
)++) += c
- (isdigit(c
) ? '0' : 'A' - 10);
266 if (*err
|| (c
!= del
) || (res
== *dp
))
274 * This registers a new binary format, it recognises the syntax
275 * ':name:type:offset:magic:mask:interpreter:'
276 * where the ':' is the IFS, that can be chosen with the first char
278 static int proc_write_register(struct file
*file
, const char *buffer
,
279 unsigned long count
, void *data
)
283 struct binfmt_entry
*e
;
284 int memsize
, cnt
= count
- 1, err
;
286 /* some sanity checks */
288 if ((count
< 11) || (count
> 256))
292 memsize
= sizeof(struct binfmt_entry
) + count
;
293 if (!(e
= (struct binfmt_entry
*) kmalloc(memsize
, GFP_USER
)))
299 dp
= (char *)e
+ sizeof(struct binfmt_entry
);
301 e
->proc_name
= copyarg(&dp
, &sp
, &cnt
, del
, 0, &err
);
303 /* we can use bit 3 of type for ext/magic
304 flag due to the nice encoding of E and M */
305 if ((*sp
& ~('E' | 'M')) || (sp
[1] != del
))
308 e
->flags
= (*sp
++ & (ENTRY_MAGIC
| ENTRY_ENABLED
));
312 while (cnt
-- && isdigit(*sp
))
313 e
->offset
= e
->offset
* 10 + *sp
++ - '0';
317 e
->magic
= copyarg(&dp
, &sp
, &cnt
, del
, (e
->flags
& ENTRY_MAGIC
), &err
);
318 e
->size
= dp
- e
->magic
;
319 e
->mask
= copyarg(&dp
, &sp
, &cnt
, del
, 1, &err
);
320 if (e
->mask
&& ((dp
- e
->mask
) != e
->size
))
322 e
->interpreter
= copyarg(&dp
, &sp
, &cnt
, del
, 0, &err
);
325 /* more sanity checks */
326 if (err
|| !(!cnt
|| (!(--cnt
) && (*sp
== '\n'))) ||
327 (e
->size
< 1) || ((e
->size
+ e
->offset
) > 127) ||
328 !(e
->proc_name
) || !(e
->interpreter
) || entry_proc_setup(e
))
331 write_lock(&entries_lock
);
334 write_unlock(&entries_lock
);
346 * Get status of entry/binfmt_misc
347 * FIXME? should an entry be marked disabled if binfmt_misc is disabled though
350 static int proc_read_status(char *page
, char **start
, off_t off
,
351 int count
, int *eof
, void *data
)
353 struct binfmt_entry
*e
;
357 #ifndef VERBOSE_STATUS
359 if (!(e
= get_entry((int) data
))) {
363 i
= e
->flags
& ENTRY_ENABLED
;
368 sprintf(page
, "%s\n", (i
? "enabled" : "disabled"));
371 sprintf(page
, "%s\n", (enabled
? "enabled" : "disabled"));
373 if (!(e
= get_entry((long) data
))) {
377 sprintf(page
, "%s\ninterpreter %s\n",
378 (e
->flags
& ENTRY_ENABLED
? "enabled" : "disabled"),
380 dp
= page
+ strlen(page
);
381 if (!(e
->flags
& ENTRY_MAGIC
)) {
382 sprintf(dp
, "extension .%s\n", e
->magic
);
383 dp
= page
+ strlen(page
);
385 sprintf(dp
, "offset %i\nmagic ", e
->offset
);
386 dp
= page
+ strlen(page
);
387 for (i
= 0; i
< e
->size
; i
++) {
388 sprintf(dp
, "%02x", 0xff & (int) (e
->magic
[i
]));
392 sprintf(dp
, "\nmask ");
394 for (i
= 0; i
< e
->size
; i
++) {
395 sprintf(dp
, "%02x", 0xff & (int) (e
->mask
[i
]));
406 elen
= strlen(page
) - off
;
409 *eof
= (elen
<= count
) ? 1 : 0;
418 * Set status of entry/binfmt_misc:
419 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
421 static int proc_write_status(struct file
*file
, const char *buffer
,
422 unsigned long count
, void *data
)
424 struct binfmt_entry
*e
;
427 if (buffer
[count
-1] == '\n')
429 if ((count
== 1) && !(buffer
[0] & ~('0' | '1'))) {
431 if ((e
= get_entry((long) data
)))
432 e
->flags
= (e
->flags
& ~ENTRY_ENABLED
)
433 | (int)(buffer
[0] - '0');
436 enabled
= buffer
[0] - '0';
438 } else if ((count
== 2) && (buffer
[0] == '-') && (buffer
[1] == '1')) {
440 clear_entry((long) data
);
450 * Remove the /proc-dir entries of one binfmt
452 static void entry_proc_cleanup(struct binfmt_entry
*e
)
454 remove_proc_entry(e
->proc_name
, bm_dir
);
458 * Create the /proc-dir entry for binfmt
460 static int entry_proc_setup(struct binfmt_entry
*e
)
462 if (!(e
->proc_dir
= create_proc_entry(e
->proc_name
,
463 S_IFREG
| S_IRUGO
| S_IWUSR
, bm_dir
)))
466 e
->proc_dir
->data
= (void *) (e
->id
);
467 e
->proc_dir
->read_proc
= proc_read_status
;
468 e
->proc_dir
->write_proc
= proc_write_status
;
475 * This is called as the fill_inode function when an inode
476 * is going into (fill = 1) or out of service (fill = 0).
477 * We use it here to manage the module use counts.
479 * Note: only the top-level directory needs to do this; if
480 * a lower level is referenced, the parent will be as well.
482 static void bm_modcount(struct inode
*inode
, int fill
)
491 int __init
init_misc_binfmt(void)
493 struct proc_dir_entry
*status
= NULL
, *reg
;
496 bm_dir
= create_proc_entry("sys/fs/binfmt_misc", S_IFDIR
, NULL
);
500 bm_dir
->fill_inode
= bm_modcount
;
503 status
= create_proc_entry("status", S_IFREG
| S_IRUGO
| S_IWUSR
,
507 status
->read_proc
= proc_read_status
;
508 status
->write_proc
= proc_write_status
;
510 reg
= create_proc_entry("register", S_IFREG
| S_IWUSR
, bm_dir
);
513 reg
->write_proc
= proc_write_register
;
515 error
= register_binfmt(&misc_format
);
520 remove_proc_entry("status", bm_dir
);
522 remove_proc_entry("sys/fs/binfmt_misc", NULL
);
528 int init_module(void)
530 return init_misc_binfmt();
533 void cleanup_module(void)
535 unregister_binfmt(&misc_format
);
536 remove_proc_entry("register", bm_dir
);
537 remove_proc_entry("status", bm_dir
);
539 remove_proc_entry("sys/fs/binfmt_misc", NULL
);
542 #undef VERBOSE_STATUS