Skip to content

Commit f67aef1

Browse files
fix: os.execvp() can create subprocess.
1 parent d9ecf8c commit f67aef1

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

apps/common/utils/tool_code.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def exec_code(self, code_str, keywords):
109109
lines = subprocess_result.stdout.splitlines()
110110
result_line = [line for line in lines if line.startswith(_id)]
111111
if not result_line:
112+
maxkb_logger.error("\n".join(lines))
112113
raise Exception("No result found.")
113114
result = json.loads(base64.b64decode(result_line[-1].split(":", 1)[1]).decode())
114115
if result.get('code') == 200:

installer/sandbox.c

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,50 +166,89 @@ static int deny() {
166166
_exit(1);
167167
return -1;
168168
}
169+
static int not_supported(const char *function_name) {
170+
fprintf(stderr, "Not supported function: %s\n", function_name);
171+
_exit(1);
172+
return -1;
173+
}
169174
#define RESOLVE_REAL(func) \
170175
static typeof(func) *real_##func = NULL; \
171176
if (!real_##func) { \
172177
real_##func = dlsym(RTLD_NEXT, #func); \
173178
}
179+
int execv(const char *path, char *const argv[]) {
180+
RESOLVE_REAL(execv);
181+
if (!allow_create_subprocess() && strstr(path, "bin/python") == NULL) return deny();
182+
return real_execv(path, argv);
183+
}
184+
int __execv(const char *path, char *const argv[]) {
185+
RESOLVE_REAL(__execv);
186+
if (!allow_create_subprocess() && strstr(path, "bin/python") == NULL) return deny();
187+
return real___execv(path, argv);
188+
}
174189
int execve(const char *filename, char *const argv[], char *const envp[]) {
175190
RESOLVE_REAL(execve);
176191
if (!allow_create_subprocess()) return deny();
177192
return real_execve(filename, argv, envp);
178193
}
179-
194+
int __execve(const char *filename, char *const argv[], char *const envp[]) {
195+
RESOLVE_REAL(__execve);
196+
if (!allow_create_subprocess()) return deny();
197+
return real___execve(filename, argv, envp);
198+
}
180199
int execveat(int dirfd, const char *pathname,
181200
char *const argv[], char *const envp[], int flags) {
182201
RESOLVE_REAL(execveat);
183202
if (!allow_create_subprocess()) return deny();
184203
return real_execveat(dirfd, pathname, argv, envp, flags);
185204
}
186-
int __execve(const char *filename, char *const argv[], char *const envp[]) {
187-
RESOLVE_REAL(__execve);
188-
if (!allow_create_subprocess()) return deny();
189-
return real___execve(filename, argv, envp);
190-
}
191205
int execvpe(const char *file, char *const argv[], char *const envp[]) {
192-
RESOLVE_REAL(execvpe);
193-
if (!allow_create_subprocess()) return deny();
194-
return real_execvpe(file, argv, envp);
206+
return not_supported("execvpe");
195207
}
196208
int __execvpe(const char *file, char *const argv[], char *const envp[]) {
197-
RESOLVE_REAL(__execvpe);
198-
if (!allow_create_subprocess()) return deny();
199-
return real___execvpe(file, argv, envp);
209+
return not_supported("__execvpe");
210+
}
211+
int execvp(const char *file, char *const argv[]) {
212+
return not_supported("execvp");
213+
}
214+
int __execvp(const char *file, char *const argv[]) {
215+
return not_supported("__execvp");
216+
}
217+
int execl(const char *path, const char *arg, ...) {
218+
return not_supported("execl");
219+
}
220+
int __execl(const char *path, const char *arg, ...) {
221+
return not_supported("__execl");
222+
}
223+
int execlp(const char *file, const char *arg, ...) {
224+
return not_supported("execlp");
225+
}
226+
int __execlp(const char *file, const char *arg, ...) {
227+
return not_supported("__execlp");
228+
}
229+
int execle(const char *path, const char *arg, ...) {
230+
return not_supported("execle");
200231
}
201232
pid_t fork(void) {
202233
RESOLVE_REAL(fork);
203234
if (!allow_create_subprocess()) return deny();
204235
return real_fork();
205236
}
206-
237+
pid_t __fork(void) {
238+
RESOLVE_REAL(__fork);
239+
if (!allow_create_subprocess()) return deny();
240+
return real___fork();
241+
}
207242
pid_t vfork(void) {
208243
RESOLVE_REAL(vfork);
209244
if (!allow_create_subprocess()) return deny();
210245
return real_vfork();
211246
}
212-
247+
pid_t __vfork(void) {
248+
RESOLVE_REAL(__vfork);
249+
if (!allow_create_subprocess()) return deny();
250+
return real___vfork();
251+
}
213252
int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...) {
214253
RESOLVE_REAL(clone);
215254
if (!allow_create_subprocess()) return deny();
@@ -259,7 +298,24 @@ int __posix_spawnp(pid_t *pid, const char *file,
259298
if (!allow_create_subprocess()) return deny();
260299
return real___posix_spawnp(pid, file, file_actions, attrp, argv, envp);
261300
}
262-
301+
FILE *popen(const char *command, const char *type) {
302+
RESOLVE_REAL(popen);
303+
if (!allow_create_subprocess()) {
304+
fprintf(stderr, "Permission denied to create subprocess.\n");
305+
errno = EACCES;
306+
return NULL;
307+
}
308+
return real_popen(command, type);
309+
}
310+
FILE *__popen(const char *command, const char *type) {
311+
RESOLVE_REAL(__popen);
312+
if (!allow_create_subprocess()) {
313+
fprintf(stderr, "Permission denied to create subprocess.\n");
314+
errno = EACCES;
315+
return NULL;
316+
}
317+
return real___popen(command, type);
318+
}
263319
int system(const char *command) {
264320
RESOLVE_REAL(system);
265321
if (!allow_create_subprocess()) return deny();
@@ -270,6 +326,16 @@ int __libc_system(const char *command) {
270326
if (!allow_create_subprocess()) return deny();
271327
return real___libc_system(command);
272328
}
329+
pid_t __libc_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...) {
330+
RESOLVE_REAL(__libc_clone);
331+
if (!allow_create_subprocess()) return deny();
332+
va_list ap;
333+
va_start(ap, arg);
334+
long a4 = va_arg(ap, long);
335+
long a5 = va_arg(ap, long);
336+
va_end(ap);
337+
return real___libc_clone(fn, child_stack, flags, arg, (void *)a4, (void *)a5);
338+
}
273339
pid_t forkpty(int *amaster, char *name, const struct termios *termp, const struct winsize *winp) {
274340
RESOLVE_REAL(forkpty);
275341
if (!allow_create_subprocess()) return deny();

0 commit comments

Comments
 (0)