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,
}