Skip to content

Commit 7f3287d

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nft_socket: make cgroupsv2 matching work with namespaces
When running in container environmment, /sys/fs/cgroup/ might not be the real root node of the sk-attached cgroup. Example: In container: % stat /sys//fs/cgroup/ Device: 0,21 Inode: 2214 .. % stat /sys/fs/cgroup/foo Device: 0,21 Inode: 2264 .. The expectation would be for: nft add rule .. socket cgroupv2 level 1 "foo" counter to match traffic from a process that got added to "foo" via "echo $pid > /sys/fs/cgroup/foo/cgroup.procs". However, 'level 3' is needed to make this work. Seen from initial namespace, the complete hierarchy is: % stat /sys/fs/cgroup/system.slice/docker-.../foo Device: 0,21 Inode: 2264 .. i.e. hierarchy is 0 1 2 3 / -> system.slice -> docker-1... -> foo ... but the container doesn't know that its "/" is the "docker-1.." cgroup. Current code will retrieve the 'system.slice' cgroup node and store its kn->id in the destination register, so compare with 2264 ("foo" cgroup id) will not match. Fetch "/" cgroup from ->init() and add its level to the level we try to extract. cgroup root-level is 0 for the init-namespace or the level of the ancestor that is exposed as the cgroup root inside the container. In the above case, cgrp->level of "/" resolved in the container is 2 (docker-1...scope/) and request for 'level 1' will get adjusted to fetch the actual level (3). v2: use CONFIG_SOCK_CGROUP_DATA, eval function depends on it. (kernel test robot) Cc: [email protected] Fixes: e0bb96d ("netfilter: nft_socket: add support for cgroupsv2") Reported-by: Nadia Pinaeva <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 8b26ff7 commit 7f3287d

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

net/netfilter/nft_socket.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
struct nft_socket {
1111
enum nft_socket_keys key:8;
12-
u8 level;
12+
u8 level; /* cgroupv2 level to extract */
13+
u8 level_user; /* cgroupv2 level provided by userspace */
1314
u8 len;
1415
union {
1516
u8 dreg;
@@ -53,6 +54,28 @@ nft_sock_get_eval_cgroupv2(u32 *dest, struct sock *sk, const struct nft_pktinfo
5354
memcpy(dest, &cgid, sizeof(u64));
5455
return true;
5556
}
57+
58+
/* process context only, uses current->nsproxy. */
59+
static noinline int nft_socket_cgroup_subtree_level(void)
60+
{
61+
struct cgroup *cgrp = cgroup_get_from_path("/");
62+
int level;
63+
64+
if (!cgrp)
65+
return -ENOENT;
66+
67+
level = cgrp->level;
68+
69+
cgroup_put(cgrp);
70+
71+
if (WARN_ON_ONCE(level > 255))
72+
return -ERANGE;
73+
74+
if (WARN_ON_ONCE(level < 0))
75+
return -EINVAL;
76+
77+
return level;
78+
}
5679
#endif
5780

5881
static struct sock *nft_socket_do_lookup(const struct nft_pktinfo *pkt)
@@ -174,9 +197,10 @@ static int nft_socket_init(const struct nft_ctx *ctx,
174197
case NFT_SOCKET_MARK:
175198
len = sizeof(u32);
176199
break;
177-
#ifdef CONFIG_CGROUPS
200+
#ifdef CONFIG_SOCK_CGROUP_DATA
178201
case NFT_SOCKET_CGROUPV2: {
179202
unsigned int level;
203+
int err;
180204

181205
if (!tb[NFTA_SOCKET_LEVEL])
182206
return -EINVAL;
@@ -185,6 +209,17 @@ static int nft_socket_init(const struct nft_ctx *ctx,
185209
if (level > 255)
186210
return -EOPNOTSUPP;
187211

212+
err = nft_socket_cgroup_subtree_level();
213+
if (err < 0)
214+
return err;
215+
216+
priv->level_user = level;
217+
218+
level += err;
219+
/* Implies a giant cgroup tree */
220+
if (WARN_ON_ONCE(level > 255))
221+
return -EOPNOTSUPP;
222+
188223
priv->level = level;
189224
len = sizeof(u64);
190225
break;
@@ -209,7 +244,7 @@ static int nft_socket_dump(struct sk_buff *skb,
209244
if (nft_dump_register(skb, NFTA_SOCKET_DREG, priv->dreg))
210245
return -1;
211246
if (priv->key == NFT_SOCKET_CGROUPV2 &&
212-
nla_put_be32(skb, NFTA_SOCKET_LEVEL, htonl(priv->level)))
247+
nla_put_be32(skb, NFTA_SOCKET_LEVEL, htonl(priv->level_user)))
213248
return -1;
214249
return 0;
215250
}

0 commit comments

Comments
 (0)