datatype: accept a numeric cgroupsv2 id on input
PATCH 1/1 datatype: accept a numeric cgroupsv2 id on input
Once a cgroup is removed there is no path left to resolve, so the listing falls back to printing the raw id:
# nft list set ip t s
table ip t {
set s {
type cgroupsv2
elements = { 50834 }
}
}That output is not valid input. cgroupv2_type_parse() only stats /sys/fs/cgroup/<identifier>, so handing the id back fails:
# nft delete element ip t s { 50834 }
Error: cgroupv2 path fails: No such file or directoryThe element cannot be addressed by key at all once its cgroup is gone, which leaves flushing the containing set as the only way to remove it. The json dump does not reload either, since the id is serialised as a string and reaches the same parser. tests/shell/testcases/packetpath/cgroupv2 works around this in its cleanup(), added by commit 9fbc77c127e7 ("tests: shell: add cgroupv2 socket match test case"), because "nft can't find the human-readable names anymore".
Take the id back when the path does not resolve, accepting a decimal number and nothing else. integer_type_parse() converts with base 0 and expr_evaluate_integer() does not check the sign, so it would take "0755" as 493, "0x1f" as 31 and "-1" as 1; strtoull() alone still accepts a leading sign or space. Requiring a leading digit closes both. The path lookup stays first, so a cgroup named as a number still resolves as a path.
A plain decimal name that does not resolve now becomes an id rather than an error, the way meta skuid falls back to the numeric uid when there is no passwd entry. nft accepting what nft printed is worth more than diagnosing a mistyped path.
---cgroupv2_type_print() still ignores -n and prints the resolved path when it can. This patch only makes the id nft already prints acceptable as input; making the print side honour -n is a separate change.
dumps/cgroupv2_stale_id.nft is empty and the json dump is the metainfo boilerplate, because the test leaves no ruleset behind and the ids it uses are not stable across runs. There is no .nodump, so the dump comparison still runs.
No NFT_TEST_REQUIRES: the test uses no socket expression, so neither NFT_TEST_HAVE_cgroupsv2 nor nft_socket is involved. It skips with 77 without a writable cgroup2 mount, and the json arm skips unless -j actually works.
Searched the netfilter-devel archive for cgroupv2: 133 messages, 2021-04-21 to 2026-05-28, none proposing that a numeric id be accepted on input.
Tested on b315dd55 with a libnftnl built from git 363b0e3, since master needs nftnl_set_elem_set_imm which is not in libnftnl 1.3.1. The suite gains this one test and no regressions; two pre-existing failures are unchanged with and without the patch.
src/datatype.c | 29 +++- .../shell/testcases/parsing/cgroupv2_stale_id | 154 ++++++++++++++++++ .../parsing/dumps/cgroupv2_stale_id.json-nft | 11 ++ .../parsing/dumps/cgroupv2_stale_id.nft | 0 4 files changed, 190 insertions(+), 4 deletions(-) create mode 100755 tests/shell/testcases/parsing/cgroupv2_stale_id create mode 100644 tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft create mode 100644 tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.nft
src/datatype.c +25 -4
@@ -1665,11 +1665,32 @@ static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, SYSFS_CGROUPSV2_PATH, sym->identifier); cgroupv2_path[sizeof (cgroupv2_path) -1 ] ='\0' ; -if (stat (cgroupv2_path, &st) <0 ) -return error (&sym->location,"cgroupv2 path fails: %s" , -strerror (errno)); +if (stat (cgroupv2_path, &st) <0 ) { +int stat_errno = errno; +char *end; + +/* The path is gone, so the listing prints the raw id; take it +* back or nft cannot parse its own output. Decimal only: a +* leading zero is read as decimal, not octal, and a bare +* strtoull() would take a leading sign or space too. A +* non-number keeps the path error, which is what it most +* likely is; only an over-wide number gets its own. +*/ +if (!isdigit ((unsigned char )sym->identifier[0 ])) +return error (&sym->location,"cgroupv2 path fails: %s" , +strerror (stat_errno)); + + errno =0 ; + ino =strtoull (sym->identifier, &end,10 ); +if (*end !='\0' ) +return error (&sym->location,"cgroupv2 path fails: %s" , +strerror (stat_errno)); +if (errno ==ERANGE ) +return error (&sym->location,"cgroupv2 id out of range" ); + }else { + ino = st.st_ino; + } - ino = st.st_ino; *res =constant_expr_alloc (&sym->location, &cgroupv2_type,BYTEORDER_HOST_ENDIAN ,sizeof (ino) *BITS_PER_BYTE , &ino);
tests/shell/testcases/parsing/cgroupv2_stale_id new file +154 -0
154 lines not on this page · raw message
tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft new file +11 -0
@@ -0,0 +1,11 @@ +{ + "nftables": [ + { + "metainfo": { + "version": "VERSION", + "release_name": "RELEASE_NAME", + "json_schema_version": 1 + } + } + ] +}
tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.nft new file +0 -0
base-commit: b315dd551dd963f052fe49e4408e0ee02ff1ae25
Signature · 1 line
2.55.0
Phil Sutter
reviewerHi,
Quoted 26 lines · Avinash Duduskar
Once a cgroup is removed there is no path left to resolve, so the listing falls back to printing the raw id:
# nft list set ip t s table ip t { set s { type cgroupsv2 elements = { 50834 } } }That output is not valid input.
cgroupv2_type_parse()only stats /sys/fs/cgroup/<identifier>, so handing the id back fails:# nft delete element ip t s { 50834 } Error: cgroupv2 path fails: No such file or directoryThe element cannot be addressed by key at all once its cgroup is gone, which leaves flushing the containing set as the only way to remove it. The json dump does not reload either, since the id is serialised as a string and reaches the same parser. tests/shell/testcases/packetpath/cgroupv2 works around this in its
cleanup(), added by commit9fbc77c127e7("tests: shell: add cgroupv2 socket match test case"), because "nft can't find the human-readable names anymore".
Quoted 7 lines
Take the id back when the path does not resolve, accepting a decimal number and nothing else.
integer_type_parse()converts with base 0 andexpr_evaluate_integer()does not check the sign, so it would take "0755" as 493, "0x1f" as 31 and "-1" as 1;strtoull()alone still accepts a leading sign or space. Requiring a leading digit closes both. The path lookup stays first, so a cgroup named as a number still resolves as a path.
So with nft printing non-existent cgroup names using PRIu64 and parsing code still performs the path lookup first, what is all the fuss about non-decimal number input?
Quoted 20 lines
A plain decimal name that does not resolve now becomes an id rather than an error, the way meta skuid falls back to the numeric uid when there is no passwd entry. nft accepting what nft printed is worth more than diagnosing a mistyped path.
Fixes 38228087252c ("src: add cgroupsv2 support")Signed-off-by Avinash Duduskar <[email protected]>---
cgroupv2_type_print()still ignores -n and prints the resolved path when it can. This patch only makes the id nft already prints acceptable as input; making the print side honour -n is a separate change.dumps/cgroupv2_stale_id.nft is empty and the json dump is the metainfo boilerplate, because the test leaves no ruleset behind and the ids it uses are not stable across runs. There is no .nodump, so the dump comparison still runs.
No
NFT_TEST_REQUIRES: the test uses no socket expression, so neitherNFT_TEST_HAVE_cgroupsv2nornft_socketis involved. It skips with 77 without a writable cgroup2 mount, and the json arm skips unless -j actually works.
Quoted 7 lines
Searched the netfilter-devel archive for cgroupv2: 133 messages, 2021-04-21 to 2026-05-28, none proposing that a numeric id be accepted on input.
Tested on
b315dd55with a libnftnl built from git 363b0e3, since master needsnftnl_set_elem_set_immwhich is not in libnftnl 1.3.1. The suite gains this one test and no regressions; two pre-existing failures are unchanged with and without the patch.
Is this AI output?
Quoted 37 lines
src/datatype.c | 29 +++- .../shell/testcases/parsing/cgroupv2_stale_id | 154 ++++++++++++++++++ .../parsing/dumps/cgroupv2_stale_id.json-nft | 11 ++ .../parsing/dumps/cgroupv2_stale_id.nft | 0 4 files changed, 190 insertions(+), 4 deletions(-) create mode 100755 tests/shell/testcases/parsing/cgroupv2_stale_id create mode 100644 tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft create mode 100644 tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.nftsrc/datatype.c +17
-3@@ -1665,11 +1665,32 @@ static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, SYSFS_CGROUPSV2_PATH, sym->identifier); cgroupv2_path[sizeof (cgroupv2_path) -1 ] ='\0' ; -if (stat (cgroupv2_path, &st) <0 ) -return error (&sym->location,"cgroupv2 path fails: %s" , -strerror (errno)); +if (stat (cgroupv2_path, &st) <0 ) { +int stat_errno = errno; +char *end; + +/* The path is gone, so the listing prints the raw id; take it +* back or nft cannot parse its own output. Decimal only: a +* leading zero is read as decimal, not octal, and a bare +* strtoull() would take a leading sign or space too. A +* non-number keeps the path error, which is what it most +* likely is; only an over-wide number gets its own. +*/ +if (!isdigit ((unsigned char )sym->identifier[0 ])) +return error (&sym->location,"cgroupv2 path fails: %s" , +strerror (stat_errno)); + + errno =0 ; + ino =strtoull (sym->identifier, &end,10 );
src/datatype.c static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, · line 1672
Quoted 10 lines
src/datatype.c +8
-1+if (*end !='\0' ) +return error (&sym->location,"cgroupv2 path fails: %s" , +strerror (stat_errno)); +if (errno ==ERANGE ) +return error (&sym->location,"cgroupv2 id out of range" ); + }else { + ino = st.st_ino; + } - ino = st.st_ino;
Can't this just be something like (untested):
| if (!stat(cgroupv2_path, &st)) {
| ino = st.st_ino;
| } else {
| char *end;
|
| ino = strtoull(sym->identifier, &end, 10);
| if (*end)
| return error(...)
| }Quoted 74 lines
src/datatype.c +0
-0*res =constant_expr_alloc (&sym->location, &cgroupv2_type,BYTEORDER_HOST_ENDIAN ,sizeof (ino) *BITS_PER_BYTE , &ino);tests/shell/testcases/parsing/cgroupv2_stale_id new file +65
-0@@ -0,0 +1,154 @@ +#!/bin/bash + +# A cgroupsv2 element has to stay addressable once its cgroup is gone: the +# listing prints the raw id, so that id has to be accepted back, and nothing +# else has to be. + +CGROUP="/sys/fs/cgroup/nft-stale-$$" +# numeric on purpose: a cgroup named as a number must still resolve as a path +CGNUM="/sys/fs/cgroup/$$" +JDUMP= + +cleanup() +{ + $NFT delete table t 2>/dev/null + rmdir "$CGROUP" 2>/dev/null + rmdir "$CGNUM" 2>/dev/null + [ -n "$JDUMP" ] && rm -f "$JDUMP" +} +trap cleanup EXIT + +if [ ! -w /sys/fs/cgroup ]; then + echo "cgroup filesystem not writable" + exit 77 +fi + +# -w alone passes on a v1 or hybrid layout, where everything below tests nothing +if [ "$(stat -f --printf=%T /sys/fs/cgroup)" != "cgroup2fs" ]; then + echo "not a cgroupv2 mount" + exit 77 +fi + +if ! mkdir "$CGROUP" 2>/dev/null; then + # unprivileged and left over from a dead run are different facts + [ -d "$CGROUP" ] && { + echo "E: $CGROUP already exists" >&2 + exit 1 + } + echo "cannot create a cgroup" + exit 77 +fi +name="${CGROUP##*/}" +id=$(stat --printf=%i "$CGROUP") + +$NFT add table t || exit 1 +$NFT add set t s '{ type cgroupsv2; }' || exit 1 +$NFT add element t s "{ \"$name\" }" || exit 1 + +rmdir "$CGROUP" || exit 1 + +# Another cgroup can take this inode between the rmdir and the listing, which +# resolves it back to a name. Narrow, and not closable from here. +$NFT list set t s | grep -qw "$id" || { + echo "E: listing does not show the stale cgroup id $id" >&2 + $NFT list set t s >&2 + exit 1 +} + +$NFT delete element t s "{ $id }" || { + echo "E: cannot delete a cgroupsv2 element by the id that was listed" >&2 + exit 1 +} + +$NFT list set t s | grep -qw "$id" && { + echo "E: element still present after delete" >&2 + exit 1
tests/shell/testcases/parsing/cgroupv2_stale_id line 75
Quoted 10 lines
tests/shell/testcases/parsing/cgroupv2_stale_id new file +10
-0+} + +$NFT add element t s "{ $id }" || exit 1 + +# json serialises the id as a string, so it reaches the same parser. The harness +# round trip at exit sees an empty ruleset here, so do it inline, scoped to this +# table. +if [ "$NFT_TEST_HAVE_json" != n ] && $NFT -j list tables >/dev/null 2>&1; then + JDUMP=$(mktemp) || exit 1 + $NFT -j list table t > "$JDUMP" || exit 1
out=$($NFT -j list table t)
tests/shell/testcases/parsing/cgroupv2_stale_id line 77
Quoted 2 lines
tests/shell/testcases/parsing/cgroupv2_stale_id new file +2
-0+ $NFT delete table t || exit 1 + $NFT -j -f "$JDUMP" || {
$NFT -j -f - <<< "$out"
Quoted 84 lines
tests/shell/testcases/parsing/cgroupv2_stale_id new file +77
-0+ echo "E: a json dump holding a stale cgroupsv2 id does not reload" >&2 + exit 1 + } + $NFT list set t s | grep -qw "$id" || { + echo "E: the id did not survive the json round trip" >&2 + exit 1 + } +else + echo "I: no json support, skipping the json reload check" +fi + +$NFT flush set t s || exit 1 + +# Nothing outside the form the listing emits: a plain strtoull() would take +# "-1", "+5" and " 42", a base 0 conversion "-1", " 42", "0x10" and "1 2". +for bogus in "-1" "+5" " 42" "0x10" "1 2" "nft-does-not-exist" "12abc"; do + $NFT add element t s "{ \"$bogus\" }" 2>/dev/null && { + echo "E: accepted \"$bogus\" as a cgroupsv2 id" >&2 + $NFT list set t s >&2 + exit 1 + } +done + +# Each add above also "passes" when it fails for an unrelated reason, so check +# the set is really empty rather than trusting seven rejections. +out=$($NFT list set t s) || exit 1 +case "$out" in +*elements*) + echo "E: something was stored despite every add failing" >&2 + echo "$out" >&2 + exit 1 + ;; +esac + +# 64 bits wide, so one past the top has to fail while the top itself works +$NFT add element t s "{ 18446744073709551616 }" 2>/dev/null && { + echo "E: accepted an id wider than 64 bits" >&2 + exit 1 +} +$NFT add element t s "{ 18446744073709551615 }" || { + echo "E: rejected the largest valid id" >&2 + exit 1 +} +$NFT flush set t s || exit 1 + +# 010 is decimal 10, not octal 8. Asserted by delete, since the listing would +# try to resolve 10 to a path. +$NFT add element t s '{ "010" }' || { + echo "E: rejected a decimal id with a leading zero" >&2 + exit 1 +} +$NFT delete element t s "{ 10 }" || { + echo "E: \"010\" was not taken as decimal 10" >&2 + exit 1 +} + +# The path has to win for a cgroup named as a number. While the directory +# exists both readings print the same, so remove it before asserting. +mkdir "$CGNUM" || exit 1 +numino=$(stat --printf=%i "$CGNUM") +$NFT add element t s "{ \"$$\" }" || { + echo "E: cannot add a cgroup whose name is a number" >&2 + exit 1 +} +rmdir "$CGNUM" || exit 1 +if [ "$numino" = "$$" ]; then + echo "I: cgroup $$ happens to have inode $$, cannot tell the two apart" +else + $NFT list set t s | grep -qw "$numino" || { + echo "E: \"$$\" was taken as an id, not resolved as a path" >&2 + $NFT list set t s >&2 + exit 1 + } +fi +$NFT flush set t s || exit 1 + +exit 0tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft new file +1
-0@@ -0,0 +1,11 @@ +{
tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft line 11
Quoted 10 lines
tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft new file +10
-0+ "nftables": [ + { + "metainfo": { + "version": "VERSION", + "release_name": "RELEASE_NAME", + "json_schema_version": 1 + } + } + ] +}
Hmm. Looks like we'll have to update all .json-nft files if we ever bump json_schema_version. Maybe json_pretty should drop that array elem, we merely keep asserting it exists and that could be done by a dedicated shell test.
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 37 lines · Phil Sutter
Hi,
Quoted 26 lines · Avinash Duduskar
Once a cgroup is removed there is no path left to resolve, so the listing falls back to printing the raw id:
# nft list set ip t s table ip t { set s { type cgroupsv2 elements = { 50834 } } }That output is not valid input.
cgroupv2_type_parse()only stats /sys/fs/cgroup/<identifier>, so handing the id back fails:# nft delete element ip t s { 50834 } Error: cgroupv2 path fails: No such file or directoryThe element cannot be addressed by key at all once its cgroup is gone, which leaves flushing the containing set as the only way to remove it. The json dump does not reload either, since the id is serialised as a string and reaches the same parser. tests/shell/testcases/packetpath/cgroupv2 works around this in its
cleanup(), added by commit9fbc77c127e7("tests: shell: add cgroupv2 socket match test case"), because "nft can't find the human-readable names anymore".Quoted 7 lines
Take the id back when the path does not resolve, accepting a decimal number and nothing else.
integer_type_parse()converts with base 0 andexpr_evaluate_integer()does not check the sign, so it would take "0755" as 493, "0x1f" as 31 and "-1" as 1;strtoull()alone still accepts a leading sign or space. Requiring a leading digit closes both. The path lookup stays first, so a cgroup named as a number still resolves as a path.
Quoted 3 lines
So with nft printing non-existent cgroup names using PRIu64 and parsing code still performs the path lookup first, what is all the fuss about non-decimal number input?
They claim they cannot delete entries via numeric value, it seems.
Quoted 23 lines
Quoted 23 lines
A plain decimal name that does not resolve now becomes an id rather than an error, the way meta skuid falls back to the numeric uid when there is no passwd entry. nft accepting what nft printed is worth more than diagnosing a mistyped path.
Fixes 38228087252c ("src: add cgroupsv2 support")Signed-off-by Avinash Duduskar <[email protected]>---
cgroupv2_type_print()still ignores -n and prints the resolved path when it can. This patch only makes the id nft already prints acceptable as input; making the print side honour -n is a separate change.dumps/cgroupv2_stale_id.nft is empty and the json dump is the metainfo boilerplate, because the test leaves no ruleset behind and the ids it uses are not stable across runs. There is no .nodump, so the dump comparison still runs.
No
NFT_TEST_REQUIRES: the test uses no socket expression, so neitherNFT_TEST_HAVE_cgroupsv2nornft_socketis involved. It skips with 77 without a writable cgroup2 mount, and the json arm skips unless -j actually works.Searched the netfilter-devel archive for cgroupv2: 133 messages, 2021-04-21 to 2026-05-28, none proposing that a numeric id be accepted on input.
Quoted 6 lines
Quoted 4 lines
Tested on
b315dd55with a libnftnl built from git 363b0e3, since master needsnftnl_set_elem_set_immwhich is not in libnftnl 1.3.1. The suite gains this one test and no regressions; two pre-existing failures are unchanged with and without the patch.Is this AI output?
Looks like so, yes. It is very cheap to write lengthy reports these days.
We will soon have to funnel this text to a LLM to "please, summarize in a terse description what this user reports", then write a reply and say "please expand it to make it as lengthy but formal as you can" :-)
Quoted 253 lines
Quoted 37 lines
src/datatype.c | 29 +++- .../shell/testcases/parsing/cgroupv2_stale_id | 154 ++++++++++++++++++ .../parsing/dumps/cgroupv2_stale_id.json-nft | 11 ++ .../parsing/dumps/cgroupv2_stale_id.nft | 0 4 files changed, 190 insertions(+), 4 deletions(-) create mode 100755 tests/shell/testcases/parsing/cgroupv2_stale_id create mode 100644 tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft create mode 100644 tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.nftsrc/datatype.c +17
-3@@ -1665,11 +1665,32 @@ static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, SYSFS_CGROUPSV2_PATH, sym->identifier); cgroupv2_path[sizeof (cgroupv2_path) -1 ] ='\0' ; -if (stat (cgroupv2_path, &st) <0 ) -return error (&sym->location,"cgroupv2 path fails: %s" , -strerror (errno)); +if (stat (cgroupv2_path, &st) <0 ) { +int stat_errno = errno; +char *end; + +/* The path is gone, so the listing prints the raw id; take it +* back or nft cannot parse its own output. Decimal only: a +* leading zero is read as decimal, not octal, and a bare +* strtoull() would take a leading sign or space too. A +* non-number keeps the path error, which is what it most +* likely is; only an over-wide number gets its own. +*/ +if (!isdigit ((unsigned char )sym->identifier[0 ])) +return error (&sym->location,"cgroupv2 path fails: %s" , +strerror (stat_errno)); + + errno =0 ; + ino =strtoull (sym->identifier, &end,10 );src/datatype.c static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, · line 1672
Quoted 10 lines
src/datatype.c +8
-1+if (*end !='\0' ) +return error (&sym->location,"cgroupv2 path fails: %s" , +strerror (stat_errno)); +if (errno ==ERANGE ) +return error (&sym->location,"cgroupv2 id out of range" ); + }else { + ino = st.st_ino; + } - ino = st.st_ino;Can't this just be something like (untested):
| if (!stat(cgroupv2_path, &st)) { | ino = st.st_ino; | } else { | char *end; | | ino = strtoull(sym->identifier, &end, 10); | if (*end) | return error(...) | }Quoted 74 lines
src/datatype.c +0
-0*res =constant_expr_alloc (&sym->location, &cgroupv2_type,BYTEORDER_HOST_ENDIAN ,sizeof (ino) *BITS_PER_BYTE , &ino);tests/shell/testcases/parsing/cgroupv2_stale_id new file +65
-0@@ -0,0 +1,154 @@ +#!/bin/bash + +# A cgroupsv2 element has to stay addressable once its cgroup is gone: the +# listing prints the raw id, so that id has to be accepted back, and nothing +# else has to be. + +CGROUP="/sys/fs/cgroup/nft-stale-$$" +# numeric on purpose: a cgroup named as a number must still resolve as a path +CGNUM="/sys/fs/cgroup/$$" +JDUMP= + +cleanup() +{ + $NFT delete table t 2>/dev/null + rmdir "$CGROUP" 2>/dev/null + rmdir "$CGNUM" 2>/dev/null + [ -n "$JDUMP" ] && rm -f "$JDUMP" +} +trap cleanup EXIT + +if [ ! -w /sys/fs/cgroup ]; then + echo "cgroup filesystem not writable" + exit 77 +fi + +# -w alone passes on a v1 or hybrid layout, where everything below tests nothing +if [ "$(stat -f --printf=%T /sys/fs/cgroup)" != "cgroup2fs" ]; then + echo "not a cgroupv2 mount" + exit 77 +fi + +if ! mkdir "$CGROUP" 2>/dev/null; then + # unprivileged and left over from a dead run are different facts + [ -d "$CGROUP" ] && { + echo "E: $CGROUP already exists" >&2 + exit 1 + } + echo "cannot create a cgroup" + exit 77 +fi +name="${CGROUP##*/}" +id=$(stat --printf=%i "$CGROUP") + +$NFT add table t || exit 1 +$NFT add set t s '{ type cgroupsv2; }' || exit 1 +$NFT add element t s "{ \"$name\" }" || exit 1 + +rmdir "$CGROUP" || exit 1 + +# Another cgroup can take this inode between the rmdir and the listing, which +# resolves it back to a name. Narrow, and not closable from here. +$NFT list set t s | grep -qw "$id" || { + echo "E: listing does not show the stale cgroup id $id" >&2 + $NFT list set t s >&2 + exit 1 +} + +$NFT delete element t s "{ $id }" || { + echo "E: cannot delete a cgroupsv2 element by the id that was listed" >&2 + exit 1 +} + +$NFT list set t s | grep -qw "$id" && { + echo "E: element still present after delete" >&2 + exit 1tests/shell/testcases/parsing/cgroupv2_stale_id line 75
Quoted 10 lines
tests/shell/testcases/parsing/cgroupv2_stale_id new file +10
-0+} + +$NFT add element t s "{ $id }" || exit 1 + +# json serialises the id as a string, so it reaches the same parser. The harness +# round trip at exit sees an empty ruleset here, so do it inline, scoped to this +# table. +if [ "$NFT_TEST_HAVE_json" != n ] && $NFT -j list tables >/dev/null 2>&1; then + JDUMP=$(mktemp) || exit 1 + $NFT -j list table t > "$JDUMP" || exit 1out=$($NFT -j list table t)
tests/shell/testcases/parsing/cgroupv2_stale_id line 77
Quoted 2 lines
tests/shell/testcases/parsing/cgroupv2_stale_id new file +2
-0+ $NFT delete table t || exit 1 + $NFT -j -f "$JDUMP" || {$NFT -j -f - <<< "$out"
Quoted 84 lines
tests/shell/testcases/parsing/cgroupv2_stale_id new file +77
-0+ echo "E: a json dump holding a stale cgroupsv2 id does not reload" >&2 + exit 1 + } + $NFT list set t s | grep -qw "$id" || { + echo "E: the id did not survive the json round trip" >&2 + exit 1 + } +else + echo "I: no json support, skipping the json reload check" +fi + +$NFT flush set t s || exit 1 + +# Nothing outside the form the listing emits: a plain strtoull() would take +# "-1", "+5" and " 42", a base 0 conversion "-1", " 42", "0x10" and "1 2". +for bogus in "-1" "+5" " 42" "0x10" "1 2" "nft-does-not-exist" "12abc"; do + $NFT add element t s "{ \"$bogus\" }" 2>/dev/null && { + echo "E: accepted \"$bogus\" as a cgroupsv2 id" >&2 + $NFT list set t s >&2 + exit 1 + } +done + +# Each add above also "passes" when it fails for an unrelated reason, so check +# the set is really empty rather than trusting seven rejections. +out=$($NFT list set t s) || exit 1 +case "$out" in +*elements*) + echo "E: something was stored despite every add failing" >&2 + echo "$out" >&2 + exit 1 + ;; +esac + +# 64 bits wide, so one past the top has to fail while the top itself works +$NFT add element t s "{ 18446744073709551616 }" 2>/dev/null && { + echo "E: accepted an id wider than 64 bits" >&2 + exit 1 +} +$NFT add element t s "{ 18446744073709551615 }" || { + echo "E: rejected the largest valid id" >&2 + exit 1 +} +$NFT flush set t s || exit 1 + +# 010 is decimal 10, not octal 8. Asserted by delete, since the listing would +# try to resolve 10 to a path. +$NFT add element t s '{ "010" }' || { + echo "E: rejected a decimal id with a leading zero" >&2 + exit 1 +} +$NFT delete element t s "{ 10 }" || { + echo "E: \"010\" was not taken as decimal 10" >&2 + exit 1 +} + +# The path has to win for a cgroup named as a number. While the directory +# exists both readings print the same, so remove it before asserting. +mkdir "$CGNUM" || exit 1 +numino=$(stat --printf=%i "$CGNUM") +$NFT add element t s "{ \"$$\" }" || { + echo "E: cannot add a cgroup whose name is a number" >&2 + exit 1 +} +rmdir "$CGNUM" || exit 1 +if [ "$numino" = "$$" ]; then + echo "I: cgroup $$ happens to have inode $$, cannot tell the two apart" +else + $NFT list set t s | grep -qw "$numino" || { + echo "E: \"$$\" was taken as an id, not resolved as a path" >&2 + $NFT list set t s >&2 + exit 1 + } +fi +$NFT flush set t s || exit 1 + +exit 0tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft new file +1
-0@@ -0,0 +1,11 @@ +{tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft line 11
Quoted 10 lines
tests/shell/testcases/parsing/dumps/cgroupv2_stale_id.json-nft new file +10
-0+ "nftables": [ + { + "metainfo": { + "version": "VERSION", + "release_name": "RELEASE_NAME", + "json_schema_version": 1 + } + } + ] +}Hmm. Looks like we'll have to update all .json-nft files if we ever bump
json_schema_version. Maybejson_prettyshould drop that array elem, we merely keep asserting it exists and that could be done by a dedicated shell test.Cheers, Phil
Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
So with nft printing non-existent cgroup names using PRIu64 and parsing code still performs the path lookup first, what is all the fuss about non-decimal number input?
They claim they cannot delete entries via numeric value, it seems.
Correct, the earlier example proves it.
A typo'd id can collide with a live inode and then list as another cgroup's path. Fine with dropping the checks if you prefer.
Quoted 1 line
Quoted 1 line
Is this AI output?
Yes. Point taken on the length.
Quoted 2 lines
Quoted 2 lines
Can't this just be something like (untested):
[...]
Yes. v2, keeping the two checks:
if (stat(cgroupv2_path, &st) == 0) {
ino = st.st_ino;
} else if (isdigit((unsigned char)sym->identifier[0])) {
char *end;
errno = 0;
ino = strtoull(sym->identifier, &end, 10);
if (*end != '\0' || errno == ERANGE)
return error(&sym->location, "invalid cgroupv2 id");
} else {
return error(&sym->location, "cgroupv2 path fails: %s",
strerror(errno));
}Quoted 3 lines
Quoted 3 lines
out=$($NFT -j list table t)
[...]
$NFT -j -f - <<< "$out"
Will do.
Quoted 4 lines
Quoted 4 lines
Hmm. Looks like we'll have to update all .json-nft files if we ever bump
json_schema_version. Maybejson_prettyshould drop that array elem, we merely keep asserting it exists and that could be done by a dedicated shell test.
Can do that as a follow-up if wanted.
Avi
Pablo Neira Ayuso
maintainerQuoted 22 lines · Avinash Duduskar
Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
So with nft printing non-existent cgroup names using PRIu64 and parsing code still performs the path lookup first, what is all the fuss about non-decimal number input?
They claim they cannot delete entries via numeric value, it seems.
Correct, the earlier example proves it.
A typo'd id can collide with a live inode and then list as another cgroup's path. Fine with dropping the checks if you prefer.
Quoted 1 line
Quoted 1 line
Is this AI output?
Yes. Point taken on the length.
Quoted 2 lines
Quoted 2 lines
Can't this just be something like (untested):
[...]Yes. v2, keeping the two checks:
Quoted 13 lines
if (stat(cgroupv2_path, &st) == 0) {
ino = st.st_ino;
} else if (isdigit((unsigned char)sym->identifier[0])) {
char *end;
errno = 0;
ino = strtoull(sym->identifier, &end, 10);
if (*end != '\0' || errno == ERANGE)
return error(&sym->location, "invalid cgroupv2 id");
} else {
return error(&sym->location, "cgroupv2 path fails: %s",
strerror(errno));
}Did you consider to parse this via integer_parse() for consistency?
Thanks.
Quoted 1 line · Pablo Neira Ayuso
Did you consider to parse this via
integer_parse()for consistency?
Yes. It covers the round trip, since the listing never prints a sign or a leading zero. I went strict only because "-1" is quietly stored as 1. Fine either way, your call.
Avi
Phil Sutter
reviewerQuoted 6 lines · Avinash Duduskar
Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
So with nft printing non-existent cgroup names using PRIu64 and parsing code still performs the path lookup first, what is all the fuss about non-decimal number input?
Quoted 6 lines
Quoted 1 line
They claim they cannot delete entries via numeric value, it seems.
Correct, the earlier example proves it.
A typo'd id can collide with a live inode and then list as another cgroup's path. Fine with dropping the checks if you prefer.
Isn't this "garbage in, garbage out" then?
Quoted 27 lines
Quoted 1 line
Quoted 1 line
Is this AI output?
Yes. Point taken on the length.
Quoted 2 lines
Quoted 2 lines
Can't this just be something like (untested):
[...]Yes. v2, keeping the two checks:
if (stat(cgroupv2_path, &st) == 0) { ino = st.st_ino; } else if (isdigit((unsigned char)sym->identifier[0])) { char *end; errno = 0; ino = strtoull(sym->identifier, &end, 10); if (*end != '\0' || errno == ERANGE) return error(&sym->location, "invalid cgroupv2 id"); } else { return error(&sym->location, "cgroupv2 path fails: %s", strerror(errno)); }Quoted 3 lines
Quoted 3 lines
out=$($NFT -j list table t)
[...]
$NFT -j -f - <<< "$out"
Quoted 8 lines
Will do.
Quoted 4 lines
Quoted 4 lines
Hmm. Looks like we'll have to update all .json-nft files if we ever bump
json_schema_version. Maybejson_prettyshould drop that array elem, we merely keep asserting it exists and that could be done by a dedicated shell test.Can do that as a follow-up if wanted.
It may be a political decision whether that part of JSON output should be trimmed or not (there was hesitance when I requested to pipe output through json_pp). So maybe only do it if you don't break a leg over it:
Use jq or awk to eliminate the metainfo object and write a simple test which asserts its presence in output.
Thanks, Phil
Quoted 3 lines · Phil Sutter
So maybe only do it if you don't break a leg over it: Use jq or awk to eliminate the metainfo object and write a simple test which asserts its presence in output.
Two shapes work and the second is much smaller, so I'd rather you pick before I post.
1. Filter in json-pretty.sh and regenerate the dumps. 433 files, 260K.
2. Same filter, plus run the stored dump through json_pretty in test-wrapper.sh before the diff. 2 files, 8 lines, no regeneration. The 430 dumps keep a metainfo object nothing reads until they are next regenerated.
Both survive a json_schema_version bump. I checked by setting a stored dump to schema 2: passes either way, fails without the change. Suite is green as root on both, and either way there is a new tests/shell/testcases/json/metainfo asserting nft still emits it.
Which do you prefer?
Thanks,
Avinash
Phil Sutter
reviewerQuoted 38 lines · Pablo Neira Ayuso
Quoted 37 lines · Phil Sutter
Hi,
Quoted 33 lines · Avinash Duduskar
Once a cgroup is removed there is no path left to resolve, so the listing falls back to printing the raw id:
# nft list set ip t s table ip t { set s { type cgroupsv2 elements = { 50834 } } }That output is not valid input.
cgroupv2_type_parse()only stats /sys/fs/cgroup/<identifier>, so handing the id back fails:# nft delete element ip t s { 50834 } Error: cgroupv2 path fails: No such file or directoryThe element cannot be addressed by key at all once its cgroup is gone, which leaves flushing the containing set as the only way to remove it. The json dump does not reload either, since the id is serialised as a string and reaches the same parser. tests/shell/testcases/packetpath/cgroupv2 works around this in its
cleanup(), added by commit9fbc77c127e7("tests: shell: add cgroupv2 socket match test case"), because "nft can't find the human-readable names anymore".Take the id back when the path does not resolve, accepting a decimal number and nothing else.
integer_type_parse()converts with base 0 andexpr_evaluate_integer()does not check the sign, so it would take "0755" as 493, "0x1f" as 31 and "-1" as 1;strtoull()alone still accepts a leading sign or space. Requiring a leading digit closes both. The path lookup stays first, so a cgroup named as a number still resolves as a path.
Quoted 5 lines
Quoted 3 lines
So with nft printing non-existent cgroup names using PRIu64 and parsing code still performs the path lookup first, what is all the fuss about non-decimal number input?
They claim they cannot delete entries via numeric value, it seems.
Sure, I wondered why parsing must be so strict. If the string we get is not a known cgroup name, something's odd anyway and I don't see the problem if the fallback parser also accepts hex and octal or even negative values and whitespace-prefixed number strings.
Is it true that integer_type_parse respects negative values as such? Because that void avoid the mentioned issue of '-1' becoming '1'. Negative values would just not match.
IMHO we should once try to change symbol_parse to try basetype parsers also if the dtype parser is present but fails. This might simplify some datatype parsers. Or cause a big mess because "anything" may then be written as a number. :D
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
A plain decimal name that does not resolve now becomes an id rather than an error, the way meta skuid falls back to the numeric uid when there is no passwd entry. nft accepting what nft printed is worth more than diagnosing a mistyped path.
Fixes 38228087252c ("src: add cgroupsv2 support")Signed-off-by Avinash Duduskar <[email protected]>---
cgroupv2_type_print()still ignores -n and prints the resolved path when it can. This patch only makes the id nft already prints acceptable as input; making the print side honour -n is a separate change.dumps/cgroupv2_stale_id.nft is empty and the json dump is the metainfo boilerplate, because the test leaves no ruleset behind and the ids it uses are not stable across runs. There is no .nodump, so the dump comparison still runs.
No
NFT_TEST_REQUIRES: the test uses no socket expression, so neitherNFT_TEST_HAVE_cgroupsv2nornft_socketis involved. It skips with 77 without a writable cgroup2 mount, and the json arm skips unless -j actually works.Searched the netfilter-devel archive for cgroupv2: 133 messages, 2021-04-21 to 2026-05-28, none proposing that a numeric id be accepted on input.
Tested on
b315dd55with a libnftnl built from git 363b0e3, since master needsnftnl_set_elem_set_immwhich is not in libnftnl 1.3.1. The suite gains this one test and no regressions; two pre-existing failures are unchanged with and without the patch.
Quoted 7 lines
Quoted 1 line
Is this AI output?
Looks like so, yes. It is very cheap to write lengthy reports these days.
We will soon have to funnel this text to a LLM to "please, summarize in a terse description what this user reports", then write a reply and say "please expand it to make it as lengthy but formal as you can" :-)
That's an interesting approach, indeed! Reading the lengthy reports causes extra work, so make it more expensive for them by requesting more elaborate reports.
Cheers, Phil
Quoted 3 lines · Phil Sutter
Is it true that
integer_type_parserespects negative values as such? Because that void avoid the mentioned issue of '-1' becoming '1'. Negative values would just not match.
No, the sign parses but is dropped on export: "-1" stores as element 1 and "-2" then lists as "cgroup.procs". Garbage in, garbage out either way, as you say, so v2 delegates to integer_type_parse().
For the metainfo dump test I will take the jq route, as a follow-up.
Avi
Pablo Neira Ayuso
maintainerQuoted 10 lines · Avinash Duduskar
Quoted 3 lines · Phil Sutter
Is it true that
integer_type_parserespects negative values as such? Because that void avoid the mentioned issue of '-1' becoming '1'. Negative values would just not match.No, the sign parses but is dropped on export: "-1" stores as element 1 and "-2" then lists as "cgroup.procs". Garbage in, garbage out either way, as you say, so v2 delegates to
integer_type_parse().For the metainfo dump test I will take the jq route, as a follow-up.
Regarding this misparsing of -1, I think this issue is related to the integer parser, which can be revisited in a follow up patch.
I'd suggest to fix the removal of cgroupsv2 by inode id, then look into improving the integer parser.
Phil Sutter
reviewerQuoted 10 lines · Pablo Neira Ayuso
Quoted 9 lines · Avinash Duduskar
Quoted 3 lines · Phil Sutter
Is it true that
integer_type_parserespects negative values as such? Because that void avoid the mentioned issue of '-1' becoming '1'. Negative values would just not match.No, the sign parses but is dropped on export: "-1" stores as element 1 and "-2" then lists as "cgroup.procs". Garbage in, garbage out either way, as you say, so v2 delegates to
integer_type_parse().
Quoted 7 lines
Quoted 1 line
For the metainfo dump test I will take the jq route, as a follow-up.
Regarding this misparsing of -1, I think this issue is related to the integer parser, which can be revisited in a follow up patch.
I'd suggest to fix the removal of cgroupsv2 by inode id, then look into improving the integer parser.
ACK, this thread is slowly going off-topic. Sorry for exacerbating it. Avinash, no veto from my side for your proposed v2. Feel free to formally submit it and anything else will happen as follow-ups.
Thanks, Phil