This commit was manufactured by cvs2svn to create tag
[linux-2.6.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 rwlock_t entries_lock = RW_LOCK_UNLOCKED;
58 static struct vfsmount *bm_mnt;
59 static int entry_count;
60
61 /* 
62  * Check if we support the binfmt
63  * if we do, return the node, else NULL
64  * locking is done in load_misc_binary
65  */
66 static Node *check_file(struct linux_binprm *bprm)
67 {
68         char *p = strrchr(bprm->interp, '.');
69         struct list_head *l;
70
71         list_for_each(l, &entries) {
72                 Node *e = list_entry(l, Node, list);
73                 char *s;
74                 int j;
75
76                 if (!test_bit(Enabled, &e->flags))
77                         continue;
78
79                 if (!test_bit(Magic, &e->flags)) {
80                         if (p && !strcmp(e->magic, p + 1))
81                                 return e;
82                         continue;
83                 }
84
85                 s = bprm->buf + e->offset;
86                 if (e->mask) {
87                         for (j = 0; j < e->size; j++)
88                                 if ((*s++ ^ e->magic[j]) & e->mask[j])
89                                         break;
90                 } else {
91                         for (j = 0; j < e->size; j++)
92                                 if ((*s++ ^ e->magic[j]))
93                                         break;
94                 }
95                 if (j == e->size)
96                         return e;
97         }
98         return NULL;
99 }
100
101 /*
102  * the loader itself
103  */
104 static int load_misc_binary(struct linux_binprm *bprm, struct pt_regs *regs)
105 {
106         Node *fmt;
107         struct file * interp_file = NULL;
108         char iname[BINPRM_BUF_SIZE];
109         char *iname_addr = iname;
110         int retval;
111         int fd_binary = -1;
112         char fd_str[12];
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                 char *fdsp = fd_str;
134
135                 files = current->files;
136                 retval = unshare_files();
137                 if (retval < 0)
138                         goto _ret;
139                 if (files == current->files) {
140                         put_files_struct(files);
141                         files = NULL;
142                 }
143                 /* if the binary should be opened on behalf of the
144                  * interpreter than keep it open and assign descriptor
145                  * to it */
146                 fd_binary = get_unused_fd();
147                 if (fd_binary < 0) {
148                         retval = fd_binary;
149                         goto _unshare;
150                 }
151                 fd_install(fd_binary, bprm->file);
152
153                 /* if the binary is not readable than enforce mm->dumpable=0
154                    regardless of the interpreter's permissions */
155                 if (permission(bprm->file->f_dentry->d_inode, MAY_READ, NULL))
156                         bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
157
158                 allow_write_access(bprm->file);
159                 bprm->file = NULL;
160
161                 /* make argv[1] be the file descriptor of the binary */
162                 snprintf(fd_str, sizeof(fd_str), "%d", fd_binary);
163                 retval = copy_strings_kernel(1, &fdsp, bprm);
164                 if (retval < 0)
165                         goto _error;
166                 bprm->argc++;
167
168         } else {
169                 allow_write_access(bprm->file);
170                 fput(bprm->file);
171                 bprm->file = NULL;
172                 /* make argv[1] be the path to the binary */
173                 retval = copy_strings_kernel (1, &bprm->interp, bprm);
174                 if (retval < 0)
175                         goto _error;
176                 bprm->argc++;
177         }
178         retval = copy_strings_kernel (1, &iname_addr, bprm);
179         if (retval < 0)
180                 goto _error;
181         bprm->argc ++;
182         bprm->interp = iname;   /* for binfmt_script */
183
184         interp_file = open_exec (iname);
185         retval = PTR_ERR (interp_file);
186         if (IS_ERR (interp_file))
187                 goto _error;
188
189         bprm->file = interp_file;
190         if (fmt->flags & MISC_FMT_CREDENTIALS) {
191                 /*
192                  * No need to call prepare_binprm(), it's already been
193                  * done.  bprm->buf is stale, update from interp_file.
194                  */
195                 memset(bprm->buf, 0, BINPRM_BUF_SIZE);
196                 retval = kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
197         } else
198                 retval = prepare_binprm (bprm);
199
200         if (retval < 0)
201                 goto _error;
202
203         retval = search_binary_handler (bprm, regs);
204         if (retval < 0)
205                 goto _error;
206
207         if (files) {
208                 steal_locks(files);
209                 put_files_struct(files);
210                 files = NULL;
211         }
212 _ret:
213         return retval;
214 _error:
215         if (fd_binary > 0)
216                 sys_close(fd_binary);
217         bprm->interp_flags = 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 inline 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 = CURRENT_TIME;
514         }
515         return inode;
516 }
517
518 static void bm_clear_inode(struct inode *inode)
519 {
520         kfree(inode->u.generic_ip);
521 }
522
523 static void kill_node(Node *e)
524 {
525         struct dentry *dentry;
526
527         write_lock(&entries_lock);
528         dentry = e->dentry;
529         if (dentry) {
530                 list_del_init(&e->list);
531                 e->dentry = NULL;
532         }
533         write_unlock(&entries_lock);
534
535         if (dentry) {
536                 dentry->d_inode->i_nlink--;
537                 d_drop(dentry);
538                 dput(dentry);
539                 simple_release_fs(&bm_mnt, &entry_count);
540         }
541 }
542
543 /* /<entry> */
544
545 static ssize_t
546 bm_entry_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos)
547 {
548         Node *e = file->f_dentry->d_inode->u.generic_ip;
549         loff_t pos = *ppos;
550         ssize_t res;
551         char *page;
552         int len;
553
554         if (!(page = (char*) __get_free_page(GFP_KERNEL)))
555                 return -ENOMEM;
556
557         entry_status(e, page);
558         len = strlen(page);
559
560         res = -EINVAL;
561         if (pos < 0)
562                 goto out;
563         res = 0;
564         if (pos >= len)
565                 goto out;
566         if (len < pos + nbytes)
567                 nbytes = len - pos;
568         res = -EFAULT;
569         if (copy_to_user(buf, page + pos, nbytes))
570                 goto out;
571         *ppos = pos + nbytes;
572         res = nbytes;
573 out:
574         free_page((unsigned long) page);
575         return res;
576 }
577
578 static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
579                                 size_t count, loff_t *ppos)
580 {
581         struct dentry *root;
582         Node *e = file->f_dentry->d_inode->u.generic_ip;
583         int res = parse_command(buffer, count);
584
585         switch (res) {
586                 case 1: clear_bit(Enabled, &e->flags);
587                         break;
588                 case 2: set_bit(Enabled, &e->flags);
589                         break;
590                 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
591                         down(&root->d_inode->i_sem);
592
593                         kill_node(e);
594
595                         up(&root->d_inode->i_sem);
596                         dput(root);
597                         break;
598                 default: return res;
599         }
600         return count;
601 }
602
603 static struct file_operations bm_entry_operations = {
604         .read           = bm_entry_read,
605         .write          = bm_entry_write,
606 };
607
608 /* /register */
609
610 static ssize_t bm_register_write(struct file *file, const char __user *buffer,
611                                size_t count, loff_t *ppos)
612 {
613         Node *e;
614         struct inode *inode;
615         struct dentry *root, *dentry;
616         struct super_block *sb = file->f_vfsmnt->mnt_sb;
617         int err = 0;
618
619         e = create_entry(buffer, count);
620
621         if (IS_ERR(e))
622                 return PTR_ERR(e);
623
624         root = dget(sb->s_root);
625         down(&root->d_inode->i_sem);
626         dentry = lookup_one_len(e->name, root, strlen(e->name));
627         err = PTR_ERR(dentry);
628         if (IS_ERR(dentry))
629                 goto out;
630
631         err = -EEXIST;
632         if (dentry->d_inode)
633                 goto out2;
634
635         inode = bm_get_inode(sb, S_IFREG | 0644);
636
637         err = -ENOMEM;
638         if (!inode)
639                 goto out2;
640
641         err = simple_pin_fs("binfmt_misc", &bm_mnt, &entry_count);
642         if (err) {
643                 iput(inode);
644                 inode = NULL;
645                 goto out2;
646         }
647
648         e->dentry = dget(dentry);
649         inode->u.generic_ip = e;
650         inode->i_fop = &bm_entry_operations;
651
652         d_instantiate(dentry, inode);
653         write_lock(&entries_lock);
654         list_add(&e->list, &entries);
655         write_unlock(&entries_lock);
656
657         err = 0;
658 out2:
659         dput(dentry);
660 out:
661         up(&root->d_inode->i_sem);
662         dput(root);
663
664         if (err) {
665                 kfree(e);
666                 return -EINVAL;
667         }
668         return count;
669 }
670
671 static struct file_operations bm_register_operations = {
672         .write          = bm_register_write,
673 };
674
675 /* /status */
676
677 static ssize_t
678 bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
679 {
680         char *s = enabled ? "enabled" : "disabled";
681         int len = strlen(s);
682         loff_t pos = *ppos;
683
684         if (pos < 0)
685                 return -EINVAL;
686         if (pos >= len)
687                 return 0;
688         if (len < pos + nbytes)
689                 nbytes = len - pos;
690         if (copy_to_user(buf, s + pos, nbytes))
691                 return -EFAULT;
692         *ppos = pos + nbytes;
693         return nbytes;
694 }
695
696 static ssize_t bm_status_write(struct file * file, const char __user * buffer,
697                 size_t count, loff_t *ppos)
698 {
699         int res = parse_command(buffer, count);
700         struct dentry *root;
701
702         switch (res) {
703                 case 1: enabled = 0; break;
704                 case 2: enabled = 1; break;
705                 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
706                         down(&root->d_inode->i_sem);
707
708                         while (!list_empty(&entries))
709                                 kill_node(list_entry(entries.next, Node, list));
710
711                         up(&root->d_inode->i_sem);
712                         dput(root);
713                 default: return res;
714         }
715         return count;
716 }
717
718 static struct file_operations bm_status_operations = {
719         .read           = bm_status_read,
720         .write          = bm_status_write,
721 };
722
723 /* Superblock handling */
724
725 static struct super_operations s_ops = {
726         .statfs         = simple_statfs,
727         .clear_inode    = bm_clear_inode,
728 };
729
730 static int bm_fill_super(struct super_block * sb, void * data, int silent)
731 {
732         static struct tree_descr bm_files[] = {
733                 [1] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
734                 [2] = {"register", &bm_register_operations, S_IWUSR},
735                 /* last one */ {""}
736         };
737         int err = simple_fill_super(sb, 0x42494e4d, bm_files);
738         if (!err)
739                 sb->s_op = &s_ops;
740         return err;
741 }
742
743 static struct super_block *bm_get_sb(struct file_system_type *fs_type,
744         int flags, const char *dev_name, void *data)
745 {
746         return get_sb_single(fs_type, flags, data, bm_fill_super);
747 }
748
749 static struct linux_binfmt misc_format = {
750         .module = THIS_MODULE,
751         .load_binary = load_misc_binary,
752 };
753
754 static struct file_system_type bm_fs_type = {
755         .owner          = THIS_MODULE,
756         .name           = "binfmt_misc",
757         .get_sb         = bm_get_sb,
758         .kill_sb        = kill_litter_super,
759 };
760
761 static int __init init_misc_binfmt(void)
762 {
763         int err = register_filesystem(&bm_fs_type);
764         if (!err) {
765                 err = register_binfmt(&misc_format);
766                 if (err)
767                         unregister_filesystem(&bm_fs_type);
768         }
769         return err;
770 }
771
772 static void __exit exit_misc_binfmt(void)
773 {
774         unregister_binfmt(&misc_format);
775         unregister_filesystem(&bm_fs_type);
776 }
777
778 core_initcall(init_misc_binfmt);
779 module_exit(exit_misc_binfmt);
780 MODULE_LICENSE("GPL");