KVM: SVM: disable CR8 intercept when tpr is not masking interrupts
[linux-2.6/zen-sources.git] / fs / binfmt_misc.c
blobdbf0ac0523de14d49edcc58b43ab7c62a407e406
1 /*
2 * binfmt_misc.c
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
8 * binfmt_mz.
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.
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>
33 enum {
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)
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;
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;
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
67 static Node *check_file(struct linux_binprm *bprm)
69 char *p = strrchr(bprm->interp, '.');
70 struct list_head *l;
72 list_for_each(l, &entries) {
73 Node *e = list_entry(l, Node, list);
74 char *s;
75 int j;
77 if (!test_bit(Enabled, &e->flags))
78 continue;
80 if (!test_bit(Magic, &e->flags)) {
81 if (p && !strcmp(e->magic, p + 1))
82 return e;
83 continue;
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;
96 if (j == e->size)
97 return e;
99 return NULL;
103 * the loader itself
105 static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
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;
114 retval = -ENOEXEC;
115 if (!enabled)
116 goto _ret;
118 /* to keep locking time low, we copy the interpreter string */
119 read_lock(&entries_lock);
120 fmt = check_file(bprm);
121 if (fmt)
122 strlcpy(iname, fmt->interpreter, BINPRM_BUF_SIZE);
123 read_unlock(&entries_lock);
124 if (!fmt)
125 goto _ret;
127 if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
128 retval = remove_arg_zero(bprm);
129 if (retval)
130 goto _ret;
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
137 * to it */
138 fd_binary = get_unused_fd();
139 if (fd_binary < 0) {
140 retval = fd_binary;
141 goto _ret;
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);
151 bprm->file = NULL;
153 /* mark the bprm that fd should be passed to interp */
154 bprm->interp_flags |= BINPRM_FLAGS_EXECFD;
155 bprm->interp_data = fd_binary;
157 } else {
158 allow_write_access(bprm->file);
159 fput(bprm->file);
160 bprm->file = NULL;
162 /* make argv[1] be the path to the binary */
163 retval = copy_strings_kernel (1, &bprm->interp, bprm);
164 if (retval < 0)
165 goto _error;
166 bprm->argc++;
168 /* add the interp as argv[0] */
169 retval = copy_strings_kernel (1, &iname_addr, bprm);
170 if (retval < 0)
171 goto _error;
172 bprm->argc ++;
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))
179 goto _error;
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);
189 } else
190 retval = prepare_binprm (bprm);
192 if (retval < 0)
193 goto _error;
195 retval = search_binary_handler (bprm, regs);
196 if (retval < 0)
197 goto _error;
199 _ret:
200 return retval;
201 _error:
202 if (fd_binary > 0)
203 sys_close(fd_binary);
204 bprm->interp_flags = 0;
205 bprm->interp_data = 0;
206 goto _ret;
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)
219 char c;
221 while ((c = *s++) != del) {
222 if (c == '\\' && *s == 'x') {
223 s++;
224 if (!isxdigit(*s++))
225 return NULL;
226 if (!isxdigit(*s++))
227 return NULL;
230 return s;
233 static int unquote(char *from)
235 char c = 0, *s = from, *p = from;
237 while ((c = *s++) != '\0') {
238 if (c == '\\' && *s == 'x') {
239 s++;
240 c = toupper(*s++);
241 *p = (c - (isdigit(c) ? '0' : 'A' - 10)) << 4;
242 c = toupper(*s++);
243 *p++ |= c - (isdigit(c) ? '0' : 'A' - 10);
244 continue;
246 *p++ = c;
248 return p - from;
251 static char * check_special_flags (char * sfs, Node * e)
253 char * p = sfs;
254 int cont = 1;
256 /* special flags */
257 while (cont) {
258 switch (*p) {
259 case 'P':
260 p++;
261 e->flags |= MISC_FMT_PRESERVE_ARGV0;
262 break;
263 case 'O':
264 p++;
265 e->flags |= MISC_FMT_OPEN_BINARY;
266 break;
267 case 'C':
268 p++;
269 /* this flags also implies the
270 open-binary flag */
271 e->flags |= (MISC_FMT_CREDENTIALS |
272 MISC_FMT_OPEN_BINARY);
273 break;
274 default:
275 cont = 0;
279 return p;
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)
288 Node *e;
289 int memsize, err;
290 char *buf, *p;
291 char del;
293 /* some sanity checks */
294 err = -EINVAL;
295 if ((count < 11) || (count > 256))
296 goto out;
298 err = -ENOMEM;
299 memsize = sizeof(Node) + count + 8;
300 e = kmalloc(memsize, GFP_USER);
301 if (!e)
302 goto out;
304 p = buf = (char *)e + sizeof(Node);
306 memset(e, 0, sizeof(Node));
307 if (copy_from_user(buf, buffer, count))
308 goto Efault;
310 del = *p++; /* delimeter */
312 memset(buf+count, del, 8);
314 e->name = p;
315 p = strchr(p, del);
316 if (!p)
317 goto Einval;
318 *p++ = '\0';
319 if (!e->name[0] ||
320 !strcmp(e->name, ".") ||
321 !strcmp(e->name, "..") ||
322 strchr(e->name, '/'))
323 goto Einval;
324 switch (*p++) {
325 case 'E': e->flags = 1<<Enabled; break;
326 case 'M': e->flags = (1<<Enabled) | (1<<Magic); break;
327 default: goto Einval;
329 if (*p++ != del)
330 goto Einval;
331 if (test_bit(Magic, &e->flags)) {
332 char *s = strchr(p, del);
333 if (!s)
334 goto Einval;
335 *s++ = '\0';
336 e->offset = simple_strtoul(p, &p, 10);
337 if (*p++)
338 goto Einval;
339 e->magic = p;
340 p = scanarg(p, del);
341 if (!p)
342 goto Einval;
343 p[-1] = '\0';
344 if (!e->magic[0])
345 goto Einval;
346 e->mask = p;
347 p = scanarg(p, del);
348 if (!p)
349 goto Einval;
350 p[-1] = '\0';
351 if (!e->mask[0])
352 e->mask = NULL;
353 e->size = unquote(e->magic);
354 if (e->mask && unquote(e->mask) != e->size)
355 goto Einval;
356 if (e->size + e->offset > BINPRM_BUF_SIZE)
357 goto Einval;
358 } else {
359 p = strchr(p, del);
360 if (!p)
361 goto Einval;
362 *p++ = '\0';
363 e->magic = p;
364 p = strchr(p, del);
365 if (!p)
366 goto Einval;
367 *p++ = '\0';
368 if (!e->magic[0] || strchr(e->magic, '/'))
369 goto Einval;
370 p = strchr(p, del);
371 if (!p)
372 goto Einval;
373 *p++ = '\0';
375 e->interpreter = p;
376 p = strchr(p, del);
377 if (!p)
378 goto Einval;
379 *p++ = '\0';
380 if (!e->interpreter[0])
381 goto Einval;
384 p = check_special_flags (p, e);
386 if (*p == '\n')
387 p++;
388 if (p != buf + count)
389 goto Einval;
390 return e;
392 out:
393 return ERR_PTR(err);
395 Efault:
396 kfree(e);
397 return ERR_PTR(-EFAULT);
398 Einval:
399 kfree(e);
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)
409 char s[4];
411 if (!count)
412 return 0;
413 if (count > 3)
414 return -EINVAL;
415 if (copy_from_user(s, buffer, count))
416 return -EFAULT;
417 if (s[count-1] == '\n')
418 count--;
419 if (count == 1 && s[0] == '0')
420 return 1;
421 if (count == 1 && s[0] == '1')
422 return 2;
423 if (count == 2 && s[0] == '-' && s[1] == '1')
424 return 3;
425 return -EINVAL;
428 /* generic stuff */
430 static void entry_status(Node *e, char *page)
432 char *dp;
433 char *status = "disabled";
434 const char * flags = "flags: ";
436 if (test_bit(Enabled, &e->flags))
437 status = "enabled";
439 if (!VERBOSE_STATUS) {
440 sprintf(page, "%s\n", status);
441 return;
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) {
451 *dp ++ = 'P';
453 if (e->flags & MISC_FMT_OPEN_BINARY) {
454 *dp ++ = 'O';
456 if (e->flags & MISC_FMT_CREDENTIALS) {
457 *dp ++ = 'C';
459 *dp ++ = '\n';
462 if (!test_bit(Magic, &e->flags)) {
463 sprintf(dp, "extension .%s\n", e->magic);
464 } else {
465 int i;
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]));
471 dp += 2;
473 if (e->mask) {
474 sprintf(dp, "\nmask ");
475 dp += 6;
476 for (i = 0; i < e->size; i++) {
477 sprintf(dp, "%02x", 0xff & (int) (e->mask[i]));
478 dp += 2;
481 *dp++ = '\n';
482 *dp = '\0';
486 static struct inode *bm_get_inode(struct super_block *sb, int mode)
488 struct inode * inode = new_inode(sb);
490 if (inode) {
491 inode->i_mode = mode;
492 inode->i_uid = 0;
493 inode->i_gid = 0;
494 inode->i_blocks = 0;
495 inode->i_atime = inode->i_mtime = inode->i_ctime =
496 current_fs_time(inode->i_sb);
498 return inode;
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);
511 dentry = e->dentry;
512 if (dentry) {
513 list_del_init(&e->list);
514 e->dentry = NULL;
516 write_unlock(&entries_lock);
518 if (dentry) {
519 dentry->d_inode->i_nlink--;
520 d_drop(dentry);
521 dput(dentry);
522 simple_release_fs(&bm_mnt, &entry_count);
526 /* /<entry> */
528 static ssize_t
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;
532 loff_t pos = *ppos;
533 ssize_t res;
534 char *page;
535 int len;
537 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
538 return -ENOMEM;
540 entry_status(e, page);
541 len = strlen(page);
543 res = -EINVAL;
544 if (pos < 0)
545 goto out;
546 res = 0;
547 if (pos >= len)
548 goto out;
549 if (len < pos + nbytes)
550 nbytes = len - pos;
551 res = -EFAULT;
552 if (copy_to_user(buf, page + pos, nbytes))
553 goto out;
554 *ppos = pos + nbytes;
555 res = nbytes;
556 out:
557 free_page((unsigned long) page);
558 return res;
561 static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
562 size_t count, loff_t *ppos)
564 struct dentry *root;
565 Node *e = file->f_path.dentry->d_inode->i_private;
566 int res = parse_command(buffer, count);
568 switch (res) {
569 case 1: clear_bit(Enabled, &e->flags);
570 break;
571 case 2: set_bit(Enabled, &e->flags);
572 break;
573 case 3: root = dget(file->f_path.mnt->mnt_sb->s_root);
574 mutex_lock(&root->d_inode->i_mutex);
576 kill_node(e);
578 mutex_unlock(&root->d_inode->i_mutex);
579 dput(root);
580 break;
581 default: return res;
583 return count;
586 static const struct file_operations bm_entry_operations = {
587 .read = bm_entry_read,
588 .write = bm_entry_write,
591 /* /register */
593 static ssize_t bm_register_write(struct file *file, const char __user *buffer,
594 size_t count, loff_t *ppos)
596 Node *e;
597 struct inode *inode;
598 struct dentry *root, *dentry;
599 struct super_block *sb = file->f_path.mnt->mnt_sb;
600 int err = 0;
602 e = create_entry(buffer, count);
604 if (IS_ERR(e))
605 return PTR_ERR(e);
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);
611 if (IS_ERR(dentry))
612 goto out;
614 err = -EEXIST;
615 if (dentry->d_inode)
616 goto out2;
618 inode = bm_get_inode(sb, S_IFREG | 0644);
620 err = -ENOMEM;
621 if (!inode)
622 goto out2;
624 err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
625 if (err) {
626 iput(inode);
627 inode = NULL;
628 goto out2;
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);
640 err = 0;
641 out2:
642 dput(dentry);
643 out:
644 mutex_unlock(&root->d_inode->i_mutex);
645 dput(root);
647 if (err) {
648 kfree(e);
649 return -EINVAL;
651 return count;
654 static const struct file_operations bm_register_operations = {
655 .write = bm_register_write,
658 /* /status */
660 static ssize_t
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);
672 struct dentry *root;
674 switch (res) {
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);
684 dput(root);
685 default: return res;
687 return count;
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},
707 /* last one */ {""}
709 int err = simple_fill_super(sb, 0x42494e4d, bm_files);
710 if (!err)
711 sb->s_op = &s_ops;
712 return err;
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",
729 .get_sb = bm_get_sb,
730 .kill_sb = kill_litter_super,
733 static int __init init_misc_binfmt(void)
735 int err = register_filesystem(&bm_fs_type);
736 if (!err) {
737 err = register_binfmt(&misc_format);
738 if (err)
739 unregister_filesystem(&bm_fs_type);
741 return err;
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");