binfmt_script.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/binfmts.h>
  11. #include <linux/init.h>
  12. #include <linux/file.h>
  13. #include <linux/err.h>
  14. #include <linux/fs.h>
  15. static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
  16. {
  17. const char *i_arg, *i_name;
  18. char *cp;
  19. struct file *file;
  20. char interp[BINPRM_BUF_SIZE];
  21. int retval;
  22. if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') ||
  23. (bprm->recursion_depth > BINPRM_MAX_RECURSION))
  24. return -ENOEXEC;
  25. /*
  26. * This section does the #! interpretation.
  27. * Sorta complicated, but hopefully it will work. -TYT
  28. */
  29. bprm->recursion_depth++;
  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. retval = remove_arg_zero(bprm);
  67. if (retval)
  68. return retval;
  69. retval = copy_strings_kernel(1, &bprm->interp, bprm);
  70. if (retval < 0) return retval;
  71. bprm->argc++;
  72. if (i_arg) {
  73. retval = copy_strings_kernel(1, &i_arg, bprm);
  74. if (retval < 0) return retval;
  75. bprm->argc++;
  76. }
  77. retval = copy_strings_kernel(1, &i_name, bprm);
  78. if (retval) return retval;
  79. bprm->argc++;
  80. bprm->interp = interp;
  81. /*
  82. * OK, now restart the process with the interpreter's dentry.
  83. */
  84. file = open_exec(interp);
  85. if (IS_ERR(file))
  86. return PTR_ERR(file);
  87. bprm->file = file;
  88. retval = prepare_binprm(bprm);
  89. if (retval < 0)
  90. return retval;
  91. return search_binary_handler(bprm,regs);
  92. }
  93. static struct linux_binfmt script_format = {
  94. .module = THIS_MODULE,
  95. .load_binary = load_script,
  96. };
  97. static int __init init_script_binfmt(void)
  98. {
  99. return register_binfmt(&script_format);
  100. }
  101. static void __exit exit_script_binfmt(void)
  102. {
  103. unregister_binfmt(&script_format);
  104. }
  105. core_initcall(init_script_binfmt);
  106. module_exit(exit_script_binfmt);
  107. MODULE_LICENSE("GPL");