Skip to content

Commit fe844bf

Browse files
shailend-ggvisor-bot
authored andcommitted
Leave nlmsg_pid at 0 for netlink multicast
Multicast messages from the kernel have nlmsg_pid=0. Unicast messages from the kernel have nlmsg_pid as the socket's pid. Multicast messages from userland have whatever nlmsg_pid set by the app. PiperOrigin-RevId: 836804950
1 parent 2a3e16f commit fe844bf

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

pkg/sentry/socket/netlink/socket.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ func (s *Socket) HandleInterfaceChangeEvent(ctx context.Context, idx int32, i in
414414
panic(fmt.Sprintf("Non-ROUTE netlink socket (protocol %d) cannot handle interface events", s.Protocol()))
415415
}
416416

417-
ms := nlmsg.NewMessageSet(s.GetPortID(), 0)
417+
// Multicast messages from the kernel leave hdr->nlmsg_pid at 0.
418+
ms := nlmsg.NewMessageSet(0 /*portID*/, 0 /*seq*/)
418419
routeProtocol.AddNewLinkMessage(ms, idx, i)
419420
s.SendResponse(ctx, ms)
420421
}
@@ -426,7 +427,8 @@ func (s *Socket) HandleInterfaceDeleteEvent(ctx context.Context, idx int32, i in
426427
panic(fmt.Sprintf("Non-ROUTE netlink socket (protocol %d) cannot handle interface events", s.Protocol()))
427428
}
428429

429-
ms := nlmsg.NewMessageSet(s.GetPortID(), 0)
430+
// Multicast messages from the kernel leave hdr->nlmsg_pid at 0.
431+
ms := nlmsg.NewMessageSet(0 /*portID*/, 0 /*seq*/)
430432
routeProtocol.AddDelLinkMessage(ms, idx, i)
431433
s.SendResponse(ctx, ms)
432434
}

test/syscalls/linux/socket_netlink_route.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,7 @@ TEST(NetlinkRouteTest, LinkMulticastGroupBasic) {
19581958
if (msg->ifi_index != tc.link.index) {
19591959
return;
19601960
}
1961+
EXPECT_EQ(hdr->nlmsg_pid, 0);
19611962
CheckLinkMsg(hdr, tc.link);
19621963
got_msg = true;
19631964
},
@@ -2040,6 +2041,7 @@ TEST(NetlinkRouteTest, LinkMulticastGroupNamespaced) {
20402041
reinterpret_cast<const struct ifinfomsg*>(NLMSG_DATA(hdr));
20412042
if (hdr->nlmsg_type != RTM_DELLINK) return;
20422043
if (msg->ifi_index != inner_veth_idx) return;
2044+
EXPECT_EQ(hdr->nlmsg_pid, 0);
20432045
got_msg = true;
20442046
},
20452047
/*expect_nlmsgerr=*/false));
@@ -2065,6 +2067,7 @@ TEST(NetlinkRouteTest, LinkMulticastGroupNamespaced) {
20652067
char ifname[IF_NAMESIZE];
20662068
EXPECT_NE(if_indextoname(msg->ifi_index, ifname), nullptr);
20672069
EXPECT_STREQ(ifname, "veth2");
2070+
EXPECT_EQ(hdr->nlmsg_pid, 0);
20682071
got_msg = true;
20692072
},
20702073
/*expect_nlmsgerr=*/false));
@@ -2232,6 +2235,8 @@ TEST(NetlinkRouteTest, LinkMulticastGroupUserToUserSend) {
22322235
const struct nlmsgerr* msg =
22332236
reinterpret_cast<const struct nlmsgerr*>(NLMSG_DATA(hdr));
22342237
ASSERT_EQ(msg->error, 0);
2238+
EXPECT_EQ(hdr->nlmsg_pid,
2239+
ASSERT_NO_ERRNO_AND_VALUE(NetlinkPortID(nlsk_send.get())));
22352240
got_msg = true;
22362241
},
22372242
/*expect_nlmsgerr=*/true));

0 commit comments

Comments
 (0)