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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 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 apparmor_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/sandbox/apparmor"
"github.com/snapcore/snapd/testutil"
)
func TestApparmor(t *testing.T) {
TestingT(t)
}
type apparmorSuite struct{}
var _ = Suite(&apparmorSuite{})
func (*apparmorSuite) TestAppArmorLevelTypeStringer(c *C) {
c.Check(apparmor.Unknown.String(), Equals, "unknown")
c.Check(apparmor.Unsupported.String(), Equals, "none")
c.Check(apparmor.Unusable.String(), Equals, "unusable")
c.Check(apparmor.Partial.String(), Equals, "partial")
c.Check(apparmor.Full.String(), Equals, "full")
c.Check(apparmor.LevelType(42).String(), Equals, "AppArmorLevelType:42")
}
func (*apparmorSuite) TestAppArmorSystemCacheFallsback(c *C) {
// if we create the system cache dir under a new rootdir, then the
// SystemCacheDir should take that value
dir1 := c.MkDir()
systemCacheDir := filepath.Join(dir1, "/etc/apparmor.d/cache")
err := os.MkdirAll(systemCacheDir, 0755)
c.Assert(err, IsNil)
dirs.SetRootDir(dir1)
c.Assert(apparmor.SystemCacheDir, Equals, systemCacheDir)
// but if we set a new root dir without the system cache dir, now the var is
// set to the CacheDir
dir2 := c.MkDir()
dirs.SetRootDir(dir2)
c.Assert(apparmor.SystemCacheDir, Equals, apparmor.CacheDir)
// finally test that it's insufficient to just have the conf dir, we need
// specifically the cache dir
dir3 := c.MkDir()
err = os.MkdirAll(filepath.Join(dir3, "/etc/apparmor.d"), 0755)
c.Assert(err, IsNil)
dirs.SetRootDir(dir3)
c.Assert(apparmor.SystemCacheDir, Equals, apparmor.CacheDir)
}
func (*apparmorSuite) TestMockAppArmorLevel(c *C) {
for _, lvl := range []apparmor.LevelType{apparmor.Unsupported, apparmor.Unusable, apparmor.Partial, apparmor.Full} {
restore := apparmor.MockLevel(lvl)
c.Check(apparmor.ProbedLevel(), Equals, lvl)
c.Check(apparmor.Summary(), testutil.Contains, "mocked apparmor level: ")
features, err := apparmor.KernelFeatures()
c.Check(err, IsNil)
c.Check(features, DeepEquals, []string{"mocked-kernel-feature"})
features, err = apparmor.ParserFeatures()
c.Check(err, IsNil)
c.Check(features, DeepEquals, []string{"mocked-parser-feature"})
restore()
}
}
// Using MockAppArmorFeatures yields in apparmor assessment
func (*apparmorSuite) TestMockAppArmorFeatures(c *C) {
// No apparmor in the kernel, apparmor is disabled.
restore := apparmor.MockFeatures([]string{}, os.ErrNotExist, []string{}, nil)
c.Check(apparmor.ProbedLevel(), Equals, apparmor.Unsupported)
c.Check(apparmor.Summary(), Equals, "apparmor not enabled")
features, err := apparmor.KernelFeatures()
c.Assert(err, Equals, os.ErrNotExist)
c.Check(features, DeepEquals, []string{})
features, err = apparmor.ParserFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{})
restore()
// No apparmor_parser, apparmor is disabled.
restore = apparmor.MockFeatures([]string{}, nil, []string{}, os.ErrNotExist)
c.Check(apparmor.ProbedLevel(), Equals, apparmor.Unsupported)
c.Check(apparmor.Summary(), Equals, "apparmor_parser not found")
features, err = apparmor.KernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{})
features, err = apparmor.ParserFeatures()
c.Assert(err, Equals, os.ErrNotExist)
c.Check(features, DeepEquals, []string{})
restore()
// Complete kernel features but apparmor is unusable because of missing required parser features.
restore = apparmor.MockFeatures(apparmor.RequiredKernelFeatures, nil, []string{}, nil)
c.Check(apparmor.ProbedLevel(), Equals, apparmor.Unusable)
c.Check(apparmor.Summary(), Equals, "apparmor_parser is available but required parser features are missing: unsafe")
features, err = apparmor.KernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, apparmor.RequiredKernelFeatures)
features, err = apparmor.ParserFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{})
restore()
// Complete parser features but apparmor is unusable because of missing required kernel features.
// The dummy feature is there to pretend that apparmor in the kernel is not entirely disabled.
restore = apparmor.MockFeatures([]string{"dummy-feature"}, nil, apparmor.RequiredParserFeatures, nil)
c.Check(apparmor.ProbedLevel(), Equals, apparmor.Unusable)
c.Check(apparmor.Summary(), Equals, "apparmor is enabled but required kernel features are missing: file")
features, err = apparmor.KernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{"dummy-feature"})
features, err = apparmor.ParserFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, apparmor.RequiredParserFeatures)
restore()
// Required kernel and parser features available, some optional features are missing though.
restore = apparmor.MockFeatures(apparmor.RequiredKernelFeatures, nil, apparmor.RequiredParserFeatures, nil)
c.Check(apparmor.ProbedLevel(), Equals, apparmor.Partial)
c.Check(apparmor.Summary(), Equals, "apparmor is enabled but some kernel features are missing: caps, dbus, domain, mount, namespaces, network, ptrace, signal")
features, err = apparmor.KernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, apparmor.RequiredKernelFeatures)
features, err = apparmor.ParserFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, apparmor.RequiredParserFeatures)
restore()
// Preferred kernel and parser features available.
restore = apparmor.MockFeatures(apparmor.PreferredKernelFeatures, nil, apparmor.PreferredParserFeatures, nil)
c.Check(apparmor.ProbedLevel(), Equals, apparmor.Full)
c.Check(apparmor.Summary(), Equals, "apparmor is enabled and all features are available")
features, err = apparmor.KernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, apparmor.PreferredKernelFeatures)
features, err = apparmor.ParserFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, apparmor.PreferredParserFeatures)
restore()
}
const featuresSysPath = "sys/kernel/security/apparmor/features"
func (s *apparmorSuite) TestProbeAppArmorKernelFeatures(c *C) {
d := c.MkDir()
// Pretend that apparmor kernel features directory doesn't exist.
restore := apparmor.MockFsRootPath(d)
defer restore()
features, err := apparmor.ProbeKernelFeatures()
c.Assert(os.IsNotExist(err), Equals, true)
c.Check(features, DeepEquals, []string{})
// Pretend that apparmor kernel features directory exists but is empty.
c.Assert(os.MkdirAll(filepath.Join(d, featuresSysPath), 0755), IsNil)
features, err = apparmor.ProbeKernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{})
// Pretend that apparmor kernel features directory contains some entries.
c.Assert(os.Mkdir(filepath.Join(d, featuresSysPath, "foo"), 0755), IsNil)
c.Assert(os.Mkdir(filepath.Join(d, featuresSysPath, "bar"), 0755), IsNil)
features, err = apparmor.ProbeKernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{"bar", "foo"})
}
func (s *apparmorSuite) TestProbeAppArmorParserFeatures(c *C) {
d := c.MkDir()
var testcases = []struct {
exit string
features []string
}{
{"exit 1", []string{}},
{"exit 0", []string{"unsafe"}},
}
for _, t := range testcases {
mockParserCmd := testutil.MockCommand(c, "apparmor_parser", fmt.Sprintf("cat > %s/stdin; %s", d, t.exit))
defer mockParserCmd.Restore()
restore := apparmor.MockParserSearchPath(mockParserCmd.BinDir())
defer restore()
features, err := apparmor.ProbeParserFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, t.features)
c.Check(mockParserCmd.Calls(), DeepEquals, [][]string{{"apparmor_parser", "--preprocess"}})
data, err := ioutil.ReadFile(filepath.Join(d, "stdin"))
c.Assert(err, IsNil)
c.Check(string(data), Equals, "profile snap-test {\n change_profile unsafe /**,\n}")
}
// Pretend that we just don't have apparmor_parser at all.
restore := apparmor.MockParserSearchPath(c.MkDir())
defer restore()
features, err := apparmor.ProbeParserFeatures()
c.Check(err, Equals, os.ErrNotExist)
c.Check(features, DeepEquals, []string{})
}
func (s *apparmorSuite) TestInterfaceSystemKey(c *C) {
apparmor.FreshAppArmorAssessment()
d := c.MkDir()
restore := apparmor.MockFsRootPath(d)
defer restore()
c.Assert(os.MkdirAll(filepath.Join(d, featuresSysPath, "policy"), 0755), IsNil)
c.Assert(os.MkdirAll(filepath.Join(d, featuresSysPath, "network"), 0755), IsNil)
mockParserCmd := testutil.MockCommand(c, "apparmor_parser", "")
defer mockParserCmd.Restore()
restore = apparmor.MockParserSearchPath(mockParserCmd.BinDir())
defer restore()
apparmor.ProbedLevel()
features, err := apparmor.KernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{"network", "policy"})
features, err = apparmor.ParserFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{"unsafe"})
}
func (s *apparmorSuite) TestAppArmorParserMtime(c *C) {
// Pretend that we have apparmor_parser.
mockParserCmd := testutil.MockCommand(c, "apparmor_parser", "")
defer mockParserCmd.Restore()
restore := apparmor.MockParserSearchPath(mockParserCmd.BinDir())
defer restore()
mtime := apparmor.ParserMtime()
fi, err := os.Stat(filepath.Join(mockParserCmd.BinDir(), "apparmor_parser"))
c.Assert(err, IsNil)
c.Check(mtime, Equals, fi.ModTime().Unix())
// Pretend that we don't have apparmor_parser.
restore = apparmor.MockParserSearchPath(c.MkDir())
defer restore()
mtime = apparmor.ParserMtime()
c.Check(mtime, Equals, int64(0))
}
func (s *apparmorSuite) TestFeaturesProbedOnce(c *C) {
apparmor.FreshAppArmorAssessment()
d := c.MkDir()
restore := apparmor.MockFsRootPath(d)
defer restore()
c.Assert(os.MkdirAll(filepath.Join(d, featuresSysPath, "policy"), 0755), IsNil)
c.Assert(os.MkdirAll(filepath.Join(d, featuresSysPath, "network"), 0755), IsNil)
mockParserCmd := testutil.MockCommand(c, "apparmor_parser", "")
defer mockParserCmd.Restore()
restore = apparmor.MockParserSearchPath(mockParserCmd.BinDir())
defer restore()
features, err := apparmor.KernelFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{"network", "policy"})
features, err = apparmor.ParserFeatures()
c.Assert(err, IsNil)
c.Check(features, DeepEquals, []string{"unsafe"})
// this makes probing fails but is not done again
err = os.RemoveAll(d)
c.Assert(err, IsNil)
_, err = apparmor.KernelFeatures()
c.Assert(err, IsNil)
// this makes probing fails but is not done again
err = os.RemoveAll(mockParserCmd.BinDir())
c.Assert(err, IsNil)
_, err = apparmor.ParserFeatures()
c.Assert(err, IsNil)
}
func (s *apparmorSuite) TestValidateFreeFromAAREUnhappy(c *C) {
var testCases = []string{"a?", "*b", "c[c", "dd]", "e{", "f}", "g^", `h"`, "f\000", "g\x00"}
for _, s := range testCases {
c.Check(apparmor.ValidateNoAppArmorRegexp(s), ErrorMatches, ".* contains a reserved apparmor char from .*", Commentf("%q is not raising an error", s))
}
}
func (s *apparmorSuite) TestValidateFreeFromAAREhappy(c *C) {
var testCases = []string{"foo", "BaR", "b-z", "foo+bar", "b00m!", "be/ep", "a%b", "a&b", "a(b", "a)b", "a=b", "a#b", "a~b", "a'b", "a_b", "a,b", "a;b", "a>b", "a<b", "a|b"}
for _, s := range testCases {
c.Check(apparmor.ValidateNoAppArmorRegexp(s), IsNil, Commentf("%q raised an error but shouldn't", s))
}
}
|