Skip to contentSkip to conversation

netfilter: Make xt_cgroup independent from net_cls

PATCH Superseded Michal Koutný 2 participants 8 messages Mar 5, 2025 – Mar 25, 2025

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Michal Koutný

author maintainer
#0 lore raw

PATCH v2 1/1 netfilter: Make xt_cgroup independent from net_cls

The xt_group matching supports the default hierarchy since commit c38c4597e4bf3 ("netfilter: implement xt_cgroup cgroup2 path match"). The cgroup v1 matching (based on clsid) and cgroup v2 matching (based on path) are rather independent. Downgrade the Kconfig dependency to mere CONFIG_SOCK_GROUP_DATA so that xt_group can be built even without CONFIG_NET_CLS_CGROUP for path matching.
Also add a message for users when they attempt to specify any non-trivial clsid.

---
 net/netfilter/Kconfig     |  2 +-
 net/netfilter/xt_cgroup.c | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

Changes from v1 (https://lore.kernel.org/r/[email protected])
- terser guard (Jan)
- verboser message (Florian)
- better select !CGROUP_BPF (kernel test robot)

net/netfilter/Kconfig +1 -1
index df2dc21304efb..346ac2152fa18 100644
@@ -1180,7 +1180,7 @@ config NETFILTER_XT_MATCH_CGROUP
 	tristate '"control group" match support'
 	depends on NETFILTER_ADVANCED
 	depends on CGROUPS
-	select CGROUP_NET_CLASSID
+	select SOCK_CGROUP_DATA
 	help
 	Socket/process control group matching allows you to match locally
 	generated packets based on which net_cls control group processes
net/netfilter/xt_cgroup.c +22 -0
index c0f5e9a4f3c65..c3055e74aa0ea 100644
@@ -23,6 +23,13 @@ MODULE_DESCRIPTION("Xtables: process control group matching");
 MODULE_ALIAS("ipt_cgroup");
 MODULE_ALIAS("ip6t_cgroup");
 
+#define NET_CLS_CLASSID_INVALID_MSG "xt_cgroup: classid invalid without net_cls cgroups\n"
+
+static bool possible_classid(u32 classid)
+{
+	return IS_ENABLED(CONFIG_CGROUP_NET_CLASSID) || classid == 0;
+}
+
 static int cgroup_mt_check_v0(const struct xt_mtchk_param *par)
 {
 	struct xt_cgroup_info_v0 *info = par->matchinfo;
@@ -30,6 +37,11 @@ static int cgroup_mt_check_v0(const struct xt_mtchk_param *par)
 	if (info->invert & ~1)
 		return -EINVAL;
 
+	if (!possible_classid(info->id)) {
+		pr_info(NET_CLS_CLASSID_INVALID_MSG);
+		return -EINVAL;
+	}
+
 	return 0;
 }
 
@@ -51,6 +63,11 @@ static int cgroup_mt_check_v1(const struct xt_mtchk_param *par)
 		return -EINVAL;
 	}
 
+	if (!possible_classid(info->classid)) {
+		pr_info(NET_CLS_CLASSID_INVALID_MSG);
+		return -EINVAL;
+	}
+
 	info->priv = NULL;
 	if (info->has_path) {
 		cgrp = cgroup_get_from_path(info->path);
@@ -83,6 +100,11 @@ static int cgroup_mt_check_v2(const struct xt_mtchk_param *par)
 		return -EINVAL;
 	}
 
+	if (info->has_classid && !possible_classid(info->classid)) {
+		pr_info(NET_CLS_CLASSID_INVALID_MSG);
+		return -EINVAL;
+	}
+
 	info->priv = NULL;
 	if (info->has_path) {
 		cgrp = cgroup_get_from_path(info->path);

base-commit: dd83757f6e686a2188997cb58b5975f744bb7786

Signature · 1 line
2.48.1
#2 lore raw
Quoted 10 lines · Michal Koutný

Hello.

On Wed, Mar 05, 2025 at 06:09:35PM +0100, Michal Koutný <[email protected]> wrote:
...

Quoted 4 lines

Changes from v1 (https://lore.kernel.org/r/[email protected])
- terser guard (Jan)
- verboser message (Florian)
- better select !CGROUP_BPF (kernel test robot)

Are there any more remarks or should I resend this?

No need to resend, thanks.

#3 lore raw
In reply to Michal Koutný

Hi Michal,

I have one question.

Quoted 19 lines · Michal Koutný
net/netfilter/xt_cgroup.c +7 -0
index c0f5e9a4f3c65..c3055e74aa0ea 100644
@@ -23,6 +23,13 @@ MODULE_DESCRIPTION("Xtables: process control group matching");
 MODULE_ALIAS("ipt_cgroup");
 MODULE_ALIAS("ip6t_cgroup");
 
+#define NET_CLS_CLASSID_INVALID_MSG "xt_cgroup: classid invalid without net_cls cgroups\n"
+
+static bool possible_classid(u32 classid)
+{
+	return IS_ENABLED(CONFIG_CGROUP_NET_CLASSID) || classid == 0;
+}
+
 static int cgroup_mt_check_v0(const struct xt_mtchk_param *par)
 {
 	struct xt_cgroup_info_v0 *info = par->matchinfo;

net/netfilter/xt_cgroup.c static int cgroup_mt_check_v0(const struct xt_mtchk_param *par) · line 40

Quoted 4 lines
net/netfilter/xt_cgroup.c +1 -0
@@ -30,6 +37,11 @@ static int cgroup_mt_check_v0(const struct xt_mtchk_param *par)
 	if (info->invert & ~1)
 		return -EINVAL;
 
+	if (!possible_classid(info->id)) {

why classid != 0 is accepted for cgroup_mt_check_v0()?

cgroup_mt_check_v0 represents revision 0 of this match, and this match only supports for clsid (groupsv1).

History of revisions of cgroupsv2:

- cgroup_mt_check_v0 added to match on clsid (initial version of this match)
- cgroup_mt_check_v1 is added to support cgroupsv2 matching
- cgroup_mt_check_v2 is added to make cgroupsv2 matching more flexible

I mean, if !IS_ENABLED(CONFIG_CGROUP_NET_CLASSID) then xt_cgroup should fail for cgroup_mt_check_v0.

But a more general question: why this check for classid == 0 in cgroup_mt_check_v1 and cgroup_mt_check_v2?

Quoted 36 lines
net/netfilter/xt_cgroup.c +14 -0
+		pr_info(NET_CLS_CLASSID_INVALID_MSG);
+		return -EINVAL;
+	}
+
 	return 0;
 }
 
@@ -51,6 +63,11 @@ static int cgroup_mt_check_v1(const struct xt_mtchk_param *par)
 		return -EINVAL;
 	}
 
+	if (!possible_classid(info->classid)) {
+		pr_info(NET_CLS_CLASSID_INVALID_MSG);
+		return -EINVAL;
+	}
+
 	info->priv = NULL;
 	if (info->has_path) {
 		cgrp = cgroup_get_from_path(info->path);
@@ -83,6 +100,11 @@ static int cgroup_mt_check_v2(const struct xt_mtchk_param *par)
 		return -EINVAL;
 	}
 
+	if (info->has_classid && !possible_classid(info->classid)) {
+		pr_info(NET_CLS_CLASSID_INVALID_MSG);
+		return -EINVAL;
+	}
+
 	info->priv = NULL;
 	if (info->has_path) {
 		cgrp = cgroup_get_from_path(info->path);

base-commit: dd83757f6e686a2188997cb58b5975f744bb7786

Signature · 1 line
2.48.1
author maintainer
#4 lore raw

Hello Pablo.

Quoted 1 line · Pablo Neira Ayuso

why classid != 0 is accepted for cgroup_mt_check_v0()?

It is opposite, only classid == 0 is accepted (that should be same for all of v0..v2). (OTOH, there should be no change in validation with CONFIG_CGROUP_NET_CLASSID.)

Quoted 11 lines

cgroup_mt_check_v0 represents revision 0 of this match, and this match only supports for clsid (groupsv1).

History of revisions of cgroupsv2:

- cgroup_mt_check_v0 added to match on clsid (initial version of this match)
- cgroup_mt_check_v1 is added to support cgroupsv2 matching
- cgroup_mt_check_v2 is added to make cgroupsv2 matching more flexible

I mean, if !IS_ENABLED(CONFIG_CGROUP_NET_CLASSID) then xt_cgroup should fail for cgroup_mt_check_v0.

I considered classid == 0 valid (regardless of CONFIG_*) as counterpart to implementation of sock_cgroup_classid() that collapses to 0 when !CONFIG_CGROUP_NET_CLASSID (thus at least rules with classid=0 remain acceptable).

Quoted 2 lines

But a more general question: why this check for classid == 0 in cgroup_mt_check_v1 and cgroup_mt_check_v2?

cgroup_mt_check_v1 is for cgroupv2 OR classid matching. Similar with cgroup_mt_check_v2.

IOW, all three versions accept classid=0 with !CONFIG_CGROUP_NET_CLASSID equally because that is the value that sockets reported classid falls back to.

But please correct me if I misunderstood the logic.

Thanks,
Michal

#5 lore raw
Quoted 8 lines · Michal Koutný

Hello Pablo.

Quoted 1 line · Pablo Neira Ayuso

why classid != 0 is accepted for cgroup_mt_check_v0()?

It is opposite, only classid == 0 is accepted (that should be same for all of v0..v2). (OTOH, there should be no change in validation with CONFIG_CGROUP_NET_CLASSID.)

Thanks for clarifying this, more questions below.

Quoted 9 lines
Quoted 8 lines

cgroup_mt_check_v0 represents revision 0 of this match, and this match only supports for clsid (groupsv1).

History of revisions of cgroupsv2:

- cgroup_mt_check_v0 added to match on clsid (initial version of this match)
- cgroup_mt_check_v1 is added to support cgroupsv2 matching
- cgroup_mt_check_v2 is added to make cgroupsv2 matching more flexible

Quoted 8 lines
Quoted 2 lines

I mean, if !IS_ENABLED(CONFIG_CGROUP_NET_CLASSID) then xt_cgroup should fail for cgroup_mt_check_v0.

I considered classid == 0 valid (regardless of CONFIG_*) as counterpart to implementation of sock_cgroup_classid() that collapses to 0 when !CONFIG_CGROUP_NET_CLASSID (thus at least rules with classid=0 remain acceptable).

That is, 0 is the default value when !CONFIG_CGROUP_NET_CLASSID.

Quoted 5 lines
Quoted 2 lines

But a more general question: why this check for classid == 0 in cgroup_mt_check_v1 and cgroup_mt_check_v2?

cgroup_mt_check_v1 is for cgroupv2 OR classid matching. Similar with cgroup_mt_check_v2.

Yes, and cgroup_mt_check_v0 only supports for classid matching.

Quoted 3 lines

IOW, all three versions accept classid=0 with !CONFIG_CGROUP_NET_CLASSID equally because that is the value that sockets reported classid falls back to.

If !CONFIG_CGROUP_NET_CLASSID, then no classid matching is possible.

So why allow a rule to match on cgroup with classid == 0?

Maybe simply do this instead?

static bool possible_classid(u32 classid)

{
       return IS_ENABLED(CONFIG_CGROUP_NET_CLASSID);
}

Thanks.

author maintainer
#6 lore raw
Quoted 3 lines · Pablo Neira Ayuso

If !CONFIG_CGROUP_NET_CLASSID, then no classid matching is possible.

So why allow a rule to match on cgroup with classid == 0?

It is conservative approach to supposed users who may have filtering rules with classid=0 but never mkdir any net_cls group. Only those who eventually need to mkdir would realize there's nowhere to mkdir on (with !CONFIG_CGROUP_NET_CLASSID). Admittedly, I have no idea if this helps to 5% of net_cls users or 0.05% or 0%. Do you have any insights into that?

Quoted 6 lines

Maybe simply do this instead?

static bool possible_classid(u32 classid)

{
       return IS_ENABLED(CONFIG_CGROUP_NET_CLASSID);
}

Yes, if the above carefulness is unnecessary, I'd like to accompany this with complete removal of sock_cgroup_classid() function then (to have it compile-checked that it's really impossible to compare any classids w/o CONFIG_CGROUP_NET_CLASSID).

Thanks,
Michal

#7 lore raw
Quoted 10 lines · Michal Koutný
Quoted 3 lines · Pablo Neira Ayuso

If !CONFIG_CGROUP_NET_CLASSID, then no classid matching is possible.

So why allow a rule to match on cgroup with classid == 0?

It is conservative approach to supposed users who may have filtering rules with classid=0 but never mkdir any net_cls group. Only those who eventually need to mkdir would realize there's nowhere to mkdir on (with !CONFIG_CGROUP_NET_CLASSID). Admittedly, I have no idea if this helps to 5% of net_cls users or 0.05% or 0%. Do you have any insights into that?

I suspect this partial support will not help anyway, because user will be most likely matching to classid != 0 in their rulesets, and the ruleset loads via iptables-restore in an atomic fashion, ie. take it all or nothing.

Quoted 11 lines
Quoted 6 lines

Maybe simply do this instead?

static bool possible_classid(u32 classid)

{
       return IS_ENABLED(CONFIG_CGROUP_NET_CLASSID);
}

Yes, if the above carefulness is unnecessary, I'd like to accompany this with complete removal of sock_cgroup_classid() function then (to have it compile-checked that it's really impossible to compare any classids w/o CONFIG_CGROUP_NET_CLASSID).

Go ahead remove this shim function and post v3.

Thanks.