diff --git a/luckfox-sdk-patches/buildroot/files/package/nftables/0001-datatype-cgroupv2-use-the-64-bit-kernfs-id.patch b/luckfox-sdk-patches/buildroot/files/package/nftables/0001-datatype-cgroupv2-use-the-64-bit-kernfs-id.patch new file mode 100644 index 0000000..f3de1fc --- /dev/null +++ b/luckfox-sdk-patches/buildroot/files/package/nftables/0001-datatype-cgroupv2-use-the-64-bit-kernfs-id.patch @@ -0,0 +1,84 @@ +From: BlueFlare Warden +Subject: [PATCH] datatype: take the cgroupv2 id from the directory's file handle + +"socket cgroupv2 level N " compares the 64-bit kernfs id of the cgroup. +nft built it from st_ino, which equals the id only on 64-bit kernels. On a +32-bit kernel (armv7) the id is (generation << 32) | ino and st_ino carries the +low half, so every rule built from a path compared against the wrong value and +never matched: nothing was counted per service. name_to_handle_at() returns the +whole id on both. The listing path (id back to a name) uses the same helper. + +Found on the WardenOS panel kernel (6.18.46, armv7) with nftables 1.1.0. + +diff --git a/src/datatype.c b/src/datatype.c +index d398a9c..c6502ec 100644 +--- a/src/datatype.c ++++ b/src/datatype.c +@@ -19,6 +19,7 @@ + #include + #include + #include ++#include + #include + + #include +@@ -1605,12 +1606,39 @@ const struct datatype policy_type = { + + #define SYSFS_CGROUPSV2_PATH "/sys/fs/cgroup" + ++/* The id the kernel compares in "socket cgroupv2" is the 64-bit kernfs id of ++ * the cgroup directory. On 64-bit kernels that is st_ino, but on 32-bit kernels ++ * it is (generation << 32) | ino and st_ino carries only the low half, so a ++ * rule built from st_ino never matches. The file handle of the directory holds ++ * the full id on both. Fall back to st_ino where handles are not available. */ ++static bool cgroupv2_path_id(const char *path, uint64_t *id) ++{ ++ struct { ++ struct file_handle fh; ++ unsigned char buf[sizeof(uint64_t)]; ++ } h; ++ struct stat st; ++ int mount_id; ++ ++ h.fh.handle_bytes = sizeof(h.buf); ++ if (name_to_handle_at(AT_FDCWD, path, &h.fh, &mount_id, 0) == 0 && ++ h.fh.handle_bytes == sizeof(*id)) { ++ memcpy(id, h.fh.f_handle, sizeof(*id)); ++ return true; ++ } ++ if (stat(path, &st) < 0) ++ return false; ++ *id = st.st_ino; ++ return true; ++} ++ + static char *cgroupv2_get_path(const char *path, uint64_t id) + { + char dent_name[PATH_MAX + 1]; + char *cgroup_path = NULL; + struct dirent *dent; + struct stat st; ++ uint64_t dent_id; + DIR *d; + + d = opendir(path); +@@ -1626,7 +1654,7 @@ static char *cgroupv2_get_path(const char *path, uint64_t id) + path, dent->d_name); + dent_name[sizeof(dent_name) - 1] = '\0'; + +- if (dent->d_ino == id) { ++ if (cgroupv2_path_id(dent_name, &dent_id) && dent_id == id) { + cgroup_path = xstrdup(dent_name); + break; + } +@@ -1674,7 +1702,9 @@ static struct error_record *cgroupv2_type_parse(struct parse_ctx *ctx, + return error(&sym->location, "cgroupv2 path fails: %s", + strerror(errno)); + +- ino = st.st_ino; ++ if (!cgroupv2_path_id(cgroupv2_path, &ino)) ++ return error(&sym->location, "cgroupv2 path fails: %s", ++ strerror(errno)); + *res = constant_expr_alloc(&sym->location, &cgroupv2_type, + BYTEORDER_HOST_ENDIAN, + sizeof(ino) * BITS_PER_BYTE, &ino);