binfmt_script.c 2.7 KB

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