| 1 | //go:build linux |
| 2 | |
| 3 | package console |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func plan9Fix2Boot(t *testing.T) string { |
| 14 | t.Helper() |
| 15 | raw, err := os.ReadFile("/proc/sys/kernel/random/boot_id") |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | return strings.TrimSpace(string(raw)) |
| 20 | } |
| 21 | |
| 22 | func TestPlan9Fix4ContainerProcfsDoesNotProveLegacyPIDAbsent(t *testing.T) { |
| 23 | boot := plan9Fix2Boot(t) |
| 24 | const ns = "4026532999" |
| 25 | priorNamespace := hostReportPIDNamespace |
| 26 | t.Cleanup(func() { hostReportPIDNamespace = priorNamespace }) |
| 27 | hostReportPIDNamespace = func() (string, error) { return ns, nil } |
| 28 | // A container with its own procfs sees itself and its init in the same |
| 29 | // namespace, while a still-live host PID is absent from both procfs and |
| 30 | // kill(0). That agreement is not evidence of the initial namespace. |
| 31 | plan9Fix2RootNamespace(t, ns, nil) |
| 32 | prior := hostReportProcReadFile |
| 33 | t.Cleanup(func() { hostReportProcReadFile = prior }) |
| 34 | hostReportProcReadFile = func(path string) ([]byte, error) { |
| 35 | if path == "/proc/999999999/stat" { |
| 36 | return nil, os.ErrNotExist |
| 37 | } |
| 38 | return prior(path) |
| 39 | } |
| 40 | now := time.Now() |
| 41 | if err := hostReportCollision(plan9Report(boot+":999999999", now), plan9Report(hostReportProcessID(boot), now), now); err == nil { |
| 42 | t.Fatal("container procfs allowed takeover of a live host's legacy report") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func plan9Fix2RootNamespace(t *testing.T, ns string, err error) { |
| 47 | t.Helper() |
| 48 | prior := hostReportRootPIDNamespace |
| 49 | t.Cleanup(func() { hostReportRootPIDNamespace = prior }) |
| 50 | hostReportRootPIDNamespace = func() (string, error) { return ns, err } |
| 51 | } |
| 52 | |
| 53 | func TestPlan9Fix2LegacyTwoPartIDProvedGoneInHostNamespace(t *testing.T) { |
| 54 | boot := plan9Fix2Boot(t) |
| 55 | const ns = hostReportInitialPIDNamespace |
| 56 | priorNamespace := hostReportPIDNamespace |
| 57 | t.Cleanup(func() { hostReportPIDNamespace = priorNamespace }) |
| 58 | hostReportPIDNamespace = func() (string, error) { return ns, nil } |
| 59 | plan9Fix2RootNamespace(t, ns, nil) |
| 60 | if !hostReportProcessGone(boot+":999999999", hostReportProcessID(boot)) { |
| 61 | t.Fatal("main's absent boot:pid was not accepted during the roll") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestPlan9Fix2LegacyTwoPartIDNeverProvedOutsideHostNamespace(t *testing.T) { |
| 66 | boot := plan9Fix2Boot(t) |
| 67 | ns, _ := hostReportPIDNamespace() |
| 68 | next := hostReportProcessID(boot) |
| 69 | plan9Fix2RootNamespace(t, ns+"0", nil) |
| 70 | if hostReportProcessGone(boot+":999999999", next) { |
| 71 | t.Fatal("legacy ID proved gone from a namespace that is not init's") |
| 72 | } |
| 73 | plan9Fix2RootNamespace(t, "", os.ErrPermission) |
| 74 | if hostReportProcessGone(boot+":999999999", next) { |
| 75 | t.Fatal("legacy ID proved gone with an unreadable init namespace") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestPlan9Fix2LegacyTwoPartIDKeepsLivePIDAndBootChecks(t *testing.T) { |
| 80 | boot := plan9Fix2Boot(t) |
| 81 | const ns = hostReportInitialPIDNamespace |
| 82 | priorNamespace := hostReportPIDNamespace |
| 83 | t.Cleanup(func() { hostReportPIDNamespace = priorNamespace }) |
| 84 | hostReportPIDNamespace = func() (string, error) { return ns, nil } |
| 85 | plan9Fix2RootNamespace(t, ns, nil) |
| 86 | next := hostReportProcessID(boot) |
| 87 | if hostReportProcessGone(fmt.Sprintf("%s:%d", boot, os.Getpid()), next) { |
| 88 | t.Fatal("a live legacy PID was proved gone") |
| 89 | } |
| 90 | if hostReportProcessGone("other-boot:999999999", hostReportProcessID("other-boot")) { |
| 91 | t.Fatal("a legacy ID from another boot was proved gone") |
| 92 | } |
| 93 | if hostReportProcessGone(boot+":999999999", hostReportProcessID("other-boot")) { |
| 94 | t.Fatal("boot mismatch between reports was ignored") |
| 95 | } |
| 96 | } |
| 97 | |