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- #define RESOLVE_REAL (func ) \
27- static typeof(func) *real_##func = NULL; \
28- if (!real_##func) { \
29- real_##func = dlsym(RTLD_NEXT, #func); \
30- }
26+
3127static char * banned_hosts = NULL ;
3228static int allow_subprocess = 0 ; // 默认禁止
3329
@@ -120,7 +116,8 @@ static int match_env_patterns(const char *target, const char *env_val) {
120116/** 拦截 connect() —— 精确匹配 IP */
121117int connect (int sockfd , const struct sockaddr * addr , socklen_t addrlen ) {
122118 static int (* real_connect )(int , const struct sockaddr * , socklen_t ) = NULL ;
123- RESOLVE_REAL (connect );
119+ if (!real_connect )
120+ real_connect = dlsym (RTLD_NEXT , "connect" );
124121 ensure_config_loaded ();
125122 char ip [INET6_ADDRSTRLEN ] = {0 };
126123 if (addr -> sa_family == AF_INET )
@@ -140,7 +137,8 @@ int getaddrinfo(const char *node, const char *service,
140137 const struct addrinfo * hints , struct addrinfo * * res ) {
141138 static int (* real_getaddrinfo )(const char * , const char * ,
142139 const struct addrinfo * , struct addrinfo * * ) = NULL ;
143- RESOLVE_REAL (getaddrinfo );
140+ if (!real_getaddrinfo )
141+ real_getaddrinfo = dlsym (RTLD_NEXT , "getaddrinfo" );
144142 ensure_config_loaded ();
145143 if (banned_hosts && * banned_hosts && node ) {
146144 // 检测 node 是否是 IP
@@ -166,6 +164,11 @@ static int deny() {
166164 _exit (1 );
167165 return -1 ;
168166}
167+ #define RESOLVE_REAL (func ) \
168+ static typeof(func) *real_##func = NULL; \
169+ if (!real_##func) { \
170+ real_##func = dlsym(RTLD_NEXT, #func); \
171+ }
169172int execve (const char * filename , char * const argv [], char * const envp []) {
170173 RESOLVE_REAL (execve );
171174 if (!allow_create_subprocess ()) return deny ();
0 commit comments