upas-pop3-stls-double-free: every STLS session on port 110 crashes pop3
The 9legacy patch upas-certchain taught pop3 to send a certificate
chain. Its stlscmd in sys/src/cmd/upas/pop3/pop3.c sets
conn.cert = tlscert->pem;
conn.chain = tlscert->next;
calls tlsServer, and afterwards calls freecertchain(tlscert). But
tlsServer frees conn.cert itself on success (pushtls(2): "The input
cert is freed"), and freecertchain frees each node's pem, so
tlscert->pem is freed twice. The allocator panics
panic: D2B called on non-block ... (double-free?)
and the process is left Broken. This happens after every
successful STLS handshake, before the first command over TLS, so
STLS on port 110 never works. It is reachable before
authentication.
Port 995 is not affected: tlssrv does the TLS there and pop3 runs
without -t. smtpd's STARTTLS, also changed by upas-certchain, frees
only the rest of the chain and is not affected.
The fix deletes the freecertchain call. After a successful
tlsServer the canonical callers free nothing (tlssrv, smtpd, httpd,
and pop3 itself before upas-certchain); the rest of the chain lives
until the process exits, which for pop3 is the end of the
connection.
Separately, upas-certchain left "static int ntlscert;" unused in
pop3.c. It is not touched here.
Apply
cd /
ape/patch -p0 < /path/to/upas-pop3-stls-double-free/upas-pop3-stls-double-free.diff
Rebuild
cd /sys/src/cmd/upas/common && mk
cd /sys/src/cmd/upas/pop3 && mk safeinstall
The first mk builds libcommon.a$O, which pop3 links against.
The old binary is kept as /$objtype/bin/upas/_pop3. No
restart is needed: aux/listen runs a new pop3 for each
connection.
safeinstall creates the new file, so it is owned by whoever
built it rather than sys. To restore the owner, open the
fossil console as the host owner:
con -l /srv/fscons
fsys main
wstat /active/386/bin/upas/pop3 - sys - - -
Then type Ctrl-\ and q at the >>> prompt to leave con.
Verification
From another machine, open STLS on port 110 and send a command
over TLS, e.g.
curl -v --ssl-reqd pop3://HOST/
Before: the session drops after the handshake, and ps shows a
Broken pop3 owned by none. After: the server answers CAPA
over TLS and no process is left Broken.
|