Merge branch 'master' into gfs2
[pandora-kernel.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                 put_files_struct(files);
208                 files = NULL;
209         }
210 _ret:
211         return retval;
212 _error:
213         if (fd_binary > 0)
214                 sys_close(fd_binary);
215         bprm->interp_flags = 0;
216         bprm->interp_data = 0;
217 _unshare:
218         if (files) {
219                 put_files_struct(current->files);
220                 current->files = files;
221         }
222         goto _ret;
223 }
224
225 /* Command parsers */
226
227 /*
228  * parses and copies one argument enclosed in del from *sp to *dp,
229  * recognising the \x special.
230  * returns pointer to the copied argument or NULL in case of an
231  * error (and sets err) or null argument length.
232  */
233 static char *scanarg(char *s, char del)
234 {
235         char c;
236
237         while ((c = *s++) != del) {
238                 if (c == '\\' && *s == 'x') {
239                         s++;
240                         if (!isxdigit(*s++))
241                                 return NULL;
242                         if (!isxdigit(*s++))
243                                 return NULL;
244                 }
245         }
246         return s;
247 }
248
249 static int unquote(char *from)
250 {
251         char c = 0, *s = from, *p = from;
252
253         while ((c = *s++) != '\0') {
254                 if (c == '\\' && *s == 'x') {
255                         s++;
256                         c = toupper(*s++);
257                         *p = (c - (isdigit(c) ? '0' : 'A' - 10)) << 4;
258                         c = toupper(*s++);
259                         *p++ |= c - (isdigit(c) ? '0' : 'A' - 10);
260                         continue;
261                 }
262                 *p++ = c;
263         }
264         return p - from;
265 }
266
267 static char * check_special_flags (char * sfs, Node * e)
268 {
269         char * p = sfs;
270         int cont = 1;
271
272         /* special flags */
273         while (cont) {
274                 switch (*p) {
275                         case 'P':
276                                 p++;
277                                 e->flags |= MISC_FMT_PRESERVE_ARGV0;
278                                 break;
279                         case 'O':
280                                 p++;
281                                 e->flags |= MISC_FMT_OPEN_BINARY;
282                                 break;
283                         case 'C':
284                                 p++;
285                                 /* this flags also implies the
286                                    open-binary flag */
287                                 e->flags |= (MISC_FMT_CREDENTIALS |
288                                                 MISC_FMT_OPEN_BINARY);
289                                 break;
290                         default:
291                                 cont = 0;
292                 }
293         }
294
295         return p;
296 }
297 /*
298  * This registers a new binary format, it recognises the syntax
299  * ':name:type:offset:magic:mask:interpreter:flags'
300  * where the ':' is the IFS, that can be chosen with the first char
301  */
302 static Node *create_entry(const char __user *buffer, size_t count)
303 {
304         Node *e;
305         int memsize, err;
306         char *buf, *p;
307         char del;
308
309         /* some sanity checks */
310         err = -EINVAL;
311         if ((count < 11) || (count > 256))
312                 goto out;
313
314         err = -ENOMEM;
315         memsize = sizeof(Node) + count + 8;
316         e = (Node *) kmalloc(memsize, GFP_USER);
317         if (!e)
318                 goto out;
319
320         p = buf = (char *)e + sizeof(Node);
321
322         memset(e, 0, sizeof(Node));
323         if (copy_from_user(buf, buffer, count))
324                 goto Efault;
325
326         del = *p++;     /* delimeter */
327
328         memset(buf+count, del, 8);
329
330         e->name = p;
331         p = strchr(p, del);
332         if (!p)
333                 goto Einval;
334         *p++ = '\0';
335         if (!e->name[0] ||
336             !strcmp(e->name, ".") ||
337             !strcmp(e->name, "..") ||
338             strchr(e->name, '/'))
339                 goto Einval;
340         switch (*p++) {
341                 case 'E': e->flags = 1<<Enabled; break;
342                 case 'M': e->flags = (1<<Enabled) | (1<<Magic); break;
343                 default: goto Einval;
344         }
345         if (*p++ != del)
346                 goto Einval;
347         if (test_bit(Magic, &e->flags)) {
348                 char *s = strchr(p, del);
349                 if (!s)
350                         goto Einval;
351                 *s++ = '\0';
352                 e->offset = simple_strtoul(p, &p, 10);
353                 if (*p++)
354                         goto Einval;
355                 e->magic = p;
356                 p = scanarg(p, del);
357                 if (!p)
358                         goto Einval;
359                 p[-1] = '\0';
360                 if (!e->magic[0])
361                         goto Einval;
362                 e->mask = p;
363                 p = scanarg(p, del);
364                 if (!p)
365                         goto Einval;
366                 p[-1] = '\0';
367                 if (!e->mask[0])
368                         e->mask = NULL;
369                 e->size = unquote(e->magic);
370                 if (e->mask && unquote(e->mask) != e->size)
371                         goto Einval;
372                 if (e->size + e->offset > BINPRM_BUF_SIZE)
373                         goto Einval;
374         } else {
375                 p = strchr(p, del);
376                 if (!p)
377                         goto Einval;
378                 *p++ = '\0';
379                 e->magic = p;
380                 p = strchr(p, del);
381                 if (!p)
382                         goto Einval;
383                 *p++ = '\0';
384                 if (!e->magic[0] || strchr(e->magic, '/'))
385                         goto Einval;
386                 p = strchr(p, del);
387                 if (!p)
388                         goto Einval;
389                 *p++ = '\0';
390         }
391         e->interpreter = p;
392         p = strchr(p, del);
393         if (!p)
394                 goto Einval;
395         *p++ = '\0';
396         if (!e->interpreter[0])
397                 goto Einval;
398
399
400         p = check_special_flags (p, e);
401
402         if (*p == '\n')
403                 p++;
404         if (p != buf + count)
405                 goto Einval;
406         return e;
407
408 out:
409         return ERR_PTR(err);
410
411 Efault:
412         kfree(e);
413         return ERR_PTR(-EFAULT);
414 Einval:
415         kfree(e);
416         return ERR_PTR(-EINVAL);
417 }
418
419 /*
420  * Set status of entry/binfmt_misc:
421  * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
422  */
423 static int parse_command(const char __user *buffer, size_t count)
424 {
425         char s[4];
426
427         if (!count)
428                 return 0;
429         if (count > 3)
430                 return -EINVAL;
431         if (copy_from_user(s, buffer, count))
432                 return -EFAULT;
433         if (s[count-1] == '\n')
434                 count--;
435         if (count == 1 && s[0] == '0')
436                 return 1;
437         if (count == 1 && s[0] == '1')
438                 return 2;
439         if (count == 2 && s[0] == '-' && s[1] == '1')
440                 return 3;
441         return -EINVAL;
442 }
443
444 /* generic stuff */
445
446 static void entry_status(Node *e, char *page)
447 {
448         char *dp;
449         char *status = "disabled";
450         const char * flags = "flags: ";
451
452         if (test_bit(Enabled, &e->flags))
453                 status = "enabled";
454
455         if (!VERBOSE_STATUS) {
456                 sprintf(page, "%s\n", status);
457                 return;
458         }
459
460         sprintf(page, "%s\ninterpreter %s\n", status, e->interpreter);
461         dp = page + strlen(page);
462
463         /* print the special flags */
464         sprintf (dp, "%s", flags);
465         dp += strlen (flags);
466         if (e->flags & MISC_FMT_PRESERVE_ARGV0) {
467                 *dp ++ = 'P';
468         }
469         if (e->flags & MISC_FMT_OPEN_BINARY) {
470                 *dp ++ = 'O';
471         }
472         if (e->flags & MISC_FMT_CREDENTIALS) {
473                 *dp ++ = 'C';
474         }
475         *dp ++ = '\n';
476
477
478         if (!test_bit(Magic, &e->flags)) {
479                 sprintf(dp, "extension .%s\n", e->magic);
480         } else {
481                 int i;
482
483                 sprintf(dp, "offset %i\nmagic ", e->offset);
484                 dp = page + strlen(page);
485                 for (i = 0; i < e->size; i++) {
486                         sprintf(dp, "%02x", 0xff & (int) (e->magic[i]));
487                         dp += 2;
488                 }
489                 if (e->mask) {
490                         sprintf(dp, "\nmask ");
491                         dp += 6;
492                         for (i = 0; i < e->size; i++) {
493                                 sprintf(dp, "%02x", 0xff & (int) (e->mask[i]));
494                                 dp += 2;
495                         }
496                 }
497                 *dp++ = '\n';
498                 *dp = '\0';
499         }
500 }
501
502 static struct inode *bm_get_inode(struct super_block *sb, int mode)
503 {
504         struct inode * inode = new_inode(sb);
505
506         if (inode) {
507                 inode->i_mode = mode;
508                 inode->i_uid = 0;
509                 inode->i_gid = 0;
510                 inode->i_blocks = 0;
511                 inode->i_atime = inode->i_mtime = inode->i_ctime =
512                         current_fs_time(inode->i_sb);
513         }
514         return inode;
515 }
516
517 static void bm_clear_inode(struct inode *inode)
518 {
519         kfree(inode->i_private);
520 }
521
522 static void kill_node(Node *e)
523 {
524         struct dentry *dentry;
525
526         write_lock(&entries_lock);
527         dentry = e->dentry;
528         if (dentry) {
529                 list_del_init(&e->list);
530                 e->dentry = NULL;
531         }
532         write_unlock(&entries_lock);
533
534         if (dentry) {
535                 dentry->d_inode->i_nlink--;
536                 d_drop(dentry);
537                 dput(dentry);
538                 simple_release_fs(&bm_mnt, &entry_count);
539         }
540 }
541
542 /* /<entry> */
543
544 static ssize_t
545 bm_entry_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos)
546 {
547         Node *e = file->f_dentry->d_inode->i_private;
548         loff_t pos = *ppos;
549         ssize_t res;
550         char *page;
551         int len;
552
553         if (!(page = (char*) __get_free_page(GFP_KERNEL)))
554                 return -ENOMEM;
555
556         entry_status(e, page);
557         len = strlen(page);
558
559         res = -EINVAL;
560         if (pos < 0)
561                 goto out;
562         res = 0;
563         if (pos >= len)
564                 goto out;
565         if (len < pos + nbytes)
566                 nbytes = len - pos;
567         res = -EFAULT;
568         if (copy_to_user(buf, page + pos, nbytes))
569                 goto out;
570         *ppos = pos + nbytes;
571         res = nbytes;
572 out:
573         free_page((unsigned long) page);
574         return res;
575 }
576
577 static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
578                                 size_t count, loff_t *ppos)
579 {
580         struct dentry *root;
581         Node *e = file->f_dentry->d_inode->i_private;
582         int res = parse_command(buffer, count);
583
584         switch (res) {
585                 case 1: clear_bit(Enabled, &e->flags);
586                         break;
587                 case 2: set_bit(Enabled, &e->flags);
588                         break;
589                 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
590                         mutex_lock(&root->d_inode->i_mutex);
591
592                         kill_node(e);
593
594                         mutex_unlock(&root->d_inode->i_mutex);
595                         dput(root);
596                         break;
597                 default: return res;
598         }
599         return count;
600 }
601
602 static const struct file_operations bm_entry_operations = {
603         .read           = bm_entry_read,
604         .write          = bm_entry_write,
605 };
606
607 /* /register */
608
609 static ssize_t bm_register_write(struct file *file, const char __user *buffer,
610                                size_t count, loff_t *ppos)
611 {
612         Node *e;
613         struct inode *inode;
614         struct dentry *root, *dentry;
615         struct super_block *sb = file->f_vfsmnt->mnt_sb;
616         int err = 0;
617
618         e = create_entry(buffer, count);
619
620         if (IS_ERR(e))
621                 return PTR_ERR(e);
622
623         root = dget(sb->s_root);
624         mutex_lock(&root->d_inode->i_mutex);
625         dentry = lookup_one_len(e->name, root, strlen(e->name));
626         err = PTR_ERR(dentry);
627         if (IS_ERR(dentry))
628                 goto out;
629
630         err = -EEXIST;
631         if (dentry->d_inode)
632                 goto out2;
633
634         inode = bm_get_inode(sb, S_IFREG | 0644);
635
636         err = -ENOMEM;
637         if (!inode)
638                 goto out2;
639
640         err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
641         if (err) {
642                 iput(inode);
643                 inode = NULL;
644                 goto out2;
645         }
646
647         e->dentry = dget(dentry);
648         inode->i_private = e;
649         inode->i_fop = &bm_entry_operations;
650
651         d_instantiate(dentry, inode);
652         write_lock(&entries_lock);
653         list_add(&e->list, &entries);
654         write_unlock(&entries_lock);
655
656         err = 0;
657 out2:
658         dput(dentry);
659 out:
660         mutex_unlock(&root->d_inode->i_mutex);
661         dput(root);
662
663         if (err) {
664                 kfree(e);
665                 return -EINVAL;
666         }
667         return count;
668 }
669
670 static const struct file_operations bm_register_operations = {
671         .write          = bm_register_write,
672 };
673
674 /* /status */
675
676 static ssize_t
677 bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
678 {
679         char *s = enabled ? "enabled" : "disabled";
680         int len = strlen(s);
681         loff_t pos = *ppos;
682
683         if (pos < 0)
684                 return -EINVAL;
685         if (pos >= len)
686                 return 0;
687         if (len < pos + nbytes)
688                 nbytes = len - pos;
689         if (copy_to_user(buf, s + pos, nbytes))
690                 return -EFAULT;
691         *ppos = pos + nbytes;
692         return nbytes;
693 }
694
695 static ssize_t bm_status_write(struct file * file, const char __user * buffer,
696                 size_t count, loff_t *ppos)
697 {
698         int res = parse_command(buffer, count);
699         struct dentry *root;
700
701         switch (res) {
702                 case 1: enabled = 0; break;
703                 case 2: enabled = 1; break;
704                 case 3: root = dget(file->f_vfsmnt->mnt_sb->s_root);
705                         mutex_lock(&root->d_inode->i_mutex);
706
707                         while (!list_empty(&entries))
708                                 kill_node(list_entry(entries.next, Node, list));
709
710                         mutex_unlock(&root->d_inode->i_mutex);
711                         dput(root);
712                 default: return res;
713         }
714         return count;
715 }
716
717 static const struct file_operations bm_status_operations = {
718         .read           = bm_status_read,
719         .write          = bm_status_write,
720 };
721
722 /* Superblock handling */
723
724 static struct super_operations s_ops = {
725         .statfs         = simple_statfs,
726         .clear_inode    = bm_clear_inode,
727 };
728
729 static int bm_fill_super(struct super_block * sb, void * data, int silent)
730 {
731         static struct tree_descr bm_files[] = {
732                 [1] = {"status", &bm_status_operations, S_IWUSR|S_IRUGO},
733                 [2] = {"register", &bm_register_operations, S_IWUSR},
734                 /* last one */ {""}
735         };
736         int err = simple_fill_super(sb, 0x42494e4d, bm_files);
737         if (!err)
738                 sb->s_op = &s_ops;
739         return err;
740 }
741
742 static int bm_get_sb(struct file_system_type *fs_type,
743         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
744 {
745         return get_sb_single(fs_type, flags, data, bm_fill_super, mnt);
746 }
747
748 static struct linux_binfmt misc_format = {
749         .module = THIS_MODULE,
750         .load_binary = load_misc_binary,
751 };
752
753 static struct file_system_type bm_fs_type = {
754         .owner          = THIS_MODULE,
755         .name           = "binfmt_misc",
756         .get_sb         = bm_get_sb,
757         .kill_sb        = kill_litter_super,
758 };
759
760 static int __init init_misc_binfmt(void)
761 {
762         int err = register_filesystem(&bm_fs_type);
763         if (!err) {
764                 err = register_binfmt(&misc_format);
765                 if (err)
766                         unregister_filesystem(&bm_fs_type);
767         }
768         return err;
769 }
770
771 static void __exit exit_misc_binfmt(void)
772 {
773         unregister_binfmt(&misc_format);
774         unregister_filesystem(&bm_fs_type);
775 }
776
777 core_initcall(init_misc_binfmt);
778 module_exit(exit_misc_binfmt);
779 MODULE_LICENSE("GPL");