Skip to content

Commit 2028235

Browse files
kerumetogvisor-bot
authored andcommitted
Add mutex locking to netfilter socket operations with nftables.
PiperOrigin-RevId: 780227522
1 parent 8f111be commit 2028235

19 files changed

+2906
-314
lines changed

pkg/abi/linux/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ go_library(
5252
"msgqueue.go",
5353
"netdevice.go",
5454
"netfilter.go",
55+
"netfilter_arp.go",
5556
"netfilter_bridge.go",
5657
"netfilter_ipv4.go",
5758
"netfilter_ipv6.go",

pkg/abi/linux/netfilter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const (
3131
NF_INET_LOCAL_OUT = 3
3232
NF_INET_POST_ROUTING = 4
3333
NF_INET_NUMHOOKS = 5
34+
NF_INET_INGRESS = NF_INET_NUMHOOKS
35+
)
36+
37+
const (
38+
NF_NETDEV_INGRESS = iota
39+
NF_NETDEV_EGRESS
40+
NF_NETDEV_NUMHOOKS
3441
)
3542

3643
// Protocol families (address families). These correspond to values in

pkg/abi/linux/netfilter_arp.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2025 The gVisor Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package linux
16+
17+
// These constants show the hooks ARP packets can be evaluated at.
18+
// From include/uapi/linux/netfilter_arp.h.
19+
const (
20+
NF_ARP_IN = iota
21+
NF_ARP_OUT
22+
NF_ARP_FORWARD
23+
NF_ARP_NUMHOOKS
24+
)

pkg/abi/linux/netfilter_bridge.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ package linux
1616

1717
import "math"
1818

19+
// Netfilter Bridge Standard Hook Points, from uapi/linux/netfilter_bridge.h.
20+
const (
21+
NF_BR_PRE_ROUTING = iota
22+
NF_BR_LOCAL_IN
23+
NF_BR_FORWARD
24+
NF_BR_LOCAL_OUT
25+
NF_BR_POST_ROUTING
26+
NF_BR_BROUTING
27+
NF_BR_NUMHOOKS
28+
)
29+
1930
// Netfilter Bridge Standard Hook Priorities, from
2031
// uapi/linux/netfilter_bridge.h.
2132
const (

pkg/abi/linux/netlink.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,35 @@ type NetlinkMessageHeader struct {
6666
// NetlinkMessageHeaderSize is the size of NetlinkMessageHeader.
6767
const NetlinkMessageHeaderSize = 16
6868

69-
// Netlink message header flags, from uapi/linux/netlink.h.
69+
// Netlink message header flag values, from uapi/linux/netlink.h.
7070
const (
7171
NLM_F_REQUEST = 0x1
7272
NLM_F_MULTI = 0x2
7373
NLM_F_ACK = 0x4
7474
NLM_F_ECHO = 0x8
7575
NLM_F_DUMP_INTR = 0x10
76-
NLM_F_ROOT = 0x100
77-
NLM_F_MATCH = 0x200
78-
NLM_F_ATOMIC = 0x400
79-
NLM_F_DUMP = NLM_F_ROOT | NLM_F_MATCH
80-
NLM_F_REPLACE = 0x100
81-
NLM_F_EXCL = 0x200
82-
NLM_F_CREATE = 0x400
83-
NLM_F_APPEND = 0x800
76+
)
77+
78+
// Netlink message header flags for GET requests, from uapi/linux/netlink.h.
79+
const (
80+
NLM_F_ROOT = 0x100
81+
NLM_F_MATCH = 0x200
82+
NLM_F_ATOMIC = 0x400
83+
NLM_F_DUMP = NLM_F_ROOT | NLM_F_MATCH
84+
)
85+
86+
// Netlink message header flags for NEW requests, from uapi/linux/netlink.h.
87+
const (
88+
NLM_F_REPLACE = 0x100
89+
NLM_F_EXCL = 0x200
90+
NLM_F_CREATE = 0x400
91+
NLM_F_APPEND = 0x800
92+
)
93+
94+
// Netlink message header flags for DELETE requests, from uapi/linux/netlink.h.
95+
const (
96+
NLM_F_NONREC = 0x100
97+
NLM_F_BULK = 0x200
8498
)
8599

86100
// Standard netlink message types, from uapi/linux/netlink.h.

pkg/abi/linux/nf_tables.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ package linux
1616

1717
// This file contains constants required to support nf_tables.
1818

19+
const NFT_MAX_HOOKS = NF_INET_NUMHOOKS + 1
20+
21+
// Name length constants for nf_table structures. These correspond to values in
22+
// include/uapi/linux/netfilter/nf_tables.h.
23+
const (
24+
NFT_NAME_MAXLEN = 256
25+
NFT_TABLE_MAXNAMELEN = NFT_NAME_MAXLEN
26+
NFT_CHAIN_MAXNAMELEN = NFT_NAME_MAXLEN
27+
NFT_SET_MAXNAMELEN = NFT_NAME_MAXLEN
28+
NFT_OBJ_MAXNAMELEN = NFT_NAME_MAXLEN
29+
NFT_USERDATA_MAXLEN = 256
30+
NFT_OSF_MAXGENRELEN = 16
31+
)
32+
1933
// 16-byte Registers that can be used to maintain state for rules.
2034
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
2135
const (
@@ -124,10 +138,25 @@ const (
124138
NFT_MSG_MAX
125139
)
126140

141+
// NfTableHookAttributes represents the netfilter hook attributes.
142+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
143+
const (
144+
NFTA_HOOK_UNSPEC uint16 = iota
145+
NFTA_HOOK_HOOKNUM
146+
NFTA_HOOK_PRIORITY
147+
NFTA_HOOK_DEV
148+
NFTA_HOOK_DEVS
149+
__NFTA_HOOK_MAX
150+
NFTA_HOOK_MAX = __NFTA_HOOK_MAX - 1
151+
)
152+
127153
// NfTableFlags represents table flags that can be set for a table, namely dormant.
128154
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
129155
const (
130-
NFT_TABLE_F_DORMANT = 0x1
156+
NFT_TABLE_F_DORMANT uint32 = 0x1
157+
NFT_TABLE_F_OWNER = 0x2
158+
NFT_TABLE_F_PERSIST = 0x4
159+
NFT_TABLE_F_MASK = NFT_TABLE_F_DORMANT | NFT_TABLE_F_OWNER | NFT_TABLE_F_PERSIST
131160
)
132161

133162
// NfTableAttributes represents the netfilter table attributes.
@@ -147,6 +176,35 @@ const (
147176
// NFTA_TABLE_MAX is the maximum netfilter table attribute.
148177
const NFTA_TABLE_MAX = __NFTA_TABLE_MAX - 1
149178

179+
// NfTableChainFlags represents chain flags that can be set for a chain.
180+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
181+
const (
182+
NFT_CHAIN_BASE uint32 = (1 << 0)
183+
NFT_CHAIN_HW_OFFLOAD = (1 << 1)
184+
NFT_CHAIN_BINDING = (1 << 2)
185+
NFT_CHAIN_FLAGS = (NFT_CHAIN_BASE | NFT_CHAIN_HW_OFFLOAD | NFT_CHAIN_BINDING)
186+
)
187+
188+
// NfTableChainAttributes represents the netfilter chain attributes.
189+
// These correspond to values in include/uapi/linux/netfilter/nf_tables.h.
190+
const (
191+
NFTA_CHAIN_UNSPEC uint16 = iota
192+
NFTA_CHAIN_TABLE
193+
NFTA_CHAIN_HANDLE
194+
NFTA_CHAIN_NAME
195+
NFTA_CHAIN_HOOK
196+
NFTA_CHAIN_POLICY
197+
NFTA_CHAIN_USE
198+
NFTA_CHAIN_TYPE
199+
NFTA_CHAIN_COUNTERS
200+
NFTA_CHAIN_PAD
201+
NFTA_CHAIN_FLAGS
202+
NFTA_CHAIN_ID
203+
NFTA_CHAIN_USERDATA
204+
__NFTA_CHAIN_MAX
205+
NFTA_CHAIN_MAX = __NFTA_CHAIN_MAX - 1
206+
)
207+
150208
// Nf table relational operators.
151209
// Used by the nft comparison operation to compare values in registers.
152210
// These correspond to enum values in include/uapi/linux/netfilter/nf_tables.h.

pkg/sentry/socket/netlink/netfilter/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ go_library(
1111
visibility = ["//pkg/sentry:internal"],
1212
deps = [
1313
"//pkg/abi/linux",
14+
"//pkg/atomicbitops",
1415
"//pkg/context",
1516
"//pkg/log",
17+
"//pkg/marshal/primitive",
1618
"//pkg/sentry/inet",
1719
"//pkg/sentry/kernel",
1820
"//pkg/sentry/socket/netlink",

0 commit comments

Comments
 (0)