hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/binfmt_script.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/fs/binfmt_script.c
34 *
....@@ -15,14 +16,14 @@
1516 #include <linux/fs.h>
1617
1718 static inline bool spacetab(char c) { return c == ' ' || c == '\t'; }
18
-static inline char *next_non_spacetab(char *first, const char *last)
19
+static inline const char *next_non_spacetab(const char *first, const char *last)
1920 {
2021 for (; first <= last; first++)
2122 if (!spacetab(*first))
2223 return first;
2324 return NULL;
2425 }
25
-static inline char *next_terminator(char *first, const char *last)
26
+static inline const char *next_terminator(const char *first, const char *last)
2627 {
2728 for (; first <= last; first++)
2829 if (spacetab(*first) || !*first)
....@@ -32,28 +33,13 @@
3233
3334 static int load_script(struct linux_binprm *bprm)
3435 {
35
- const char *i_arg, *i_name;
36
- char *cp, *buf_end;
36
+ const char *i_name, *i_sep, *i_arg, *i_end, *buf_end;
3737 struct file *file;
3838 int retval;
3939
4040 /* Not ours to exec if we don't start with "#!". */
4141 if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
4242 return -ENOEXEC;
43
-
44
- /*
45
- * If the script filename will be inaccessible after exec, typically
46
- * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give
47
- * up now (on the assumption that the interpreter will want to load
48
- * this file).
49
- */
50
- if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
51
- return -ENOENT;
52
-
53
- /* Release since we are not mapping a binary into memory. */
54
- allow_write_access(bprm->file);
55
- fput(bprm->file);
56
- bprm->file = NULL;
5743
5844 /*
5945 * This section handles parsing the #! line into separate
....@@ -70,39 +56,43 @@
7056 * parse them on its own.
7157 */
7258 buf_end = bprm->buf + sizeof(bprm->buf) - 1;
73
- cp = strnchr(bprm->buf, sizeof(bprm->buf), '\n');
74
- if (!cp) {
75
- cp = next_non_spacetab(bprm->buf + 2, buf_end);
76
- if (!cp)
59
+ i_end = strnchr(bprm->buf, sizeof(bprm->buf), '\n');
60
+ if (!i_end) {
61
+ i_end = next_non_spacetab(bprm->buf + 2, buf_end);
62
+ if (!i_end)
7763 return -ENOEXEC; /* Entire buf is spaces/tabs */
7864 /*
7965 * If there is no later space/tab/NUL we must assume the
8066 * interpreter path is truncated.
8167 */
82
- if (!next_terminator(cp, buf_end))
68
+ if (!next_terminator(i_end, buf_end))
8369 return -ENOEXEC;
84
- cp = buf_end;
70
+ i_end = buf_end;
8571 }
86
- /* NUL-terminate the buffer and any trailing spaces/tabs. */
87
- *cp = '\0';
88
- while (cp > bprm->buf) {
89
- cp--;
90
- if ((*cp == ' ') || (*cp == '\t'))
91
- *cp = '\0';
92
- else
93
- break;
94
- }
95
- for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
96
- if (*cp == '\0')
72
+ /* Trim any trailing spaces/tabs from i_end */
73
+ while (spacetab(i_end[-1]))
74
+ i_end--;
75
+
76
+ /* Skip over leading spaces/tabs */
77
+ i_name = next_non_spacetab(bprm->buf+2, i_end);
78
+ if (!i_name || (i_name == i_end))
9779 return -ENOEXEC; /* No interpreter name found */
98
- i_name = cp;
80
+
81
+ /* Is there an optional argument? */
9982 i_arg = NULL;
100
- for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++)
101
- /* nothing */ ;
102
- while ((*cp == ' ') || (*cp == '\t'))
103
- *cp++ = '\0';
104
- if (*cp)
105
- i_arg = cp;
83
+ i_sep = next_terminator(i_name, i_end);
84
+ if (i_sep && (*i_sep != '\0'))
85
+ i_arg = next_non_spacetab(i_sep, i_end);
86
+
87
+ /*
88
+ * If the script filename will be inaccessible after exec, typically
89
+ * because it is a "/dev/fd/<fd>/.." path against an O_CLOEXEC fd, give
90
+ * up now (on the assumption that the interpreter will want to load
91
+ * this file).
92
+ */
93
+ if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
94
+ return -ENOENT;
95
+
10696 /*
10797 * OK, we've parsed out the interpreter name and
10898 * (optional) argument.
....@@ -116,17 +106,19 @@
116106 retval = remove_arg_zero(bprm);
117107 if (retval)
118108 return retval;
119
- retval = copy_strings_kernel(1, &bprm->interp, bprm);
109
+ retval = copy_string_kernel(bprm->interp, bprm);
120110 if (retval < 0)
121111 return retval;
122112 bprm->argc++;
113
+ *((char *)i_end) = '\0';
123114 if (i_arg) {
124
- retval = copy_strings_kernel(1, &i_arg, bprm);
115
+ *((char *)i_sep) = '\0';
116
+ retval = copy_string_kernel(i_arg, bprm);
125117 if (retval < 0)
126118 return retval;
127119 bprm->argc++;
128120 }
129
- retval = copy_strings_kernel(1, &i_name, bprm);
121
+ retval = copy_string_kernel(i_name, bprm);
130122 if (retval)
131123 return retval;
132124 bprm->argc++;
....@@ -141,11 +133,8 @@
141133 if (IS_ERR(file))
142134 return PTR_ERR(file);
143135
144
- bprm->file = file;
145
- retval = prepare_binprm(bprm);
146
- if (retval < 0)
147
- return retval;
148
- return search_binary_handler(bprm);
136
+ bprm->interpreter = file;
137
+ return 0;
149138 }
150139
151140 static struct linux_binfmt script_format = {