5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin#!/bin/bash
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin# ex: ts=8 sw=4 sts=4 et filetype=sh
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginTEST_DESCRIPTION="SELinux tests"
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin# Requirements:
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin# Fedora 23
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin# selinux-policy-targeted
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin# selinux-policy-devel
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin. $TEST_BASE_DIR/test-functions
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginSETUP_SELINUX=yes
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginKERNEL_APPEND="$KERNEL_APPEND selinux=1"
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagincheck_result_qemu() {
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ret=1
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin mkdir -p $TESTDIR/root
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin mount ${LOOPDEV}p1 $TESTDIR/root
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin [[ -e $TESTDIR/root/testok ]] && ret=0
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin [[ -f $TESTDIR/root/failed ]] && cp -a $TESTDIR/root/failed $TESTDIR
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin cp -a $TESTDIR/root/var/log/journal $TESTDIR
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin umount $TESTDIR/root
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ls -l $TESTDIR/journal/*/*.journal
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin test -s $TESTDIR/failed && ret=$(($ret+1))
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin return $ret
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin}
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagintest_run() {
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin if run_qemu; then
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin check_result_qemu || return 1
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin else
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin dwarn "can't run QEMU, skipping"
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin fi
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin return 0
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin}
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagintest_setup() {
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin create_empty_image
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin mkdir -p $TESTDIR/root
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin mount ${LOOPDEV}p1 $TESTDIR/root
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin # Create what will eventually be our root filesystem onto an overlay
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin (
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin LOG_LEVEL=5
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin setup_basic_environment
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin # setup the testsuite service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin cat <<EOF >$initdir/etc/systemd/system/testsuite.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin[Unit]
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginDescription=Testsuite service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginAfter=multi-user.target
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin[Service]
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginExecStart=/test-selinux-checks.sh
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginType=oneshot
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginEOF
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin cat <<EOF >$initdir/etc/systemd/system/hola.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin[Service]
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginType=oneshot
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginExecStart=/bin/echo Start Hola
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginExecReload=/bin/echo Reload Hola
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginExecStop=/bin/echo Stop Hola
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginRemainAfterExit=yes
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginEOF
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin setup_testsuite
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin cat <<EOF >$initdir/etc/systemd/system/load-systemd-test-module.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin[Unit]
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginDescription=Load systemd-test module
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginDefaultDependencies=no
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginRequires=local-fs.target
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginConflicts=shutdown.target
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginAfter=local-fs.target
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginBefore=sysinit.target shutdown.target autorelabel.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginConditionSecurity=selinux
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginConditionPathExists=|/.load-systemd-test-module
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin[Service]
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && cd /systemd-test-module && make -f /usr/share/selinux/devel/Makefile load && rm /.load-systemd-test-module'
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginType=oneshot
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginTimeoutSec=0
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginRemainAfterExit=yes
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny VereshchaginEOF
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin touch $initdir/.load-systemd-test-module
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin mkdir -p $initdir/etc/systemd/system/basic.target.wants
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ln -fs load-systemd-test-module.service $initdir/etc/systemd/system/basic.target.wants/load-systemd-test-module.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin local _modules_dir=/var/lib/selinux
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin rm -rf $initdir/$_modules_dir
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin if ! cp -ar $_modules_dir $initdir/$_modules_dir; then
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin dfatal "Failed to copy $_modules_dir"
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin exit 1
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin fi
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin local _policy_headers_dir=/usr/share/selinux/devel
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin rm -rf $initdir/$_policy_headers_dir
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin inst_dir /usr/share/selinux
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin if ! cp -ar $_policy_headers_dir $initdir/$_policy_headers_dir; then
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin dfatal "Failed to copy $_policy_headers_dir"
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin exit 1
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin fi
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin mkdir $initdir/systemd-test-module
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin cp systemd_test.te $initdir/systemd-test-module
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin cp systemd_test.if $initdir/systemd-test-module
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin cp test-selinux-checks.sh $initdir
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin dracut_install -o sesearch
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin dracut_install runcon
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin dracut_install checkmodule semodule semodule_package m4 make /usr/libexec/selinux/hll/pp load_policy sefcontext_compile
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ) || return 1
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin # mask some services that we do not want to run in these tests
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ln -s /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ln -s /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ln -s /dev/null $initdir/etc/systemd/system/systemd-networkd.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ln -s /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ln -s /dev/null $initdir/etc/systemd/system/systemd-resolved.service
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin ddebug "umount $TESTDIR/root"
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin umount $TESTDIR/root
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin}
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagintest_cleanup() {
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin umount $TESTDIR/root 2>/dev/null
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin [[ $LOOPDEV ]] && losetup -d $LOOPDEV
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin return 0
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin}
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagin
5c7290b1956453024fc14abba2385ea9e9bccf8cEvgeny Vereshchagindo_test "$@"