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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2019-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package security
import (
"fmt"
"os"
"os/exec"
"testing"
"github.com/apptainer/apptainer/e2e/internal/e2e"
"github.com/apptainer/apptainer/e2e/internal/testhelper"
"github.com/apptainer/apptainer/internal/pkg/buildcfg"
"github.com/apptainer/apptainer/internal/pkg/test/tool/require"
"github.com/apptainer/apptainer/pkg/util/capabilities"
)
type ctx struct {
env e2e.TestEnv
}
// testSecurityUnpriv tests the security flag functionality for apptainer exec without elevated privileges
func (c ctx) testSecurityUnpriv(t *testing.T) {
tests := []struct {
name string
image string
argv []string
opts []string
preFn func(*testing.T)
userNs bool
expectOp e2e.ApptainerCmdResultOp
expectExit int
}{
// target UID/GID
{
name: "Set_uid",
argv: []string{"id", "-u"},
opts: []string{"--security", "uid:99"},
expectExit: 255,
},
{
name: "Set_gid",
argv: []string{"id", "-g"},
opts: []string{"--security", "gid:99"},
expectExit: 255,
},
{
name: "Set_uid",
argv: []string{"id", "-u"},
opts: []string{"--security", "uid:99"},
userNs: true,
expectExit: 0,
},
{
name: "Set_gid",
argv: []string{"id", "-g"},
opts: []string{"--security", "gid:99"},
userNs: true,
expectExit: 0,
},
// seccomp from json file
{
name: "SecComp_BlackList",
argv: []string{"mkdir", "/tmp/foo"},
opts: []string{"--security", "seccomp:./security/testdata/seccomp-profile.json"},
preFn: require.Seccomp,
expectExit: 159, // process should be killed with SIGSYS (128+31)
},
{
name: "SecComp_true",
argv: []string{"true"},
opts: []string{"--security", "seccomp:./security/testdata/seccomp-profile.json"},
preFn: require.Seccomp,
expectExit: 0,
},
// capabilities
{
name: "capabilities_keep_true",
argv: []string{"grep", "^CapEff:", "/proc/self/status"},
opts: []string{"--keep-privs"},
expectExit: 255, // apptainer errors out, --keep-privs needs root
},
{
// we start without any capabilities, the
// expected set is empty.
name: "capabilities_keep-false",
argv: []string{"grep", "^CapEff:", "/proc/self/status"},
expectOp: e2e.ExpectOutput(e2e.RegexMatch, `CapEff:\s+0+\n`),
},
{
// this one is tricky: we are asking to drop
// cap_net_raw, but since we start without any
// capabilities the expected set is empty.
name: "capabilities_drop",
argv: []string{"grep", "^Cap.*:", "/proc/self/status"},
opts: []string{"--drop-caps", "CAP_NET_RAW"},
expectOp: e2e.ExpectOutput(e2e.RegexMatch, `CapEff:\s+0+\n`),
},
}
e2e.EnsureImage(t, c.env)
for _, tt := range tests {
optArgs := []string{}
optArgs = append(optArgs, tt.opts...)
optArgs = append(optArgs, c.env.ImagePath)
optArgs = append(optArgs, tt.argv...)
profile := e2e.UserProfile
if tt.userNs {
profile = e2e.UserNamespaceProfile
}
c.env.RunApptainer(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(optArgs...),
e2e.PreRun(tt.preFn),
e2e.ExpectExit(tt.expectExit, tt.expectOp),
)
}
}
// testSecurityPriv tests security flag functionality for apptainer exec with elevated privileges
func (c ctx) testSecurityPriv(t *testing.T) {
var caps uint64
var err error
// Get the capability set for root on this system
// e.g. "CapEff: 000001ffffffffff"
e2e.Privileged(func(_ *testing.T) {
caps, err = capabilities.GetProcessEffective()
})(t)
if err != nil {
t.Fatalf("Could not get CapEff: %v", err)
}
fullCap := fmt.Sprintf("CapEff:\t%0.16x", caps)
// We are going to drop CAP_NET_RAW which should result in the CapEff
// string ending dfff
dropCap := fmt.Sprintf("CapEff:\t%0.16x", caps-uint64(1<<capabilities.Map["CAP_NET_RAW"].Value))
tests := []struct {
name string
argv []string
opts []string
preFn func(*testing.T)
expectOp e2e.ApptainerCmdResultOp
expectExit int
}{
// target UID/GID
{
name: "Set_uid",
argv: []string{"id", "-u"},
opts: []string{"--security", "uid:99"},
expectOp: e2e.ExpectOutput(e2e.ExactMatch, "99"),
expectExit: 0,
},
{
name: "Set_gid",
argv: []string{"id", "-g"},
opts: []string{"--security", "gid:99"},
expectOp: e2e.ExpectOutput(e2e.ExactMatch, "99"),
expectExit: 0,
},
// seccomp from json file
{
name: "SecComp_BlackList",
argv: []string{"mkdir", "/tmp/foo"},
opts: []string{"--security", "seccomp:./testdata/seccomp-profile.json"},
preFn: require.Seccomp,
expectExit: 159, // process should be killed with SIGSYS (128+31)
},
{
name: "SecComp_true",
argv: []string{"true"},
opts: []string{"--security", "seccomp:./testdata/seccomp-profile.json"},
preFn: require.Seccomp,
expectExit: 0,
},
// capabilities
{
name: "capabilities_keep",
argv: []string{"grep", "^CapEff:", "/proc/self/status"},
opts: []string{"--keep-privs"},
expectOp: e2e.ExpectOutput(e2e.ContainMatch, fullCap),
},
{
name: "capabilities_drop",
argv: []string{"grep", "^CapEff:", "/proc/self/status"},
opts: []string{"--drop-caps", "CAP_NET_RAW"},
expectOp: e2e.ExpectOutput(e2e.ContainMatch, dropCap),
},
}
e2e.EnsureImage(t, c.env)
for _, tt := range tests {
optArgs := []string{}
optArgs = append(optArgs, tt.opts...)
optArgs = append(optArgs, c.env.ImagePath)
optArgs = append(optArgs, tt.argv...)
c.env.RunApptainer(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.RootProfile),
e2e.WithCommand("exec"),
e2e.WithArgs(optArgs...),
e2e.PreRun(tt.preFn),
e2e.ExpectExit(tt.expectExit, tt.expectOp),
)
}
}
// testSecurityConfOwnership tests checks on config files ownerships
func (c ctx) testSecurityConfOwnership(t *testing.T) {
e2e.EnsureImage(t, c.env)
configFile := buildcfg.APPTAINER_CONF_FILE
c.env.RunApptainer(
t,
e2e.AsSubtest("non root config"),
e2e.WithProfile(e2e.UserProfile),
e2e.PreRun(func(t *testing.T) {
e2e.Privileged(func(t *testing.T) {
// Change file ownership (do not try this at home)
err := os.Chown(configFile, 1001, 0)
if err != nil {
t.Fatalf("failed to change owner for: %s: %s", configFile, err)
}
})(t)
}),
e2e.PostRun(func(t *testing.T) {
e2e.Privileged(func(t *testing.T) {
// return file ownership to normal
err := os.Chown(configFile, 0, 0)
if err != nil {
t.Fatalf("failed to change config file owner to root: %s: %s", configFile, err)
}
})(t)
}),
e2e.WithCommand("exec"),
e2e.WithArgs([]string{c.env.ImagePath, "/bin/true"}...),
e2e.ExpectExit(255),
)
}
// testApparmor tests the apparmor security flag.
func (c ctx) testApparmor(t *testing.T) {
require.Apparmor(t)
e2e.EnsureImage(t, c.env)
tests := []struct {
name string
image string
argv []string
opts []string
expectOp e2e.ApptainerCmdResultOp
expectExit int
}{
// apparmor
// Uses the profile for /usr/bin/man which will block `ls` in container root.
{
name: "apparmor applied",
argv: []string{"cat", "/proc/self/attr/current"},
opts: []string{"--security", "apparmor:/usr/bin/man"},
expectOp: e2e.ExpectOutput(e2e.ExactMatch, "/usr/bin/man (enforce)"),
},
{
name: "apparmor denial",
argv: []string{"ls", "/"},
opts: []string{"--security", "apparmor:/usr/bin/man"},
expectExit: 1,
expectOp: e2e.ExpectError(e2e.ContainMatch, "Permission denied"),
},
}
for _, profile := range e2e.Profiles {
t.Run(profile.String(), func(t *testing.T) {
for _, tt := range tests {
optArgs := []string{}
optArgs = append(optArgs, tt.opts...)
optArgs = append(optArgs, c.env.ImagePath)
optArgs = append(optArgs, tt.argv...)
c.env.RunApptainer(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(optArgs...),
e2e.ExpectExit(tt.expectExit, tt.expectOp),
)
}
})
}
}
// testSELinux tests the selinux security flag.
func (c ctx) testSELinux(t *testing.T) {
require.Selinux(t)
require.Command(t, "chcon")
e2e.EnsureImage(t, c.env)
sandbox, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "sandbox-", "")
defer cleanup(t)
// convert test image to sandbox
c.env.RunApptainer(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("build"),
e2e.WithArgs("--force", "--sandbox", sandbox, c.env.ImagePath),
e2e.ExpectExit(0),
)
// label as `container_ro_t`
cmd := exec.Command("chcon", "-R", "-t", "container_ro_file_t", sandbox)
if err := cmd.Run(); err != nil {
t.Fatalf("while labeling sandbox: %v", err)
}
tests := []struct {
name string
image string
argv []string
opts []string
expectOp e2e.ApptainerCmdResultOp
expectExit int
}{
// selinux
// Uses the container_t label which will block `ls` in /tmp bind from host.
{
name: "selinux applied",
argv: []string{"cat", "/proc/self/attr/current"},
opts: []string{"--security", "selinux:unconfined_u:unconfined_r:container_t:s0"},
expectOp: e2e.ExpectOutput(e2e.ContainMatch, "unconfined_u:unconfined_r:container_t:s0"),
},
{
name: "selinux denial",
argv: []string{"ls", "/tmp"},
opts: []string{"--security", "selinux:unconfined_u:unconfined_r:container_t:s0"},
expectExit: 1,
expectOp: e2e.ExpectError(e2e.ContainMatch, "Permission denied"),
},
}
for _, profile := range e2e.Profiles {
t.Run(profile.String(), func(t *testing.T) {
for _, tt := range tests {
optArgs := []string{}
optArgs = append(optArgs, tt.opts...)
optArgs = append(optArgs, sandbox)
optArgs = append(optArgs, tt.argv...)
c.env.RunApptainer(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(optArgs...),
e2e.ExpectExit(tt.expectExit, tt.expectOp),
)
}
})
}
}
// E2ETests is the main func to trigger the test suite
func E2ETests(env e2e.TestEnv) testhelper.Tests {
c := ctx{
env: env,
}
np := testhelper.NoParallel
return testhelper.Tests{
"apptainerSecurityUnpriv": c.testSecurityUnpriv,
"apptainerSecurityPriv": c.testSecurityPriv,
"testSecurityConfOwnership": np(c.testSecurityConfOwnership),
"testApparmor": c.testApparmor,
"testSELinux": c.testSELinux,
}
}
|