From 5cd842e3be2f51b82e4ff38af6ff1a5c3010c07d Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Sat, 21 Nov 2026 16:12:40 +0300 Subject: [PATCH] port: a listening socket reserved while the configuration is read port tcp|udp [ip:]port [socket_options] opens, binds and listens at the time the line is read, so a privileged port can be taken before setuid and chroot drop the rights to open it: a service of the configuration on the same address, of the same type, takes the socket over when it starts, instead of binding one of its own, and a port no service takes stays reserved until the configuration is left. A reservation of the configuration before holds the port over a reload: the new one shares its socket, each dropping its hold with its configuration. The socket is what the reservation made of it: the options which act only before bind() cannot follow a service's wishes on a socket already bound, so the service takes it as it is, its other options set on it. The reservation is built from the same default address a service is, so it matches in every byte a listener is compared by. --- man/3proxy.cfg.5 | 21 +++++ src/conf.c | 46 ++++++++++ src/mainlisten.c | 183 +++++++++++++++++++++++++++++++++---- src/proxy.h | 2 + src/structures.h | 14 ++- tests/cases/portreserve.py | 73 +++++++++++++++ 6 files changed, 316 insertions(+), 23 deletions(-) create mode 100644 tests/cases/portreserve.py diff --git a/man/3proxy.cfg.5 b/man/3proxy.cfg.5 index 48875b1..197df78 100644 --- a/man/3proxy.cfg.5 +++ b/man/3proxy.cfg.5 @@ -1526,6 +1526,27 @@ and If file monitored changes in modification time or size, 3proxy reloads configuration within one minute. Any number of files may be monitored. +.br +.BR port +\fItcp|udp\fR \fI[:]\fR [\fIsocket_options\fR] +.br + Reserves a listening socket at the time the configuration is read: a +privileged port can be opened before \fBsetuid\fR and \fBchroot\fR take the +rights to open it away, the \fBport\fR line placed above them and the +service below. A service of the configuration on the same address, of +the same type, takes the socket over when it starts, instead of opening +one of its own; a port no service takes stays reserved until the +configuration is left. The address is an IPv4 one by default, an IPv6 +one in brackets. The socket options are written as for \fB-o\fR, and are +what the socket has: those which act only before bind() cannot be +changed on a socket the service takes over. +.br + port tcp 443 +.br + port tcp [::]:80 TCP_FASTOPEN +.br + port udp 53 + .br .BR setuid \fI\fR diff --git a/src/conf.c b/src/conf.c index 9ec6429..cc6ce7a 100644 --- a/src/conf.c +++ b/src/conf.c @@ -435,6 +435,43 @@ static int h_threadpool(int argc, unsigned char **argv){ return 0; } +/* Reserves a listening socket while the configuration is read: a + privileged port is opened before setuid and chroot take the rights + away, and a service of the configuration on the same address takes + the socket over when it starts. */ +static int h_port(int argc, unsigned char **argv){ + struct portreservation *r; + PROXYSOCKADDRTYPE sa; + struct sockoptset opts; + int isudp; + + if(!strcmp((char *)argv[1], "udp")) isudp = 1; + else if(!strcmp((char *)argv[1], "tcp")) isudp = 0; + else return 1; + memset(&opts, 0, sizeof(opts)); + if(argc > 3 && getopts((char *)argv[3], &opts)) return 2; + /* as a service's address: from the defaults, so the same in every + byte a listener is matched by */ + sa = conf->intsa; + if(strchr((char *)argv[2], ':') || *argv[2] == '['){ + if(parsehost(46, argv[2], (struct sockaddr *)&sa)) return 3; + } + else { + *SAFAMILY(&sa) = AF_INET; + *SAPORT(&sa) = htons((uint16_t)atoi((char *)argv[2])); + } + if(!*SAPORT(&sa)) return 3; + if(!(r = malloc(sizeof(*r)))) return 4; + if(!(r->listener = mainreserve(isudp, &sa, &opts, 0))){ + free(r); + fprintf(stderr, "Unable to reserve port, line %d\n", linenum); + return 5; + } + r->next = conf->reservations; + conf->reservations = r; + return 0; +} + static int h_force(int argc, unsigned char **argv){ conf->noforce = 0; @@ -2143,6 +2180,7 @@ struct commands commandhandlers[]={ {NULL, "minlogqueue", h_minlogqueue, 2, 2}, {NULL, "maxlogqueue", h_maxlogqueue, 2, 2}, {NULL, "threadpool", h_threadpool, 2, 3}, + {NULL, "port", h_port, 3, 4}, {NULL, "force", h_force, 1, 1}, {NULL, "noforce", h_noforce, 1, 1}, {NULL, "parentretries", h_parentretries, 2, 2}, @@ -2411,11 +2449,19 @@ void freeconf(struct extparam *confp){ struct trafcount * tc; struct ace *acl; struct filemon *fm; + struct portreservation *pr; int counterd, archiverc; unsigned char **archiver; unsigned char * logformat; int i; + while(confp->reservations){ + pr = confp->reservations; + confp->reservations = pr->next; + maindropreserved(pr->listener); + free(pr); + } + _3proxy_mutex_lock(&tc_mutex); confp->trafcountfunc = NULL; tc = confp->trafcounter; diff --git a/src/mainlisten.c b/src/mainlisten.c index f9f6085..c13c0a5 100644 --- a/src/mainlisten.c +++ b/src/mainlisten.c @@ -289,26 +289,30 @@ static int bindtimeopt(const char *name){ !strcmp(name, "TCP_FASTOPEN"); } -/* Whether a socket with the options of l can be made one with those of srv: +/* Whether a socket with the options of l can be made one with those of b: the same bind-time ones, with the same values; TCP_MAXSEG, whose default is not 0, cannot be turned off again. */ -static int sameopts(struct listener *l, struct srvparam *srv){ - const struct sockoptset *a = &l->opts, *b = &srv->lissockopts; +static int sameoptset(struct listener *l, const struct sockoptset *b){ + const struct sockoptset *a = &l->opts; int i; - for(i = 0; sockopts[i].optname && i < MAXSOCKOPTS; i++){ - unsigned bit = 1u << i; + for(i = 0; sockopts[i].optname && i < MAXSOCKOPTS; i++){ + unsigned bit = 1u << i; - if(bindtimeopt(sockopts[i].optname)){ - if((a->set & bit) != (b->set & bit)) return 0; - if((a->set & bit) && a->value[i] != b->value[i]) return 0; - } + if(bindtimeopt(sockopts[i].optname)){ + if((a->set & bit) != (b->set & bit)) return 0; + if((a->set & bit) && a->value[i] != b->value[i]) return 0; + } #ifdef TCP_MAXSEG - else if(sockopts[i].opt == TCP_MAXSEG && *sockopts[i].optname == 'T' && - (a->set & bit) && !(b->set & bit)) return 0; + else if(sockopts[i].opt == TCP_MAXSEG && *sockopts[i].optname == 'T' && + (a->set & bit) && !(b->set & bit)) return 0; #endif - } - return 1; + } + return 1; +} + +static int sameopts(struct listener *l, struct srvparam *srv){ + return sameoptset(l, &srv->lissockopts); } /* Sets the options of srv on the socket of l taken over: those given, and @@ -345,6 +349,32 @@ static int samelistener(struct listener *l, struct srvparam *srv, int isudp){ sameopts(l, srv); } +/* samelistener without the options: the address, and what else makes a + socket of a place. */ +static int samelistener_noopts(struct listener *l, struct srvparam *srv, int isudp){ + if(l->isudp != isudp || l->cbdest) return 0; +#ifdef __linux__ + if(!samestr(l->netns, srv->inetns)) return 0; +#endif +#if defined SO_BINDTODEVICE || defined IP_BOUND_IF + if(!samestr(l->device, srv->ibindtodevice)) return 0; +#endif + return SASIZE(&l->sa) == SASIZE(&srv->intsa) && + !memcmp(&l->sa, &srv->intsa, SASIZE(&srv->intsa)); +} + +/* Whether a service takes over a reserved port: the socket is bound + already, so the options which act only before bind() cannot follow the + service's wishes — it must name them the same, or not name them at all. + The rest of its options are set on the socket when it is taken. */ +/* Whether a service takes over a reserved port: the socket is bound + already, so it is what the reservation made of it — the options which + act only before bind() can follow neither's wishes but their author's, + and the service's other options are set on it when it is taken. */ +static int samereservation(struct listener *l, struct srvparam *srv, int isudp){ + return samelistener_noopts(l, srv, isudp); +} + /* Whether s goes on listener l: one connecting back (-r) on the pseudo-listener of the place it connects to, any other on the socket for its address. */ @@ -410,14 +440,24 @@ void mainclaim(void *service){ } _3proxy_mutex_lock(&listener_mutex); for(l = listeners; l; l = l->next){ - if(!l->owners || !listenerfor(l, s)) continue; - if(listenerhas(l, srv->config)){ - found = l; - break; + /* a reserved port: no service of it, its socket open; a service + of the address takes it over. A listener whose services are + gone, their last clients draining, has no socket to give */ + if(!l->owners){ + if(l->sock != INVALID_SOCKET && samereservation(l, srv, s->isudp)){ + found = l; + break; + } + continue; + } + if(!listenerfor(l, s)) continue; + if(listenerhas(l, srv->config)){ + found = l; + break; + } + if(!found || (l->owners->service == srv->service && found->owners->service != srv->service)) + found = l; } - if(!found || (l->owners->service == srv->service && found->owners->service != srv->service)) - found = l; - } if(!found && (found = newl)){ newl = NULL; found->next = listeners; @@ -502,6 +542,109 @@ static void slotrelease(struct srvparam *srv){ _3proxy_mutex_unlock(&srv->counter_mutex); } +/* The port command: the listening socket opened here, at the time the + configuration is read, while the rights to open a privileged port may + still be had — before setuid and chroot. A service of the configuration + on the same address, with the same bind-time options, takes the socket + over when it starts; a reservation no service takes keeps the port until + the configuration is left. A reservation of the configuration before + already holds the port: its socket is shared, both reservations dropping + their hold with the configuration that made them. NULL if the socket + cannot be opened. */ +void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg){ + struct listener *l, *nl; + SOCKET sock; + + if(!(nl = malloc(sizeof(*nl)))) return NULL; + memset(nl, 0, sizeof(*nl)); + nl->isudp = isudp; + nl->sa = *sa; + nl->opts = *opts; + nl->maxseg = maxseg; + /* older than the configuration being read: a service taking the + socket over applies its own options to it, as one of a + configuration before does */ + nl->optversion = conf->version - 1; + nl->sock = INVALID_SOCKET; + _3proxy_mutex_lock(&listener_mutex); + for(l = listeners; l; l = l->next){ + if(l->isudp != isudp || l->cbdest || l->sock == INVALID_SOCKET) continue; + if(SASIZE(&l->sa) != SASIZE(sa) || memcmp(&l->sa, sa, SASIZE(sa))) continue; + if(!sameoptset(l, opts)) continue; + l->users++; /* this reservation's hold with it */ + _3proxy_mutex_unlock(&listener_mutex); + free(nl); + return l; + } + sock = so._socket(so.state, SASOCK(sa), isudp? SOCK_DGRAM: SOCK_STREAM, + isudp? IPPROTO_UDP: IPPROTO_TCP); + if(sock == INVALID_SOCKET){ + _3proxy_mutex_unlock(&listener_mutex); + free(nl); + return NULL; + } + setopts(sock, opts, maxseg); +#ifndef _WIN32 + fcntl(sock, F_SETFL, O_NONBLOCK | fcntl(sock, F_GETFL)); +#else + { + unsigned long ul = 1; + ioctlsocket(sock, FIONBIO, &ul); + } +#endif + if(so._bind(so.state, sock, (struct sockaddr *)sa, SASIZE(sa)) == -1 || + (!isudp && so._listen(so.state, sock, 1+(conf->maxchild>>3)) == -1)){ + so._closesocket(so.state, sock); + _3proxy_mutex_unlock(&listener_mutex); + free(nl); + return NULL; + } + nl->sock = sock; + nl->users = 1; /* the reservation's hold */ + _3proxy_mutex_init(&nl->open_mutex); + _3proxy_mutex_init(&nl->count_mutex); + nl->next = listeners; + listeners = nl; + _3proxy_mutex_unlock(&listener_mutex); + return nl; +} + +/* The configuration which made the reservation is left: its hold on the + listener is given back, the socket closed and the listener freed by + whoever lets go of it last, a reservation or the clients of a service. */ +void maindropreserved(void *v){ + struct listener *l = (struct listener *)v, **lp; + SOCKET sock = INVALID_SOCKET; + struct udpsession *hand = NULL; + int last; + + _3proxy_mutex_lock(&listener_mutex); + last = !--l->users; + if(last) for(lp = &listeners; *lp; lp = &(*lp)->next) if(*lp == l){ + *lp = l->next; + sock = l->sock; + l->sock = INVALID_SOCKET; + hand = l->udphand; + l->udphand = NULL; + break; + } + _3proxy_mutex_unlock(&listener_mutex); +#ifndef NOUDPMAIN + udpendall(hand, 26); +#endif + if(sock != INVALID_SOCKET) so._closesocket(so.state, sock); + if(!last) return; +#ifdef WITH_UN + ununlink(&l->sa, l->undev, l->unino); +#endif + _3proxy_mutex_destroy(&l->open_mutex); + _3proxy_mutex_destroy(&l->count_mutex); + if(l->netns) free(l->netns); + if(l->device) free(l->device); + if(l->cbdest) free(l->cbdest); + free(l); +} + /* After a reload, unless noforce: a client goes by the rules of a newest service of its type on its listener, now accepting there, re-authenticated as it if auth is set. It takes one of that service's maxconn slots, diff --git a/src/proxy.h b/src/proxy.h index 37fb1ca..024c8ca 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -644,6 +644,8 @@ int getopts(const char *s, struct sockoptset *opts); void setopts(SOCKET s, const struct sockoptset *opts, int maxseg); char * printopts(char *sep); char * printfeatures(char *sep); +void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg); +void maindropreserved(void *listener); #define WEBBANNERS 35 diff --git a/src/structures.h b/src/structures.h index d797ff0..eb6b5d2 100644 --- a/src/structures.h +++ b/src/structures.h @@ -943,9 +943,16 @@ struct threadpool { extern struct threadpool tpool; struct filemon { - char * path; - struct stat sb; - struct filemon *next; + char * path; + struct stat sb; + struct filemon *next; +}; + +/* A port the configuration reserved with the port command: its listener, + opaque here, held until the configuration is left */ +struct portreservation { + struct portreservation *next; + void *listener; }; @@ -1001,6 +1008,7 @@ struct extparam { int refs; struct hashtable pwl_table; /* users */ int resolvmode; /* RESOLVE_*: the last read, each service taking the one read before it */ + struct portreservation *reservations; /* the port command's, dropped with the configuration */ }; struct property { diff --git a/tests/cases/portreserve.py b/tests/cases/portreserve.py new file mode 100644 index 0000000..23f004f --- /dev/null +++ b/tests/cases/portreserve.py @@ -0,0 +1,73 @@ +"""The port command: a listening socket reserved where the configuration +is read, a service of the same address taking it over. + +A privileged port is opened before setuid and chroot take the rights +away, and the service named after them runs on the socket that is +already open: what would be a bind() denied is a takeover instead. A +port no service takes stays reserved, its socket listening on its own. +""" + +import time + + +def run(t): + if not t.skip_unless("httpsrv"): + return + + origin = t.free_port() + t.start("port_origin", f""" + log + auth iponly + allow * + http data * /data** + httpsrv -p{origin} + """, ports=[origin]) + + # a port reserved and never taken: the socket listens by itself + solo = t.free_port() + t.start("port_solo", f""" + log + port tcp 127.0.0.1:{solo} + """, ports=[solo]) + t.ok("a port with no service is kept listening") \ + if t.wait_port(solo) else \ + t.fail("a port with no service is kept listening", + f"port {solo} open", "closed") + + # a service after the reservation takes the socket over + reserved = t.free_port() + server = t.start("port_taken", f""" + log + port tcp 127.0.0.1:{reserved} + auth iponly + allow * + proxy -p{reserved} -i127.0.0.1 + """, ports=[reserved]) + + out = t.wait_output(server, "taken over") + t.contains(out, "taken over", + "the service runs on the socket the reservation opened") + + t.eq(200, t.http(f"http://127.0.0.1:{origin}/data", + proxy=f"127.0.0.1:{reserved}").status, + "a request through the taken-over port is served") + + # a reserved UDP port is taken over the same way: bound by the + # reservation, answered by the service + udpp = t.free_port() + dns = t.start("port_udp", f""" + log + port udp 127.0.0.1:{udpp} + nserver 127.0.0.1 + nscache 1024 + nsrecord here.test 10.11.12.13 + dnspr -p{udpp} -i127.0.0.1 + """) + + deadline = time.time() + 5 + while time.time() < deadline: + if t.dns_query(udpp, "here.test"): + break + time.sleep(0.05) + t.eq(["10.11.12.13"], t.dns_query(udpp, "here.test"), + "a UDP service answers on its reserved port")