fs/affs/file.c: use BUG_ON
[safe/jmp/linux-2.6] / 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
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
114         retval = -ENOEXEC;
115         if (!enabled)
116                 goto _ret;
117
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;
126
127         if (!(fmt->flags & MISC_FMT_PRESERVE_ARGV0)) {
128                 retval = remove_arg_zero(bprm);
129                 if (retval)
130                         goto _ret;
131         }
132
133         if (fmt->flags & MISC_FMT_OPEN_BINARY) {
134
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;
142                 }
143                 fd_install(fd_binary, bprm->file);
144
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;
149
150                 allow_write_access(bprm->file);
151                 bprm->file = NULL;
152
153                 /* mark the bprm that fd should be passed to interp */
154                 bprm->interp_flags |= BINPRM_FLAGS_EXECFD;
155                 bprm->interp_data = fd_binary;
156
157         } else {
158                 allow_write_access(bprm->file);
159                 fput(bprm->file);
160                 bprm->file = NULL;
161         }
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++;
167
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 ++;
173
174         bprm->interp = iname;   /* for binfmt_script */
175
176         interp_file = open_exec (iname);
177         retval = PTR_ERR (interp_file);
178         if (IS_ERR (interp_file))
179                 goto _error;
180
181         bprm->file = interp_file;
182         if (fmt->flags & MISC_FMT_CREDENTIALS) {
183                 /*
184                  * No need to call prepare_binprm(), it's already been
185                  * done.  bprm->buf is stale, update from interp_file.
186                  */
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);
191
192         if (retval < 0)
193                 goto _error;
194
195         retval = search_binary_handler (bprm, regs);
196         if (retval < 0)
197                 goto _error;
198
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;
207 }
208
209 /* Command parsers */
210
211 /*
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.
216  */
217 static char *scanarg(char *s, char del)
218 {
219         char c;
220
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;
228                 }
229         }
230         return s;
231 }
232
233 static int unquote(char *from)
234 {
235         char c = 0, *s = from, *p = from;
236
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;
245                 }
246                 *p++ = c;
247         }
248         return p - from;
249 }
250
251 static char * check_special_flags (char * sfs, Node * e)
252 {
253         char * p = sfs;
254         int cont = 1;
255
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;
276                 }
277         }
278
279         return p;
280 }
281 /*
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
285  */
286 static Node *create_entry(const char __user *buffer, size_t count)
287 {
288         Node *e;
289         int memsize, err;
290         char *buf, *p;
291         char del;
292
293         /* some sanity checks */
294         err = -EINVAL;
295         if ((count < 11) || (count > 256))
296                 goto out;
297
298         err = -ENOMEM;
299         memsize = sizeof(Node) + count + 8;
300         e = kmalloc(memsize, GFP_USER);
301         if (!e)
302                 goto out;
303
304         p = buf = (char *)e + sizeof(Node);
305
306         memset(e, 0, sizeof(Node));
307         if (copy_from_user(buf, buffer, count))
308                 goto Efault;
309
310         del = *p++;     /* delimeter */
311
312         memset(buf+count, del, 8);
313
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;
328         }
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';
374         }
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;
382
383
384         p = check_special_flags (p, e);
385
386         if (*p == '\n')
387                 p++;
388         if (p != buf + count)
389                 goto Einval;
390         return e;
391
392 out:
393         return ERR_PTR(err);
394
395 Efault:
396         kfree(e);
397         return ERR_PTR(-EFAULT);
398 Einval:
399         kfree(e);
400         return ERR_PTR(-EINVAL);
401 }
402
403 /*
404  * Set status of entry/binfmt_misc:
405  * '1' enables, '0' disables and '-1' clears entry/binfmt_misc
406  */
407 static int parse_command(const char __user *buffer, size_t count)
408 {
409         char s[4];
410
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;
426 }
427
428 /* generic stuff */
429
430 static void entry_status(Node *e, char *page)
431 {
432         char *dp;
433         char *status = "disabled";
434         const char * flags = "flags: ";
435
436         if (test_bit(Enabled, &e->flags))
437                 status = "enabled";
438
439         if (!VERBOSE_STATUS) {
440                 sprintf(page, "%s\n", status);
441                 return;
442         }
443
444         sprintf(page, "%s\ninterpreter %s\n", status, e->interpreter);
445         dp = page + strlen(page);
446
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';
452         }
453         if (e->flags & MISC_FMT_OPEN_BINARY) {
454                 *dp ++ = 'O';
455         }
456         if (e->flags & MISC_FMT_CREDENTIALS) {
457                 *dp ++ = 'C';
458         }
459         *dp ++ = '\n';
460
461
462         if (!test_bit(Magic, &e->flags)) {
463                 sprintf(dp, "extension .%s\n", e->magic);
464         } else {
465                 int i;
466
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;
472                 }
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;
479                         }
480                 }
481                 *dp++ = '\n';
482                 *dp = '\0';
483         }
484 }
485
486 static struct inode *bm_get_inode(struct super_block *sb, int mode)
487 {
488         struct inode * inode = new_inode(sb);
489
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);
497         }
498         return inode;
499 }
500
501 static void bm_clear_inode(struct inode *inode)
502 {
503         kfree(inode->i_private);
504 }
505
506 static void kill_node(Node *e)
507 {
508         struct dentry *dentry;
509
510         write_lock(&entries_lock);
511         dentry = e->dentry;
512         if (dentry) {
513                 list_del_init(&e->list);
514                 e->dentry = NULL;
515         }
516         write_unlock(&entries_lock);
517
518         if (dentry) {
519                 dentry->d_inode->i_nlink--;
520                 d_drop(dentry);
521                 dput(dentry);
522                 simple_release_fs(&bm_mnt, &entry_count);
523         }
524 }
525
526 /* /<entry> */
527
528 static ssize_t
529 bm_entry_read(struct file * file, char __user * buf, size_t nbytes, loff_t *ppos)
530 {
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;
536
537         if (!(page = (char*) __get_free_page(GFP_KERNEL)))
538                 return -ENOMEM;
539
540         entry_status(e, page);
541         len = strlen(page);
542
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;
559 }
560
561 static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
562                                 size_t count, loff_t *ppos)
563 {
564         struct dentry *root;
565         Node *e = file->f_path.dentry->d_inode->i_private;
566         int res = parse_command(buffer, count);
567
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);
575
576                         kill_node(e);
577
578                         mutex_unlock(&root->d_inode->i_mutex);
579                         dput(root);
580                         break;
581                 default: return res;
582         }
583         return count;
584 }
585
586 static const struct file_operations bm_entry_operations = {
587         .read           = bm_entry_read,
588         .write          = bm_entry_write,
589 };
590
591 /* /register */
592
593 static ssize_t bm_register_write(struct file *file, const char __user *buffer,
594                                size_t count, loff_t *ppos)
595 {
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;
601
602         e = create_entry(buffer, count);
603
604         if (IS_ERR(e))
605                 return PTR_ERR(e);
606
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;
613
614         err = -EEXIST;
615         if (dentry->d_inode)
616                 goto out2;
617
618         inode = bm_get_inode(sb, S_IFREG | 0644);
619
620         err = -ENOMEM;
621         if (!inode)
622                 goto out2;
623
624         err = simple_pin_fs(&bm_fs_type, &bm_mnt, &entry_count);
625         if (err) {
626                 iput(inode);
627                 inode = NULL;
628                 goto out2;
629         }
630
631         e->dentry = dget(dentry);
632         inode->i_private = e;
633         inode->i_fop = &bm_entry_operations;
634
635         d_instantiate(dentry, inode);
636         write_lock(&entries_lock);
637         list_add(&e->list, &entries);
638         write_unlock(&entries_lock);
639
640         err = 0;
641 out2:
642         dput(dentry);
643 out:
644         mutex_unlock(&root->d_inode->i_mutex);
645         dput(root);
646
647         if (err) {
648                 kfree(e);
649                 return -EINVAL;
650         }
651         return count;
652 }
653
654 static const struct file_operations bm_register_operations = {
655         .write          = bm_register_write,
656 };
657
658 /* /status */
659
660 static ssize_t
661 bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
662 {
663         char *s = enabled ? "enabled" : "disabled";
664
665         return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
666 }
667
668 static ssize_t bm_status_write(struct file * file, const char __user * buffer,
669                 size_t count, loff_t *ppos)
670 {
671         int res = parse_command(buffer, count);
672         struct dentry *root;
673
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);
679
680                         while (!list_empty(&entries))
681                                 kill_node(list_entry(entries.next, Node, list));
682
683                         mutex_unlock(&root->d_inode->i_mutex);
684                         dput(root);
685                 default: return res;
686         }
687         return count;
688 }
689
690 static const struct file_operations bm_status_operations = {
691         .read           = bm_status_read,
692         .write          = bm_status_write,
693 };
694
695 /* Superblock handling */
696
697 static const struct super_operations s_ops = {
698         .statfs         = simple_statfs,
699         .clear_inode    = bm_clear_inode,
700 };
701
702 static int bm_fill_super(struct super_block * sb, void * data, int silent)
703 {
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 */ {""}
708         };
709         int err = simple_fill_super(sb, 0x42494e4d, bm_files);
710         if (!err)
711                 sb->s_op = &s_ops;
712         return err;
713 }
714
715 static int bm_get_sb(struct file_system_type *fs_type,
716         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
717 {
718         return get_sb_single(fs_type, flags, data, bm_fill_super, mnt);
719 }
720
721 static struct linux_binfmt misc_format = {
722         .module = THIS_MODULE,
723         .load_binary = load_misc_binary,
724 };
725
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,
731 };
732
733 static int __init init_misc_binfmt(void)
734 {
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);
740         }
741         return err;
742 }
743
744 static void __exit exit_misc_binfmt(void)
745 {
746         unregister_binfmt(&misc_format);
747         unregister_filesystem(&bm_fs_type);
748 }
749
750 core_initcall(init_misc_binfmt);
751 module_exit(exit_misc_binfmt);
752 MODULE_LICENSE("GPL");