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:37 +03:00
1 parent 5cd842e3be
commit e193732fcd
4 files changed
+74 -14

No files matched your search

+8 -5
View File
@@ -1535,11 +1535,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
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.
one of its own; a port no service takes stays reserved. After chroot no
reservation can be made again, so a reload keeps a port only while the
configuration coming in force reserves it anew or has a service on it:
the port line can be left out of what is read there, the service on
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
port tcp 443
.br
+19 -2
View File
@@ -438,7 +438,9 @@ static int h_threadpool(int argc, unsigned char **argv){
/* 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. */
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){
struct portreservation *r;
PROXYSOCKADDRTYPE sa;
@@ -2458,7 +2460,9 @@ void freeconf(struct extparam *confp){
while(confp->reservations){
pr = confp->reservations;
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);
}
@@ -2607,6 +2611,7 @@ int reload (void){
int error = -2;
struct extparam *old = conf, *new;
struct deferred *d;
struct portreservation *pr;
if(!(new = malloc(sizeof(*new)))) return -1;
*new = confdefaults;
@@ -2674,6 +2679,18 @@ int reload (void){
/* the new services take over the listening sockets of the old ones on
the same addresses, which then stop accepting, and start */
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++;
while((d = deferred)){
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
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. */
on the same address, of the same type, takes the socket over when it
starts; a port no service takes stays reserved while the configuration
in force has it: a reload drops it unless the new configuration
reserves it anew or a service of it claims it — after chroot only the
usage of the socket keeps it. A reservation of the configuration
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){
struct listener *l, *nl;
SOCKET sock;
+39 -1
View File
@@ -36,13 +36,15 @@ def run(t):
# a service after the reservation takes the socket over
reserved = t.free_port()
adm = 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])
admin -p{adm}
""", ports=[reserved, adm])
out = t.wait_output(server, "taken over")
t.contains(out, "taken over",
@@ -52,6 +54,42 @@ def run(t):
proxy=f"127.0.0.1:{reserved}").status,
"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
# reservation, answered by the service
udpp = t.free_port()