From 19f6b0c07fcdf8c3ac4d6d4bc5eafff9585e1e6b Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Wed, 31 Jul 2024 11:47:07 -0700 Subject: [PATCH] Use /proc/sys/net/core/dev_weight to determine if we are in the root netns. After 19249c0724f2 ("net: make net.core.{r,w}mem_{default,max} namespaced") /proc/sys/net/core/rmem_default is present in non-initial netns as well. /proc/sys/net/core/dev_weight has existed from the beginning of Linux git repo in 1da177e4c3f4 ("Linux-2.6.12-rc2"). So it should be safe to use this file. Demo: ``` $ sudo ip netns add foo $ sudo ip netns exec foo stat /proc/sys/net/core/dev_weight stat: cannot statx '/proc/sys/net/core/dev_weight': No such file or directory $ sudo ip netns delete foo ``` Fixes #10704 PiperOrigin-RevId: 658087447 --- runsc/sandbox/network.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/runsc/sandbox/network.go b/runsc/sandbox/network.go index d06932511..cd0c4a508 100644 --- a/runsc/sandbox/network.go +++ b/runsc/sandbox/network.go @@ -103,17 +103,17 @@ func joinNetNS(nsPath string) (func(), error) { }, nil } -// isRootNS determines whether we are running in the root net namespace. -// /proc/sys/net/core/rmem_default only exists in root network namespace. -func isRootNS() (bool, error) { - err := unix.Access("/proc/sys/net/core/rmem_default", unix.F_OK) +// isRootNetNS determines whether we are running in the root net namespace. +// /proc/sys/net/core/dev_weight only exists in root network namespace. +func isRootNetNS() (bool, error) { + err := unix.Access("/proc/sys/net/core/dev_weight", unix.F_OK) switch err { case nil: return true, nil case unix.ENOENT: return false, nil default: - return false, fmt.Errorf("failed to access /proc/sys/net/core/rmem_default: %v", err) + return false, fmt.Errorf("failed to access /proc/sys/net/core/dev_weight: %v", err) } } @@ -151,7 +151,7 @@ func createInterfacesAndRoutesFromNS(conn *urpc.Client, nsPath string, conf *con return fmt.Errorf("querying interfaces: %w", err) } - isRoot, err := isRootNS() + isRoot, err := isRootNetNS() if err != nil { return err }