port: a reservation kept only while the configuration in force has it

Once reserved, a port is dropped when a configuration comes in force
that neither reserves it anew nor has a service on it: after chroot no
reservation can be made again, only the usage of the socket a service
takes over keeps it. The reservation of the configuration before no
longer waits for its last client to let it go: the reload releases it
at once, the socket living on where a service of the new one has
claimed it, closing where none has.
This commit is contained in:
Vladimir Dubrovin committed 2026-11-21 16:17:35 +03:00
1 parent 17306f63f9
commit eec09dc4e9
4 files changed
+74 -14

No files matched your search

+8 -5
View File
@@ -1516,11 +1516,14 @@ 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 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 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 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 one of its own; a port no service takes stays reserved. After chroot no
configuration is left. The address is an IPv4 one by default, an IPv6 reservation can be made again, so a reload keeps a port only while the
one in brackets. The socket options are written as for \fB-o\fR, and are configuration coming in force reserves it anew or has a service on it:
what the socket has: those which act only before bind() cannot be the port line can be left out of what is read there, the service on
changed on a socket the service takes over. the socket that is already open keeping it. 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 .br
port tcp 443 port tcp 443
.br .br
+19 -2
View File
@@ -442,7 +442,9 @@ static int h_threadpool(int argc, unsigned char **argv){
/* Reserves a listening socket while the configuration is read: a /* Reserves a listening socket while the configuration is read: a
privileged port is opened before setuid and chroot take the rights privileged port is opened before setuid and chroot take the rights
away, and a service of the configuration on the same address takes away, and a service of the configuration on the same address takes
the socket over when it starts. */ the socket over when it starts. After chroot no reservation can be
made again: a reload keeps the port only while the configuration in
force reserves it anew or has a service on it. */
static int h_port(int argc, unsigned char **argv){ static int h_port(int argc, unsigned char **argv){
struct portreservation *r; struct portreservation *r;
PROXYSOCKADDRTYPE sa; PROXYSOCKADDRTYPE sa;
@@ -2461,7 +2463,9 @@ void freeconf(struct extparam *confp){
while(confp->reservations){ while(confp->reservations){
pr = confp->reservations; pr = confp->reservations;
confp->reservations = pr->next; confp->reservations = pr->next;
maindropreserved(pr->listener); /* dropped already at the reload, the configuration after it neither
reserving the port nor using it */
if(pr->listener) maindropreserved(pr->listener);
free(pr); free(pr);
} }
@@ -2610,6 +2614,7 @@ int reload (void){
int error = -2; int error = -2;
struct extparam *old = conf, *new; struct extparam *old = conf, *new;
struct deferred *d; struct deferred *d;
struct portreservation *pr;
if(!(new = malloc(sizeof(*new)))) return -1; if(!(new = malloc(sizeof(*new)))) return -1;
*new = confdefaults; *new = confdefaults;
@@ -2677,6 +2682,18 @@ int reload (void){
/* the new services take over the listening sockets of the old ones on /* the new services take over the listening sockets of the old ones on
the same addresses, which then stop accepting, and start */ the same addresses, which then stop accepting, and start */
for(d = deferred; d; d = d->next) mainclaim(d->service); for(d = deferred; d; d = d->next) mainclaim(d->service);
/* A port reserved before setuid and chroot is dropped now, unless the
configuration in force reserves it anew or has a service on it:
after chroot no reservation can be made again, only the usage of
the socket a service takes over keeps it */
for(pr = old->reservations; pr; pr = pr->next){
struct portreservation *pn;
for(pn = new->reservations; pn; pn = pn->next) if(pn->listener == pr->listener) break;
if(pn) continue;
maindropreserved(pr->listener);
pr->listener = NULL;
}
old->paused++; old->paused++;
while((d = deferred)){ while((d = deferred)){
deferred = d->next; deferred = d->next;
+8 -6
View File
@@ -545,12 +545,14 @@ static void slotrelease(struct srvparam *srv){
/* The port command: the listening socket opened here, at the time the /* The port command: the listening socket opened here, at the time the
configuration is read, while the rights to open a privileged port may configuration is read, while the rights to open a privileged port may
still be had — before setuid and chroot. A service of the configuration 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 on the same address, of the same type, takes the socket over when it
over when it starts; a reservation no service takes keeps the port until starts; a port no service takes stays reserved while the configuration
the configuration is left. A reservation of the configuration before in force has it: a reload drops it unless the new configuration
already holds the port: its socket is shared, both reservations dropping reserves it anew or a service of it claims it — after chroot only the
their hold with the configuration that made them. NULL if the socket usage of the socket keeps it. A reservation of the configuration
cannot be opened. */ before already holds the port: its socket is shared, the holds dropped
when the configurations that made them are left or a reload without
a claim on it comes. NULL if the socket cannot be opened. */
void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg){ void * mainreserve(int isudp, PROXYSOCKADDRTYPE *sa, const struct sockoptset *opts, int maxseg){
struct listener *l, *nl; struct listener *l, *nl;
SOCKET sock; SOCKET sock;
+39 -1
View File
@@ -36,13 +36,15 @@ def run(t):
# a service after the reservation takes the socket over # a service after the reservation takes the socket over
reserved = t.free_port() reserved = t.free_port()
adm = t.free_port()
server = t.start("port_taken", f""" server = t.start("port_taken", f"""
log log
port tcp 127.0.0.1:{reserved} port tcp 127.0.0.1:{reserved}
auth iponly auth iponly
allow * allow *
proxy -p{reserved} -i127.0.0.1 proxy -p{reserved} -i127.0.0.1
""", ports=[reserved]) admin -p{adm}
""", ports=[reserved, adm])
out = t.wait_output(server, "taken over") out = t.wait_output(server, "taken over")
t.contains(out, "taken over", t.contains(out, "taken over",
@@ -52,6 +54,42 @@ def run(t):
proxy=f"127.0.0.1:{reserved}").status, proxy=f"127.0.0.1:{reserved}").status,
"a request through the taken-over port is served") "a request through the taken-over port is served")
# --- after chroot no reservation can be made again: a reload with
# the service alone, the port line left out, keeps the port -------
taken_before = server.output().count("taken over")
t.write_config("port_taken", f"""
log
auth iponly
allow *
proxy -p{reserved} -i127.0.0.1
admin -p{adm}
""")
t.http(f"http://127.0.0.1:{adm}/R")
out = t.wait_output(server, "taken over", since=len(server.output()))
t.ok("a reload with the service alone keeps the reserved port") \
if server.output().count("taken over") > taken_before else \
t.fail("a reload with the service alone keeps the reserved port",
"the socket taken over again", out)
t.eq(200, t.http(f"http://127.0.0.1:{origin}/data",
proxy=f"127.0.0.1:{reserved}").status,
"and it is served as before")
# a reload with neither the reservation nor a service drops the port
t.write_config("port_taken", f"""
log
auth iponly
allow *
admin -p{adm}
""")
t.http(f"http://127.0.0.1:{adm}/R")
deadline = time.time() + 5
while time.time() < deadline and t.wait_port(reserved, timeout=0.2):
time.sleep(0.2)
t.ok("a reload without the reservation and without a service drops it") \
if not t.wait_port(reserved, timeout=0.2) else \
t.fail("a reload without the reservation and without a service drops it",
f"port {reserved} closed", "open")
# a reserved UDP port is taken over the same way: bound by the # a reserved UDP port is taken over the same way: bound by the
# reservation, answered by the service # reservation, answered by the service
udpp = t.free_port() udpp = t.free_port()