carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package instanceexec
4
5import (
6 "errors"
7 "os"
8 "os/exec"
9 "runtime"
10 "testing"
11
12 "golang.org/x/sys/unix"
13)
14
15func TestPlan14ClaimGenerationDescriptorBoundary(t *testing.T) {
16 if os.Getenv("PLAN14_CLAIM_GENERATION_CHILD") == "1" {
17 runtime.LockOSThread()
18 if err := ScopeClaimGeneration(); err != nil {
19 t.Fatal(err)
20 }
21 for _, call := range []uintptr{unix.SYS_RECVMSG, unix.SYS_RECVMMSG, unix.SYS_PTRACE, unix.SYS_PIDFD_GETFD, unix.SYS_OPEN_BY_HANDLE_AT, unix.SYS_IO_URING_SETUP, unix.SYS_IO_URING_ENTER, unix.SYS_IO_URING_REGISTER, unix.SYS_SETNS, unix.SYS_UNSHARE} {
22 _, _, err := unix.RawSyscall6(call, ^uintptr(0), 0, 0, 0, 0, 0)
23 if err != unix.EPERM {
24 t.Fatalf("syscall %d: %v; want EPERM", call, err)
25 }
26 }
27 data, control := make([]byte, 1), make([]byte, unix.CmsgSpace(4))
28 if _, _, _, _, err := unix.Recvmsg(3, data, control, 0); !errors.Is(err, unix.EPERM) {
29 t.Fatal("descriptor receipt permitted", err)
30 }
31 if n, err := unix.Read(3, data); err != nil || n != 1 || data[0] != 'x' {
32 t.Fatal("ordinary socket read failed", n, err)
33 }
34 return
35 }
36 sockets, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
37 if err != nil {
38 t.Fatal(err)
39 }
40 parent, child := os.NewFile(uintptr(sockets[0]), "parent"), os.NewFile(uintptr(sockets[1]), "child")
41 defer parent.Close()
42 defer child.Close()
43 file, err := os.CreateTemp(t.TempDir(), "staged")
44 if err != nil {
45 t.Fatal(err)
46 }
47 defer file.Close()
48 if err := unix.Sendmsg(int(parent.Fd()), []byte{'x'}, unix.UnixRights(int(file.Fd())), nil, 0); err != nil {
49 t.Fatal(err)
50 }
51 cmd := exec.Command(os.Args[0], "-test.run=^TestPlan14ClaimGenerationDescriptorBoundary$")
52 cmd.Env = append(os.Environ(), "PLAN14_CLAIM_GENERATION_CHILD=1")
53 cmd.ExtraFiles = []*os.File{child}
54 if out, err := cmd.CombinedOutput(); err != nil {
55 t.Fatalf("boundary child: %v\n%s", err, out)
56 }
57}
58
59func TestPlan14ClaimGenerationNamespaceBoundary(t *testing.T) {
60 if os.Getenv("PLAN14_CLAIM_NAMESPACE_CHILD") == "1" {
61 runtime.LockOSThread()
62 if err := ScopeClaimGeneration(); err != nil {
63 t.Fatal(err)
64 }
65 _, _, err := unix.RawSyscall6(unix.SYS_CLONE, uintptr(unix.CLONE_NEWUSER|unix.CLONE_THREAD), 0, 0, 0, 0, 0)
66 if err != unix.EPERM {
67 t.Fatal("namespace clone permitted", err)
68 }
69 _, _, err = unix.RawSyscall6(unix.SYS_CLONE3, 0, 0, 0, 0, 0, 0)
70 if err != unix.ENOSYS {
71 t.Fatal("clone3 permitted", err)
72 }
73 cmd := exec.Command("/bin/true")
74 if out, err := cmd.CombinedOutput(); err != nil {
75 t.Fatalf("ordinary fork/exec: %v %s", err, out)
76 }
77 return
78 }
79 cmd := exec.Command(os.Args[0], "-test.run=^TestPlan14ClaimGenerationNamespaceBoundary$")
80 cmd.Env = append(os.Environ(), "PLAN14_CLAIM_NAMESPACE_CHILD=1")
81 if out, err := cmd.CombinedOutput(); err != nil {
82 t.Fatalf("namespace boundary: %v\n%s", err, out)
83 }
84}
85