binfmt_script.c 4.4 KB

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