]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc-test-apparmor-mount
meson: Remove non-existent tests
[mirror_lxc.git] / src / tests / lxc-test-apparmor-mount
CommitLineData
7aff4f43
SH
1#!/bin/sh
2
3# apparmor_mount: test proper handling of apparmor in kernels
4# without mount features
5
6# These require the ubuntu lxc package to be installed.
7
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU Lesser General Public
10# License as published by the Free Software Foundation; either
11# version 2.1 of the License, or (at your option) any later version.
12
13# This library is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# Lesser General Public License for more details.
17
18# You should have received a copy of the GNU Lesser General Public
19# License along with this library; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
22# This test assumes an Ubuntu host
23
24set -e
25
39e2cbec
WB
26# Only run on a normally configured ubuntu lxc system
27if [ ! -d /sys/class/net/lxcbr0 ]; then
28 echo "lxcbr0 is not configured."
29 exit 1
30fi
31if [ "$(id -u)" != "0" ]; then
32 echo "ERROR: Must run as root."
33 exit 1
34fi
35
f58236fd
SH
36if [ -f /proc/self/ns/cgroup ]; then
37 default_profile="lxc-container-default-cgns (enforce)"
38else
39 default_profile="lxc-container-default (enforce)"
40fi
41
7aff4f43
SH
42FAIL() {
43 echo -n "Failed " >&2
44 echo "$*" >&2
45 exit 1
46}
47
48run_cmd() {
49 sudo -i -u $TUSER \
50 env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
5f850cf9
EV
51 XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) ASAN_OPTIONS=${ASAN_OPTIONS:-} \
52 UBSAN_OPTIONS=${UBSAN_OPTIONS:-} $*
7aff4f43
SH
53}
54
55DONE=0
56MOUNTSR=/sys/kernel/security/apparmor/features/mount
ac46b356
DH
57dnam=$(mktemp -d)
58logfile=$(mktemp)
59cname=$(basename $dnam)
7aff4f43
SH
60cleanup() {
61 run_cmd lxc-destroy -f -n $cname || true
62 umount -l $MOUNTSR || true
63 rmdir $dnam || true
d0ab6d91 64 pkill -u $(id -u $TUSER) -9 || true
7aff4f43
SH
65 sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet
66 sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
67 rm -Rf $HDIR /run/user/$(id -u $TUSER)
68 deluser $TUSER
69 if [ $DONE -eq 0 ]; then
d6523915
WB
70 echo 'Failed container log:' >&2
71 cat "$logfile" >&2
72 echo 'End log' >&2
73 rm -f "$logfile"
7aff4f43
SH
74 echo "FAIL"
75 exit 1
76 fi
d6523915 77 rm -f "$logfile"
7aff4f43
SH
78 echo "PASS"
79}
80
d6523915
WB
81clear_log() {
82 truncate -s0 "$logfile"
83}
84
7aff4f43
SH
85trap cleanup exit
86
d6523915
WB
87chmod 0666 "$logfile"
88
7aff4f43
SH
89# This would be much simpler if we could run it as
90# root. However, in order to not have the bind mount
c239013f 91# of an empty directory over the securityfs 'mount' directory
7aff4f43
SH
92# be removed, we need to do this as non-root.
93
4c69af0c 94command -v newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; }
7aff4f43
SH
95# create a test user
96TUSER=lxcunpriv
97HDIR=/home/$TUSER
98
7aff4f43
SH
99deluser $TUSER && rm -Rf $HDIR || true
100useradd $TUSER
101
102mkdir -p $HDIR
efdca59e 103echo "$TUSER veth lxcbr0 2" >> /etc/lxc/lxc-usernet
7aff4f43
SH
104sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
105
106usermod -v 910000-919999 -w 910000-919999 $TUSER
107
108mkdir -p $HDIR/.config/lxc/
109cat > $HDIR/.config/lxc/default.conf << EOF
7fa3f2e9 110lxc.net.0.type = veth
111lxc.net.0.link = lxcbr0
bdcbb6b3
CB
112lxc.idmap = u 0 910000 9999
113lxc.idmap = g 0 910000 9999
7aff4f43
SH
114EOF
115chown -R $TUSER: $HDIR
116
117mkdir -p /run/user/$(id -u $TUSER)
118chown -R $TUSER: /run/user/$(id -u $TUSER)
119
120cd $HDIR
121
4c69af0c 122if command -v cgm >/dev/null 2>&1; then
42e5c987
SG
123 cgm create all $TUSER
124 cgm chown all $TUSER $(id -u $TUSER) $(id -g $TUSER)
125 cgm movepid all $TUSER $$
126elif [ -e /sys/fs/cgroup/cgmanager/sock ]; then
127 for d in $(cut -d : -f 2 /proc/self/cgroup); do
128 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
129 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
130 string:$d string:$TUSER >/dev/null
131
132 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
133 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
134 string:$d string:$TUSER int32:$(id -u $TUSER) int32:$(id -g $TUSER) >/dev/null
135
136 dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
137 --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
138 string:$d string:$TUSER int32:$$ >/dev/null
139 done
140else
141 for d in /sys/fs/cgroup/*; do
8d5a91fc 142 [ "$d" = "/sys/fs/cgroup/unified" ] && continue
177f793a 143 [ -f $d/cgroup.clone_children ] && echo 1 > $d/cgroup.clone_children
42e5c987
SG
144 [ ! -d $d/lxctest ] && mkdir $d/lxctest
145 chown -R $TUSER: $d/lxctest
146 echo $$ > $d/lxctest/tasks
147 done
148fi
149
7aff4f43 150
adb14537 151run_cmd lxc-create -t busybox -n $cname
7aff4f43
SH
152
153echo "test default confined container"
d6523915 154run_cmd lxc-start -n $cname -d -lDEBUG -o "$logfile"
7aff4f43 155run_cmd lxc-wait -n $cname -s RUNNING
ac46b356
DH
156pid=$(run_cmd lxc-info -p -H -n $cname)
157profile=$(cat /proc/$pid/attr/current)
f58236fd 158if [ "x$profile" != "x${default_profile}" ]; then
7aff4f43
SH
159 echo "FAIL: confined container was in profile $profile"
160 exit 1
161fi
09ef0838 162run_cmd lxc-stop -n $cname -k
d6523915 163clear_log
7aff4f43
SH
164
165echo "test regular unconfined container"
a1d5fdfd 166echo "lxc.apparmor.profile = unconfined" >> $HDIR/.local/share/lxc/$cname/config
d6523915 167run_cmd lxc-start -n $cname -d -lDEBUG -o "$logfile"
7aff4f43 168run_cmd lxc-wait -n $cname -s RUNNING
ac46b356
DH
169pid=$(run_cmd lxc-info -p -H -n $cname)
170profile=$(cat /proc/$pid/attr/current)
7aff4f43
SH
171if [ "x$profile" != "xunconfined" ]; then
172 echo "FAIL: unconfined container was in profile $profile"
173 exit 1
174fi
09ef0838 175run_cmd lxc-stop -n $cname -k
d6523915 176clear_log
7aff4f43
SH
177
178echo "masking $MOUNTSR"
179mount --bind $dnam $MOUNTSR
180
181echo "test default confined container"
a1d5fdfd 182sed -i '/apparmor.profile/d' $HDIR/.local/share/lxc/$cname/config
7aff4f43
SH
183run_cmd lxc-start -n $cname -d || true
184sleep 3
ac46b356 185pid=$(run_cmd lxc-info -p -H -n $cname) || true
7aff4f43
SH
186if [ -n "$pid" -a "$pid" != "-1" ]; then
187 echo "FAIL: confined container started without mount restrictions"
188 echo "pid was $pid"
189 exit 1
190fi
191
192echo "test regular unconfined container"
a1d5fdfd 193echo "lxc.apparmor.profile = unconfined" >> $HDIR/.local/share/lxc/$cname/config
d6523915 194run_cmd lxc-start -n $cname -d -lDEBUG -o "$logfile"
7aff4f43 195run_cmd lxc-wait -n $cname -s RUNNING
ac46b356 196pid=$(run_cmd lxc-info -p -H -n $cname)
7aff4f43
SH
197if [ "$pid" = "-1" ]; then
198 echo "FAIL: unconfined container failed to start without mount restrictions"
199 exit 1
200fi
ac46b356 201profile=$(cat /proc/$pid/attr/current)
7aff4f43
SH
202if [ "x$profile" != "xunconfined" ]; then
203 echo "FAIL: confined container was in profile $profile"
204 exit 1
205fi
09ef0838 206run_cmd lxc-stop -n $cname -k
d6523915 207clear_log
7aff4f43
SH
208
209echo "testing override"
a1d5fdfd 210sed -i '/apparmor.profile/d' $HDIR/.local/share/lxc/$cname/config
69e38e00 211echo "lxc.apparmor.allow_incomplete = 1" >> $HDIR/.local/share/lxc/$cname/config
d6523915 212run_cmd lxc-start -n $cname -d -lDEBUG -o "$logfile"
7aff4f43 213run_cmd lxc-wait -n $cname -s RUNNING
ac46b356 214pid=$(run_cmd lxc-info -p -H -n $cname)
7aff4f43
SH
215if [ "$pid" = "-1" ]; then
216 echo "FAIL: excepted container failed to start without mount restrictions"
217 exit 1
218fi
ac46b356 219profile=$(cat /proc/$pid/attr/current)
f58236fd 220if [ "x$profile" != "x${default_profile}" ]; then
7aff4f43
SH
221 echo "FAIL: confined container was in profile $profile"
222 exit 1
223fi
09ef0838 224run_cmd lxc-stop -n $cname -k
d6523915 225clear_log
7aff4f43
SH
226
227DONE=1