f-91: netns: retour au netns du thread courant, thread détruit sinon #91

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-26 14:08:07 +01:00
commit 7b6b454b60
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
4 changed files with 250 additions and 59 deletions

View file

@ -5,22 +5,11 @@ package netns
import (
"fmt"
"os"
"runtime"
"golang.org/x/sys/unix"
)
func call(name string, fn func() error) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// sauvegarde du netns courant
orig, err := os.Open("/proc/self/ns/net")
if err != nil {
return err
}
defer orig.Close()
// entrer dans le netns cible
f, err := os.Open(fmt.Sprintf("/var/run/netns/%s", name))
if err != nil {
@ -28,17 +17,7 @@ func call(name string, fn func() error) error {
}
defer f.Close()
if err := unix.Setns(int(f.Fd()), unix.CLONE_NEWNET); err != nil {
return err
}
// exécuter la fonction dans le netns
err = fn()
// toujours revenir au netns d'origine
if restoreErr := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restoreErr != nil {
return restoreErr
}
return err
return onThread(func() error {
return unix.Setns(int(f.Fd()), unix.CLONE_NEWNET)
}, fn)
}

View file

@ -5,15 +5,11 @@ package netns
import (
"fmt"
"os"
"runtime"
"golang.org/x/sys/unix"
)
func create(name string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
base := "/var/run/netns"
path := base + "/" + name
@ -34,36 +30,20 @@ func create(name string) error {
}
f.Close()
// sauvegarde du netns courant
orig, err := os.Open("/proc/self/ns/net")
if err != nil {
return err
}
defer orig.Close()
// nouveau netns
if err := unix.Unshare(unix.CLONE_NEWNET); err != nil {
return err
}
// bind mount du netns du thread courant vers /var/run/netns/<name>
// /proc/self/ns/net pointe vers le ns du processus (thread principal),
// pas du thread courant — il faut utiliser le tid explicitement
threadNsPath := fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid())
if err := unix.Mount(
threadNsPath,
path,
"",
unix.MS_BIND,
"",
); err != nil {
return err
}
// revenir au netns original
if err := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); err != nil {
return err
}
return nil
return onThread(func() error {
return unix.Unshare(unix.CLONE_NEWNET)
}, func() error {
// bind mount du netns du thread courant vers /var/run/netns/<name>
// /proc/self/ns/net pointe vers le ns du processus (thread principal),
// pas du thread courant — il faut utiliser le tid explicitement
threadNsPath := fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid())
return unix.Mount(
threadNsPath,
path,
"",
unix.MS_BIND,
"",
)
})
}

View file

@ -0,0 +1,33 @@
//go:build linux
package netns
import (
"os"
"runtime"
"golang.org/x/sys/unix"
)
func onThread(enter func() error, fn func() error) error {
runtime.LockOSThread()
orig, err := os.Open("/proc/thread-self/ns/net")
if err != nil {
runtime.UnlockOSThread()
return err
}
defer orig.Close()
err = enter()
if err == nil {
err = fn()
}
if restoreErr := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restoreErr != nil {
return restoreErr
}
runtime.UnlockOSThread()
return err
}

View file

@ -0,0 +1,199 @@
//go:build linux
package netns
import (
"errors"
"fmt"
"os"
"runtime"
"testing"
"golang.org/x/sys/unix"
)
func init() {
runtime.LockOSThread()
}
var onMain = make(chan func())
func TestMain(m *testing.M) {
code := make(chan int)
go func() { code <- m.Run() }()
for {
select {
case f := <-onMain:
f()
case c := <-code:
os.Exit(c)
}
}
}
func runOnMain(f func()) {
done := make(chan struct{})
onMain <- func() {
f()
close(done)
}
<-done
}
type nsID struct{ dev, ino uint64 }
func statNS(t *testing.T, path string) nsID {
t.Helper()
var st unix.Stat_t
if err := unix.Stat(path, &st); err != nil {
t.Fatalf("stat %s : %v", path, err)
}
return nsID{uint64(st.Dev), st.Ino}
}
func threadNS(t *testing.T) nsID {
t.Helper()
return statNS(t, "/proc/thread-self/ns/net")
}
func setupMainInOtherNS(t *testing.T) (nsID, string) {
t.Helper()
if os.Geteuid() != 0 {
t.Skip("netns : root requis")
}
runtime.LockOSThread()
root := threadNS(t)
rootFD, err := os.Open("/proc/thread-self/ns/net")
if err != nil {
t.Fatalf("netns racine : %v", err)
}
runtime.UnlockOSThread()
other := fmt.Sprintf("t91-main-%d", os.Getpid())
if err := Create(other); err != nil {
t.Fatalf("création de %s : %v", other, err)
}
t.Cleanup(func() {
if err := Delete(other); err != nil {
t.Errorf("suppression de %s : %v", other, err)
}
})
var setErr error
runOnMain(func() {
f, err := os.Open("/var/run/netns/" + other)
if err != nil {
setErr = err
return
}
defer f.Close()
setErr = unix.Setns(int(f.Fd()), unix.CLONE_NEWNET)
})
if setErr != nil {
t.Fatalf("thread principal dans %s : %v", other, setErr)
}
t.Cleanup(func() {
var err error
runOnMain(func() { err = unix.Setns(int(rootFD.Fd()), unix.CLONE_NEWNET) })
rootFD.Close()
if err != nil {
t.Fatalf("thread principal ramené au netns racine : %v", err)
}
})
return root, other
}
func assertBackToRoot(t *testing.T, root nsID) {
t.Helper()
if got := threadNS(t); got != root {
t.Fatalf("thread laissé dans le netns %v, netns racine %v attendu", got, root)
}
runtime.UnlockOSThread()
}
func TestCall_ThreadPrincipalDansUnAutreNetns(t *testing.T) {
root, other := setupMainInOtherNS(t)
target := fmt.Sprintf("t91-cible-%d", os.Getpid())
if err := Create(target); err != nil {
t.Fatalf("création de %s : %v", target, err)
}
t.Cleanup(func() {
if err := Delete(target); err != nil {
t.Errorf("suppression de %s : %v", target, err)
}
})
runtime.LockOSThread()
var inside nsID
if err := Call(target, func() error {
inside = threadNS(t)
return nil
}); err != nil {
t.Fatalf("Call : %v", err)
}
if want := statNS(t, "/var/run/netns/"+target); inside != want {
t.Errorf("fn exécutée dans %v, netns %s %v attendu", inside, target, want)
}
if inside == statNS(t, "/var/run/netns/"+other) {
t.Errorf("fn exécutée dans le netns du thread principal")
}
assertBackToRoot(t, root)
}
func TestCall_ErreurDeFnRenvoyeeEtThreadRestaure(t *testing.T) {
root, _ := setupMainInOtherNS(t)
target := fmt.Sprintf("t91-err-%d", os.Getpid())
if err := Create(target); err != nil {
t.Fatalf("création de %s : %v", target, err)
}
t.Cleanup(func() {
if err := Delete(target); err != nil {
t.Errorf("suppression de %s : %v", target, err)
}
})
runtime.LockOSThread()
want := errors.New("échec de fn")
if err := Call(target, func() error { return want }); !errors.Is(err, want) {
t.Errorf("Call : %v, %v attendu", err, want)
}
assertBackToRoot(t, root)
}
func TestCall_NetnsAbsent(t *testing.T) {
root, _ := setupMainInOtherNS(t)
runtime.LockOSThread()
called := false
err := Call(fmt.Sprintf("t91-absent-%d", os.Getpid()), func() error {
called = true
return nil
})
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("Call : %v, netns absent attendu", err)
}
if called {
t.Errorf("fn appelée sans netns")
}
assertBackToRoot(t, root)
}
func TestCreate_ThreadPrincipalDansUnAutreNetns(t *testing.T) {
root, other := setupMainInOtherNS(t)
name := fmt.Sprintf("t91-cree-%d", os.Getpid())
runtime.LockOSThread()
if err := Create(name); err != nil {
t.Fatalf("Create : %v", err)
}
t.Cleanup(func() {
if err := Delete(name); err != nil {
t.Errorf("suppression de %s : %v", name, err)
}
})
created := statNS(t, "/var/run/netns/"+name)
if created == root || created == statNS(t, "/var/run/netns/"+other) {
t.Errorf("netns %s %v : nouveau netns attendu", name, created)
}
assertBackToRoot(t, root)
}