I just fixed a 12 year old bug in my v4frontend. Recently my IPv4 frontend for I...
kasperd @kasperd@westergaard.social
I just fixed a 12 year old bug in my v4frontend.
Recently my IPv4 frontend for IPv6-only servers has been segfaulting approximately once every 4-5 months. A bug happening that rarely is not easy to narrow down. For a long time I had no idea how to reproduce the problem.
Initially I looked very carefully on the most recent code changes (of which there hadn’t been many) to see if I could spot a possible explanation. But nothing came up.
I made a script to restart the process with an older build after any segfault occurred hoping this would keep it stable in the time frame between a segfault and when I would be able to inspect it. But eventually the older version would segfault as well.
Finally I started strace commands to observe exactly what’s going on in two of the helper processes to see exactly what’s happening to them during any future segfault. I left out the main process for performance reasons. The plan was to wait however many months it would take to get some information out of that.
I have also been working on additional monitoring so I was looking into bringing up a new daemon for monitoring purposes on the frontend host. In doing so I was considering the possibility the listening port might conflict with the frontend. At that point I realized that if such an issue could happen there would already be a problem with sshd and named listening on port 22 and 53.
When the frontend connects to the IPv6-only backend it preserves the original client port number and the client IPv4 address is embedded in the lower 32 bits of the IPv6 address. Setting up a socket like that required root privileges, so a separate process is used for just that purpose so the main process doesn’t need root privileges.
But what would happen if the client port happened to be 22 or 53? I knew I had no code to deal with that. And upon testing I found that doing so would trigger exactly the crash I had experienced. So now I knew exactly how to reproduce the problem.
So what was going wrong in the code?
The helper process would get an error code saying address already in use. In that case the helper process just gave up and shut down.
The main process would see an end-of-file condition on the socket from the helper process. The main process had a bug in the error handling so the end-of-file was interpreted as a message having been received. So it would try to parse a message even though none had been received at which point the main process segfaulted (probably on a NULL pointer dereference).
I fixed the error handling so it would no longer segfault, but that alone wouldn’t be enough as the main process would still shut down because the loss of the helper process was a fatal error. So I also fixed the helper process. If the port is already in use it will now fall back to using an ephemeral port instead.
The result of this is that sometimes backend servers might see a random client port number rather than the one the client actually used, but they will still see the correct client IPv4 address. That seems like a much better deal than crashing the frontend.
So how come such a bug could exist for 12 years and not be regularly triggered by port scanning? There is a couple of reasons. Port scanning tend to be looking through many destination ports, but it might not be attempting all different source ports. Additionally simply scanning the port is not enough. After connecting the client has to also send a valid hostname in order to trigger the buggy code.
So what clients triggered this bug in the first place? I don’t know, and given that the bug is now fixed it’s not important to find out. Maybe some port scans would actually use client port 22 or 53 (possibly as an attempt to evade firewalls) and send a valid hostname through TLS-SNI. Alternatively it might be a legitimate client connecting through a CGN that makes use of all possible port numbers for connection mappings, and occasionally that could include port 22 or 53.