Merge branch 'master' of /home/trondmy/kernel/linux-2.6/
[deliverable/linux.git] / fs / binfmt_misc.c
1 /*
2 * binfmt_misc.c
3 *
4 * Copyright (C) 1997 Richard Günther
5 *
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
8 * binfmt_mz.
9 *
10 * 1997-04-25 first version
11 * [...]
12 * 1997-05-19 cleanup
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.
17 */
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21
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>
30
31 #include <asm/uaccess.h>
32
33 enum {
34 VERBOSE_STATUS = 1 /* make it zero to save 400 bytes kernel memory */
35 };
36
37 static LIST_HEAD(entries);
38 static int enabled = 1;
39
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)
44
45 typedef struct {
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 */
53 char *name;
54 struct dentry *dentry;
55 } Node;
56
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;
61
62 /*
63 * Check if we support the binfmt
64 * if we do, return the node, else NULL
65 * locking is done in load_misc_binary
66 */
67 static Node *check_file(struct linux_binprm *bprm)
68 {
69 char *p = strrchr(bprm->interp, '.');
70 struct list_head *l;
71
72 list_for_each(l, &entries) {
73 Node *e = list_entry(l, Node, list);
74 char *s;
75 int j;
76
77 if (!test_bit(Enabled, &e->flags))
78 continue;
79
80 if (!test_bit(Magic, &e->flags)) {
81 if (p && !strcmp(e->magic, p + 1))
82 return e;
83 continue;
84 }
85
86 s = bprm->buf + e->offset;
87 if (e->mask) {
88 for (j = 0; j < e->size; j++)
89 if ((*s++ ^ e->magic[j]) & e->mask[j])
90 break;
91 } else {
92 for (j = 0; j < e->size; j++)
93 if ((*s++ ^ e->magic[j]))
94 break;
95 }
96 if (j == e->size)
97 return e;
98 }
99 return NULL;
100 }
101
102 /*
103 * the loader itself
104 */
105 static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
106 {
107 Node *fmt;
108 struct file * interp_file = NULL;
109 char iname[BINPRM_BUF_SIZE];
110 char *iname_addr = iname;
111 int retval;
112 int fd_binary = -1;
113 struct files_struct *files = NULL;
114
115 retval = -ENOEXEC;
116 if (!enabled)
117 goto _ret;
118
119 /* to keep locking time low, we copy the interpreter string */
120 read_lock(&entries_lock);
121 fmt = check_file(bprm);
122 if (fmt)
123 strlcpy(iname, fmt->interpreter, BINPRM_BUF_SIZE);
124 read_unlock(&entries_lock);
125 if (!fmt)
126 goto _ret;
127
128 if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
129 remove_arg_zero(bprm);
130 }
131
132 if (fmt->flags & MISC_FMT_OPEN_BINARY) {
133
134 files = current->files;
135 retval = unshare_files();
136 if (retval < 0)
137 goto _ret;
138 if (files == current->files) {
139 put_files_struct(files);
140 files = NULL;
141 }
142 /* if the binary should be opened on behalf of the
143 * interpreter than keep it open and assign descriptor
144 * to it */
145 fd_binary = get_unused_fd();
146 if (fd_binary < 0) {
147 retval = fd_binary;
148 goto _unshare;
149 }
150 fd_install(fd_binary, bprm->file);
151
152 /* if the binary is not readable than enforce mm->dumpable=0
153 regardless of the interpreter's permissions */
154 if (file_permission(bprm->file, MAY_READ))
155 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
156
157 allow_write_access(bprm->file);
158 bprm->file = NULL;
159
160 /* mark the bprm that fd should be passed to interp */
161 bprm->interp_flags |= BINPRM_FLAGS_EXECFD;
162 bprm->interp_data = fd_binary;
163
164 } else {
165 allow_write_access(bprm->file);
166 fput(bprm->file);
167 bprm->file = NULL;
168 }
169 /* make argv[1] be the path to the binary */
170 retval = copy_strings_kernel (1, &bprm->interp, bprm);
171 if (retval < 0)
172 goto _error;
173 bprm->argc++;
174
175 /* add the interp as argv[0] */
176 retval = copy_strings_kernel (1, &iname_addr, bprm);
177 if (retval < 0)
178 goto _error;
179 bprm->argc ++;
180
181 bprm->interp = iname; /* for binfmt_script */
182
183 interp_file = open_exec (iname);
184 retval = PTR_ERR (interp_file);
185 if (IS_ERR (interp_file))
186 goto _error;
187
188 bprm->file = interp_file;
189 if (fmt->flags & MISC_FMT_CREDENTIALS) {
190 /*
191 * No need to call prepare_binprm(), it's already been
192 * done. bprm->buf is stale, update from interp_file.
193 */
194 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
195 retval = kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
196 } else
197 retval = prepare_binprm (bprm);
198
199 if (retval < 0)
200 goto _error;
201
202 retval = search_binary_handler (bprm, regs);
203 if (retval < 0)
204 goto _error;
205
206 if (files) {
207 steal_locks(files);
208 put_files_struct(files);
209 files = NULL;
210 }
211 _ret:
212 return retval;
213 _error:
214 if (fd_binary > 0)
215 sys_close(fd_binary);
216 bprm->interp_flags = 0;
217 bprm->interp_data = 0;
218 _unshare:
219 if (files) {
220 put_files_struct(current->files);
221 current->files = files;
222 }
223 goto _ret;
224 }
225
226 /* Command parsers */
227
228 /*
229 * parses and copies one argument enclosed in del from *sp to *dp,
230 * recognising the \x special.
231 * returns pointer to the copied argument or NULL in case of an
232 * error (and sets err) or null argument length.
233 */
234 static char *scanarg(char *s, char del)
235 {
236 char c;
237
238 while ((c = *s++) != del) {
239 if (c == '\\' && *s == 'x') {
240 s++;
241 if (!isxdigit(*s++))
242 return NULL;
243 if (!isxdigit(*s++))
244 return NULL;
245 }
246 }
247 return s;
248 }
249
250 static int unquote(char *from)
251 {
252 char c = 0, *s = from, *p = from;
253
254 while ((c = *s++) != '\0') {
255 if (c == '\\' && *s == 'x') {
256 s++;
257 c = toupper(*s++);
258 *p = (c - (isdigit(c) ? '0' : 'A' - 10)) << 4;
259 c = toupper(*s++);
260 *p++ |= c - (isdigit(c) ? '0' : 'A' - 10);
261 continue;
262 }
263 *p++ = c;
264 }
265 return p - from;
266 }
267
268 static char * check_special_flags (char * sfs, Node * e)
269 {
270 char * p = sfs;
271 int cont = 1;
272
273 /* special flags */
274 while (cont) {
275 switch (*p) {
276 case 'P':
277 p++;
278 e->flags |= MISC_FMT_PRESERVE_ARGV0;
279 break;
280 case 'O':
281 p++;
282 e->flags |= MISC_FMT_OPEN_BINARY;
283 break;
284 case 'C':
285 p++;
286 /* this flags also implies the
287 open-binary flag */
288 e->flags |= (MISC_FMT_CREDENTIALS |
289 MISC_FMT_OPEN_BINARY);
290 break;
291 default:
292 cont = 0;
293 }
294 }
295
296 return p;
297 }
298 /*
299 * This registers a new binary format, it recognises the syntax
300 * ':name:type:offset:magic:mask:interpreter:flags'
301 * where the ':' is the IFS, that can be chosen with the first char
302 */
303 static Node *create_entry(const char __user *buffer, size_t count)
304 {
305 Node *e;
306 int memsize, err;
307 char *buf, *p;
308 char del;
309
310 /* some sanity checks */
311 err = -EINVAL;
312 if ((count < 11) || (count > 256))
313 goto out;
314
315 err = -ENOMEM;
316 memsize = sizeof(Node) + count + 8;
317 e = (Node *) kmalloc(memsize, GFP_USER);
318 if (!e)
319 goto out;
320
321 p = buf = (char *)e + sizeof(Node);
322
323 memset(e, 0, sizeof(Node));
324 if (copy_from_user(buf, buffer, count))
325 goto Efault;
326
327 del = *p++; /* delimeter */
328
329 memset(buf+count, del, 8);
330
331 e->name = p;
332 p = strchr(p, del);
333 if (!p)
334 goto Einval;
335 *p++ = '\0';
336 if (!e->name[0] ||
337 !strcmp(e->name, ".") ||
338 !strcmp(e->name, "..") ||
339 strchr(e->name, '/'))
340 goto Einval;
341 switch (*p++) {
342 case 'E': e->flags = 1<<Enabled; break;
343 case 'M': e->flags = (1<<Enabled) | (1<<Magic); break;
344 default: goto Einval;
345 }
346 if (*p++ != del)
347 goto Einval;
348 if (test_bit(Magic, &e->flags)) {
349 char *s = strchr(p, del);
350 if (!s)
351 goto Einval;
352 *s++ = '\0';
353 e->offset = simple_strtoul(p, &p, 10);
354 if (*p++)
355 goto Einval;
356 e->magic = p;
357 p = scanarg(p, del);
358 if (!p)
359 goto Einval;
360 p[-1] = '\0';
361 if (!e->magic[0])
362 goto Einval;
363 e->mask = p;
364 p = scanarg(p, del);
365 if (!p)
366 goto Einval;
367 p[-1] = '\0';
368 if (!e->mask[0])
369 e->mask = NULL;
370 e->size = unquote(e->magic);
371 if (e->mask && unquote(e->mask) != e->size)
372 goto Einval;
373 if (e->size + e->offset > BINPRM_BUF_SIZE)
374 goto Einval;
375 } else {
376 p = strchr(p, del);
377 if (!p)
378 goto Einval;
379 *p++ = '\0';
380 e->magic = p;
381 p = strchr(p, del);
382 if (!p)
383 goto Einval;
384 *p++ = '\0';
385 if (!e->magic[0] || strchr(e->magic, '/'))
386 goto Einval;
387 p = strchr(p, del);
388 if (!p)
389 goto Einval;
390 *p++ = '\0';
391 }
392 e->interpreter = p;
393 p = strchr(p, del);
394 if (!p)
395 goto Einval;
396 *p++ = '\0';
397 if (!e->interpreter[0])
398 goto Einval;
399
400
401 p = check_special_flags (p, e);
402
403 if (*p == '\n')
404 p++;
405 if (p != buf + count)
406 goto Einval;
407 return e;
408
409 out:
410 return ERR_PTR(err);
411
412 Efault:
413 kfree(e);
414 return ERR_PTR(-EFAULT);
415 Einval:
416 kfree(e);
417 return ERR_PTR(-EINVAL);
418 }
419
420 /*
421 * Set status of entry/binfmt_misc:
422 * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
423 */
424 static int parse_command(const char __user *buffer, size_t count)
425 {
426 char s[4];
427
428 if (!count)
429 return 0;
430 if (count > 3)
431 return -EINVAL;
432 if (copy_from_user(s, buffer, count))
433 return -EFAULT;
434 if (s[count-1] == '\n')
435 count--;
436 if (count == 1 && s[0] == '0')
437 return 1;
438 if (count == 1 && s[0] == '1')
439 return 2;
440 if (count == 2 && s[0] == '-' && s[1] == '1')
441 return 3;
442 return -EINVAL;
443 }
444
445 /* generic stuff */
446
447 static void entry_status(Node *e, char *page)
448 {
449 char *dp;
450 char *status = "disabled";
451 const char * flags = "flags: ";
452
453 if (test_bit(Enabled, &e->flags))
454 status = "enabled";
455
456 if (!VERBOSE_STATUS) {
457 sprintf(page, "%s\n", status);
458 return;
459 }
460
461 sprintf(page, "%s\ninterpreter %s\n", status, e->interpreter);
462 dp = page + strlen(page);
463
464 /* print the special flags */
465 sprintf (dp, "%s", flags);
466 dp += strlen (flags);
467 if (e->flags & MISC_FMT_PRESERVE_ARGV0) {
468 *dp ++ = 'P';
469 }
470 if (e->flags & MISC_FMT_OPEN_BINARY) {
471 *dp ++ = 'O';
472 }
473 if (e->flags & MISC_FMT_CREDENTIALS) {
474 *dp ++ = 'C';
475 }
476 *dp ++ = '\n';
477
478
479 if (!test_bit(Magic, &e->flags)) {
480 sprintf(dp, "extension .%s\n", e->magic);
481 } else {
482 int i;
483
484 sprintf(dp, "offset %i\nmagic ", e->offset);
485 dp = page + strlen(page);
486 for (i = 0; i < e->size; i++) {
487 sprintf(dp, "%02x", 0xff & (int) (e->magic[i]));
488 dp += 2;
489 }
490 if (e->mask) {
491 sprintf(dp, "\nmask ");
492 dp += 6;
493 for (i = 0; i < e->size; i++) {
494 sprintf(dp, "%02x", 0xff & (int) (e->mask[i]));
495 dp += 2;
496 }
497 }
498 *dp++ = '\n';
499 *dp = '\0';
500 }
501 }
502
503 static struct inode *bm_get_inode(struct super_block *sb, int mode)
504 {
505 struct inode * inode = new_inode(sb);
506
507 if (inode) {
508 inode->i_mode = mode;
509 inode->i_uid = 0;
510 inode->i_gid = 0;
511 inode->i_blksize = PAGE_CACHE_SIZE;
512 inode->i_blocks = 0;
513 inode->i_atime = inode->i_mtime = inode->i_ctime =
514 current_fs_time(inode->i_sb);
515 }
516 return inode;
517 }
518
519 static void bm_clear_inode(struct inode *inode)
520 {
521 kfree(inode->u.generic_ip);
522 }
523
524 static void kill_node(Node *e)
525 {
526 struct dentry *dentry;
527
528 write_lock(&entries_lock);
529 dentry = e->dentry;
530 if (dentry) {
531 list_del_init(&e->list);
532 e->dentry = NULL;
533 }
534 write_unlock(&entries_lock);
535
536 if (dentry) {
537 dentry->d_inode->i_nlink--;
538 d_drop(dentry);
539 dput(dentry);
540 simple_release_fs(&bm_mnt, &entry_count);
541 }
542 }
543
544 /* /<entry> */
545
546 static ssize_t
547 bm_entry_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos)
548 {
549 Node *e = file->f_dentry->d_inode->u.generic_ip;
550 loff_t pos = *ppos;
551 ssize_t res;
552 char *page;
553 int len;
554
555 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
556 return -ENOMEM;
557
558 entry_status(e, page);
559 len = strlen(page);
560
561 res = -EINVAL;
562 if (pos < 0)
563 goto out;
564 res = 0;
565 if (pos >= len)
566 goto out;
567 if (len < pos + nbytes)
568 nbytes = len - pos;
569 res = -EFAULT;
570 if (copy_to_user(buf, page + pos, nbytes))
571 goto out;
572 *ppos = pos + nbytes;
573 res = nbytes;
574 out:
575 free_page((unsigned long) page);
576 return res;
577 }
578
579 static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
580 size_t count, loff_t *ppos)
581 {
582 struct dentry *root;
583 Node *e = file->f_dentry->d_inode->u.generic_ip;
584 int res = parse_command(buffer, count);
585
586 switch (res) {
587 case 1: clear_bit(Enabled, &e->flags);
588 break;
589 case 2: set_bit(Enabled, &e->flags);
590 break;
591 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
592 mutex_lock(&root->d_inode->i_mutex);
593
594 kill_node(e);
595
596 mutex_unlock(&root->d_inode->i_mutex);
597 dput(root);
598 break;
599 default: return res;
600 }
601 return count;
602 }
603
604 static const struct file_operations bm_entry_operations = {
605 .read = bm_entry_read,
606 .write = bm_entry_write,
607 };
608
609 /* /register */
610
611 static ssize_t bm_register_write(struct file *file, const char __user *buffer,
612 size_t count, loff_t *ppos)
613 {
614 Node *e;
615 struct inode *inode;
616 struct dentry *root, *dentry;
617 struct super_block *sb = file->f_vfsmnt->mnt_sb;
618 int err = 0;
619
620 e = create_entry(buffer, count);
621
622 if (IS_ERR(e))
623 return PTR_ERR(e);
624
625 root = dget(sb->s_root);
626 mutex_lock(&root->d_inode->i_mutex);
627 dentry = lookup_one_len(e->name, root, strlen(e->name));
628 err = PTR_ERR(dentry);
629 if (IS_ERR(dentry))
630 goto out;
631
632 err = -EEXIST;
633 if (dentry->d_inode)
634 goto out2;
635
636 inode = bm_get_inode(sb, S_IFREG | 0644);
637
638 err = -ENOMEM;
639 if (!inode)
640 goto out2;
641
642 err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
643 if (err) {
644 iput(inode);
645 inode = NULL;
646 goto out2;
647 }
648
649 e->dentry = dget(dentry);
650 inode->u.generic_ip = e;
651 inode->i_fop = &bm_entry_operations;
652
653 d_instantiate(dentry, inode);
654 write_lock(&entries_lock);
655 list_add(&e->list, &entries);
656 write_unlock(&entries_lock);
657
658 err = 0;
659 out2:
660 dput(dentry);
661 out:
662 mutex_unlock(&root->d_inode->i_mutex);
663 dput(root);
664
665 if (err) {
666 kfree(e);
667 return -EINVAL;
668 }
669 return count;
670 }
671
672 static const struct file_operations bm_register_operations = {
673 .write = bm_register_write,
674 };
675
676 /* /status */
677
678 static ssize_t
679 bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
680 {
681 char *s = enabled ? "enabled" : "disabled";
682 int len = strlen(s);
683 loff_t pos = *ppos;
684
685 if (pos < 0)
686 return -EINVAL;
687 if (pos >= len)
688 return 0;
689 if (len < pos + nbytes)
690 nbytes = len - pos;
691 if (copy_to_user(buf, s + pos, nbytes))
692 return -EFAULT;
693 *ppos = pos + nbytes;
694 return nbytes;
695 }
696
697 static ssize_t bm_status_write(struct file * file, const char __user * buffer,
698 size_t count, loff_t *ppos)
699 {
700 int res = parse_command(buffer, count);
701 struct dentry *root;
702
703 switch (res) {
704 case 1: enabled = 0; break;
705 case 2: enabled = 1; break;
706 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
707 mutex_lock(&root->d_inode->i_mutex);
708
709 while (!list_empty(&entries))
710 kill_node(list_entry(entries.next, Node, list));
711
712 mutex_unlock(&root->d_inode->i_mutex);
713 dput(root);
714 default: return res;
715 }
716 return count;
717 }
718
719 static const struct file_operations bm_status_operations = {
720 .read = bm_status_read,
721 .write = bm_status_write,
722 };
723
724 /* Superblock handling */
725
726 static struct super_operations s_ops = {
727 .statfs = simple_statfs,
728 .clear_inode = bm_clear_inode,
729 };
730
731 static int bm_fill_super(struct super_block * sb, void * data, int silent)
732 {
733 static struct tree_descr bm_files[] = {
734 [1] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
735 [2] = {"register", &bm_register_operations, S_IWUSR},
736 /* last one */ {""}
737 };
738 int err = simple_fill_super(sb, 0x42494e4d, bm_files);
739 if (!err)
740 sb->s_op = &s_ops;
741 return err;
742 }
743
744 static struct super_block *bm_get_sb(struct file_system_type *fs_type,
745 int flags, const char *dev_name, void *data)
746 {
747 return get_sb_single(fs_type, flags, data, bm_fill_super);
748 }
749
750 static struct linux_binfmt misc_format = {
751 .module = THIS_MODULE,
752 .load_binary = load_misc_binary,
753 };
754
755 static struct file_system_type bm_fs_type = {
756 .owner = THIS_MODULE,
757 .name = "binfmt_misc",
758 .get_sb = bm_get_sb,
759 .kill_sb = kill_litter_super,
760 };
761
762 static int __init init_misc_binfmt(void)
763 {
764 int err = register_filesystem(&bm_fs_type);
765 if (!err) {
766 err = register_binfmt(&misc_format);
767 if (err)
768 unregister_filesystem(&bm_fs_type);
769 }
770 return err;
771 }
772
773 static void __exit exit_misc_binfmt(void)
774 {
775 unregister_binfmt(&misc_format);
776 unregister_filesystem(&bm_fs_type);
777 }
778
779 core_initcall(init_misc_binfmt);
780 module_exit(exit_misc_binfmt);
781 MODULE_LICENSE("GPL");
This page took 0.06361 seconds and 6 git commands to generate.