datatype: accept a numeric cgroupsv2 id on input
PATCH v2 1/1 datatype: accept a numeric cgroupsv2 id on input
nft prints non-existent cgroup names as the raw id using PRIu64, but cgroupv2_type_parse() only stats /sys/fs/cgroup/<identifier>, so the listing does not load back:
# nft list set ip t s
table ip t {
set s {
type cgroupsv2
elements = { 50834 }
}
}
# nft delete element ip t s { 50834 }
Error: cgroupv2 path fails: No such file or directoryThe element cannot be deleted by key once its cgroup is gone, only flushed with the set, and a dump does not restore. The json dump has the same problem, and the stale id has been visible in the wild since 2022 (see Link). tests/shell/testcases/packetpath/cgroupv2 already works around this in cleanup().
Fall back to integer_type_parse() when the path does not resolve, as boolean_type_parse() already does. The path lookup stays first, so a cgroup named as a number still resolves as a path. Improving the integer parser is left for a follow-up as discussed in v1.
---
Changes since v1 (https://lore.kernel.org/netfilter-devel/[email protected]/):
- drop the strict decimal parser, delegate to integer_type_parse()
(Pablo, Phil)
- json test arm uses a here-string instead of a tempfile (Phil)
- shorter commit message and test
src/datatype.c | 13 +- .../shell/testcases/parsing/cgroupv2_stale_id | 129 ++++++++++++++++++ .../parsing/dumps/cgroupv2_stale_id.json-nft | 11 ++ .../parsing/dumps/cgroupv2_stale_id.nft | 0 4 files changed, 151 insertions(+), 2 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 +11 -2
@@ -1665,9 +1665,18 @@ 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 ) +if (stat (cgroupv2_path, &st) <0 ) { +struct error_record *erec; +int stat_errno = errno; + +/* the listing prints a raw id once the path is gone */ + erec =integer_type_parse (ctx, sym, res); +if (!erec) +return NULL ; +erec_destroy (erec);return error (&sym->location,"cgroupv2 path fails: %s" , -strerror (errno)); +strerror (stat_errno)); + } ino = st.st_ino; *res = constant_expr_alloc(&sym->location, &cgroupv2_type,
tests/shell/testcases/parsing/cgroupv2_stale_id new file +129 -0
129 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: 31fb7af1a99d67c87e94b4415e553589b21db2d1
Signature · 1 line
2.55.0
Pablo Neira Ayuso
maintainerQuoted 52 lines · Avinash Duduskar
nft prints non-existent cgroup names as the raw id using PRIu64, but
cgroupv2_type_parse()only stats /sys/fs/cgroup/<identifier>, so the listing does not load back:# nft list set ip t s table ip t { set s { type cgroupsv2 elements = { 50834 } } } # nft delete element ip t s { 50834 } Error: cgroupv2 path fails: No such file or directoryThe element cannot be deleted by key once its cgroup is gone, only flushed with the set, and a dump does not restore. The json dump has the same problem, and the stale id has been visible in the wild since 2022 (see Link). tests/shell/testcases/packetpath/cgroupv2 already works around this in
cleanup().Fall back to
integer_type_parse()when the path does not resolve, asboolean_type_parse()already does. The path lookup stays first, so a cgroup named as a number still resolves as a path. Improving the integer parser is left for a follow-up as discussed in v1.Fixes 38228087252c ("src: add cgroupsv2 support")Signed-off-by Avinash Duduskar <[email protected]>---
Changes since v1 (https://lore.kernel.org/netfilter-devel/[email protected]/):
- drop the strict decimal parser, delegate tointeger_type_parse()
(Pablo, Phil)
- json test arm uses a here-string instead of a tempfile (Phil)
- shorter commit message and testsrc/datatype.c | 13 +- .../shell/testcases/parsing/cgroupv2_stale_id | 129 ++++++++++++++++++ .../parsing/dumps/cgroupv2_stale_id.json-nft | 11 ++ .../parsing/dumps/cgroupv2_stale_id.nft | 0 4 files changed, 151 insertions(+), 2 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 +0
-0@@ -1665,9 +1665,18 @@ static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, SYSFS_CGROUPSV2_PATH, sym->identifier); cgroupv2_path[sizeof (cgroupv2_path) -1 ] ='\0' ;
src/datatype.c static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, · line 1676
Quoted 10 lines
src/datatype.c +9
-1-if (stat (cgroupv2_path, &st) <0 ) +if (stat (cgroupv2_path, &st) <0 ) { +struct error_record *erec; +int stat_errno = errno; + +/* the listing prints a raw id once the path is gone */ + erec =integer_type_parse (ctx, sym, res); +if (!erec) +return NULL ; +erec_destroy (erec);
Any reason to ignore the error that integer_type_parse() provides? Instead I would suggest:
if (erec)
return erec;Thanks.
Quoted 7 lines
src/datatype.c +2
-1return error (&sym->location,"cgroupv2 path fails: %s" , -strerror (errno)); +strerror (stat_errno)); + } ino = st.st_ino; *res = constant_expr_alloc(&sym->location, &cgroupv2_type,
Quoted 1 line · Pablo Neira Ayuso
Any reason to ignore the error that
integer_type_parse()provides?
No, it was left over from the strict parser that owned its own message. Nothing follows the call, so it reduces to a direct return and the hunk goes from ten added lines to two:
+ /* the listing prints a raw id once the path is gone */
if (stat(cgroupv2_path, &st) < 0)
- return error(&sym->location, "cgroupv2 path fails: %s",
- strerror(errno));
+ return integer_type_parse(ctx, sym, res);
v3 to follow.
Avi