| 1 | |
| 2 | |
| 3 | package guestos |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "net" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/Microsoft/go-winio" |
| 15 | "github.com/google/uuid" |
| 16 | "github.com/sylvinus/agent-vm/internal/cage" |
| 17 | "golang.org/x/sys/windows" |
| 18 | ) |
| 19 | |
| 20 | func storagePipeFixture(t *testing.T, impersonation winio.PipeImpLevel, run func(net.Conn, string)) { |
| 21 | t.Helper() |
| 22 | user, err := windows.GetCurrentProcessToken().GetTokenUser() |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | sid := user.User.Sid.String() |
| 27 | name := `\\.\pipe\Cage.StorageTest.` + uuid.NewString() |
| 28 | listener, err := winio.ListenPipe(name, &winio.PipeConfig{SecurityDescriptor: "D:P(A;;GA;;;" + sid + ")"}) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | defer listener.Close() |
| 33 | done := make(chan struct{}) |
| 34 | go func() { |
| 35 | defer close(done) |
| 36 | connection, err := listener.Accept() |
| 37 | if err != nil { |
| 38 | t.Error(err) |
| 39 | return |
| 40 | } |
| 41 | defer connection.Close() |
| 42 | var request [1]byte |
| 43 | if _, err := connection.Read(request[:]); err != nil { |
| 44 | t.Error(err) |
| 45 | return |
| 46 | } |
| 47 | run(connection, sid) |
| 48 | }() |
| 49 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 50 | defer cancel() |
| 51 | client, err := winio.DialPipeAccessImpLevel(ctx, name, 0x120003, impersonation) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | defer client.Close() |
| 56 | if _, err := client.Write([]byte("x")); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | select { |
| 60 | case <-done: |
| 61 | case <-ctx.Done(): |
| 62 | t.Fatal("private storage pipe test timed out") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestWindowsStorageDestinationUsesCallerAccess(t *testing.T) { |
| 67 | root := t.TempDir() |
| 68 | path := filepath.Join(root, "system.vhdx") |
| 69 | if err := os.WriteFile(path, []byte("synthetic VHDX identity"), 0600); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if err := protectWindowsProfile(path); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | storagePipeFixture(t, winio.PipeImpLevelImpersonation, func(connection net.Conn, sid string) { |
| 76 | handle, identity, err := managerOpenStorageDestination(connection, path, sid, false) |
| 77 | if err != nil { |
| 78 | t.Error(err) |
| 79 | return |
| 80 | } |
| 81 | defer windows.CloseHandle(handle) |
| 82 | volume, id, err := managerDiskIdentity(path) |
| 83 | if err != nil || identity.Volume != volume || identity.FileID != id { |
| 84 | t.Error("destination object identity changed", err) |
| 85 | } |
| 86 | if err := os.Rename(path, path+".replaced"); err == nil { |
| 87 | t.Error("verified disk was replaceable during privileged operation") |
| 88 | } |
| 89 | if _, _, err := managerOpenStorageDestination(connection, path, "S-1-5-21-1-2-3-999", false); err == nil { |
| 90 | t.Error("different user gained destination authority") |
| 91 | } |
| 92 | }) |
| 93 | storagePipeFixture(t, winio.PipeImpLevelIdentification, func(connection net.Conn, sid string) { |
| 94 | if handle, _, err := managerOpenStorageDestination(connection, path, sid, false); err == nil { |
| 95 | windows.CloseHandle(handle) |
| 96 | t.Error("identity-only pipe was used for a privileged destination open") |
| 97 | } |
| 98 | }) |
| 99 | user, err := windows.GetCurrentProcessToken().GetTokenUser() |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | sd, err := windows.SecurityDescriptorFromString("D:P(D;;FW;;;" + user.User.Sid.String() + ")(A;;FR;;;" + user.User.Sid.String() + ")(A;;FA;;;SY)(A;;FA;;;BA)") |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | acl, _, err := sd.DACL() |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | if err := windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION|windows.PROTECTED_DACL_SECURITY_INFORMATION, nil, nil, acl, nil); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | t.Cleanup(func() { _ = protectWindowsProfile(path) }) |
| 115 | storagePipeFixture(t, winio.PipeImpLevelImpersonation, func(connection net.Conn, sid string) { |
| 116 | if handle, _, err := managerOpenStorageDestination(connection, path, sid, false); err == nil { |
| 117 | windows.CloseHandle(handle) |
| 118 | t.Error("read-only caller selected a disk for privileged mutation") |
| 119 | } |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | func TestWindowsStoragePinsDirectoryIdentity(t *testing.T) { |
| 124 | root := t.TempDir() |
| 125 | parent := filepath.Join(root, "parent") |
| 126 | if err := os.Mkdir(parent, 0700); err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | path := filepath.Join(parent, "system.vhdx") |
| 130 | if err := os.WriteFile(path, []byte("disk"), 0600); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | closeParents, err := managerPinStorageParents(path) |
| 134 | if err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | defer closeParents() |
| 138 | if err := os.Rename(parent, parent+"-renamed"); err == nil { |
| 139 | t.Fatal("ancestor could be replaced while VM storage paths were pinned") |
| 140 | } |
| 141 | closeParents() |
| 142 | if err := os.Rename(parent, parent+"-renamed"); err != nil { |
| 143 | t.Fatal("parent pin leaked after operation", err) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestWindowsStorageCopiedPairingKeepsProtectedACL(t *testing.T) { |
| 148 | profile, runtime, _, _ := windowsStorageFixture(t, false) |
| 149 | before, err := cage.GetStorageLocation(profile) |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | if _, err := cage.MoveStorageWithRuntime(context.Background(), profile, filepath.Join(t.TempDir(), "new"), before, runtime, nil); err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | binding, err := LoadWindowsProfile(profile) |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | for _, path := range []string{binding.PairingFile, binding.NetworkPairingFile} { |
| 161 | sd, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION) |
| 162 | if err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | control, _, err := sd.Control() |
| 166 | if err != nil || control&windows.SE_DACL_PROTECTED == 0 { |
| 167 | t.Fatal("copied pairing is rejected by production PrivateFile", path, err) |
| 168 | } |
| 169 | if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { |
| 170 | t.Fatal("pairing disappeared after migration") |
| 171 | } |
| 172 | } |
| 173 | } |