binfmt_script.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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)
  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. return -ENOEXEC;
  24. /*
  25. * If the script filename will be inaccessible after exec, typically
  26. * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give
  27. * up now (on the assumption that the interpreter will want to load
  28. * this file).
  29. */
  30. if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
  31. return -ENOENT;
  32. /*
  33. * This section does the #! interpretation.
  34. * Sorta complicated, but hopefully it will work. -TYT
  35. */
  36. allow_write_access(bprm->file);
  37. fput(bprm->file);
  38. bprm->file = NULL;
  39. bprm->buf[BINPRM_BUF_SIZE - 1] = '\0';
  40. if ((cp = strchr(bprm->buf, '\n')) == NULL)
  41. cp = bprm->buf+BINPRM_BUF_SIZE-1;
  42. *cp = '\0';
  43. while (cp > bprm->buf) {
  44. cp--;
  45. if ((*cp == ' ') || (*cp == '\t'))
  46. *cp = '\0';
  47. else
  48. break;
  49. }
  50. for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
  51. if (*cp == '\0')
  52. return -ENOEXEC; /* No interpreter name found */
  53. i_name = cp;
  54. i_arg = NULL;
  55. for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++)
  56. /* nothing */ ;
  57. while ((*cp == ' ') || (*cp == '\t'))
  58. *cp++ = '\0';
  59. if (*cp)
  60. i_arg = cp;
  61. strcpy (interp, i_name);
  62. /*
  63. * OK, we've parsed out the interpreter name and
  64. * (optional) argument.
  65. * Splice in (1) the interpreter's name for argv[0]
  66. * (2) (optional) argument to interpreter
  67. * (3) filename of shell script (replace argv[0])
  68. *
  69. * This is done in reverse order, because of how the
  70. * user environment and arguments are stored.
  71. */
  72. retval = remove_arg_zero(bprm);
  73. if (retval)
  74. return retval;
  75. retval = copy_strings_kernel(1, &bprm->interp, bprm);
  76. if (retval < 0) return retval;
  77. bprm->argc++;
  78. if (i_arg) {
  79. retval = copy_strings_kernel(1, &i_arg, bprm);
  80. if (retval < 0) return retval;
  81. bprm->argc++;
  82. }
  83. retval = copy_strings_kernel(1, &i_name, bprm);
  84. if (retval) return retval;
  85. bprm->argc++;
  86. retval = bprm_change_interp(interp, bprm);
  87. if (retval < 0)
  88. return retval;
  89. /*
  90. * OK, now restart the process with the interpreter's dentry.
  91. */
  92. file = open_exec(interp);
  93. if (IS_ERR(file))
  94. return PTR_ERR(file);
  95. bprm->file = file;
  96. retval = prepare_binprm(bprm);
  97. if (retval < 0)
  98. return retval;
  99. return search_binary_handler(bprm);
  100. }
  101. static struct linux_binfmt script_format = {
  102. .module = THIS_MODULE,
  103. .load_binary = load_script,
  104. };
  105. static int __init init_script_binfmt(void)
  106. {
  107. register_binfmt(&script_format);
  108. return 0;
  109. }
  110. static void __exit exit_script_binfmt(void)
  111. {
  112. unregister_binfmt(&script_format);
  113. }
  114. core_initcall(init_script_binfmt);
  115. module_exit(exit_script_binfmt);
  116. MODULE_LICENSE("GPL");