|
8 | 8 | #include <regex.h> |
9 | 9 | #include <unistd.h> |
10 | 10 | #include <sys/socket.h> |
| 11 | +#include <sys/un.h> |
11 | 12 | #include <errno.h> |
12 | 13 | #include <limits.h> |
13 | 14 | #include <libgen.h> |
@@ -89,109 +90,158 @@ static int is_sandbox_user() { |
89 | 90 | * 限制网络访问 |
90 | 91 | */ |
91 | 92 | // ------------------ 匹配 域名 黑名单 ------------------ |
92 | | -static int match_banned_domain(const char *target, const char *env_val) { |
93 | | - if (!target || !env_val || !*env_val) return 0; |
94 | | - char *patterns = strdup(env_val); |
95 | | - char *token = strtok(patterns, ","); |
| 93 | +static int match_banned_domain(const char *target, const char *rules) { |
| 94 | + if (!target || !rules || !*rules) return 0; |
| 95 | + char *list = strdup(rules); |
| 96 | + char *token = strtok(list, ","); |
96 | 97 | int matched = 0; |
97 | 98 | while (token) { |
98 | 99 | while (*token == ' ' || *token == '\t') token++; |
99 | | - char *end = token + strlen(token) - 1; |
100 | | - while (end > token && (*end == ' ' || *end == '\t')) *end-- = '\0'; |
101 | 100 | if (*token) { |
102 | | - regex_t regex; |
103 | | - char fullpattern[512]; |
104 | | - snprintf(fullpattern, sizeof(fullpattern), "^%s$", token); |
105 | | - if (regcomp(®ex, fullpattern, REG_EXTENDED | REG_NOSUB | REG_ICASE) == 0) { |
106 | | - if (regexec(®ex, target, 0, NULL, 0) == 0) { |
| 101 | + regex_t re; |
| 102 | + char buf[512]; |
| 103 | + snprintf(buf, sizeof(buf), "^%s$", token); |
| 104 | + if (regcomp(&re, buf, REG_EXTENDED | REG_NOSUB | REG_ICASE) == 0) { |
| 105 | + if (regexec(&re, target, 0, NULL, 0) == 0) |
107 | 106 | matched = 1; |
108 | | - regfree(®ex); |
109 | | - break; |
110 | | - } |
111 | | - regfree(®ex); |
| 107 | + regfree(&re); |
112 | 108 | } |
113 | 109 | } |
| 110 | + if (matched) break; |
114 | 111 | token = strtok(NULL, ","); |
115 | 112 | } |
116 | | - free(patterns); |
| 113 | + free(list); |
117 | 114 | return matched; |
118 | 115 | } |
119 | 116 | // ------------------ 匹配 IP/CIDR 黑名单 ------------------ |
120 | | -static int match_banned_ip(const char *ip_str, const char *banned_list) { |
121 | | - if (!ip_str || !banned_list || !*banned_list) return 0; |
122 | | - char *list = strdup(banned_list); |
| 117 | +static int match_banned_ip(const char *ip_str, const char *rules) { |
| 118 | + if (!ip_str || !rules || !*rules) return 0; |
| 119 | + struct in_addr ip4; |
| 120 | + struct in6_addr ip6; |
| 121 | + int is_v4 = inet_pton(AF_INET, ip_str, &ip4) == 1; |
| 122 | + int is_v6 = inet_pton(AF_INET6, ip_str, &ip6) == 1; |
| 123 | + if (!is_v4 && !is_v6) return 0; |
| 124 | + char *list = strdup(rules); |
123 | 125 | char *token = strtok(list, ","); |
124 | 126 | int blocked = 0; |
125 | 127 | while (token) { |
126 | 128 | while (*token == ' ' || *token == '\t') token++; |
127 | | - char *end = token + strlen(token) - 1; |
128 | | - while (end > token && (*end == ' ' || *end == '\t')) *end-- = '\0'; |
129 | | - if (*token) { |
130 | | - char *slash = strchr(token, '/'); |
131 | | - if (!slash) { |
132 | | - if (strcmp(ip_str, token) == 0) { |
133 | | - blocked = 1; |
134 | | - break; |
| 129 | + if (!*token) goto next; |
| 130 | + char *slash = strchr(token, '/'); |
| 131 | + int prefix = -1; |
| 132 | + if (slash) { |
| 133 | + *slash++ = '\0'; |
| 134 | + prefix = atoi(slash); |
| 135 | + } |
| 136 | + /* ---------- IPv4 ---------- */ |
| 137 | + if (is_v4) { |
| 138 | + struct in_addr net4; |
| 139 | + if (inet_pton(AF_INET, token, &net4) == 1) { |
| 140 | + if (prefix < 0) { |
| 141 | + /* 单 IP */ |
| 142 | + if (ip4.s_addr == net4.s_addr) { |
| 143 | + blocked = 1; |
| 144 | + break; |
| 145 | + } |
| 146 | + } else if (prefix >= 0 && prefix <= 32) { |
| 147 | + uint32_t mask = prefix == 0 |
| 148 | + ? 0 |
| 149 | + : htonl(0xFFFFFFFFu << (32 - prefix)); |
| 150 | + if ((ip4.s_addr & mask) == (net4.s_addr & mask)) { |
| 151 | + blocked = 1; |
| 152 | + break; |
| 153 | + } |
135 | 154 | } |
136 | | - } else { |
137 | | - *slash = 0; |
138 | | - int prefix = atoi(slash + 1); |
139 | | - struct in_addr ip, net, mask; |
140 | | - if (inet_pton(AF_INET, token, &net) == 1 && |
141 | | - inet_pton(AF_INET, ip_str, &ip) == 1) { |
142 | | - mask.s_addr = prefix == 0 ? 0 : htonl(0xFFFFFFFF << (32 - prefix)); |
143 | | - if ((ip.s_addr & mask.s_addr) == (net.s_addr & mask.s_addr)) { |
| 155 | + } |
| 156 | + } |
| 157 | + /* ---------- IPv6 ---------- */ |
| 158 | + if (is_v6) { |
| 159 | + struct in6_addr net6; |
| 160 | + if (inet_pton(AF_INET6, token, &net6) == 1) { |
| 161 | + if (prefix < 0) { |
| 162 | + /* 单 IP */ |
| 163 | + if (memcmp(&ip6, &net6, sizeof(ip6)) == 0) { |
144 | 164 | blocked = 1; |
145 | 165 | break; |
146 | 166 | } |
| 167 | + } else if (prefix >= 0 && prefix <= 128) { |
| 168 | + int full = prefix / 8; |
| 169 | + int rem = prefix % 8; |
| 170 | + if (full && |
| 171 | + memcmp(ip6.s6_addr, net6.s6_addr, full) != 0) |
| 172 | + goto next; |
| 173 | + if (rem) { |
| 174 | + uint8_t mask = (uint8_t)(0xFF << (8 - rem)); |
| 175 | + if ((ip6.s6_addr[full] & mask) != |
| 176 | + (net6.s6_addr[full] & mask)) |
| 177 | + goto next; |
| 178 | + } |
| 179 | + blocked = 1; |
| 180 | + break; |
147 | 181 | } |
148 | 182 | } |
149 | 183 | } |
| 184 | + next: |
150 | 185 | token = strtok(NULL, ","); |
151 | 186 | } |
152 | 187 | free(list); |
153 | 188 | return blocked; |
154 | 189 | } |
| 190 | + |
155 | 191 | // ------------------ 网络拦截 ------------------ |
156 | 192 | int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { |
157 | 193 | static int (*real_connect)(int, const struct sockaddr *, socklen_t) = NULL; |
158 | 194 | if (!real_connect) |
159 | 195 | real_connect = dlsym(RTLD_NEXT, "connect"); |
160 | 196 | ensure_config_loaded(); |
| 197 | + if (is_sandbox_user() && addr->sa_family == AF_UNIX) { |
| 198 | + struct sockaddr_un *un = (struct sockaddr_un *)addr; |
| 199 | + fprintf(stderr, |
| 200 | + "Permission denied to access unix socket: %s\n", |
| 201 | + un->sun_path[0] ? un->sun_path : "(abstract)"); |
| 202 | + errno = EACCES; |
| 203 | + return -1; |
| 204 | + } |
161 | 205 | char ip[INET6_ADDRSTRLEN] = {0}; |
162 | | - if (addr->sa_family == AF_INET) |
163 | | - inet_ntop(AF_INET, &((struct sockaddr_in *)addr)->sin_addr, ip, sizeof(ip)); |
164 | | - else if (addr->sa_family == AF_INET6) |
165 | | - inet_ntop(AF_INET6, &((struct sockaddr_in6 *)addr)->sin6_addr, ip, sizeof(ip)); |
166 | | - |
167 | | - if (is_sandbox_user() && banned_hosts && *banned_hosts) { |
168 | | - if (ip[0] && match_banned_ip(ip, banned_hosts)) { |
169 | | - fprintf(stderr, "Permission denied to access %s.\n", ip); |
170 | | - errno = EACCES; // Permission denied |
171 | | - return -1; |
| 206 | + if (addr->sa_family == AF_INET) { |
| 207 | + inet_ntop(AF_INET, |
| 208 | + &((struct sockaddr_in *)addr)->sin_addr, |
| 209 | + ip, sizeof(ip)); |
| 210 | + } else if (addr->sa_family == AF_INET6) { |
| 211 | + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; |
| 212 | + if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
| 213 | + struct in_addr v4; |
| 214 | + memcpy(&v4, &sin6->sin6_addr.s6_addr[12], sizeof(v4)); |
| 215 | + inet_ntop(AF_INET, &v4, ip, sizeof(ip)); |
| 216 | + } else { |
| 217 | + inet_ntop(AF_INET6, &sin6->sin6_addr, ip, sizeof(ip)); |
172 | 218 | } |
173 | 219 | } |
| 220 | + if (is_sandbox_user() && match_banned_ip(ip, banned_hosts)) { |
| 221 | + fprintf(stderr, "Permission denied to access %s.\n", ip); |
| 222 | + errno = EACCES; |
| 223 | + return -1; |
| 224 | + } |
174 | 225 | return real_connect(sockfd, addr, addrlen); |
175 | 226 | } |
176 | 227 | int getaddrinfo(const char *node, const char *service, |
177 | | - const struct addrinfo *hints, struct addrinfo **res) { |
| 228 | + const struct addrinfo *hints, |
| 229 | + struct addrinfo **res) { |
178 | 230 | static int (*real_getaddrinfo)(const char *, const char *, |
179 | | - const struct addrinfo *, struct addrinfo **) = NULL; |
| 231 | + const struct addrinfo *, |
| 232 | + struct addrinfo **) = NULL; |
180 | 233 | if (!real_getaddrinfo) |
181 | 234 | real_getaddrinfo = dlsym(RTLD_NEXT, "getaddrinfo"); |
182 | 235 | ensure_config_loaded(); |
183 | | - if (banned_hosts && *banned_hosts && node && is_sandbox_user()) { |
184 | | - struct in_addr ipv4; |
185 | | - struct in6_addr ipv6; |
186 | | - int is_ip = inet_pton(AF_INET, node, &ipv4) == 1 || |
187 | | - inet_pton(AF_INET6, node, &ipv6) == 1; |
188 | | - if (!is_ip) { |
189 | | - // 仅对域名进行阻塞 |
190 | | - if (match_banned_domain(node, banned_hosts)) { |
191 | | - fprintf(stderr, "Permission denied to access %s.\n", node); |
192 | | - errno = EACCES; |
193 | | - return EAI_SYSTEM; |
194 | | - } |
| 236 | + if (node && is_sandbox_user()) { |
| 237 | + struct in_addr ip4; |
| 238 | + struct in6_addr ip6; |
| 239 | + int is_ip = inet_pton(AF_INET, node, &ip4) == 1 || |
| 240 | + inet_pton(AF_INET6, node, &ip6) == 1; |
| 241 | + if (!is_ip && match_banned_domain(node, banned_hosts)) { |
| 242 | + fprintf(stderr, "Permission denied to access %s.\n", node); |
| 243 | + errno = EACCES; |
| 244 | + return EAI_SYSTEM; |
195 | 245 | } |
196 | 246 | } |
197 | 247 | return real_getaddrinfo(node, service, hints, res); |
|
0 commit comments