Skip to content

Commit 6237637

Browse files
shailend-ggvisor-bot
authored andcommitted
Allow netlink sockets to send to RTMGRP_LINK
sendmsg() to the multicast group now works. connect() is still disallowed. This patch also reduces the length for which a netlink socket's mu is held. Previously, once it was taken in Socket.sendMsg(), it was held throughout Protocol.Receive() -> Socket.ProcessMessages() -> Socket.SendResponse(). When in fact the only field it was needed for was Socket.portID. PiperOrigin-RevId: 830546353
1 parent 8bc2d00 commit 6237637

File tree

5 files changed

+336
-63
lines changed

5 files changed

+336
-63
lines changed

pkg/sentry/inet/nlmcast.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ func (m *McastTable) RemoveSocket(s NetlinkSocket) {
104104
delete(m.socks[p], s)
105105
}
106106

107-
func (m *McastTable) forEachMcastSock(protocol int, mcastGroup int, fn func(s NetlinkSocket)) {
107+
// ForEachMcastSock calls fn on all Netlink sockets that are members of the given multicast group.
108+
func (m *McastTable) ForEachMcastSock(protocol int, mcastGroup int, fn func(s NetlinkSocket)) {
108109
m.mu.Lock()
109110
defer m.mu.Unlock()
110111
if _, ok := m.socks[protocol]; !ok {
@@ -122,15 +123,15 @@ func (m *McastTable) forEachMcastSock(protocol int, mcastGroup int, fn func(s Ne
122123
// OnInterfaceChangeEvent implements InterfaceEventSubscriber.OnInterfaceChangeEvent.
123124
func (m *McastTable) OnInterfaceChangeEvent(ctx context.Context, idx int32, i Interface) {
124125
// Relay the event to RTNLGRP_LINK subscribers.
125-
m.forEachMcastSock(routeProtocol, routeLinkMcastGroup, func(s NetlinkSocket) {
126+
m.ForEachMcastSock(routeProtocol, routeLinkMcastGroup, func(s NetlinkSocket) {
126127
s.HandleInterfaceChangeEvent(ctx, idx, i)
127128
})
128129
}
129130

130131
// OnInterfaceDeleteEvent implements InterfaceEventSubscriber.OnInterfaceDeleteEvent.
131132
func (m *McastTable) OnInterfaceDeleteEvent(ctx context.Context, idx int32, i Interface) {
132133
// Relay the event to RTNLGRP_LINK subscribers.
133-
m.forEachMcastSock(routeProtocol, routeLinkMcastGroup, func(s NetlinkSocket) {
134+
m.ForEachMcastSock(routeProtocol, routeLinkMcastGroup, func(s NetlinkSocket) {
134135
s.HandleInterfaceDeleteEvent(ctx, idx, i)
135136
})
136137
}

pkg/sentry/socket/netlink/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ go_library(
2828
"//pkg/sentry/kernel/auth",
2929
"//pkg/sentry/ktime",
3030
"//pkg/sentry/socket",
31+
"//pkg/sentry/socket/control",
3132
"//pkg/sentry/socket/netlink/nlmsg",
3233
"//pkg/sentry/socket/netlink/port",
3334
"//pkg/sentry/socket/unix",

pkg/sentry/socket/netlink/netfilter/protocol.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (p *Protocol) Receive(ctx context.Context, s *netlink.Socket, buf []byte) *
8282
// TODO: b/434785410 - Support batch messages.
8383
if hdr.Type == linux.NFNL_MSG_BATCH_BEGIN {
8484
ms := nlmsg.NewMessageSet(s.GetPortID(), hdr.Seq)
85-
if err := p.receiveBatchMessage(ctx, s, ms, buf); err != nil {
85+
if err := p.receiveBatchMessage(ctx, ms, buf); err != nil {
8686
log.Debugf("Nftables: Failed to process batch message: %v", err)
8787
netlink.DumpErrorMessage(hdr, ms, err.GetError())
8888
}
@@ -1215,7 +1215,7 @@ func (p *Protocol) ProcessMessage(ctx context.Context, s *netlink.Socket, msg *n
12151215
}
12161216

12171217
// receiveBatchMessage processes a NETFILTER batch message.
1218-
func (p *Protocol) receiveBatchMessage(ctx context.Context, s *netlink.Socket, ms *nlmsg.MessageSet, buf []byte) *syserr.AnnotatedError {
1218+
func (p *Protocol) receiveBatchMessage(ctx context.Context, ms *nlmsg.MessageSet, buf []byte) *syserr.AnnotatedError {
12191219
// Linux ignores messages that are too small.
12201220
// From net/netfilter/nfnetlink.c:nfnetlink_rcv_skb_batch
12211221
if len(buf) < linux.NetlinkMessageHeaderSize+linux.SizeOfNetfilterGenMsg {
@@ -1254,7 +1254,7 @@ func (p *Protocol) receiveBatchMessage(ctx context.Context, s *netlink.Socket, m
12541254
// The resource ID is a 16-bit value that is stored in network byte order.
12551255
// We ensure that it is in host byte order before passing it for processing.
12561256
resID := nlmsg.NetToHostU16(nfGenMsg.ResourceID)
1257-
if err := p.processBatchMessage(ctx, s, buf, ms, hdr, resID); err != nil {
1257+
if err := p.processBatchMessage(ctx, buf, ms, hdr, resID); err != nil {
12581258
log.Debugf("Failed to process batch message: %v", err)
12591259
netlink.DumpErrorMessage(hdr, ms, err.GetError())
12601260
}
@@ -1263,7 +1263,7 @@ func (p *Protocol) receiveBatchMessage(ctx context.Context, s *netlink.Socket, m
12631263
}
12641264

12651265
// processBatchMessage processes a batch message.
1266-
func (p *Protocol) processBatchMessage(ctx context.Context, s *netlink.Socket, buf []byte, ms *nlmsg.MessageSet, batchHdr linux.NetlinkMessageHeader, subsysID uint16) *syserr.AnnotatedError {
1266+
func (p *Protocol) processBatchMessage(ctx context.Context, buf []byte, ms *nlmsg.MessageSet, batchHdr linux.NetlinkMessageHeader, subsysID uint16) *syserr.AnnotatedError {
12671267
if subsysID >= linux.NFNL_SUBSYS_COUNT {
12681268
return syserr.NewAnnotatedError(syserr.ErrInvalidArgument, fmt.Sprintf("Nftables: Unknown subsystem id %d", subsysID))
12691269
}

0 commit comments

Comments
 (0)