Skip to content

Commit 64561a7

Browse files
fix: forbid sandbox to replace process, only creating is permitted
1 parent 94b2659 commit 64561a7

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

installer/sandbox.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ static void load_sandbox_config() {
5656
char *value = strtok(NULL, "\n");
5757
if (!key || !value) continue;
5858
while (*key == ' ' || *key == '\t') key++;
59-
char *kend = key + strlen(key) - 1;
60-
while (kend > key && (*kend == ' ' || *kend == '\t')) *kend-- = '\0';
59+
char *keyend = key + strlen(key) - 1;
60+
while (keyend > key && (*keyend == ' ' || *keyend == '\t')) *keyend-- = '\0';
6161
while (*value == ' ' || *value == '\t') value++;
6262
char *vend = value + strlen(value) - 1;
6363
while (vend > value && (*vend == ' ' || *vend == '\t')) *vend-- = '\0';
@@ -171,19 +171,34 @@ static int not_supported(const char *function_name) {
171171
_exit(1);
172172
return -1;
173173
}
174+
static pid_t ppid = 0;
175+
// 在进程初始化时保存 PID
176+
__attribute__((constructor)) static void init_sandbox() {
177+
ppid = getpid();
178+
}
174179
#define RESOLVE_REAL(func) \
175180
static typeof(func) *real_##func = NULL; \
176181
if (!real_##func) { \
177182
real_##func = dlsym(RTLD_NEXT, #func); \
178183
}
179184
int execv(const char *path, char *const argv[]) {
180185
RESOLVE_REAL(execv);
181-
if (!allow_create_subprocess() && strstr(path, "bin/python") == NULL) return deny();
186+
// fprintf(stdout, "execv path: %s ppid=%d pid=%d\n", path, sandbox_pid, getpid());
187+
if (!allow_create_subprocess()) {
188+
// 只允许创建python进程,但不允许python进程替换(用os.execvp里又启动另一个python进程)
189+
if (strstr(path, "bin/python") == NULL || getpid() == ppid) {
190+
return deny();
191+
}
192+
}
182193
return real_execv(path, argv);
183194
}
184195
int __execv(const char *path, char *const argv[]) {
185196
RESOLVE_REAL(__execv);
186-
if (!allow_create_subprocess() && strstr(path, "bin/python") == NULL) return deny();
197+
if (!allow_create_subprocess()) {
198+
if (strstr(path, "bin/python") == NULL || getpid() == ppid) {
199+
return deny();
200+
}
201+
}
187202
return real___execv(path, argv);
188203
}
189204
int execve(const char *filename, char *const argv[], char *const envp[]) {

0 commit comments

Comments
 (0)