Source file src/crypto/internal/fips140test/check_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     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  	testenv.MustHaveExec(t)
    48  	cryptotest.MustSupportFIPS140(t)
    49  
    50  	bin, err := os.ReadFile(os.Args[0])
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	// Replace the expected module checksum with a different value.
    56  	bin = bytes.ReplaceAll(bin, Linkinfo.Sum[:], bytes.Repeat([]byte("X"), len(Linkinfo.Sum)))
    57  
    58  	binPath := filepath.Join(t.TempDir(), "fips140test.exe")
    59  	if err := os.WriteFile(binPath, bin, 0o755); err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	if runtime.GOOS == "darwin" {
    64  		// Regenerate the macOS ad-hoc code signature.
    65  		cmd := testenv.Command(t, "codesign", "-s", "-", "-f", binPath)
    66  		out, err := cmd.CombinedOutput()
    67  		if err != nil {
    68  			t.Fatalf("codesign failed: %v\n%s", err, out)
    69  		}
    70  	}
    71  
    72  	t.Logf("running modified binary...")
    73  	cmd := testenv.Command(t, binPath, "-test.v", "-test.run=^TestIntegrityCheck$")
    74  	cmd.Env = append(cmd.Environ(), "GODEBUG=fips140=on")
    75  	out, err := cmd.CombinedOutput()
    76  	t.Logf("%s", out)
    77  	if err == nil {
    78  		t.Errorf("modified binary did not fail as expected")
    79  	}
    80  	if !bytes.Contains(out, []byte("fips140: verification mismatch")) {
    81  		t.Errorf("modified binary did not fail with expected message")
    82  	}
    83  	if bytes.Contains(out, []byte("verified")) {
    84  		t.Errorf("modified binary did not exit")
    85  	}
    86  }
    87  
    88  func TestIntegrityCheckInfo(t *testing.T) {
    89  	cryptotest.MustSupportFIPS140(t)
    90  
    91  	// Check that the checktest symbols are initialized properly.
    92  	if checktest.NOPTRDATA != 1 {
    93  		t.Errorf("checktest.NOPTRDATA = %d, want 1", checktest.NOPTRDATA)
    94  	}
    95  	if checktest.RODATA != 2 {
    96  		t.Errorf("checktest.RODATA = %d, want 2", checktest.RODATA)
    97  	}
    98  	if checktest.DATA.P != &checktest.NOPTRDATA {
    99  		t.Errorf("checktest.DATA.P = %p, want &checktest.NOPTRDATA (%p)", checktest.DATA.P, &checktest.NOPTRDATA)
   100  	}
   101  	if checktest.DATA.X != 3 {
   102  		t.Errorf("checktest.DATA.X = %d, want 3", checktest.DATA.X)
   103  	}
   104  	if checktest.NOPTRBSS != 0 {
   105  		t.Errorf("checktest.NOPTRBSS = %d, want 0", checktest.NOPTRBSS)
   106  	}
   107  	if checktest.BSS != nil {
   108  		t.Errorf("checktest.BSS = %p, want nil", checktest.BSS)
   109  	}
   110  	if p := checktest.PtrStaticData(); p != nil && *p != 10 {
   111  		t.Errorf("*checktest.PtrStaticData() = %d, want 10", *p)
   112  	}
   113  
   114  	// Check that the checktest symbols are in the right go:fipsinfo sections.
   115  	sect := func(i int, name string, p unsafe.Pointer) {
   116  		s := Linkinfo.Sects[i]
   117  		if !(uintptr(s.Start) <= uintptr(p) && uintptr(p) < uintptr(s.End)) {
   118  			t.Errorf("checktest.%s (%#x) not in section #%d (%#x..%#x)", name, p, i, s.Start, s.End)
   119  		}
   120  	}
   121  	sect(0, "TEXT", unsafe.Pointer(abi.FuncPCABIInternal(checktest.TEXT)))
   122  	if p := checktest.PtrStaticText(); p != nil {
   123  		sect(0, "StaticText", p)
   124  	}
   125  	sect(1, "RODATA", unsafe.Pointer(&checktest.RODATA))
   126  	sect(2, "NOPTRDATA", unsafe.Pointer(&checktest.NOPTRDATA))
   127  	if p := checktest.PtrStaticData(); p != nil {
   128  		sect(2, "StaticData", unsafe.Pointer(p))
   129  	}
   130  	sect(3, "DATA", unsafe.Pointer(&checktest.DATA))
   131  
   132  	// Check that some symbols are not in FIPS sections.
   133  	no := func(name string, p unsafe.Pointer, ix ...int) {
   134  		for _, i := range ix {
   135  			s := Linkinfo.Sects[i]
   136  			if uintptr(s.Start) <= uintptr(p) && uintptr(p) < uintptr(s.End) {
   137  				t.Errorf("%s (%#x) unexpectedly in section #%d (%#x..%#x)", name, p, i, s.Start, s.End)
   138  			}
   139  		}
   140  	}
   141  
   142  	// Check that the symbols are not in unexpected sections (that is, no overlaps).
   143  	no("checktest.TEXT", unsafe.Pointer(abi.FuncPCABIInternal(checktest.TEXT)), 1, 2, 3)
   144  	no("checktest.RODATA", unsafe.Pointer(&checktest.RODATA), 0, 2, 3)
   145  	no("checktest.NOPTRDATA", unsafe.Pointer(&checktest.NOPTRDATA), 0, 1, 3)
   146  	no("checktest.DATA", unsafe.Pointer(&checktest.DATA), 0, 1, 2)
   147  
   148  	// Check that non-FIPS symbols are not in any of the sections.
   149  	no("fmt.Printf", unsafe.Pointer(abi.FuncPCABIInternal(fmt.Printf)), 0, 1, 2, 3)     // TEXT
   150  	no("unicode.Categories", unsafe.Pointer(&unicode.Categories), 0, 1, 2, 3)           // BSS
   151  	no("unicode.ASCII_Hex_Digit", unsafe.Pointer(&unicode.ASCII_Hex_Digit), 0, 1, 2, 3) // DATA
   152  
   153  	// Check that we have enough data in total.
   154  	// On arm64 the fips sections in this test currently total 23 kB.
   155  	n := uintptr(0)
   156  	for _, s := range Linkinfo.Sects {
   157  		n += uintptr(s.End) - uintptr(s.Start)
   158  	}
   159  	if n < 16*1024 {
   160  		t.Fatalf("fips sections not big enough: %d, want at least 16 kB", n)
   161  	}
   162  }
   163  

View as plain text