🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Gopal Malaviya Tue, 20 Oct 2026 22:51
[PATCH iproute2] lib/fs: fix cgroup2 path for cgroups outside the cgroup namespace
#5770
Author: Gopal Malaviya
Date: Tue, 20 Oct 2026 22:51
75 lines
12861 bytes
get_cgroup2_path() opens a cgroup by its ID, reads the path of the
resulting fd from /proc/self/fd and returns it with the cgroup2 mount
point stripped. It assumes that the path always starts with the mount
point.

That is not true for a cgroup outside the caller's cgroup namespace:
the link then reads as just "/". Skipping strlen(mnt) characters of
it reads past the end of the string into stale stack data, which ss
then prints as the cgroup path.

This happens when ss runs with CAP_DAC_READ_SEARCH (needed for
open_by_handle_at()) inside a cgroup namespace and shows sockets of
processes outside of it, for example in a container started with
--cgroupns private and --cap-add DAC_READ_SEARCH or --privileged.
Without that capability open_by_handle_at() fails, and the socket is
already shown as unreachable.

Reproducer, as root, with a listener in the original cgroup:

  # python3 -m http.server 8080 --bind 127.0.0.1 &
  # mkdir /sys/fs/cgroup/test
  # echo $$ > /sys/fs/cgroup/test/cgroup.procs
  # unshare --cgroup --mount --propagation private sh
  # umount /sys/fs/cgroup; mount -t cgroup2 none /sys/fs/cgroup
  # cat /proc/self/cgroup
  0::/
  # ss -tlne sport = :8080 | grep -o "cgroup:[^ ]*"
  cgroup:-microsoft-standard-WSL2

The garbage depends on what is left on the stack; here it is part of
the kernel release string, and in an Alpine container it was
"s/cgroup".

Only strip the mount point when the path starts with it. Otherwise
return NULL, so that cg_id_to_path() shows the cgroup as unreachable,
the same as when open_by_handle_at() fails:

  # ss -tlne sport = :8080 | grep -o "cgroup:[^ ]*"
  cgroup:unreachable:550d

Fixes: d5e6ee0dac64 ("ss: introduce cgroup2 cache and helper functions")
Signed-off-by: Gopal Malaviya <gopalmalaviya53@gmail.com>
---
 lib/fs.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/lib/fs.c b/lib/fs.c
index 6fe1d6db..1562d25c 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -238,10 +238,18 @@ char *get_cgroup2_path(__u64 id, bool full)
 	}
 	link_buf[link_len] = '\0';
 
-	if (full)
+	if (full) {
 		path = strdup(link_buf);
-	else
-		path = strdup(link_buf + strlen(mnt));
+	} else {
+		size_t mnt_len = strlen(mnt);
+
+		/* A cgroup outside our cgroup namespace is not under the
+		 * mount and the link reads as just "/".
+		 */
+		if (strncmp(link_buf, mnt, mnt_len) != 0)
+			goto out;
+		path = strdup(link_buf + mnt_len);
+	}
 	if (!path)
 		fprintf(stderr,
 			"Failed to allocate memory for cgroup2 path\n");
-- 
2.43.0
Re: [PATCH iproute2] lib/fs: fix cgroup2 path for cgroups outside the cgroup namespace
#6570
Author: patchwork-bot+netdevbpf
Date: Thu, 22 Oct 2026 18:30
27 lines
8766 bytes
Hello:

This patch was applied to iproute2/iproute2.git (main)
by Stephen Hemminger <stephen@networkplumber.org>:

On Tue, 20 Oct 2026 22:51:13 +0000 you wrote:
> get_cgroup2_path() opens a cgroup by its ID, reads the path of the
> resulting fd from /proc/self/fd and returns it with the cgroup2 mount
> point stripped. It assumes that the path always starts with the mount
> point.
> 
> That is not true for a cgroup outside the caller's cgroup namespace:
> the link then reads as just "/". Skipping strlen(mnt) characters of
> it reads past the end of the string into stale stack data, which ss
> then prints as the cgroup path.
> 
> [...]

Here is the summary with links:
  - [iproute2] lib/fs: fix cgroup2 path for cgroups outside the cgroup namespace
    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=7a0464e280dd

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads