carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package claimactivation
2
3import (
4 "encoding/binary"
5 "net/http"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "testing"
10
11 "github.com/carlosframework/platform/internal/release"
12)
13
14// realConsole builds a real stripped linux-arm64 program at
15// github.com/carlosframework/platform/cmd/console with an
16// internal/version.Build variable stamped tag, the shape a release console
17// has.
18func realConsole(t *testing.T, tag string) []byte {
19 t.Helper()
20 dir := t.TempDir()
21 for name, body := range map[string]string{
22 "go.mod": "module github.com/carlosframework/platform\n\ngo 1.21\n",
23 "internal/version/version.go": "package version\n\nvar Build = \"dev\"\n",
24 "cmd/console/main.go": "package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/carlosframework/platform/internal/version\"\n)\n\nfunc main() { fmt.Println(version.Build) }\n",
25 } {
26 if err := os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0o755); err != nil {
27 t.Fatal(err)
28 }
29 if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil {
30 t.Fatal(err)
31 }
32 }
33 out := filepath.Join(dir, "console")
34 cmd := exec.Command("go", "build", "-ldflags", "-s -w -X "+release.BuildStampFlag+"="+tag, "-o", out, "./cmd/console")
35 cmd.Dir = dir
36 cmd.Env = append(os.Environ(), "GOOS=linux", "GOARCH=arm64", "CGO_ENABLED=0", "GOFLAGS=-buildvcs=false", "GOWORK=off", "GOPROXY=off")
37 if output, err := cmd.CombinedOutput(); err != nil {
38 t.Fatal(string(output), err)
39 }
40 b, err := os.ReadFile(out)
41 if err != nil {
42 t.Fatal(err)
43 }
44 return b
45}
46
47// F1: publish-identity reads a console's build info only after the bounded
48// ELF pre-check, with the production reader (no test seam). A real stripped
49// console publishes; a crafted one, its section-name table flagged
50// compressed or its section count oversized, is refused with no write.
51func TestPublishIdentityBoundsTheConsoleBinary(t *testing.T) {
52 if testing.Short() {
53 t.Skip("builds Go binaries")
54 }
55 console := realConsole(t, "v0.28.2")
56 le := binary.LittleEndian
57 mutate := func(change func(c []byte)) []byte {
58 c := append([]byte(nil), console...)
59 change(c)
60 return c
61 }
62 shoff := le.Uint64(console[40:])
63 shstrndx := uint64(le.Uint16(console[62:]))
64 for name, tc := range map[string]struct {
65 body []byte
66 ok bool
67 }{
68 "real stripped console": {console, true},
69 "compressed section-name table": {mutate(func(c []byte) {
70 h := c[shoff+shstrndx*64:]
71 le.PutUint64(h[8:], le.Uint64(h[8:])|0x800)
72 }), false},
73 "oversized shnum": {mutate(func(c []byte) { le.PutUint16(c[60:], 65000) }), false},
74 // Astra's re-review: every section named by one 32 MiB string, which
75 // debug/elf would copy once per section.
76 "repeated 32 MiB name": {repeatedName(console, 32<<20), false},
77 "console stamped another tag": {realConsole(t, "v0.28.1"), false},
78 } {
79 t.Run(name, func(t *testing.T) {
80 // The crafted inputs are the pre-check's to refuse; the wrong tag
81 // passes it and is refused on its stamp.
82 if bounded := release.CheckELFBounds(tc.body) == nil; bounded != (tc.ok || name == "console stamped another tag") {
83 t.Fatal("pre-check verdict", bounded)
84 }
85 w := newArtifactsWorld(t)
86 w.r.ConsoleBuild = nil
87 w.ship(t, "eee5555", tc.body, ConsoleProgram)
88 if err := w.r.Signer.Promote(t.Context(), w.store, "bab", "console", "canary/console", "eee5555"); err != nil {
89 t.Fatal(err)
90 }
91 p := w.identityPlan(t, "eee5555")
92 mark := w.mark()
93 result, err := w.r.Run(t.Context(), p, false)
94 if tc.ok {
95 if err != nil || len(result.Output) == 0 {
96 t.Fatal("real console refused", err)
97 }
98 return
99 }
100 if err == nil {
101 t.Fatal("crafted console accepted")
102 }
103 for _, r := range w.requests()[mark:] {
104 if r.Method != http.MethodGet && r.Method != http.MethodHead {
105 t.Fatalf("refusal wrote: %+v", r)
106 }
107 }
108 })
109 }
110}
111
112// repeatedName appends a name table holding one name of size bytes to a
113// real build and points the section-name table, and every section's name,
114// at it.
115func repeatedName(b []byte, size int) []byte {
116 c := append(append([]byte(nil), b...), make([]byte, size+1)...)
117 for i := len(b); i < len(b)+size; i++ {
118 c[i] = 'a'
119 }
120 le := binary.LittleEndian
121 shoff, shnum, idx := le.Uint64(c[40:]), uint64(le.Uint16(c[60:])), uint64(le.Uint16(c[62:]))
122 h := c[shoff+idx*64:]
123 le.PutUint64(h[24:], uint64(len(b)))
124 le.PutUint64(h[32:], uint64(size+1))
125 for i := uint64(0); i < shnum; i++ {
126 le.PutUint32(c[shoff+i*64:], 0)
127 }
128 return c
129}
130