lxc-test-apparmor-mount revision 42e5c9878f0d20b3e9682ef441afed2f0228b298
2693N/A#!/bin/sh
2693N/A
2693N/A# apparmor_mount: test proper handling of apparmor in kernels
2693N/A# without mount features
2693N/A
2693N/A# These require the ubuntu lxc package to be installed.
2693N/A
2693N/A# This program is free software; you can redistribute it and/or
2693N/A# modify it under the terms of the GNU Lesser General Public
2693N/A# License as published by the Free Software Foundation; either
2693N/A# version 2.1 of the License, or (at your option) any later version.
2693N/A
2693N/A# This library is distributed in the hope that it will be useful,
2693N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
2693N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2693N/A# Lesser General Public License for more details.
2693N/A
2693N/A# You should have received a copy of the GNU Lesser General Public
2693N/A# License along with this library; if not, write to the Free Software
2693N/A# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2693N/A
2693N/A# This test assumes an Ubuntu host
2693N/A
2693N/Aset -e
2693N/A
2693N/AFAIL() {
2693N/A echo -n "Failed " >&2
2693N/A echo "$*" >&2
2693N/A exit 1
2693N/A}
2693N/A
2693N/Arun_cmd() {
6271N/A sudo -i -u $TUSER \
2693N/A env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
2693N/A XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) $*
2693N/A}
2693N/A
2693N/ADONE=0
2693N/AMOUNTSR=/sys/kernel/security/apparmor/features/mount
2693N/Adnam=`mktemp -d`
2693N/Acname=`basename $dnam`
2693N/Acleanup() {
2693N/A run_cmd lxc-destroy -f -n $cname || true
2693N/A umount -l $MOUNTSR || true
2693N/A rmdir $dnam || true
2693N/A pkill -u $(id -u $TUSER) -9
2693N/A sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet
2693N/A sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
2693N/A rm -Rf $HDIR /run/user/$(id -u $TUSER)
2693N/A deluser $TUSER
2693N/A if [ $DONE -eq 0 ]; then
2693N/A echo "FAIL"
2693N/A exit 1
2693N/A fi
2693N/A echo "PASS"
2693N/A}
2693N/A
2693N/Atrap cleanup exit
2693N/A
2693N/A# Only run on a normally configured ubuntu lxc system
2693N/Aif [ ! -d /sys/class/net/lxcbr0 ]; then
2693N/A echo "lxcbr0 is not configured."
2693N/A exit 1
2693N/Afi
2693N/Aif [ "$(id -u)" != "0" ]; then
2693N/A echo "ERROR: Must run as root."
2693N/A exit 1
2693N/Afi
2693N/A
2693N/A# This would be much simpler if we could run it as
2693N/A# root. However, in order to not have the bind mount
2693N/A# of an empty directory over the securitfs 'mount' directory
2693N/A# be removed, we need to do this as non-root.
2693N/A
2693N/Awhich newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; }
2693N/A# create a test user
2693N/ATUSER=lxcunpriv
2693N/AHDIR=/home/$TUSER
2693N/A
2693N/AARCH=i386
2693N/Aif type dpkg >/dev/null 2>&1; then
2693N/A ARCH=$(dpkg --print-architecture)
2693N/Afi
2693N/A
2693N/Adeluser $TUSER && rm -Rf $HDIR || true
2693N/Auseradd $TUSER
6271N/A
2693N/Amkdir -p $HDIR
2693N/Aecho "$TUSER veth lxcbr0 2" > /etc/lxc/lxc-usernet
6271N/Ased -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
2693N/A
2693N/Ausermod -v 910000-919999 -w 910000-919999 $TUSER
2693N/A
2693N/Amkdir -p $HDIR/.config/lxc/
2693N/Acat > $HDIR/.config/lxc/default.conf << EOF
2693N/Alxc.network.type = veth
2693N/Alxc.network.link = lxcbr0
2693N/Alxc.id_map = u 0 910000 9999
2693N/Alxc.id_map = g 0 910000 9999
2693N/AEOF
2693N/Achown -R $TUSER: $HDIR
2693N/A
2693N/Amkdir -p /run/user/$(id -u $TUSER)
2693N/Achown -R $TUSER: /run/user/$(id -u $TUSER)
2693N/A
2693N/Acd $HDIR
2693N/A
2693N/Aif which cgm >/dev/null 2>&1; then
2693N/A cgm create all $TUSER
2693N/A cgm chown all $TUSER $(id -u $TUSER) $(id -g $TUSER)
2693N/A cgm movepid all $TUSER $$
2693N/Aelif [ -e /sys/fs/cgroup/cgmanager/sock ]; then
2693N/A for d in $(cut -d : -f 2 /proc/self/cgroup); do
2693N/A dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
2693N/A --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Create \
2693N/A string:$d string:$TUSER >/dev/null
2693N/A
2693N/A dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
2693N/A --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.Chown \
2693N/A string:$d string:$TUSER int32:$(id -u $TUSER) int32:$(id -g $TUSER) >/dev/null
2693N/A
2693N/A dbus-send --print-reply --address=unix:path=/sys/fs/cgroup/cgmanager/sock \
2693N/A --type=method_call /org/linuxcontainers/cgmanager org.linuxcontainers.cgmanager0_0.MovePid \
2693N/A string:$d string:$TUSER int32:$$ >/dev/null
2693N/A done
2693N/Aelse
2693N/A for d in /sys/fs/cgroup/*; do
2693N/A [ ! -d $d/lxctest ] && mkdir $d/lxctest
2693N/A chown -R $TUSER: $d/lxctest
2693N/A echo $$ > $d/lxctest/tasks
2693N/A done
2693N/Afi
2693N/A
2693N/A
2693N/Arun_cmd mkdir -p $HDIR/.cache/lxc
2693N/A[ -d /var/cache/lxc/download ] && \
2693N/A cp -R /var/cache/lxc/download $HDIR/.cache/lxc && \
2693N/A chown -R $TUSER: $HDIR/.cache/lxc
2693N/A
2693N/Arun_cmd lxc-create -t download -n $cname -- -d ubuntu -r trusty -a $ARCH
2693N/A
2693N/Aecho "test default confined container"
2693N/Arun_cmd lxc-start -n $cname -d
2693N/Arun_cmd lxc-wait -n $cname -s RUNNING
2693N/Apid=`run_cmd lxc-info -p -H -n $cname`
2693N/Aprofile=`cat /proc/$pid/attr/current`
2693N/Aif [ "x$profile" != "xlxc-container-default (enforce)" ]; then
2693N/A echo "FAIL: confined container was in profile $profile"
2693N/A exit 1
2693N/Afi
2693N/Arun_cmd lxc-stop -n $cname
2693N/A
2693N/Aecho "test regular unconfined container"
2693N/Aecho "lxc.aa_profile = unconfined" >> $HDIR/.local/share/lxc/$cname/config
2693N/Arun_cmd lxc-start -n $cname -d
2693N/Arun_cmd lxc-wait -n $cname -s RUNNING
6271N/Apid=`run_cmd lxc-info -p -H -n $cname`
2693N/Aprofile=`cat /proc/$pid/attr/current`
2693N/Aif [ "x$profile" != "xunconfined" ]; then
2693N/A echo "FAIL: unconfined container was in profile $profile"
2693N/A exit 1
2693N/Afi
2693N/Arun_cmd lxc-stop -n $cname
2693N/A
2693N/Aecho "masking $MOUNTSR"
6271N/Amount --bind $dnam $MOUNTSR
6271N/A
6271N/Aecho "test default confined container"
6271N/Ased -i '/aa_profile/d' $HDIR/.local/share/lxc/$cname/config
6271N/Arun_cmd lxc-start -n $cname -d || true
6271N/Asleep 3
6271N/Apid=`run_cmd lxc-info -p -H -n $cname` || true
6271N/Aif [ -n "$pid" -a "$pid" != "-1" ]; then
6271N/A echo "FAIL: confined container started without mount restrictions"
6271N/A echo "pid was $pid"
6271N/A exit 1
6271N/Afi
6271N/A
6271N/Aecho "test regular unconfined container"
6271N/Aecho "lxc.aa_profile = unconfined" >> $HDIR/.local/share/lxc/$cname/config
6271N/Arun_cmd lxc-start -n $cname -d
6271N/Arun_cmd lxc-wait -n $cname -s RUNNING
6271N/Apid=`run_cmd lxc-info -p -H -n $cname`
6271N/Aif [ "$pid" = "-1" ]; then
6271N/A echo "FAIL: unconfined container failed to start without mount restrictions"
6271N/A exit 1
6271N/Afi
2693N/Aprofile=`cat /proc/$pid/attr/current`
2693N/Aif [ "x$profile" != "xunconfined" ]; then
2693N/A echo "FAIL: confined container was in profile $profile"
2693N/A exit 1
2693N/Afi
2693N/Arun_cmd lxc-stop -n $cname
2693N/A
2693N/Aecho "testing override"
6271N/Ased -i '/aa_profile/d' $HDIR/.local/share/lxc/$cname/config
6271N/Aecho "lxc.aa_allow_incomplete = 1" >> $HDIR/.local/share/lxc/$cname/config
2693N/Arun_cmd lxc-start -n $cname -d
2693N/Arun_cmd lxc-wait -n $cname -s RUNNING
2693N/Apid=`run_cmd lxc-info -p -H -n $cname`
2693N/Aif [ "$pid" = "-1" ]; then
2693N/A echo "FAIL: excepted container failed to start without mount restrictions"
2693N/A exit 1
2693N/Afi
2693N/Aprofile=`cat /proc/$pid/attr/current`
2693N/Aif [ "x$profile" != "xlxc-container-default (enforce)" ]; then
2693N/A echo "FAIL: confined container was in profile $profile"
2693N/A exit 1
2693N/Afi
2693N/Arun_cmd lxc-stop -n $cname
2693N/A
2693N/ADONE=1
2693N/A