Debian Codegraph @

src/tests/lxc-test-apparmor-generated

raw
1 #!/bin/sh
2
3 # SPDX-License-Identifier: LGPL-2.1+
4
5 # lxc: linux Container library
6
7 # This is a test script for generated apparmor profiles
8
9 if ! command -v apparmor_parser >/dev/null 2>&1; then
10 echo 'SKIP: test for generated apparmor profiles: apparmor_parser missing'
11 exit 0
12 fi
13
14 DONE=0
15 LOGFILE="/tmp/lxc-test-$$.log"
16 cleanup() {
17 lxc-destroy -n $CONTAINER_NAME >/dev/null 2>&1 || true
18
19 if [ $DONE -eq 0 ]; then
20 [ -f "$LOGFILE" ] && cat "$LOGFILE" >&2
21 rm -f "$LOGFILE"
22 echo "FAIL"
23 exit 1
24 fi
25 rm -f "$LOGFILE"
26 echo "PASS"
27 }
28
29 trap cleanup EXIT HUP INT TERM
30 set -eu
31
32 # Create a container
33 CONTAINER_NAME=lxc-test-apparmor-generated
34
35 lxc-create -t busybox -n $CONTAINER_NAME -B dir
36 CONTAINER_PATH=$(dirname $(lxc-info -n $CONTAINER_NAME -c lxc.rootfs.path -H) | sed -e 's/dir://')
37 cp $CONTAINER_PATH/config $CONTAINER_PATH/config.bak
38
39 # Set the profile to be auto-generated
40 echo "lxc.apparmor.profile = generated" >> $CONTAINER_PATH/config
41
42 # Start it
43 lxc-start -n $CONTAINER_NAME -lDEBUG -o "$LOGFILE"
44 lxc-wait -n $CONTAINER_NAME -t 5 -s RUNNING || (echo "Container didn't start" && exit 1)
45 pid=$(lxc-info -p -H -n $CONTAINER_NAME)
46 profile=$(cat /proc/$pid/attr/current)
47 expected_profile="lxc-${CONTAINER_NAME}_</var/lib/lxc>//&:lxc-${CONTAINER_NAME}_<-var-lib-lxc>:unconfined (enforce)"
48 lxc-stop -n $CONTAINER_NAME -k
49 if [ "x$profile" != "x$expected_profile" ]; then
50 echo "FAIL: container was in profile $profile" >&2
51 echo "expected profile: $expected_profile" >&2
52 exit 1
53 fi
54
55 DONE=1