Static compilation may cause the program to SIGSEGV during resolve()
I compiled static microsocks(master version) with the following modifications, However, some crashes occurred during runtime. Does this mean that static gcc compilation is not supported? By default dynamic compilation, everything runs perfectly.
```diff
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,7 @@ OBJS = $(SRCS:.c=.o)
LIBS = -lpthread
CFLAGS += -Wall -std=c99
+LDFLAGS += -static-libgcc -static
INSTALL = ./install.sh
```
When executing the curl command for proxy, a segmentation error occurs
```
$ curl --socks5-hostname 127.0.0.1:1080 -vvv google.com >/dev/null 2>&1
$ curl --socks5-hostname 127.0.0.1:1080 -vvv github.com >/dev/null 2>&1
$ curl --socks5-hostname 127.0.0.1:1080 -vvv cloudflare.com >/dev/null 2>&1
```
```
$ ./microsocks
client[4] 127.0.0.1: connected to google.com:80
Segmentation fault (core dumped)
```
gdb info
<details>
```
pwndbg> r
Starting program: /tmp/test/microsocks/microsocks
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7ffa700 (LWP 73069)]
client[4] 127.0.0.1: connected to google.com:80
[Thread 0x7ffff7ffa700 (LWP 73069) exited]
[New Thread 0x7ffff7ff1700 (LWP 73071)]
Thread 3 "microsocks" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff7ff1700 (LWP 73071)]
0x00007ffff7fcc5ca in internal_getent (stream=stream@entry=0x7fffe8000b60, result=result@entry=0x7ffff7fefdc0, buffer=buffer@entry=0x7ffff7ff0210 "", buflen=buflen@entry=1024, errnop=errnop@entry=0x7ffff7ff16b0, herrnop=herrnop@entry=0x7ffff7ff16e8, af=0) at nss_files/files-XXX.c:173
173 nss_files/files-XXX.c: No such file or directory.
LEGEND: STACK | HEAP | CODE | DATA | RWX | RODATA
─────────────────────────────────────[ REGISTERS / show-flags off / show-compact-regs off ]─────────────────────────────────────
*RAX 0x31
*RBX 0x7ffff7ff0610 ◂— 0x0
RCX 0x0
*RDX 0x31
*RDI 0x7ffff7ff0230 ◂— '127.0.0.1 localhost\n'
*RSI 0x7fffe8000d40 ◂— 0x2e302e302e373231 ('127.0.0.')
*R8 0x6
*R9 0x7c
*R10 0x7ffff7ff0210 ◂— 0x0
*R11 0x246
*R12 0x7ffff7ff0230 ◂— '127.0.0.1 localhost\n'
*R13 0x7ffff7ff0230 ◂— '127.0.0.1 localhost\n'
*R14 0x7fffe8000b60 ◂— 0xfbad2488
*R15 0x7ffff7ff1620 ◂— 0x0
*RBP 0x7ffff7ff0210 ◂— 0x0
*RSP 0x7ffff7fefcf0 —▸ 0x7ffff7e66fb0 (flush_cleanup) ◂— endbr64
*RIP 0x7ffff7fcc5ca (internal_getent+122) ◂— test byte ptr [rcx + rdx*2 + 1], 0x20
──────────────────────────────────────────────[ DISASM / x86-64 / set emulate on ]──────────────────────────────────────────────
► 0x7ffff7fcc5ca <internal_getent+122> test byte ptr [rcx + rdx*2 + 1], 0x20
0x7ffff7fcc5cf <internal_getent+127> je internal_getent+156 <internal_getent+156>
↓
0x7ffff7fcc5ec <internal_getent+156> test al, al
0x7ffff7fcc5ee <internal_getent+158> je internal_getent+64 <internal_getent+64>
↓
0x7ffff7fcc590 <internal_getent+64> mov rdx, qword ptr [rsp + 8]
0x7ffff7fcc595 <internal_getent+69> mov rsi, r13
0x7ffff7fcc598 <internal_getent+72> mov rdi, r14
0x7ffff7fcc59b <internal_getent+75> call __libc_readline_unlocked@plt <__libc_readline_unlocked@plt>
0x7ffff7fcc5a0 <internal_getent+80> mov qword ptr [rsp + 0x10], rax
0x7ffff7fcc5a5 <internal_getent+85> test rax, rax
0x7ffff7fcc5a8 <internal_getent+88> js internal_getent+1152 <internal_getent+1152>
───────────────────────────────────────────────────────────[ STACK ]────────────────────────────────────────────────────────────
00:0000│ rsp 0x7ffff7fefcf0 —▸ 0x7ffff7e66fb0 (flush_cleanup) ◂— endbr64
01:0008│-518 0x7ffff7fefcf8 ◂— 0x3e0
02:0010│-510 0x7ffff7fefd00 ◂— 0x14
03:0018│-508 0x7ffff7fefd08 —▸ 0x7ffff7fefdc0 ◂— 0xffffffff
04:0020│-500 0x7ffff7fefd10 —▸ 0x7ffff7fd2003 ◂— 0x6374652f00656372 /* 'rce' */
05:0028│-4f8 0x7ffff7fefd18 —▸ 0x7ffff7ff16b0 ◂— 0x0
06:0030│-4f0 0x7ffff7fefd20 —▸ 0x7ffff7fd2077 ◂— '/etc/hosts'
07:0038│-4e8 0x7ffff7fefd28 —▸ 0x7ffff7ff16e8 ◂— 0x0
─────────────────────────────────────────────────────────[ BACKTRACE ]──────────────────────────────────────────────────────────
► 0 0x7ffff7fcc5ca internal_getent+122
1 0x7ffff7fcd9f4 _nss_files_gethostbyname4_r+180
2 0x45a78b gaih_inet.constprop+4299
3 0x45c3a9 getaddrinfo+297
4 0x403294 resolve+162
5 0x401fca connect_socks_target+567
6 0x402ad7 handshake+466
7 0x402b9e clientthread+36
─────────────────────────────────────────────────────[ THREADS (2 TOTAL) ]──────────────────────────────────────────────────────
► 3 "microsocks" stopped: 0x7ffff7fcc5ca <internal_getent+122>
1 "microsocks" stopped: 0x408acf <accept+79>
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
```
</details>
system info
```
$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
$ uname -srp
Linux 5.4.0-146-generic x86_64
$ curl --version
curl 7.68.0 (x86_64-pc-linux-gnu)
```
关闭于 2025-04-11 4 条评论