binfmt_script.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/binfmt_script.c
  4. *
  5. * Copyright (C) 1996 Martin von Löwis
  6. * original #!-checking implemented by tytso.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/string.h>
  10. #include <linux/stat.h>
  11. #include <linux/binfmts.h>
  12. #include <linux/init.h>
  13. #include <linux/file.h>
  14. #include <linux/err.h>
  15. #include <linux/fs.h>
  16. static inline bool spacetab(char c) { return c == ' ' || c == '\t'; }
  17. static inline const char *next_non_spacetab(const char *first, const char *last)
  18. {
  19. for (; first <= last; first++)
  20. if (!spacetab(*first))
  21. return first;
  22. return NULL;
  23. }
  24. static inline const char *next_terminator(const char *first, const char *last)
  25. {
  26. for (; first <= last; first++)
  27. if (spacetab(*first) || !*first)
  28. return first;
  29. return NULL;
  30. }
  31. static int load_script(struct linux_binprm *bprm)
  32. {
  33. const char *i_name, *i_sep, *i_arg, *i_end, *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. * This section handles parsing the #! line into separate
  41. * interpreter path and argument strings. We must be careful
  42. * because bprm->buf is not yet guaranteed to be NUL-terminated
  43. * (though the buffer will have trailing NUL padding when the
  44. * file size was smaller than the buffer size).
  45. *
  46. * We do not want to exec a truncated interpreter path, so either
  47. * we find a newline (which indicates nothing is truncated), or
  48. * we find a space/tab/NUL after the interpreter path (which
  49. * itself may be preceded by spaces/tabs). Truncating the
  50. * arguments is fine: the interpreter can re-read the script to
  51. * parse them on its own.
  52. */
  53. buf_end = bprm->buf + sizeof(bprm->buf) - 1;
  54. i_end = strnchr(bprm->buf, sizeof(bprm->buf), '\n');
  55. if (!i_end) {
  56. i_end = next_non_spacetab(bprm->buf + 2, buf_end);
  57. if (!i_end)
  58. return -ENOEXEC; /* Entire buf is spaces/tabs */
  59. /*
  60. * If there is no later space/tab/NUL we must assume the
  61. * interpreter path is truncated.
  62. */
  63. if (!next_terminator(i_end, buf_end))
  64. return -ENOEXEC;
  65. i_end = buf_end;
  66. }
  67. /* Trim any trailing spaces/tabs from i_end */
  68. while (spacetab(i_end[-1]))
  69. i_end--;
  70. /* Skip over leading spaces/tabs */
  71. i_name = next_non_spacetab(bprm->buf+2, i_end);
  72. if (!i_name || (i_name == i_end))
  73. return -ENOEXEC; /* No interpreter name found */
  74. /* Is there an optional argument? */
  75. i_arg = NULL;
  76. i_sep = next_terminator(i_name, i_end);
  77. if (i_sep && (*i_sep != '\0'))
  78. i_arg = next_non_spacetab(i_sep, i_end);
  79. /*
  80. * If the script filename will be inaccessible after exec, typically
  81. * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give
  82. * up now (on the assumption that the interpreter will want to load
  83. * this file).
  84. */
  85. if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
  86. return -ENOENT;
  87. /*
  88. * OK, we've parsed out the interpreter name and
  89. * (optional) argument.
  90. * Splice in (1) the interpreter's name for argv[0]
  91. * (2) (optional) argument to interpreter
  92. * (3) filename of shell script (replace argv[0])
  93. *
  94. * This is done in reverse order, because of how the
  95. * user environment and arguments are stored.
  96. */
  97. retval = remove_arg_zero(bprm);
  98. if (retval)
  99. return retval;
  100. retval = copy_string_kernel(bprm->interp, bprm);
  101. if (retval < 0)
  102. return retval;
  103. bprm->argc++;
  104. *((char *)i_end) = '\0';
  105. if (i_arg) {
  106. *((char *)i_sep) = '\0';
  107. retval = copy_string_kernel(i_arg, bprm);
  108. if (retval < 0)
  109. return retval;
  110. bprm->argc++;
  111. }
  112. retval = copy_string_kernel(i_name, bprm);
  113. if (retval)
  114. return retval;
  115. bprm->argc++;
  116. retval = bprm_change_interp(i_name, bprm);
  117. if (retval < 0)
  118. return retval;
  119. /*
  120. * OK, now restart the process with the interpreter's dentry.
  121. */
  122. file = open_exec(i_name);
  123. if (IS_ERR(file))
  124. return PTR_ERR(file);
  125. bprm->interpreter = file;
  126. return 0;
  127. }
  128. static struct linux_binfmt script_format = {
  129. .module = THIS_MODULE,
  130. .load_binary = load_script,
  131. };
  132. static int __init init_script_binfmt(void)
  133. {
  134. register_binfmt(&script_format);
  135. return 0;
  136. }
  137. static void __exit exit_script_binfmt(void)
  138. {
  139. unregister_binfmt(&script_format);
  140. }
  141. core_initcall(init_script_binfmt);
  142. module_exit(exit_script_binfmt);
  143. MODULE_LICENSE("GPL");