1 // SPDX-License-Identifier: BSD-3-Clause
3 * Very simple script interpreter that can evaluate two different commands (one
5 * - "?" to initialize a counter from user's input;
6 * - "+" to increment the counter (which is set to 0 by default).
8 * See tools/testing/selftests/exec/check-exec-tests.sh and
9 * Documentation/userspace-api/check_exec.rst
11 * Copyright © 2024 Microsoft Corporation
16 #include <linux/fcntl.h>
17 #include <linux/prctl.h>
18 #include <linux/securebits.h>
23 #include <sys/prctl.h>
24 #include <sys/syscall.h>
27 static int sys_execveat(int dirfd, const char *pathname, char *const argv[],
28 char *const envp[], int flags)
30 return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
33 /* Returns 1 on error, 0 otherwise. */
34 static int interpret_buffer(char *buffer, size_t buffer_size)
36 char *line, *saveptr = NULL;
39 /* Each command is the first character of a line. */
41 line = strtok_r(buffer, "\n", &saveptr);
43 if (*line != '#' && strlen(line) != 1) {
44 fprintf(stderr, "# ERROR: Unknown string\n");
49 /* Skips shebang and comments. */
52 /* Increments and prints the number. */
54 printf("%lld\n", number);
57 /* Reads integer from stdin. */
58 fprintf(stderr, "> Enter new number: \n");
59 if (scanf("%lld", &number) != 1) {
61 "# WARNING: Failed to read number from stdin\n");
65 fprintf(stderr, "# ERROR: Unknown character '%c'\n",
69 line = strtok_r(NULL, "\n", &saveptr);
74 /* Returns 1 on error, 0 otherwise. */
75 static int interpret_stream(FILE *script, char *const script_name,
76 char *const *const envp, const bool restrict_stream)
79 char *const script_argv[] = { script_name, NULL };
81 size_t buf_size = sizeof(buf);
84 * We pass a valid argv and envp to the kernel to emulate a native
85 * script execution. We must use the script file descriptor instead of
86 * the script path name to avoid race conditions.
88 err = sys_execveat(fileno(script), "", script_argv, envp,
89 AT_EMPTY_PATH | AT_EXECVE_CHECK);
90 if (err && restrict_stream) {
91 perror("ERROR: Script execution check");
96 buf_size = fread(buf, 1, buf_size - 1, script);
97 return interpret_buffer(buf, buf_size);
100 static void print_usage(const char *argv0)
102 fprintf(stderr, "usage: %s <script.inc> | -i | -c <command>\n\n",
104 fprintf(stderr, "Example:\n");
105 fprintf(stderr, " ./set-exec -fi -- ./inc -i < script-exec.inc\n");
108 int main(const int argc, char *const argv[], char *const *const envp)
112 char *script_name = NULL;
113 bool interpret_stdin = false;
114 FILE *script_file = NULL;
116 bool deny_interactive, restrict_file;
119 secbits = prctl(PR_GET_SECUREBITS);
122 * This should never happen, except with a buggy seccomp
125 perror("ERROR: Failed to get securebits");
129 deny_interactive = !!(secbits & SECBIT_EXEC_DENY_INTERACTIVE);
130 restrict_file = !!(secbits & SECBIT_EXEC_RESTRICT_FILE);
132 while ((opt = getopt(argc, argv, "c:i")) != -1) {
136 fprintf(stderr, "ERROR: Command already set");
142 interpret_stdin = true;
145 print_usage(argv[0]);
150 /* Checks that only one argument is used, or read stdin. */
151 arg_nb = !!cmd + !!interpret_stdin;
152 if (arg_nb == 0 && argc == 2) {
153 script_name = argv[1];
154 } else if (arg_nb != 1) {
155 print_usage(argv[0]);
161 * Other kind of interactive interpretations should be denied
162 * as well (e.g. CLI arguments passing script snippets,
163 * environment variables interpreted as script). However, any
164 * way to pass script files should only be restricted according
167 if (deny_interactive) {
169 "ERROR: Interactive interpretation denied.\n");
173 return interpret_buffer(cmd, strlen(cmd));
176 if (interpret_stdin && !script_name) {
179 * As for any execve(2) call, this path may be logged by the
182 script_name = "/proc/self/fd/0";
184 * When stdin is used, it can point to a regular file or a
185 * pipe. Restrict stdin execution according to
186 * SECBIT_EXEC_DENY_INTERACTIVE but always allow executable
187 * files (which are not considered as interactive inputs).
189 return interpret_stream(script_file, script_name, envp,
191 } else if (script_name && !interpret_stdin) {
193 * In this sample, we don't pass any argument to scripts, but
194 * otherwise we would have to forge an argv with such
197 script_file = fopen(script_name, "r");
199 perror("ERROR: Failed to open script");
203 * Restricts file execution according to
204 * SECBIT_EXEC_RESTRICT_FILE.
206 return interpret_stream(script_file, script_name, envp,
210 print_usage(argv[0]);