From 46297ecf7e3928e007ae17b3b9b5d7503020fb6f Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Wed, 18 Nov 2026 20:10:33 +0300 Subject: [PATCH] socks: a host name with control bytes or spaces is refused where it is read --- src/common.c | 15 ++++++++ src/proxy.h | 1 + src/socks.c | 2 ++ src/udpsockmap.c | 1 + tests/cases/socks.py | 83 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+) diff --git a/src/common.c b/src/common.c index 3571845..2ad4af4 100644 --- a/src/common.c +++ b/src/common.c @@ -542,11 +542,26 @@ int parsehost(int family, unsigned char *host, struct sockaddr *sa){ return ret; } +/* A host name is written into the request lines of parent protocols and + matched against rules, so a control byte or a space in it could end a + line early or pose as another destination. A name carrying one is + refused where it is read, and every name a client sends is read on + its way to the field. */ +int badhostname(const char *hostname){ + const unsigned char *c; + + if(!hostname) return 1; + for(c = (const unsigned char *)hostname; *c; c++) + if(*c <= ' ' || *c == 0x7f) return 1; + return 0; +} + int parsehostname(char *hostname, struct clientparam *param, uint16_t port){ char *sp=NULL,*se=NULL; int ret = 0; if(!hostname || !*hostname)return 2; + if(badhostname(hostname)) return 1; if(*hostname == '[') se=strchr(hostname, ']'); if ((sp = strchr(se?se:hostname, ':'))) { if(strchr(sp+1, ':'))sp = NULL; diff --git a/src/proxy.h b/src/proxy.h index 539b4de..face6db 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -370,6 +370,7 @@ void hashdelete(struct hashtable *ht, void* name); int hashresolv(struct hashtable *ht, void* name, void* value, uint32_t *ttl); int parsehost(int family, unsigned char *host, struct sockaddr *sa); +int badhostname(const char *hostname); int parsehostname(char *hostname, struct clientparam *param, uint16_t port); int parseusername(char *username, struct clientparam *param, int extpasswd); int parseconnusername(char *username, struct clientparam *param, int extpasswd, uint16_t port); diff --git a/src/socks.c b/src/socks.c index 3eebb95..40a132f 100644 --- a/src/socks.c +++ b/src/socks.c @@ -137,6 +137,7 @@ void * sockschild(struct clientparam* param) { default: RETURN(997); } + if(badhostname((char *)buf)) {RETURN(21);} if(param->hostname)free(param->hostname); param->hostname = (unsigned char *)strdup((char *)buf); if(!param->hostname){RETURN(21);} @@ -159,6 +160,7 @@ void * sockschild(struct clientparam* param) { param->service = S_SOCKS45; if(sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, 0, conf.timeouts[STRING_S]) < 0) {RETURN(441);} buf[127] = 0; + if(badhostname((char *)buf)) {RETURN(21);} if(param->hostname)free(param->hostname); param->hostname = (unsigned char *)strdup((char *)buf); if(!param->hostname){RETURN(21);} diff --git a/src/udpsockmap.c b/src/udpsockmap.c index a4999bd..8e60f62 100644 --- a/src/udpsockmap.c +++ b/src/udpsockmap.c @@ -281,6 +281,7 @@ int udpsockmap(struct clientparam *param, int timeo) if (len < 7 + sz) return 485; memcpy(dstnamebuf, base + 5, sz); dstnamebuf[sz] = 0; + if (badhostname(dstnamebuf)) continue; dstname = dstnamebuf; i = 5 + sz; if (!getip46(param->srv->family, (unsigned char *)dstnamebuf, diff --git a/tests/cases/socks.py b/tests/cases/socks.py index 8f63edf..69c3d7e 100644 --- a/tests/cases/socks.py +++ b/tests/cases/socks.py @@ -1,5 +1,19 @@ """The SOCKS proxy, reaching the built-in server.""" +import socket +import struct + + +def recvall(sock, count): + """Bytes from a socket, fewer only when the peer stops sending.""" + data = b"" + while len(data) < count: + chunk = sock.recv(count - len(data)) + if not chunk: + break + data += chunk + return data + def run(t): if not t.skip_unless("httpsrv"): @@ -96,3 +110,72 @@ def run(t): "wrong SOCKS5 credentials are refused") t.ne(None, t.socks_connect(guarded, "127.0.0.1", srv), "SOCKS5 without credentials is refused") + + # --- names a client may not choose ------------------------------------- + # A destination name travels into the request lines of parent protocols, + # so a control byte or a space in it could end the line early and name + # another destination. Such a name is refused where it is read, before + # anything is resolved or connected: reply 1, not the reply of a + # destination which does not answer. + sname = t.free_port() + standin = t.free_port() + t.start("socks_names", f""" + log + fakeresolve + auth iponly + allow * + socks -p{sname} + """, ports=[sname]) + + def socks5_name(name, port): + """The reply code of a SOCKS5 CONNECT naming the given name.""" + with socket.create_connection(("127.0.0.1", sname), t.timeout) as s: + s.settimeout(t.timeout) + s.sendall(b"\x05\x01\x00") + if recvall(s, 2) != b"\x05\x00": + return None + s.sendall(b"\x05\x01\x00\x03" + bytes([len(name)]) + name + + struct.pack("!H", port)) + reply = recvall(s, 4) + return reply[1] if len(reply) > 1 else None + + def socks4_name(name, port): + """The reply code of a SOCKS4a CONNECT naming the given name.""" + with socket.create_connection(("127.0.0.1", sname), t.timeout) as s: + s.settimeout(t.timeout) + s.sendall(struct.pack(">BBH", 4, 1, port) + + socket.inet_aton("0.0.0.0") + b"case\x00" + name + + b"\x00") + reply = recvall(s, 8) + return reply[1] if len(reply) > 1 else None + + t.eq(1, socks5_name(b"127.0.0.1\r\nGET /other HTTP/1.1\r\n\r\nstand-in.example", + standin), + "a name with CR LF in it is refused") + t.eq(91, socks4_name(b"127.0.0.1\r\nGET /other HTTP/1.1\r\n\r\nstand-in.example", + standin), + "SOCKS4a refuses a name with CR LF in it") + t.eq(1, socks5_name(b"127.0.0.1 stand-in.example", standin), + "a name with a space in it is refused") + t.eq(1, socks5_name(b"stand-in\x7f.example", standin), + "a name with a control byte in it is refused") + + # a well-formed name is let through, to the 127.0.0.2 fakeresolve maps + # every name to; where that address can be served, the tunnel has to be + # carried all the way to it + probe = socket.socket() + try: + probe.bind(("127.0.0.2", 0)) + except OSError: + probe.close() + t.skip("the stand-in address 127.0.0.2 can not be bound here") + else: + probe.close() + stop = t.raw_server(standin, b"from the stand-in\r\n", + host="127.0.0.2") + t.eq(0, socks5_name(b"stand-in.example", standin), + "a well-formed name is connected by name") + t.eq(90, socks4_name(b"stand-in.example", standin), + "a well-formed name is connected by name over SOCKS4a") + stop() +