invalid data used in `recvmmsg` handling
On FreeBSD (and potentially other platforms), uv__udp_recvmmsg can pass the recv callback a pointer to uninitialized stack memory as the peer address, which can cause a crash in consumers that read it.
For FreeBSD versions >= 11 the uv__udp_recvmmsg path is used. FreeBSD does not have a native recvmmsg; libc emulates it by looping recvmsg, counting every >= 0 return as a message and stopping on the first -1. If the socket is in a receive-shutdown/EOF state, each recvmsg returns 0 (with msg_namelen == 0 and no address written), so the loop fills the entire vector with zero-length, addressless messages.
My understanding is Linux's native recvmmsg doesn't return a batch of zero-length messages from a single EOF the way the FreeBSD libc loop does.
But, I don't believe such return values are exclusive to FreeBSD. For example, a connected UDP socket: after connect(), recvmsg does not populate msg_name and returns msg_namelen == 0. In general the kernel sets msg_namelen to 0 whenever it has no address to report. So I suspect this is reproducible on Linux using a connected datagram socket on the recvmmsg path.
Regardless of how we get a zero msg_namelen, uv__udp_recvmmsg then passes msg_hdr.msg_name to the recv callback unconditionally in udp.c:
handle->recv_cb(handle,
msgs[k].msg_len,
&chunk_buf,
msgs[k].msg_hdr.msg_name, <--------------------
flags);
As seen above, msg_hdr.msg_name is passed even when msg_hdr.msg_namelen == 0. But in that case, the data is uninitialized stack memory (from struct sockaddr_in6 peers[20]; above).
I think a platform-agnostic fix is to check msg_hdr.msg_namelen here. If it's zero, pass NULL instead of msg_hdr.msg_name. I've tested that internally and it appears to resolve this issue.
Unfortunately I cannot share a more specific reproducer due to employer restrictions. But hopefully the description provides enough detail of what I suspect is going on. Please forgive any misunderstandings, I'm looking at this code for the first time.
Trying to understand the scenario on FreeBSD. Are we talking about connected UDP socket after shutdown() ?
I apologize for the confusion. The connected socket was a generalized example where I suspect this is possible on Linux. I added it only to say I don't think the fix is specific to FreeBSD.
But the scenario that I actually hit on FreeBSD was an unconnected, bound UDP socket. I see these flags set in the crashing case:
UV_HANDLE_UDP_RECVMMSGUV_HANDLE_BOUNDUV_HANDLE_REFUV_HANDLE_ACTIVE
And notably not UV_HANDLE_UDP_CONNECTED.
For this unconnected socket, recvmmsg returned nread == 19 with every message having both msg_len and msg_namelen set to 0. I suspect that's because of the previously mentioned recvmsg loop that FreeBSD has.
Okay. I was confused by this line:
If the socket is in a receive-shutdown/EOF state
Ok, I found a reproducer on FreeBSD sending shutdown() on a connected udp socket. I sent this PR: https://github.com/libuv/libuv/pull/5163 which I think addresses the issue.