Fix unshare network namespace with capabilities

This commit is contained in:
世界
2026-10-23 19:57:05 +08:00
parent 7a0ae5f358
commit 9c24112d12
3 changed files with 31 additions and 4 deletions
+7 -1
View File
@@ -15,6 +15,8 @@ import (
"golang.org/x/sys/unix"
)
var initialDir, _ = os.Getwd()
func runInUserNamespaceIfNeeded(options option.Options, optionsList []*OptionsEntry) error {
if !common.Any(options.NetworkNamespaces, func(namespace option.NetworkNamespace) bool {
return namespace.Type == C.NetNsTypeUnshare
@@ -31,8 +33,12 @@ func runInUserNamespaceIfNeeded(options option.Options, optionsList []*OptionsEn
if data[0].Effective&(1<<unix.CAP_SYS_ADMIN) != 0 {
return nil
}
if data[0].Effective != 0 || data[1].Effective != 0 {
return E.New("unshare network namespace requires CAP_SYS_ADMIN when running with other capabilities, as the rootless user namespace drops them")
}
command := exec.Command("/proc/self/exe", os.Args[1:]...)
command.Args = os.Args
command.Dir = initialDir
command.Stdin = os.Stdin
for _, entry := range optionsList {
if entry.path == "stdin" {
@@ -50,7 +56,7 @@ func runInUserNamespaceIfNeeded(options option.Options, optionsList []*OptionsEn
{ContainerID: os.Getegid(), HostID: os.Getegid(), Size: 1},
},
GidMappingsEnableSetgroups: false,
AmbientCaps: []uintptr{unix.CAP_SYS_ADMIN, unix.CAP_NET_ADMIN, unix.CAP_NET_RAW},
AmbientCaps: []uintptr{unix.CAP_SYS_ADMIN, unix.CAP_NET_ADMIN, unix.CAP_NET_RAW, unix.CAP_NET_BIND_SERVICE},
Setpgid: true,
Pdeathsig: syscall.SIGKILL,
}
+21 -3
View File
@@ -16,6 +16,7 @@ import (
"github.com/sagernet/sing/common/logger"
vnetns "github.com/vishvananda/netns"
"golang.org/x/sys/unix"
)
type Manager struct {
@@ -112,14 +113,31 @@ func (m *Manager) startNamespace(namespace option.NetworkNamespace, scope *adapt
}
m.paths[namespace.Tag] = netnsPath(pid)
m.logger.Info("created network namespace[", namespace.Tag, "], holder pid: ", pid)
if os.Geteuid() == 0 {
m.logger.Info("enter network namespace[", namespace.Tag, "] with: nsenter -n -t ", pid)
} else {
if ownsUserNamespace() {
m.logger.Info("enter network namespace[", namespace.Tag, "] with: nsenter -U --preserve-credentials -n -t ", pid)
} else {
m.logger.Info("enter network namespace[", namespace.Tag, "] with: nsenter -n -t ", pid)
}
return nil
}
func ownsUserNamespace() bool {
effectiveUID := os.Geteuid()
if effectiveUID == 0 {
return false
}
userNamespace, err := os.Open("/proc/self/ns/user")
if err != nil {
return false
}
defer userNamespace.Close()
ownerUID, err := unix.IoctlGetUint32(int(userNamespace.Fd()), unix.NS_GET_OWNER_UID)
if err != nil {
return false
}
return int(ownerUID) == effectiveUID
}
func (m *Manager) ResolvePath(nameOrPath string) string {
path, loaded := m.paths[nameOrPath]
if loaded && path != "" {
+3
View File
@@ -2,6 +2,8 @@ package netns
import (
"os"
"os/signal"
"syscall"
"github.com/sagernet/sing-box/adapter"
)
@@ -9,6 +11,7 @@ import (
var _ adapter.NetworkNamespaceManager = (*Manager)(nil)
func Hold() {
signal.Ignore(os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
buffer := make([]byte, 1)
for {
_, err := os.Stdin.Read(buffer)