mirror of
https://github.com/espressif/openthread.git
synced 2026-09-18 15:10:12 +00:00
[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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user