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