1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
package snapdtool_test
import (
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/snapdtool"
"github.com/snapcore/snapd/testutil"
)
type fipsSuite struct {
testutil.BaseTest
logbuf *bytes.Buffer
}
var _ = Suite(&fipsSuite{})
func (s *fipsSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
os.Setenv("SNAPD_DEBUG", "1")
s.AddCleanup(func() { os.Unsetenv("SNAPD_DEBUG") })
buf, restore := logger.MockLogger()
s.AddCleanup(restore)
s.logbuf = buf
s.AddCleanup(release.MockReleaseInfo(&release.OS{ID: "ubuntu"}))
dirs.SetRootDir(c.MkDir())
s.AddCleanup(func() { dirs.SetRootDir("") })
s.AddCleanup(snapdtool.MockSyscallExec(func(argv0 string, argv []string, envv []string) (err error) {
c.Fatal("exec not mocked")
return fmt.Errorf("exec not mocked")
}))
}
func (s *fipsSuite) TearDownTest(c *C) {
c.Logf("logs:\n%s", s.logbuf.String())
s.BaseTest.TearDownTest(c)
}
func mockFipsEnabledWithContent(c *C, root, content string) {
f := filepath.Join(root, "/proc/sys/crypto/fips_enabled")
err := os.MkdirAll(filepath.Dir(f), 0755)
c.Assert(err, IsNil)
err = os.WriteFile(f, []byte(content), 0444)
c.Assert(err, IsNil)
}
type fipsConf struct {
fipsEnabledPresent bool
fipsEnabledYes bool
moduleAvaialble bool
onCore bool
}
func (s *fipsSuite) mockFIPSState(c *C, conf fipsConf) (selfExe string) {
if conf.fipsEnabledPresent {
content := "0\n"
if conf.fipsEnabledYes {
content = "1\n"
}
mockFipsEnabledWithContent(c, dirs.GlobalRootDir, content)
}
mockSelfExe := filepath.Join(dirs.SnapMountDir, "snapd/123/usr/lib/snapd/snapd")
if conf.onCore {
mockSelfExe = filepath.Join(dirs.DistroLibExecDir, "snapd")
}
restore := snapdtool.MockOsReadlink(func(p string) (string, error) {
switch {
case p == "/snap/snapd/current":
return "123", nil
case strings.HasSuffix(p, "/proc/self/exe"):
return mockSelfExe, nil
}
return "", fmt.Errorf("unexpected path %q", p)
})
s.AddCleanup(restore)
if conf.moduleAvaialble {
// even on Core modules are still part of the snapd snap
snaptest.PopulateDir(filepath.Join(dirs.SnapMountDir, "snapd/123"), [][]string{
{"usr/lib/x86_64-linux-gnu/libcrypto.so.3", ""},
{"usr/lib/x86_64-linux-gnu/ossl-modules-3/fips.so", ""},
})
}
return mockSelfExe
}
func (s *fipsSuite) TestMaybeSetupFIPSFullWithReexecClassic(c *C) {
// everything is set up correctly
mockSelfExe := s.mockFIPSState(c, fipsConf{
fipsEnabledPresent: true,
fipsEnabledYes: true,
moduleAvaialble: true,
onCore: false,
})
osArgs := os.Args
s.AddCleanup(func() { os.Args = osArgs })
os.Args = []string{"--arg"}
var observedEnv []string
var observedArgv []string
var observedArg0 string
restore := snapdtool.MockSyscallExec(func(argv0 string, argv []string, envv []string) (err error) {
observedArg0 = argv0
observedArgv = argv
observedEnv = envv
return fmt.Errorf("exec in tests on classic")
})
s.AddCleanup(restore)
c.Check(snapdtool.MaybeSetupFIPS, PanicMatches, "exec in tests on classic")
c.Check(observedArg0, Equals, mockSelfExe)
c.Check(observedArgv, DeepEquals, []string{"--arg"})
// FIPS mode is required
c.Check(observedEnv, testutil.Contains, "GOFIPS=1")
// module was found, and relevant env was added
c.Check(observedEnv, testutil.Contains,
"OPENSSL_MODULES="+filepath.Join(dirs.SnapMountDir, "snapd/123/usr/lib/x86_64-linux-gnu/ossl-modules-3"))
c.Check(observedEnv, testutil.Contains, "GO_OPENSSL_VERSION_OVERRIDE=3")
// bootstrap done
c.Check(observedEnv, testutil.Contains, "SNAPD_FIPS_BOOTSTRAP=1")
}
func (s *fipsSuite) TestMaybeSetupFIPSFullWithReexecCore(c *C) {
// everything is set up correctly
s.AddCleanup(release.MockOnClassic(false))
mockSelfExe := s.mockFIPSState(c, fipsConf{
fipsEnabledPresent: true,
fipsEnabledYes: true,
moduleAvaialble: true,
onCore: true,
})
osArgs := os.Args
s.AddCleanup(func() { os.Args = osArgs })
os.Args = []string{"--arg"}
var observedEnv []string
var observedArgv []string
var observedArg0 string
restore := snapdtool.MockSyscallExec(func(argv0 string, argv []string, envv []string) (err error) {
observedArg0 = argv0
observedArgv = argv
observedEnv = envv
return fmt.Errorf("exec in tests on core")
})
s.AddCleanup(restore)
c.Check(snapdtool.MaybeSetupFIPS, PanicMatches, "exec in tests on core")
c.Check(observedArg0, Equals, mockSelfExe)
c.Check(observedArgv, DeepEquals, []string{"--arg"})
// FIPS mode is required
c.Check(observedEnv, testutil.Contains, "GOFIPS=1")
// module was found, and relevant env was added, and still points to the
// snapd snap mount directory
c.Check(observedEnv, testutil.Contains,
"OPENSSL_MODULES="+filepath.Join(dirs.SnapMountDir, "snapd/123/usr/lib/x86_64-linux-gnu/ossl-modules-3"))
c.Check(observedEnv, testutil.Contains, "GO_OPENSSL_VERSION_OVERRIDE=3")
// bootstrap done
c.Check(observedEnv, testutil.Contains, "SNAPD_FIPS_BOOTSTRAP=1")
}
func (s *fipsSuite) TestMaybeSetupFIPSNoModulesButStillReexec(c *C) {
// FIPS is enabled, we do not find the module, but still reexec into
// mandatory FIPS mode to obtain an predictable error from FIPS
// initialization
mockSelfExe := s.mockFIPSState(c, fipsConf{
fipsEnabledPresent: true,
fipsEnabledYes: true,
moduleAvaialble: false,
})
var observedEnv []string
var observedArgv []string
var observedArg0 string
osArgs := os.Args
s.AddCleanup(func() { os.Args = osArgs })
os.Args = []string{"--arg"}
restore := snapdtool.MockSyscallExec(func(argv0 string, argv []string, envv []string) (err error) {
observedArg0 = argv0
observedArgv = argv
observedEnv = envv
return fmt.Errorf("exec in tests")
})
s.AddCleanup(restore)
c.Check(snapdtool.MaybeSetupFIPS, PanicMatches, "exec in tests")
c.Check(observedArg0, Equals, mockSelfExe)
c.Check(observedArgv, DeepEquals, []string{"--arg"})
// FIPS mode is erquired
c.Check(observedEnv, testutil.Contains, "GOFIPS=1")
// module was not found, so paths are not set
for _, env := range observedEnv {
if strings.HasPrefix(env, "OPENSSL_MODULES=") || strings.HasPrefix(env, "GO_OPENSSL_VERSION_OVERRIDE=") {
c.Fatalf("found unexpected env %q", env)
}
}
// bootstrap is done
c.Check(observedEnv, testutil.Contains, "SNAPD_FIPS_BOOTSTRAP=1")
}
func (s *fipsSuite) TestMaybeSetupFIPSBootstrapAlreadyDone(c *C) {
// bootstrap was already completed
s.mockFIPSState(c, fipsConf{
fipsEnabledPresent: true,
fipsEnabledYes: true,
})
defer func() {
os.Unsetenv("GOFIPS")
os.Unsetenv("SNAPD_FIPS_BOOSTRAP")
os.Unsetenv("OPENSSL_MODULES")
os.Unsetenv("GO_OPENSSL_VERSION_OVERRIDE")
}()
os.Setenv("SNAPD_FIPS_BOOTSTRAP", "1")
os.Setenv("GOFIPS", "1")
os.Setenv("OPENSSL_MODULES", "bogus-dir")
os.Setenv("GO_OPENSSL_VERSION_OVERRIDE", "123-xyz")
err := snapdtool.MaybeSetupFIPS()
c.Assert(err, IsNil)
c.Check(os.Getenv("SNAPD_FIPS_BOOTSTRAP"), Equals, "")
c.Check(os.Getenv("GOFIPS"), Equals, "")
c.Check(os.Getenv("OPENSSL_MODULES"), Equals, "")
c.Check(os.Getenv("GO_OPENSSL_VERSION_OVERRIDE"), Equals, "")
}
func (s *fipsSuite) TestMaybeSetupFIPSSnapdNotFromSnapOnClassic(c *C) {
// FIPS is enabled, but snapd is not running from the snapd snap
s.mockFIPSState(c, fipsConf{
fipsEnabledPresent: true,
fipsEnabledYes: true,
})
mockSelfExe := filepath.Join(dirs.DistroLibExecDir, "snapd")
// override mocked os.Readlink()
restore := snapdtool.MockOsReadlink(func(p string) (string, error) {
switch {
case p == "/snap/snapd/current":
return "", fs.ErrNotExist
case strings.HasSuffix(p, "/proc/self/exe"):
return mockSelfExe, nil
}
return "", fmt.Errorf("unexpected path %q", p)
})
s.AddCleanup(restore)
osArgs := os.Args
s.AddCleanup(func() { os.Args = osArgs })
os.Args = []string{"--arg"}
var observedEnv []string
var observedArgv []string
var observedArg0 string
restore = snapdtool.MockSyscallExec(func(argv0 string, argv []string, envv []string) (err error) {
observedArg0 = argv0
observedArgv = argv
observedEnv = envv
return fmt.Errorf("exec in tests")
})
s.AddCleanup(restore)
c.Assert(snapdtool.MaybeSetupFIPS, PanicMatches, "exec in tests")
c.Check(observedArg0, Equals, mockSelfExe)
c.Check(observedArgv, DeepEquals, []string{"--arg"})
// FIPS mode is erquired
c.Check(observedEnv, testutil.Contains, "GOFIPS=1")
// since we're not reexecuting from snapd snap, no additional env for openssl modules is set
for _, env := range observedEnv {
if strings.HasPrefix(env, "OPENSSL_MODULES=") || strings.HasPrefix(env, "GO_OPENSSL_VERSION_OVERRIDE=") {
c.Fatalf("found unexpected env %q", env)
}
}
// bootstrap is done
c.Check(observedEnv, testutil.Contains, "SNAPD_FIPS_BOOTSTRAP=1")
}
func (s *fipsSuite) TestMaybeSetupFIPSSnapdNotFromSnapFIPSNotEnabled(c *C) {
// FIPS is not enabled, snapd is not running from the snapd snap
s.mockFIPSState(c, fipsConf{
fipsEnabledPresent: false,
fipsEnabledYes: false,
})
mockSelfExe := filepath.Join(dirs.DistroLibExecDir, "snapd")
// override mocked os.Readlink()
restore := snapdtool.MockOsReadlink(func(p string) (string, error) {
switch {
case p == "/snap/snapd/current":
return "", fs.ErrNotExist
case strings.HasSuffix(p, "/proc/self/exe"):
return mockSelfExe, nil
}
return "", fmt.Errorf("unexpected path %q", p)
})
s.AddCleanup(restore)
c.Assert(snapdtool.MaybeSetupFIPS(), IsNil)
}
|