1
2
3
4
5 package fipstest
6
7 import (
8 "bytes"
9 "crypto/internal/cryptotest"
10 . "crypto/internal/fips140/check"
11 "crypto/internal/fips140/check/checktest"
12 "fmt"
13 "internal/abi"
14 "internal/godebug"
15 "internal/testenv"
16 "os"
17 "path/filepath"
18 "runtime"
19 "testing"
20 "unicode"
21 "unsafe"
22 )
23
24 func TestIntegrityCheck(t *testing.T) {
25 if Verified {
26 t.Logf("verified")
27 return
28 }
29
30 if godebug.New("fips140").Value() == "on" {
31 t.Fatalf("GODEBUG=fips140=on but verification did not run")
32 }
33
34 cryptotest.MustSupportFIPS140(t)
35
36 cmd := testenv.Command(t, testenv.Executable(t), "-test.v", "-test.run=^TestIntegrityCheck$")
37 cmd.Env = append(cmd.Environ(), "GODEBUG=fips140=on")
38 out, err := cmd.CombinedOutput()
39 if err != nil {
40 t.Fatalf("GODEBUG=fips140=on %v failed: %v\n%s", cmd.Args, err, out)
41 }
42 t.Logf("exec'ed GODEBUG=fips140=on and succeeded:\n%s", out)
43 }
44
45 func TestIntegrityCheckFailure(t *testing.T) {
46 moduleStatus(t)
47 cryptotest.MustSupportFIPS140(t)
48
49 bin, err := os.ReadFile(os.Args[0])
50 if err != nil {
51 t.Fatal(err)
52 }
53
54
55 bin = bytes.ReplaceAll(bin, Linkinfo.Sum[:], bytes.Repeat([]byte("X"), len(Linkinfo.Sum)))
56
57 binPath := filepath.Join(t.TempDir(), "fips140test.exe")
58 if err := os.WriteFile(binPath, bin, 0o755); err != nil {
59 t.Fatal(err)
60 }
61
62 if runtime.GOOS == "darwin" {
63
64 cmd := testenv.Command(t, "codesign", "-s", "-", "-f", binPath)
65 out, err := cmd.CombinedOutput()
66 if err != nil {
67 t.Fatalf("codesign failed: %v\n%s", err, out)
68 }
69 }
70
71 t.Logf("running modified binary...")
72 cmd := testenv.Command(t, binPath, "-test.v", "-test.run=^TestIntegrityCheck$")
73 cmd.Env = append(cmd.Environ(), "GODEBUG=fips140=on")
74 out, err := cmd.CombinedOutput()
75 t.Logf("running with GODEBUG=fips140=on:\n%s", out)
76 if err == nil {
77 t.Errorf("modified binary did not fail as expected")
78 }
79 if !bytes.Contains(out, []byte("fips140: verification mismatch")) {
80 t.Errorf("modified binary did not fail with expected message")
81 }
82 if bytes.Contains(out, []byte("verified")) {
83 t.Errorf("modified binary did not exit")
84 }
85 }
86
87 func TestIntegrityCheckInfo(t *testing.T) {
88 cryptotest.MustSupportFIPS140(t)
89
90
91 if checktest.NOPTRDATA != 1 {
92 t.Errorf("checktest.NOPTRDATA = %d, want 1", checktest.NOPTRDATA)
93 }
94 if checktest.RODATA != 2 {
95 t.Errorf("checktest.RODATA = %d, want 2", checktest.RODATA)
96 }
97 if checktest.DATA.P != &checktest.NOPTRDATA {
98 t.Errorf("checktest.DATA.P = %p, want &checktest.NOPTRDATA (%p)", checktest.DATA.P, &checktest.NOPTRDATA)
99 }
100 if checktest.DATA.X != 3 {
101 t.Errorf("checktest.DATA.X = %d, want 3", checktest.DATA.X)
102 }
103 if checktest.NOPTRBSS != 0 {
104 t.Errorf("checktest.NOPTRBSS = %d, want 0", checktest.NOPTRBSS)
105 }
106 if checktest.BSS != nil {
107 t.Errorf("checktest.BSS = %p, want nil", checktest.BSS)
108 }
109 if p := checktest.PtrStaticData(); p != nil && *p != 10 {
110 t.Errorf("*checktest.PtrStaticData() = %d, want 10", *p)
111 }
112
113
114 sect := func(i int, name string, p unsafe.Pointer) {
115 s := Linkinfo.Sects[i]
116 if !(uintptr(s.Start) <= uintptr(p) && uintptr(p) < uintptr(s.End)) {
117 t.Errorf("checktest.%s (%#x) not in section #%d (%#x..%#x)", name, p, i, s.Start, s.End)
118 }
119 }
120 sect(0, "TEXT", unsafe.Pointer(abi.FuncPCABIInternal(checktest.TEXT)))
121 if p := checktest.PtrStaticText(); p != nil {
122 sect(0, "StaticText", p)
123 }
124 sect(1, "RODATA", unsafe.Pointer(&checktest.RODATA))
125 sect(2, "NOPTRDATA", unsafe.Pointer(&checktest.NOPTRDATA))
126 if p := checktest.PtrStaticData(); p != nil {
127 sect(2, "StaticData", unsafe.Pointer(p))
128 }
129 sect(3, "DATA", unsafe.Pointer(&checktest.DATA))
130
131
132 no := func(name string, p unsafe.Pointer, ix ...int) {
133 for _, i := range ix {
134 s := Linkinfo.Sects[i]
135 if uintptr(s.Start) <= uintptr(p) && uintptr(p) < uintptr(s.End) {
136 t.Errorf("%s (%#x) unexpectedly in section #%d (%#x..%#x)", name, p, i, s.Start, s.End)
137 }
138 }
139 }
140
141
142 no("checktest.TEXT", unsafe.Pointer(abi.FuncPCABIInternal(checktest.TEXT)), 1, 2, 3)
143 no("checktest.RODATA", unsafe.Pointer(&checktest.RODATA), 0, 2, 3)
144 no("checktest.NOPTRDATA", unsafe.Pointer(&checktest.NOPTRDATA), 0, 1, 3)
145 no("checktest.DATA", unsafe.Pointer(&checktest.DATA), 0, 1, 2)
146
147
148 no("fmt.Printf", unsafe.Pointer(abi.FuncPCABIInternal(fmt.Printf)), 0, 1, 2, 3)
149 no("unicode.Categories", unsafe.Pointer(&unicode.Categories), 0, 1, 2, 3)
150 no("unicode.ASCII_Hex_Digit", unsafe.Pointer(&unicode.ASCII_Hex_Digit), 0, 1, 2, 3)
151
152
153
154 n := uintptr(0)
155 for _, s := range Linkinfo.Sects {
156 n += uintptr(s.End) - uintptr(s.Start)
157 }
158 if n < 16*1024 {
159 t.Fatalf("fips sections not big enough: %d, want at least 16 kB", n)
160 }
161 }
162
View as plain text