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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
snapd_apparmor "github.com/snapcore/snapd/cmd/snapd-apparmor"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/testutil"
)
// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }
type mainSuite struct {
testutil.BaseTest
}
var _ = Suite(&mainSuite{})
func (s *mainSuite) SetUpTest(c *C) {
dirs.SetRootDir(c.MkDir())
s.AddCleanup(mockWSL(0))
}
func (s *mainSuite) TearDownTest(c *C) {
dirs.SetRootDir("/")
}
// Mocks WSL check. Values:
// - 0 to mock not being on WSL.
// - 1 to mock being on WSL 1.
// - 2 to mock being on WSL 2.
func mockWSL(version int) (restore func()) {
restoreOnWSL := testutil.Backup(&release.OnWSL)
restoreWSLVersion := testutil.Backup(&release.WSLVersion)
release.OnWSL = version != 0
release.WSLVersion = version
return func() {
restoreOnWSL()
restoreWSLVersion()
}
}
func (s *mainSuite) TestIsContainerWithInternalPolicy_NotContainer(c *C) {
// since "apparmorfs" is not present within our test root dir setup we
// expect this to return false
restore := mockWSL(0)
defer restore()
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, false)
appArmorSecurityFSPath := filepath.Join(dirs.GlobalRootDir, "/sys/kernel/security/apparmor/")
err := os.MkdirAll(appArmorSecurityFSPath, 0755)
c.Assert(err, IsNil)
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, false)
}
func (s *mainSuite) TestIsContainerWithInternalPolicy_WSL1(c *C) {
restore := mockWSL(1)
defer restore()
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, false)
}
func (s *mainSuite) TestIsContainerWithInternalPolicy_WSL2(c *C) {
restore := mockWSL(2)
defer restore()
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, false)
}
func (s *mainSuite) TestIsContainerWithInternalPolicy_WSL2WithSecurityFS(c *C) {
restore := mockWSL(2)
defer restore()
appArmorSecurityFSPath := filepath.Join(dirs.GlobalRootDir, "/sys/kernel/security/apparmor/")
err := os.MkdirAll(appArmorSecurityFSPath, 0755)
c.Assert(err, IsNil)
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, true)
}
func (s *mainSuite) TestIsContainerWithInternalPolicy_LinuxContainers(c *C) {
restore := mockWSL(0)
defer restore()
appArmorSecurityFSPath := filepath.Join(dirs.GlobalRootDir, "/sys/kernel/security/apparmor/")
err := os.MkdirAll(appArmorSecurityFSPath, 0755)
c.Assert(err, IsNil)
for _, prefix := range []string{"lxc", "lxd", "incus"} {
// simulate being inside a container environment
restore := testutil.MockCommand(c, "systemd-detect-virt", "echo "+prefix)
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, false)
err = os.WriteFile(filepath.Join(appArmorSecurityFSPath, ".ns_stacked"), []byte("yes"), 0644)
c.Assert(err, IsNil)
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, false)
err = os.WriteFile(filepath.Join(appArmorSecurityFSPath, ".ns_name"), nil, 0644)
c.Assert(err, IsNil)
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, false)
err = os.WriteFile(filepath.Join(appArmorSecurityFSPath, ".ns_name"), []byte("foo"), 0644)
c.Assert(err, IsNil)
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, false)
// lxc/lxd name should result in a container with internal policy
err = os.WriteFile(filepath.Join(appArmorSecurityFSPath, ".ns_name"), []byte(prefix+"-foo"), 0644)
c.Assert(err, IsNil)
c.Assert(snapd_apparmor.IsContainerWithInternalPolicy(), Equals, true)
os.Remove(filepath.Join(appArmorSecurityFSPath, ".ns_name"))
os.Remove(filepath.Join(appArmorSecurityFSPath, ".ns_stacked"))
restore.Restore()
}
}
func (s *mainSuite) TestLoadAppArmorProfiles(c *C) {
parserCmd := testutil.MockCommand(c, "apparmor_parser", "")
defer parserCmd.Restore()
restore := snapd_apparmor.MockParserSearchPath(parserCmd.BinDir())
defer restore()
err := snapd_apparmor.LoadAppArmorProfiles()
c.Assert(err, IsNil)
// since no profiles to load the parser should not have been called
c.Assert(parserCmd.Calls(), HasLen, 0)
// mock a profile
err = os.MkdirAll(dirs.SnapAppArmorDir, 0755)
c.Assert(err, IsNil)
profile := filepath.Join(dirs.SnapAppArmorDir, "foo")
err = os.WriteFile(profile, nil, 0644)
c.Assert(err, IsNil)
// pretend that the host apparmor has a 3.0 abi file.
c.Assert(os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/apparmor.d/abi"), 0755), IsNil)
c.Assert(os.WriteFile(filepath.Join(dirs.GlobalRootDir, "/etc/apparmor.d/abi/3.0"), nil, 0644), IsNil)
// ensure SNAPD_DEBUG is set in the environment so then --quiet
// will *not* be included in the apparmor_parser arguments (since
// when these test are run in via CI SNAPD_DEBUG is set)
os.Setenv("SNAPD_DEBUG", "1")
err = snapd_apparmor.LoadAppArmorProfiles()
c.Assert(err, IsNil)
// check arguments to the parser are as expected
c.Assert(parserCmd.Calls(), DeepEquals, [][]string{
{"apparmor_parser", "--policy-features", filepath.Join(dirs.GlobalRootDir, "/etc/apparmor.d/abi/3.0"),
"--replace", "--write-cache",
fmt.Sprintf("--cache-loc=%s/var/cache/apparmor", dirs.GlobalRootDir), profile}})
// test error case
parserCmd = testutil.MockCommand(c, "apparmor_parser", "echo mocked parser failed > /dev/stderr; exit 1")
defer parserCmd.Restore()
restore = snapd_apparmor.MockParserSearchPath(parserCmd.BinDir())
defer restore()
err = snapd_apparmor.LoadAppArmorProfiles()
c.Check(err.Error(), Equals, "cannot load apparmor profiles: exit status 1\napparmor_parser output:\nmocked parser failed\n")
// rename so file is ignored
err = os.Rename(profile, profile+"~")
c.Assert(err, IsNil)
// forget previous calls so we can check below that as a result of
// having no profiles again that no invocation of the parser occurs
parserCmd.ForgetCalls()
err = snapd_apparmor.LoadAppArmorProfiles()
c.Assert(err, IsNil)
c.Assert(parserCmd.Calls(), HasLen, 0)
}
func (s *mainSuite) TestIsContainer(c *C) {
detectCmd := testutil.MockCommand(c, "systemd-detect-virt", "exit 1")
defer detectCmd.Restore()
c.Check(snapd_apparmor.IsContainer(), Equals, false)
c.Assert(detectCmd.Calls(), DeepEquals, [][]string{
{"systemd-detect-virt", "--quiet", "--container"}})
detectCmd = testutil.MockCommand(c, "systemd-detect-virt", "")
c.Check(snapd_apparmor.IsContainer(), Equals, true)
c.Assert(detectCmd.Calls(), DeepEquals, [][]string{
{"systemd-detect-virt", "--quiet", "--container"}})
// test error cases too
detectCmd = testutil.MockCommand(c, "systemd-detect-virt", "echo failed > /dev/stderr; exit 1")
c.Check(snapd_apparmor.IsContainer(), Equals, false)
c.Assert(detectCmd.Calls(), DeepEquals, [][]string{
{"systemd-detect-virt", "--quiet", "--container"}})
// Test WSL2 with custom kernel
// systemd-detect-virt may return a non-zero exit code as it fails to recognize it as WSL
// This will happen when the kernel name includes neither "WSL" not "Microsoft"
detectCmd = testutil.MockCommand(c, "systemd-detect-virt", "echo none; exit 1")
defer mockWSL(2)()
c.Check(snapd_apparmor.IsContainer(), Equals, true)
c.Assert(detectCmd.Calls(), DeepEquals, [][]string(nil))
}
func (s *mainSuite) TestValidateArgs(c *C) {
testCases := []struct {
args []string
errMsg string
}{
{
args: []string{"start"},
errMsg: "",
},
{
args: []string{"foo"},
errMsg: "Expected to be called with a single 'start' argument.",
},
{
args: []string{"start", "foo"},
errMsg: "Expected to be called with a single 'start' argument.",
},
}
for _, tc := range testCases {
err := snapd_apparmor.ValidateArgs(tc.args)
if err != nil {
c.Check(err.Error(), Equals, tc.errMsg)
} else {
c.Check(tc.errMsg, Equals, "")
}
}
}
type integrationSuite struct {
testutil.BaseTest
logBuf *bytes.Buffer
parserCmd *testutil.MockCmd
}
var _ = Suite(&integrationSuite{})
func (s *integrationSuite) SetUpTest(c *C) {
s.AddCleanup(mockWSL(0))
dirs.SetRootDir(c.MkDir())
s.AddCleanup(func() { dirs.SetRootDir("/") })
logBuf, r := logger.MockLogger()
s.AddCleanup(r)
s.logBuf = logBuf
// simulate a single profile to load
s.parserCmd = testutil.MockCommand(c, "apparmor_parser", "")
s.AddCleanup(s.parserCmd.Restore)
restore := snapd_apparmor.MockParserSearchPath(s.parserCmd.BinDir())
s.AddCleanup(restore)
err := os.MkdirAll(dirs.SnapAppArmorDir, 0755)
c.Assert(err, IsNil)
profile := filepath.Join(dirs.SnapAppArmorDir, "foo")
err = os.WriteFile(profile, nil, 0644)
c.Assert(err, IsNil)
os.Args = []string{"snapd-apparmor", "start"}
}
func (s *integrationSuite) TestRunInContainerSkipsLoading(c *C) {
testutil.MockCommand(c, "systemd-detect-virt", "exit 0")
err := snapd_apparmor.Run()
c.Assert(err, IsNil)
c.Check(s.logBuf.String(), testutil.Contains, "DEBUG: inside container environment")
c.Check(s.logBuf.String(), testutil.Contains, "Inside container environment without internal policy")
c.Assert(s.parserCmd.Calls(), HasLen, 0)
}
func (s *integrationSuite) TestRunInContainerWithInternalPolicyLoadsProfiles(c *C) {
defer mockWSL(1)()
err := snapd_apparmor.Run()
c.Assert(err, IsNil)
c.Check(s.logBuf.String(), testutil.Contains, "DEBUG: inside container environment")
c.Check(s.logBuf.String(), testutil.Contains, "Inside container environment without internal policy")
c.Assert(s.parserCmd.Calls(), HasLen, 0)
}
func (s *integrationSuite) TestRunNormalLoadsProfiles(c *C) {
// simulate a normal system (not a container)
testutil.MockCommand(c, "systemd-detect-virt", "exit 1")
detectCmd := testutil.MockCommand(c, "systemd-detect-virt", "exit 1")
defer detectCmd.Restore()
err := snapd_apparmor.Run()
c.Assert(err, IsNil)
c.Assert(s.parserCmd.Calls(), HasLen, 1)
c.Check(s.logBuf.String(), Matches, `(?s).* main.go:[0-9]+: Loading profiles \[.*/var/lib/snapd/apparmor/profiles/foo\].*`)
}
|