-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathsecurity_readonly_volume_remount_integration_test.go
More file actions
114 lines (100 loc) · 3.6 KB
/
Copy pathsecurity_readonly_volume_remount_integration_test.go
File metadata and controls
114 lines (100 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//go:build boxlite_dev
// Regression test for GHSA-g6ww-w5j2-r7x3 (read-only volume remount bypass).
//
// A host directory mounted via WithBindMountReadOnly must stay read-only even
// against a malicious guest that runs `mount -o remount,rw`. Before v0.9.0
// the guest could remount the virtiofs share read-write (it had
// CAP_SYS_ADMIN) and write through to the host.
//
// Go-SDK counterpart of:
// - sdks/python/tests/test_readonly_volume_remount.py
// - sdks/node/tests/security-readonly-volume-remount.integration.test.ts
// - src/boxlite/tests/security_enforcement.rs::readonly_volume_blocks_remount
package boxlite
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
const (
roGuestMount = "/mnt/sensitive"
roOriginal = "original content\n"
roAttackPayload = "modified content"
)
func TestSecurityReadonlyVolumeRemountBypass(t *testing.T) {
hostDir, err := os.MkdirTemp("/tmp", "virtiofs-ro-poc-")
if err != nil {
t.Fatalf("MkdirTemp: %v", err)
}
t.Cleanup(func() { _ = os.RemoveAll(hostDir) })
roFile := filepath.Join(hostDir, "read_only.txt")
if err := os.WriteFile(roFile, []byte(roOriginal), 0o644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
rt := newTestRuntime(t)
box := createStartedBoxOrSkip(t, rt, "alpine:latest",
WithAutoRemove(false),
WithBindMountReadOnly(hostDir, roGuestMount),
)
ctx := context.Background()
// The share must be exposed read-only to the guest.
mounts, err := box.Exec(ctx, "sh", "-c", "cat /proc/mounts | grep sensitive")
if err != nil {
t.Fatalf("Exec(mounts): %v", err)
}
if !strings.Contains(mounts.Stdout, " ro,") {
t.Fatalf("volume not mounted read-only: %q", mounts.Stdout)
}
// Direct write is rejected (client-side MS_RDONLY active).
write1, err := box.Exec(ctx, "sh", "-c",
"echo '"+roAttackPayload+"' > "+roGuestMount+"/read_only.txt 2>&1")
if err != nil {
t.Fatalf("Exec(write1): %v", err)
}
if write1.ExitCode == 0 {
t.Fatalf("initial write to read-only volume should fail; got exit=0 stdout=%q", write1.Stdout)
}
// ATTACK: try to remount the share read-write.
if _, err := box.Exec(ctx, "sh", "-c",
"mount -o remount,rw "+roGuestMount+" 2>&1"); err != nil {
// Exec itself may report the underlying mount failure as an
// error or a non-zero exit; either way we proceed to verify
// the mount state and the host file.
t.Logf("remount Exec returned err (expected/ok): %v", err)
}
// The mount must still be read-only after the remount attempt.
after, err := box.Exec(ctx, "sh", "-c", "cat /proc/mounts | grep sensitive")
if err != nil {
t.Fatalf("Exec(mounts after): %v", err)
}
if !strings.Contains(after.Stdout, " ro,") || strings.Contains(after.Stdout, " rw,") {
t.Fatalf("volume became writable after remount: %q", after.Stdout)
}
// A post-attack write must still fail.
write2, err := box.Exec(ctx, "sh", "-c",
"echo '"+roAttackPayload+"' > "+roGuestMount+"/read_only.txt 2>&1")
if err != nil {
t.Fatalf("Exec(write2): %v", err)
}
if write2.ExitCode == 0 {
t.Fatalf("write after remount bypass should still fail; got exit=0")
}
// Guest-visible content unchanged.
guestView, err := box.Exec(ctx, "cat", roGuestMount+"/read_only.txt")
if err != nil {
t.Fatalf("Exec(cat): %v", err)
}
if guestView.Stdout != roOriginal {
t.Fatalf("guest modified the file: %q", guestView.Stdout)
}
// HOST VERIFICATION — the advisory's exploit oracle.
hostBytes, err := os.ReadFile(roFile)
if err != nil {
t.Fatalf("ReadFile(host): %v", err)
}
if string(hostBytes) != roOriginal {
t.Fatalf("GHSA-g6ww-w5j2-r7x3 regression: host file modified from inside the sandbox: got %q", string(hostBytes))
}
}