3 # apparmor_mount: test proper handling of apparmor in kernels
4 # without mount features
6 # These require the ubuntu lxc package to be installed.
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.
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.
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
22 # This test assumes an Ubuntu host
26 if [ -f /proc
/self
/ns
/cgroup
]; then
27 default_profile
="lxc-container-default-cgns (enforce)"
29 default_profile
="lxc-container-default (enforce)"
40 env http_proxy
=${http_proxy:-} https_proxy
=${https_proxy:-} \
41 XDG_RUNTIME_DIR
=/run
/user
/$
(id
-u $TUSER) $
*
45 KNOWN_RELEASES
="precise trusty xenial yakkety zesty"
46 MOUNTSR
=/sys
/kernel
/security
/apparmor
/features
/mount
48 cname
=`basename $dnam`
50 run_cmd lxc-destroy
-f -n $cname || true
51 umount
-l $MOUNTSR || true
53 pkill
-u $
(id
-u $TUSER) -9 || true
54 sed -i '/lxcunpriv/d' /run
/lxc
/nics
/etc
/lxc
/lxc-usernet
55 sed -i '/^lxcunpriv:/d' /etc
/subuid
/etc
/subgid
56 rm -Rf $HDIR /run
/user
/$
(id
-u $TUSER)
58 if [ $DONE -eq 0 ]; then
67 # Only run on a normally configured ubuntu lxc system
68 if [ ! -d /sys
/class
/net
/lxcbr0
]; then
69 echo "lxcbr0 is not configured."
72 if [ "$(id -u)" != "0" ]; then
73 echo "ERROR: Must run as root."
77 # This would be much simpler if we could run it as
78 # root. However, in order to not have the bind mount
79 # of an empty directory over the securitfs 'mount' directory
80 # be removed, we need to do this as non-root.
82 which newuidmap
>/dev
/null
2>&1 ||
{ echo "'newuidmap' command is missing" >&2; exit 1; }
88 if type dpkg
>/dev
/null
2>&1; then
89 ARCH
=$
(dpkg
--print-architecture)
92 deluser
$TUSER && rm -Rf $HDIR || true
96 echo "$TUSER veth lxcbr0 2" >> /etc
/lxc
/lxc-usernet
97 sed -i '/^lxcunpriv:/d' /etc
/subuid
/etc
/subgid
99 usermod
-v 910000-919999 -w 910000-919999 $TUSER
101 mkdir
-p $HDIR/.config
/lxc
/
102 cat > $HDIR/.config
/lxc
/default.conf
<< EOF
103 lxc.network.type = veth
104 lxc.network.link = lxcbr0
105 lxc.id_map = u 0 910000 9999
106 lxc.id_map = g 0 910000 9999
108 chown
-R $TUSER: $HDIR
110 mkdir
-p /run
/user
/$
(id
-u $TUSER)
111 chown
-R $TUSER: /run
/user
/$
(id
-u $TUSER)
115 if which cgm
>/dev
/null
2>&1; then
116 cgm create all
$TUSER
117 cgm chown all
$TUSER $
(id
-u $TUSER) $
(id
-g $TUSER)
118 cgm movepid all
$TUSER $$
119 elif [ -e /sys
/fs
/cgroup
/cgmanager
/sock
]; then
120 for d
in $
(cut
-d : -f 2 /proc
/self
/cgroup
); do
121 dbus-send
--print-reply --address=unix
:path
=/sys
/fs
/cgroup
/cgmanager
/sock \
122 --type=method_call
/org
/linuxcontainers
/cgmanager org.linuxcontainers.cgmanager0_0.Create \
123 string
:$d string
:$TUSER >/dev
/null
125 dbus-send
--print-reply --address=unix
:path
=/sys
/fs
/cgroup
/cgmanager
/sock \
126 --type=method_call
/org
/linuxcontainers
/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
127 string
:$d string
:$TUSER int32
:$
(id
-u $TUSER) int32
:$
(id
-g $TUSER) >/dev
/null
129 dbus-send
--print-reply --address=unix
:path
=/sys
/fs
/cgroup
/cgmanager
/sock \
130 --type=method_call
/org
/linuxcontainers
/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
131 string
:$d string
:$TUSER int32
:$$
>/dev
/null
134 for d
in /sys
/fs
/cgroup
/*; do
135 [ -f $d/cgroup.clone_children
] && echo 1 > $d/cgroup.clone_children
136 [ ! -d $d/lxctest
] && mkdir
$d/lxctest
137 chown
-R $TUSER: $d/lxctest
138 echo $$
> $d/lxctest
/tasks
143 run_cmd mkdir
-p $HDIR/.cache
/lxc
144 [ -d /var
/cache
/lxc
/download
] && \
145 cp -R /var
/cache
/lxc
/download
$HDIR/.cache
/lxc
&& \
146 chown
-R $TUSER: $HDIR/.cache
/lxc
148 # default release is trusty, or the systems release if recognized
150 if [ -f /etc
/lsb-release
]; then
152 rels
=$
(ubuntu-distro-info
--supported 2>/dev
/null
) ||
153 rels
="$KNOWN_RELEASES"
155 [ "$DISTRIB_CODENAME" = "$r" ] && release
="$r"
159 run_cmd lxc-create
-t download
-n $cname -- -d ubuntu
-r $release -a $ARCH
161 echo "test default confined container"
162 run_cmd lxc-start
-n $cname -d
163 run_cmd lxc-wait
-n $cname -s RUNNING
164 pid
=`run_cmd lxc-info -p -H -n $cname`
165 profile
=`cat /proc/$pid/attr/current`
166 if [ "x$profile" != "x${default_profile}" ]; then
167 echo "FAIL: confined container was in profile $profile"
170 run_cmd lxc-stop
-n $cname -k
172 echo "test regular unconfined container"
173 echo "lxc.aa_profile = unconfined" >> $HDIR/.local
/share
/lxc
/$cname/config
174 run_cmd lxc-start
-n $cname -d
175 run_cmd lxc-wait
-n $cname -s RUNNING
176 pid
=`run_cmd lxc-info -p -H -n $cname`
177 profile
=`cat /proc/$pid/attr/current`
178 if [ "x$profile" != "xunconfined" ]; then
179 echo "FAIL: unconfined container was in profile $profile"
182 run_cmd lxc-stop
-n $cname -k
184 echo "masking $MOUNTSR"
185 mount
--bind $dnam $MOUNTSR
187 echo "test default confined container"
188 sed -i '/aa_profile/d' $HDIR/.local
/share
/lxc
/$cname/config
189 run_cmd lxc-start
-n $cname -d || true
191 pid
=`run_cmd lxc-info -p -H -n $cname` || true
192 if [ -n "$pid" -a "$pid" != "-1" ]; then
193 echo "FAIL: confined container started without mount restrictions"
198 echo "test regular unconfined container"
199 echo "lxc.aa_profile = unconfined" >> $HDIR/.local
/share
/lxc
/$cname/config
200 run_cmd lxc-start
-n $cname -d
201 run_cmd lxc-wait
-n $cname -s RUNNING
202 pid
=`run_cmd lxc-info -p -H -n $cname`
203 if [ "$pid" = "-1" ]; then
204 echo "FAIL: unconfined container failed to start without mount restrictions"
207 profile
=`cat /proc/$pid/attr/current`
208 if [ "x$profile" != "xunconfined" ]; then
209 echo "FAIL: confined container was in profile $profile"
212 run_cmd lxc-stop
-n $cname -k
214 echo "testing override"
215 sed -i '/aa_profile/d' $HDIR/.local
/share
/lxc
/$cname/config
216 echo "lxc.aa_allow_incomplete = 1" >> $HDIR/.local
/share
/lxc
/$cname/config
217 run_cmd lxc-start
-n $cname -d
218 run_cmd lxc-wait
-n $cname -s RUNNING
219 pid
=`run_cmd lxc-info -p -H -n $cname`
220 if [ "$pid" = "-1" ]; then
221 echo "FAIL: excepted container failed to start without mount restrictions"
224 profile
=`cat /proc/$pid/attr/current`
225 if [ "x$profile" != "x${default_profile}" ]; then
226 echo "FAIL: confined container was in profile $profile"
229 run_cmd lxc-stop
-n $cname -k