binfmt_script.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 inline bool spacetab(char c) { return c == ' ' || c == '\t'; }
  16. static inline char *next_non_spacetab(char *first, const char *last)
  17. {
  18. for (; first <= last; first++)
  19. if (!spacetab(*first))
  20. return first;
  21. return NULL;
  22. }
  23. static inline char *next_terminator(char *first, const char *last)
  24. {
  25. for (; first <= last; first++)
  26. if (spacetab(*first) || !*first)
  27. return first;
  28. return NULL;
  29. }
  30. static int load_script(struct linux_binprm *bprm)
  31. {
  32. const char *i_arg, *i_name;
  33. char *cp, *buf_end;
  34. struct file *file;
  35. int retval;
  36. /* Not ours to exec if we don't start with "#!". */
  37. if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
  38. return -ENOEXEC;
  39. /*
  40. * If the script filename will be inaccessible after exec, typically
  41. * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give
  42. * up now (on the assumption that the interpreter will want to load
  43. * this file).
  44. */
  45. if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
  46. return -ENOENT;
  47. /* Release since we are not mapping a binary into memory. */
  48. allow_write_access(bprm->file);
  49. fput(bprm->file);
  50. bprm->file = NULL;
  51. /*
  52. * This section handles parsing the #! line into separate
  53. * interpreter path and argument strings. We must be careful
  54. * because bprm->buf is not yet guaranteed to be NUL-terminated
  55. * (though the buffer will have trailing NUL padding when the
  56. * file size was smaller than the buffer size).
  57. *
  58. * We do not want to exec a truncated interpreter path, so either
  59. * we find a newline (which indicates nothing is truncated), or
  60. * we find a space/tab/NUL after the interpreter path (which
  61. * itself may be preceded by spaces/tabs). Truncating the
  62. * arguments is fine: the interpreter can re-read the script to
  63. * parse them on its own.
  64. */
  65. buf_end = bprm->buf + sizeof(bprm->buf) - 1;
  66. cp = strnchr(bprm->buf, sizeof(bprm->buf), '\n');
  67. if (!cp) {
  68. cp = next_non_spacetab(bprm->buf + 2, buf_end);
  69. if (!cp)
  70. return -ENOEXEC; /* Entire buf is spaces/tabs */
  71. /*
  72. * If there is no later space/tab/NUL we must assume the
  73. * interpreter path is truncated.
  74. */
  75. if (!next_terminator(cp, buf_end))
  76. return -ENOEXEC;
  77. cp = buf_end;
  78. }
  79. /* NUL-terminate the buffer and any trailing spaces/tabs. */
  80. *cp = '\0';
  81. while (cp > bprm->buf) {
  82. cp--;
  83. if ((*cp == ' ') || (*cp == '\t'))
  84. *cp = '\0';
  85. else
  86. break;
  87. }
  88. for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
  89. if (*cp == '\0')
  90. return -ENOEXEC; /* No interpreter name found */
  91. i_name = cp;
  92. i_arg = NULL;
  93. for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++)
  94. /* nothing */ ;
  95. while ((*cp == ' ') || (*cp == '\t'))
  96. *cp++ = '\0';
  97. if (*cp)
  98. i_arg = cp;
  99. /*
  100. * OK, we've parsed out the interpreter name and
  101. * (optional) argument.
  102. * Splice in (1) the interpreter's name for argv[0]
  103. * (2) (optional) argument to interpreter
  104. * (3) filename of shell script (replace argv[0])
  105. *
  106. * This is done in reverse order, because of how the
  107. * user environment and arguments are stored.
  108. */
  109. retval = remove_arg_zero(bprm);
  110. if (retval)
  111. return retval;
  112. retval = copy_strings_kernel(1, &bprm->interp, bprm);
  113. if (retval < 0)
  114. return retval;
  115. bprm->argc++;
  116. if (i_arg) {
  117. retval = copy_strings_kernel(1, &i_arg, bprm);
  118. if (retval < 0)
  119. return retval;
  120. bprm->argc++;
  121. }
  122. retval = copy_strings_kernel(1, &i_name, bprm);
  123. if (retval)
  124. return retval;
  125. bprm->argc++;
  126. retval = bprm_change_interp(i_name, bprm);
  127. if (retval < 0)
  128. return retval;
  129. /*
  130. * OK, now restart the process with the interpreter's dentry.
  131. */
  132. file = open_exec(i_name);
  133. if (IS_ERR(file))
  134. return PTR_ERR(file);
  135. bprm->file = file;
  136. retval = prepare_binprm(bprm);
  137. if (retval < 0)
  138. return retval;
  139. return search_binary_handler(bprm);
  140. }
  141. static struct linux_binfmt script_format = {
  142. .module = THIS_MODULE,
  143. .load_binary = load_script,
  144. };
  145. static int __init init_script_binfmt(void)
  146. {
  147. register_binfmt(&script_format);
  148. return 0;
  149. }
  150. static void __exit exit_script_binfmt(void)
  151. {
  152. unregister_binfmt(&script_format);
  153. }
  154. core_initcall(init_script_binfmt);
  155. module_exit(exit_script_binfmt);
  156. MODULE_LICENSE("GPL");