Debian Codegraph @

src/tests/lxc-test-apparmor-mount

raw
1 #!/bin/sh
2
3 # SPDX-License-Identifier: LGPL-2.1+
4
5 # apparmor_mount: test proper handling of apparmor in kernels
6 # without mount features
7
8 # These require the ubuntu lxc package to be installed.
9
10 set -e
11
12 # Only run on a normally configured ubuntu lxc system
13 if [ ! -d /sys/class/net/lxcbr0 ]; then
14 echo "lxcbr0 is not configured."
15 exit 1
16 fi
17 if [ "$(id -u)" != "0" ]; then
18 echo "ERROR: Must run as root."
19 exit 1
20 fi
21
22 if [ -f /proc/self/ns/cgroup ]; then
23 default_profile="lxc-container-default-cgns (enforce)"
24 else
25 default_profile="lxc-container-default (enforce)"
26 fi
27
28 FAIL() {
29 echo -n "Failed " >&2
30 echo "$*" >&2
31 exit 1
32 }
33
34 run_cmd() {
35 sudo -i -u $TUSER \
36 env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
37 XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) ASAN_OPTIONS=${ASAN_OPTIONS:-} \
38 UBSAN_OPTIONS=${UBSAN_OPTIONS:-} $*
39 }
40
41 DONE=0
42 MOUNTSR=/sys/kernel/security/apparmor/features/mount
43 dnam=$(mktemp -d)
44 logfile=$(mktemp)
45 cname=$(basename $dnam)
46 cleanup() {
47 run_cmd lxc-destroy -f -n $cname || true
48 umount -l $MOUNTSR || true
49 rmdir $dnam || true
50 pkill -u $(id -u $TUSER) -9 || true
51 sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet
52 sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
53 rm -Rf $HDIR /run/user/$(id -u $TUSER) || true
54 deluser $TUSER
55 if [ $DONE -eq 0 ]; then
56 echo 'Failed container log:' >&2
57 cat "$logfile" >&2
58 echo 'End log' >&2
59 rm -f "$logfile"
60 echo "FAIL"
61 exit 1
62 fi
63 rm -f "$logfile"
64 echo "PASS"
65 }
66
67 clear_log() {
68 truncate -s0 "$logfile"
69 }
70
71 trap cleanup exit
72
73 chmod 0666 "$logfile"
74
75 # This would be much simpler if we could run it as
76 # root. However, in order to not have the bind mount
77 # of an empty directory over the securityfs 'mount' directory
78 # be removed, we need to do this as non-root.
79
80 command -v newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; }
81 # create a test user
82 TUSER=lxcunpriv
83 HDIR=/home/$TUSER
84
85 deluser $TUSER && rm -Rf $HDIR || true
86 useradd $TUSER
87
88 mkdir -p $HDIR
89 echo "$TUSER veth lxcbr0 2" >> /etc/lxc/lxc-usernet
90 sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
91
92 usermod -v 910000-919999 -w 910000-919999 $TUSER
93
94 mkdir -p $HDIR/.config/lxc/
95 cat > $HDIR/.config/lxc/default.conf << EOF
96 lxc.net.0.type = veth
97 lxc.net.0.link = lxcbr0
98 lxc.idmap = u 0 910000 9999
99 lxc.idmap = g 0 910000 9999
100 EOF
101 chown -R $TUSER: $HDIR
102
103 mkdir -p /run/user/$(id -u $TUSER)
104 chown -R $TUSER: /run/user/$(id -u $TUSER)
105
106 cd $HDIR
107
108 run_cmd lxc-create -t busybox -n $cname
109
110 echo "test default confined container"
111 run_cmd lxc-start -n $cname -d -lDEBUG -o "$logfile"
112 run_cmd lxc-wait -n $cname -s RUNNING
113 pid=$(run_cmd lxc-info -p -H -n $cname)
114 profile=$(cat /proc/$pid/attr/current)
115 if [ "x$profile" != "x${default_profile}" ]; then
116 echo "FAIL: confined container was in profile $profile"
117 exit 1
118 fi
119 run_cmd lxc-stop -n $cname -k
120 clear_log
121
122 echo "test regular unconfined container"
123 echo "lxc.apparmor.profile = unconfined" >> $HDIR/.local/share/lxc/$cname/config
124 run_cmd lxc-start -n $cname -d -lDEBUG -o "$logfile"
125 run_cmd lxc-wait -n $cname -s RUNNING
126 pid=$(run_cmd lxc-info -p -H -n $cname)
127 profile=$(cat /proc/$pid/attr/current)
128 if [ "x$profile" != "xunconfined" ]; then
129 echo "FAIL: unconfined container was in profile $profile"
130 exit 1
131 fi
132 run_cmd lxc-stop -n $cname -k
133 clear_log
134
135 echo "masking $MOUNTSR"
136 mount --bind $dnam $MOUNTSR
137
138 echo "test default confined container"
139 sed -i '/apparmor.profile/d' $HDIR/.local/share/lxc/$cname/config
140 run_cmd lxc-start -n $cname -d || true
141 sleep 3
142 pid=$(run_cmd lxc-info -p -H -n $cname) || true
143 if [ -n "$pid" -a "$pid" != "-1" ]; then
144 echo "FAIL: confined container started without mount restrictions"
145 echo "pid was $pid"
146 exit 1
147 fi
148
149 echo "test regular unconfined container"
150 echo "lxc.apparmor.profile = unconfined" >> $HDIR/.local/share/lxc/$cname/config
151 run_cmd lxc-start -n $cname -d -lDEBUG -o "$logfile"
152 run_cmd lxc-wait -n $cname -s RUNNING
153 pid=$(run_cmd lxc-info -p -H -n $cname)
154 if [ "$pid" = "-1" ]; then
155 echo "FAIL: unconfined container failed to start without mount restrictions"
156 exit 1
157 fi
158 profile=$(cat /proc/$pid/attr/current)
159 if [ "x$profile" != "xunconfined" ]; then
160 echo "FAIL: confined container was in profile $profile"
161 exit 1
162 fi
163 run_cmd lxc-stop -n $cname -k
164 clear_log
165
166 echo "testing override"
167 sed -i '/apparmor.profile/d' $HDIR/.local/share/lxc/$cname/config
168 echo "lxc.apparmor.allow_incomplete = 1" >> $HDIR/.local/share/lxc/$cname/config
169 run_cmd lxc-start -n $cname -d -lDEBUG -o "$logfile"
170 run_cmd lxc-wait -n $cname -s RUNNING
171 pid=$(run_cmd lxc-info -p -H -n $cname)
172 if [ "$pid" = "-1" ]; then
173 echo "FAIL: excepted container failed to start without mount restrictions"
174 exit 1
175 fi
176 profile=$(cat /proc/$pid/attr/current)
177 if [ "x$profile" != "x${default_profile}" ]; then
178 echo "FAIL: confined container was in profile $profile"
179 exit 1
180 fi
181 run_cmd lxc-stop -n $cname -k
182 clear_log
183
184 DONE=1