* [PATCH nf 2/2] netfilter: nft_socket: make cgroupsv2 matching work with namespaces
[not found] <20240905105451.28857-1-fw@strlen.de>
@ 2024-09-05 10:54 ` Florian Westphal
2024-09-06 19:39 ` kernel test robot
2024-09-08 17:06 ` kernel test robot
0 siblings, 2 replies; 3+ messages in thread
From: Florian Westphal @ 2024-09-05 10:54 UTC (permalink / raw)
To: netfilter-devel; +Cc: Florian Westphal, cgroups, Nadia Pinaeva
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).
Cc: cgroups@vger.kernel.org
Fixes: e0bb96db96f8 ("netfilter: nft_socket: add support for cgroupsv2")
Reported-by: Nadia Pinaeva <n.m.pinaeva@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
---
net/netfilter/nft_socket.c | 39 ++++++++++++++++++++++++++++++++++++--
1 file changed, 37 insertions(+), 2 deletions(-)
diff --git a/net/netfilter/nft_socket.c b/net/netfilter/nft_socket.c
index 765ffd6e06bc..0e8367c8f280 100644
--- a/net/netfilter/nft_socket.c
+++ b/net/netfilter/nft_socket.c
@@ -9,7 +9,8 @@
struct nft_socket {
enum nft_socket_keys key:8;
- u8 level;
+ u8 level; /* cgroupv2 level to extract */
+ u8 level_user; /* cgroupv2 level provided by userspace */
u8 len;
union {
u8 dreg;
@@ -53,6 +54,28 @@ nft_sock_get_eval_cgroupv2(u32 *dest, struct sock *sk, const struct nft_pktinfo
memcpy(dest, &cgid, sizeof(u64));
return true;
}
+
+/* process context only, uses current->nsproxy. */
+static noinline int nft_socket_cgroup_subtree_level(void)
+{
+ struct cgroup *cgrp = cgroup_get_from_path("/");
+ int level;
+
+ if (!cgrp)
+ return -ENOENT;
+
+ level = cgrp->level;
+
+ cgroup_put(cgrp);
+
+ if (WARN_ON_ONCE(level > 255))
+ return -ERANGE;
+
+ if (WARN_ON_ONCE(level < 0))
+ return -EINVAL;
+
+ return level;
+}
#endif
static struct sock *nft_socket_do_lookup(const struct nft_pktinfo *pkt)
@@ -177,6 +200,7 @@ static int nft_socket_init(const struct nft_ctx *ctx,
#ifdef CONFIG_CGROUPS
case NFT_SOCKET_CGROUPV2: {
unsigned int level;
+ int err;
if (!tb[NFTA_SOCKET_LEVEL])
return -EINVAL;
@@ -185,6 +209,17 @@ static int nft_socket_init(const struct nft_ctx *ctx,
if (level > 255)
return -EOPNOTSUPP;
+ err = nft_socket_cgroup_subtree_level();
+ if (err < 0)
+ return err;
+
+ priv->level_user = level;
+
+ level += err;
+ /* Implies a giant cgroup tree */
+ if (WARN_ON_ONCE(level > 255))
+ return -EOPNOTSUPP;
+
priv->level = level;
len = sizeof(u64);
break;
@@ -209,7 +244,7 @@ static int nft_socket_dump(struct sk_buff *skb,
if (nft_dump_register(skb, NFTA_SOCKET_DREG, priv->dreg))
return -1;
if (priv->key == NFT_SOCKET_CGROUPV2 &&
- nla_put_be32(skb, NFTA_SOCKET_LEVEL, htonl(priv->level)))
+ nla_put_be32(skb, NFTA_SOCKET_LEVEL, htonl(priv->level_user)))
return -1;
return 0;
}
--
2.44.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH nf 2/2] netfilter: nft_socket: make cgroupsv2 matching work with namespaces
2024-09-05 10:54 ` [PATCH nf 2/2] netfilter: nft_socket: make cgroupsv2 matching work with namespaces Florian Westphal
@ 2024-09-06 19:39 ` kernel test robot
2024-09-08 17:06 ` kernel test robot
1 sibling, 0 replies; 3+ messages in thread
From: kernel test robot @ 2024-09-06 19:39 UTC (permalink / raw)
To: Florian Westphal, netfilter-devel
Cc: oe-kbuild-all, Florian Westphal, cgroups, Nadia Pinaeva
Hi Florian,
kernel test robot noticed the following build errors:
[auto build test ERROR on netfilter-nf/main]
url: https://github.com/intel-lab-lkp/linux/commits/Florian-Westphal/netfilter-nft_socket-make-cgroupsv2-matching-work-with-namespaces/20240905-185930
base: https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main
patch link: https://lore.kernel.org/r/20240905105451.28857-2-fw%40strlen.de
patch subject: [PATCH nf 2/2] netfilter: nft_socket: make cgroupsv2 matching work with namespaces
config: arm-randconfig-002-20240907 (https://download.01.org/0day-ci/archive/20240907/202409070305.pBDk8EVS-lkp@intel.com/config)
compiler: arm-linux-gnueabi-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240907/202409070305.pBDk8EVS-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409070305.pBDk8EVS-lkp@intel.com/
All errors (new ones prefixed by >>):
net/netfilter/nft_socket.c: In function 'nft_socket_init':
>> net/netfilter/nft_socket.c:212:23: error: implicit declaration of function 'nft_socket_cgroup_subtree_level' [-Wimplicit-function-declaration]
212 | err = nft_socket_cgroup_subtree_level();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vim +/nft_socket_cgroup_subtree_level +212 net/netfilter/nft_socket.c
169
170 static int nft_socket_init(const struct nft_ctx *ctx,
171 const struct nft_expr *expr,
172 const struct nlattr * const tb[])
173 {
174 struct nft_socket *priv = nft_expr_priv(expr);
175 unsigned int len;
176
177 if (!tb[NFTA_SOCKET_DREG] || !tb[NFTA_SOCKET_KEY])
178 return -EINVAL;
179
180 switch(ctx->family) {
181 case NFPROTO_IPV4:
182 #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
183 case NFPROTO_IPV6:
184 #endif
185 case NFPROTO_INET:
186 break;
187 default:
188 return -EOPNOTSUPP;
189 }
190
191 priv->key = ntohl(nla_get_be32(tb[NFTA_SOCKET_KEY]));
192 switch(priv->key) {
193 case NFT_SOCKET_TRANSPARENT:
194 case NFT_SOCKET_WILDCARD:
195 len = sizeof(u8);
196 break;
197 case NFT_SOCKET_MARK:
198 len = sizeof(u32);
199 break;
200 #ifdef CONFIG_CGROUPS
201 case NFT_SOCKET_CGROUPV2: {
202 unsigned int level;
203 int err;
204
205 if (!tb[NFTA_SOCKET_LEVEL])
206 return -EINVAL;
207
208 level = ntohl(nla_get_be32(tb[NFTA_SOCKET_LEVEL]));
209 if (level > 255)
210 return -EOPNOTSUPP;
211
> 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
223 priv->level = level;
224 len = sizeof(u64);
225 break;
226 }
227 #endif
228 default:
229 return -EOPNOTSUPP;
230 }
231
232 priv->len = len;
233 return nft_parse_register_store(ctx, tb[NFTA_SOCKET_DREG], &priv->dreg,
234 NULL, NFT_DATA_VALUE, len);
235 }
236
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH nf 2/2] netfilter: nft_socket: make cgroupsv2 matching work with namespaces
2024-09-05 10:54 ` [PATCH nf 2/2] netfilter: nft_socket: make cgroupsv2 matching work with namespaces Florian Westphal
2024-09-06 19:39 ` kernel test robot
@ 2024-09-08 17:06 ` kernel test robot
1 sibling, 0 replies; 3+ messages in thread
From: kernel test robot @ 2024-09-08 17:06 UTC (permalink / raw)
To: Florian Westphal, netfilter-devel
Cc: llvm, oe-kbuild-all, Florian Westphal, cgroups, Nadia Pinaeva
Hi Florian,
kernel test robot noticed the following build errors:
[auto build test ERROR on netfilter-nf/main]
url: https://github.com/intel-lab-lkp/linux/commits/Florian-Westphal/netfilter-nft_socket-make-cgroupsv2-matching-work-with-namespaces/20240908-025647
base: https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main
patch link: https://lore.kernel.org/r/20240905105451.28857-2-fw%40strlen.de
patch subject: [PATCH nf 2/2] netfilter: nft_socket: make cgroupsv2 matching work with namespaces
config: i386-randconfig-006-20240908 (https://download.01.org/0day-ci/archive/20240909/202409090022.jxFbbVYj-lkp@intel.com/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240909/202409090022.jxFbbVYj-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409090022.jxFbbVYj-lkp@intel.com/
All errors (new ones prefixed by >>):
>> net/netfilter/nft_socket.c:212:9: error: call to undeclared function 'nft_socket_cgroup_subtree_level'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
212 | err = nft_socket_cgroup_subtree_level();
| ^
1 error generated.
vim +/nft_socket_cgroup_subtree_level +212 net/netfilter/nft_socket.c
169
170 static int nft_socket_init(const struct nft_ctx *ctx,
171 const struct nft_expr *expr,
172 const struct nlattr * const tb[])
173 {
174 struct nft_socket *priv = nft_expr_priv(expr);
175 unsigned int len;
176
177 if (!tb[NFTA_SOCKET_DREG] || !tb[NFTA_SOCKET_KEY])
178 return -EINVAL;
179
180 switch(ctx->family) {
181 case NFPROTO_IPV4:
182 #if IS_ENABLED(CONFIG_NF_TABLES_IPV6)
183 case NFPROTO_IPV6:
184 #endif
185 case NFPROTO_INET:
186 break;
187 default:
188 return -EOPNOTSUPP;
189 }
190
191 priv->key = ntohl(nla_get_be32(tb[NFTA_SOCKET_KEY]));
192 switch(priv->key) {
193 case NFT_SOCKET_TRANSPARENT:
194 case NFT_SOCKET_WILDCARD:
195 len = sizeof(u8);
196 break;
197 case NFT_SOCKET_MARK:
198 len = sizeof(u32);
199 break;
200 #ifdef CONFIG_CGROUPS
201 case NFT_SOCKET_CGROUPV2: {
202 unsigned int level;
203 int err;
204
205 if (!tb[NFTA_SOCKET_LEVEL])
206 return -EINVAL;
207
208 level = ntohl(nla_get_be32(tb[NFTA_SOCKET_LEVEL]));
209 if (level > 255)
210 return -EOPNOTSUPP;
211
> 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
223 priv->level = level;
224 len = sizeof(u64);
225 break;
226 }
227 #endif
228 default:
229 return -EOPNOTSUPP;
230 }
231
232 priv->len = len;
233 return nft_parse_register_store(ctx, tb[NFTA_SOCKET_DREG], &priv->dreg,
234 NULL, NFT_DATA_VALUE, len);
235 }
236
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-09-08 17:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20240905105451.28857-1-fw@strlen.de>
2024-09-05 10:54 ` [PATCH nf 2/2] netfilter: nft_socket: make cgroupsv2 matching work with namespaces Florian Westphal
2024-09-06 19:39 ` kernel test robot
2024-09-08 17:06 ` kernel test robot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox