2323#define CONFIG_FILE ".sandbox.conf"
2424#define KEY_BANNED_HOSTS "SANDBOX_PYTHON_BANNED_HOSTS"
2525#define KEY_ALLOW_SUBPROCESS "SANDBOX_PYTHON_ALLOW_SUBPROCESS"
26-
26+ #define RESOLVE_REAL (func ) \
27+ static typeof(func) *real_##func = NULL; \
28+ if (!real_##func) { \
29+ real_##func = dlsym(RTLD_NEXT, #func); \
30+ }
2731static char * banned_hosts = NULL ;
2832static int allow_subprocess = 0 ; // 默认禁止
2933
@@ -116,8 +120,7 @@ static int match_env_patterns(const char *target, const char *env_val) {
116120/** 拦截 connect() —— 精确匹配 IP */
117121int connect (int sockfd , const struct sockaddr * addr , socklen_t addrlen ) {
118122 static int (* real_connect )(int , const struct sockaddr * , socklen_t ) = NULL ;
119- if (!real_connect )
120- real_connect = dlsym (RTLD_NEXT , "connect" );
123+ RESOLVE_REAL (connect );
121124 ensure_config_loaded ();
122125 char ip [INET6_ADDRSTRLEN ] = {0 };
123126 if (addr -> sa_family == AF_INET )
@@ -137,8 +140,7 @@ int getaddrinfo(const char *node, const char *service,
137140 const struct addrinfo * hints , struct addrinfo * * res ) {
138141 static int (* real_getaddrinfo )(const char * , const char * ,
139142 const struct addrinfo * , struct addrinfo * * ) = NULL ;
140- if (!real_getaddrinfo )
141- real_getaddrinfo = dlsym (RTLD_NEXT , "getaddrinfo" );
143+ RESOLVE_REAL (getaddrinfo );
142144 ensure_config_loaded ();
143145 if (banned_hosts && * banned_hosts && node ) {
144146 // 检测 node 是否是 IP
@@ -164,12 +166,6 @@ static int deny() {
164166 _exit (1 );
165167 return -1 ;
166168}
167- #define RESOLVE_REAL (func ) \
168- static typeof(func) *real_##func = NULL; \
169- if (!real_##func) { \
170- real_##func = dlsym(RTLD_NEXT, #func); \
171- }
172-
173169int execve (const char * filename , char * const argv [], char * const envp []) {
174170 RESOLVE_REAL (execve );
175171 if (!allow_create_subprocess ()) return deny ();
@@ -182,7 +178,21 @@ int execveat(int dirfd, const char *pathname,
182178 if (!allow_create_subprocess ()) return deny ();
183179 return real_execveat (dirfd , pathname , argv , envp , flags );
184180}
185-
181+ int __execve (const char * filename , char * const argv [], char * const envp []) {
182+ RESOLVE_REAL (__execve );
183+ if (!allow_create_subprocess ()) return deny ();
184+ return real___execve (filename , argv , envp );
185+ }
186+ int execvpe (const char * file , char * const argv [], char * const envp []) {
187+ RESOLVE_REAL (execvpe );
188+ if (!allow_create_subprocess ()) return deny ();
189+ return real_execvpe (file , argv , envp );
190+ }
191+ 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 );
195+ }
186196pid_t fork (void ) {
187197 RESOLVE_REAL (fork );
188198 if (!allow_create_subprocess ()) return deny ();
@@ -253,7 +263,7 @@ int __libc_system(const char *command) {
253263}
254264long (* real_syscall )(long , ...) = NULL ;
255265long syscall (long number , ...) {
256- if (! real_syscall ) real_syscall = dlsym ( RTLD_NEXT , " syscall" );
266+ RESOLVE_REAL ( syscall );
257267 va_list ap ;
258268 va_start (ap , number );
259269 long a1 = va_arg (ap , long );
0 commit comments