[posix] more robust netlink message handler (#9613)

Check and ensure at least the netlink header is received in the netlink
socket to be safe before intepreting the buffer as a netlink message.

Per https://linux.die.net/man/3/netlink, should make sure
current message type is not NLMSG_DONE before calling NLMSG_NEXT
to properly handle multi-part netlink message.
This commit is contained in:
Kangping
2023-11-17 21:39:54 +01:00
committed by GitHub
parent 700f6247e5
commit 02ccb9a506
+18 -1
View File
@@ -1636,7 +1636,18 @@ static void processNetlinkEvent(otInstance *aInstance)
length = recv(sNetlinkFd, msgBuffer.buffer, sizeof(msgBuffer.buffer), 0);
VerifyOrExit(length > 0);
#if defined(__linux__)
#define HEADER_SIZE sizeof(nlmsghdr)
#else
#define HEADER_SIZE sizeof(rt_msghdr)
#endif
// Ensures full netlink header is received
if (length < static_cast<ssize_t>(HEADER_SIZE))
{
otLogWarnPlat("[netif] Unexpected netlink recv() result: %ld", static_cast<long>(length));
ExitNow();
}
#if defined(__linux__)
for (struct nlmsghdr *msg = &msgBuffer.nlMsg; NLMSG_OK(msg, static_cast<size_t>(length));
@@ -1654,6 +1665,12 @@ static void processNetlinkEvent(otInstance *aInstance)
#endif
switch (msg->nlmsg_type)
{
#if defined(__linux__)
case NLMSG_DONE:
// NLMSG_DONE indicates the end of the netlink message, exits now
ExitNow();
#endif
case RTM_NEWADDR:
case RTM_DELADDR:
processNetifAddrEvent(aInstance, msg);