Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/binfmt_script.c |
| 3 | * |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 4 | * Copyright (C) 1996 Martin von Löwis |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * original #!-checking implemented by tytso. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/string.h> |
| 10 | #include <linux/stat.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/binfmts.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/fs.h> |
| 17 | |
| 18 | static int load_script(struct linux_binprm *bprm,struct pt_regs *regs) |
| 19 | { |
| 20 | char *cp, *i_name, *i_arg; |
| 21 | struct file *file; |
| 22 | char interp[BINPRM_BUF_SIZE]; |
| 23 | int retval; |
| 24 | |
| 25 | if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') || (bprm->sh_bang)) |
| 26 | return -ENOEXEC; |
| 27 | /* |
| 28 | * This section does the #! interpretation. |
| 29 | * Sorta complicated, but hopefully it will work. -TYT |
| 30 | */ |
| 31 | |
| 32 | bprm->sh_bang++; |
| 33 | allow_write_access(bprm->file); |
| 34 | fput(bprm->file); |
| 35 | bprm->file = NULL; |
| 36 | |
| 37 | bprm->buf[BINPRM_BUF_SIZE - 1] = '\0'; |
| 38 | if ((cp = strchr(bprm->buf, '\n')) == NULL) |
| 39 | cp = bprm->buf+BINPRM_BUF_SIZE-1; |
| 40 | *cp = '\0'; |
| 41 | while (cp > bprm->buf) { |
| 42 | cp--; |
| 43 | if ((*cp == ' ') || (*cp == '\t')) |
| 44 | *cp = '\0'; |
| 45 | else |
| 46 | break; |
| 47 | } |
| 48 | for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++); |
| 49 | if (*cp == '\0') |
| 50 | return -ENOEXEC; /* No interpreter name found */ |
| 51 | i_name = cp; |
| 52 | i_arg = NULL; |
| 53 | for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) |
| 54 | /* nothing */ ; |
| 55 | while ((*cp == ' ') || (*cp == '\t')) |
| 56 | *cp++ = '\0'; |
| 57 | if (*cp) |
| 58 | i_arg = cp; |
| 59 | strcpy (interp, i_name); |
| 60 | /* |
| 61 | * OK, we've parsed out the interpreter name and |
| 62 | * (optional) argument. |
| 63 | * Splice in (1) the interpreter's name for argv[0] |
| 64 | * (2) (optional) argument to interpreter |
| 65 | * (3) filename of shell script (replace argv[0]) |
| 66 | * |
| 67 | * This is done in reverse order, because of how the |
| 68 | * user environment and arguments are stored. |
| 69 | */ |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 70 | retval = remove_arg_zero(bprm); |
| 71 | if (retval) |
| 72 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | retval = copy_strings_kernel(1, &bprm->interp, bprm); |
| 74 | if (retval < 0) return retval; |
| 75 | bprm->argc++; |
| 76 | if (i_arg) { |
| 77 | retval = copy_strings_kernel(1, &i_arg, bprm); |
| 78 | if (retval < 0) return retval; |
| 79 | bprm->argc++; |
| 80 | } |
| 81 | retval = copy_strings_kernel(1, &i_name, bprm); |
| 82 | if (retval) return retval; |
| 83 | bprm->argc++; |
| 84 | bprm->interp = interp; |
| 85 | |
| 86 | /* |
| 87 | * OK, now restart the process with the interpreter's dentry. |
| 88 | */ |
| 89 | file = open_exec(interp); |
| 90 | if (IS_ERR(file)) |
| 91 | return PTR_ERR(file); |
| 92 | |
| 93 | bprm->file = file; |
| 94 | retval = prepare_binprm(bprm); |
| 95 | if (retval < 0) |
| 96 | return retval; |
| 97 | return search_binary_handler(bprm,regs); |
| 98 | } |
| 99 | |
| 100 | static struct linux_binfmt script_format = { |
| 101 | .module = THIS_MODULE, |
| 102 | .load_binary = load_script, |
| 103 | }; |
| 104 | |
| 105 | static int __init init_script_binfmt(void) |
| 106 | { |
| 107 | return register_binfmt(&script_format); |
| 108 | } |
| 109 | |
| 110 | static void __exit exit_script_binfmt(void) |
| 111 | { |
| 112 | unregister_binfmt(&script_format); |
| 113 | } |
| 114 | |
| 115 | core_initcall(init_script_binfmt); |
| 116 | module_exit(exit_script_binfmt); |
| 117 | MODULE_LICENSE("GPL"); |