package claimactivation import ( "encoding/binary" "net/http" "os" "os/exec" "path/filepath" "testing" "github.com/carlosframework/platform/internal/release" ) // realConsole builds a real stripped linux-arm64 program at // github.com/carlosframework/platform/cmd/console with an // internal/version.Build variable stamped tag, the shape a release console // has. func realConsole(t *testing.T, tag string) []byte { t.Helper() dir := t.TempDir() for name, body := range map[string]string{ "go.mod": "module github.com/carlosframework/platform\n\ngo 1.21\n", "internal/version/version.go": "package version\n\nvar Build = \"dev\"\n", "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", } { if err := os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil { t.Fatal(err) } } out := filepath.Join(dir, "console") cmd := exec.Command("go", "build", "-ldflags", "-s -w -X "+release.BuildStampFlag+"="+tag, "-o", out, "./cmd/console") cmd.Dir = dir cmd.Env = append(os.Environ(), "GOOS=linux", "GOARCH=arm64", "CGO_ENABLED=0", "GOFLAGS=-buildvcs=false", "GOWORK=off", "GOPROXY=off") if output, err := cmd.CombinedOutput(); err != nil { t.Fatal(string(output), err) } b, err := os.ReadFile(out) if err != nil { t.Fatal(err) } return b } // F1: publish-identity reads a console's build info only after the bounded // ELF pre-check, with the production reader (no test seam). A real stripped // console publishes; a crafted one, its section-name table flagged // compressed or its section count oversized, is refused with no write. func TestPublishIdentityBoundsTheConsoleBinary(t *testing.T) { if testing.Short() { t.Skip("builds Go binaries") } console := realConsole(t, "v0.28.2") le := binary.LittleEndian mutate := func(change func(c []byte)) []byte { c := append([]byte(nil), console...) change(c) return c } shoff := le.Uint64(console[40:]) shstrndx := uint64(le.Uint16(console[62:])) for name, tc := range map[string]struct { body []byte ok bool }{ "real stripped console": {console, true}, "compressed section-name table": {mutate(func(c []byte) { h := c[shoff+shstrndx*64:] le.PutUint64(h[8:], le.Uint64(h[8:])|0x800) }), false}, "oversized shnum": {mutate(func(c []byte) { le.PutUint16(c[60:], 65000) }), false}, // Astra's re-review: every section named by one 32 MiB string, which // debug/elf would copy once per section. "repeated 32 MiB name": {repeatedName(console, 32<<20), false}, "console stamped another tag": {realConsole(t, "v0.28.1"), false}, } { t.Run(name, func(t *testing.T) { // The crafted inputs are the pre-check's to refuse; the wrong tag // passes it and is refused on its stamp. if bounded := release.CheckELFBounds(tc.body) == nil; bounded != (tc.ok || name == "console stamped another tag") { t.Fatal("pre-check verdict", bounded) } w := newArtifactsWorld(t) w.r.ConsoleBuild = nil w.ship(t, "eee5555", tc.body, ConsoleProgram) if err := w.r.Signer.Promote(t.Context(), w.store, "bab", "console", "canary/console", "eee5555"); err != nil { t.Fatal(err) } p := w.identityPlan(t, "eee5555") mark := w.mark() result, err := w.r.Run(t.Context(), p, false) if tc.ok { if err != nil || len(result.Output) == 0 { t.Fatal("real console refused", err) } return } if err == nil { t.Fatal("crafted console accepted") } for _, r := range w.requests()[mark:] { if r.Method != http.MethodGet && r.Method != http.MethodHead { t.Fatalf("refusal wrote: %+v", r) } } }) } } // repeatedName appends a name table holding one name of size bytes to a // real build and points the section-name table, and every section's name, // at it. func repeatedName(b []byte, size int) []byte { c := append(append([]byte(nil), b...), make([]byte, size+1)...) for i := len(b); i < len(b)+size; i++ { c[i] = 'a' } le := binary.LittleEndian shoff, shnum, idx := le.Uint64(c[40:]), uint64(le.Uint16(c[60:])), uint64(le.Uint16(c[62:])) h := c[shoff+idx*64:] le.PutUint64(h[24:], uint64(len(b))) le.PutUint64(h[32:], uint64(size+1)) for i := uint64(0); i < shnum; i++ { le.PutUint32(c[shoff+i*64:], 0) } return c }