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
This commit is contained in:
Ayush Ranjan
2024-07-31 11:50:13 -07:00
committed by gVisor bot
parent 5b47d132c2
commit 19f6b0c07f
+6 -6
View File
@@ -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
}