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