psss / tests / selinux

Forked from tests/selinux 6 years ago
Clone

Blame selinux-policy/ping-and-similar/runtest.sh

8a3f41c
#!/bin/bash
8a3f41c
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
8a3f41c
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8a3f41c
#
8a3f41c
#   runtest.sh of /CoreOS/selinux-policy/Regression/ping-and-similar
8a3f41c
#   Description: the service was running as initrc_t or init_t, now it is confined by SELinux
8a3f41c
#   Author: Milos Malik <mmalik@redhat.com>
8a3f41c
#
8a3f41c
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8a3f41c
#
8a3f41c
#   Copyright (c) 2012 Red Hat, Inc. All rights reserved.
8a3f41c
#
8a3f41c
#   This copyrighted material is made available to anyone wishing
8a3f41c
#   to use, modify, copy, or redistribute it subject to the terms
8a3f41c
#   and conditions of the GNU General Public License version 2.
8a3f41c
#
8a3f41c
#   This program is distributed in the hope that it will be
8a3f41c
#   useful, but WITHOUT ANY WARRANTY; without even the implied
8a3f41c
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8a3f41c
#   PURPOSE. See the GNU General Public License for more details.
8a3f41c
#
8a3f41c
#   You should have received a copy of the GNU General Public
8a3f41c
#   License along with this program; if not, write to the Free
8a3f41c
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
8a3f41c
#   Boston, MA 02110-1301, USA.
8a3f41c
#
8a3f41c
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8a3f41c
8a3f41c
# Include Beaker environment
8a3f41c
. /usr/share/beakerlib/beakerlib.sh || exit 1
8a3f41c
8a3f41c
PACKAGE="selinux-policy"
8a3f41c
ROOT_PASSWORD="redhat"
8a3f41c
FILE_PATH="/bin/ping"
8a3f41c
FILE_CONTEXT="ping_exec_t"
8a3f41c
SERVICE_PACKAGE="iputils"
8a3f41c
PROCESS_NAME="ping"
8a3f41c
PROCESS_CONTEXT="ping_t"
8a3f41c
8a3f41c
rlJournalStart
8a3f41c
    rlPhaseStartSetup
8a3f41c
        rlRun "rlImport 'selinux-policy/common'"
8a3f41c
        rlSESatisfyRequires
8a3f41c
        rlAssertRpm ${PACKAGE}
8a3f41c
        rlAssertRpm ${PACKAGE}-targeted
8a3f41c
        rlAssertRpm ${SERVICE_PACKAGE}
8a3f41c
8a3f41c
        rlFileBackup /etc/shadow
8a3f41c
8a3f41c
        rlSESetEnforce
8a3f41c
        rlSEStatus
8a3f41c
        rlSESetTimestamp
8a3f41c
        sleep 2
8a3f41c
    rlPhaseEnd
8a3f41c
8a3f41c
    if seinfo -c | grep -q icmp_socket ; then
8a3f41c
    rlPhaseStartTest "bz#1552128"
8a3f41c
        rlSEMatchPathCon "${FILE_PATH}" "${FILE_CONTEXT}"
8a3f41c
        if rlIsRHEL 5 ; then
8a3f41c
            SOURCE_TYPE="initrc_t"
8a3f41c
            BOOLEANS="[ ]"
8a3f41c
        elif rlIsRHEL 6 ; then
8a3f41c
            SOURCE_TYPE="initrc_t"
8a3f41c
        else # RHEL-7 etc.
8a3f41c
            SOURCE_TYPE="init_t" # systemd runs the process
8a3f41c
        fi
8a3f41c
        rlSESearchRule "allow ${SOURCE_TYPE} ${FILE_CONTEXT} : file { getattr open read execute } $BOOLEANS"
8a3f41c
        rlSESearchRule "allow ${SOURCE_TYPE} ${PROCESS_CONTEXT} : process { transition } $BOOLEANS"
8a3f41c
        rlSESearchRule "type_transition ${SOURCE_TYPE} ${FILE_CONTEXT} : process ${PROCESS_CONTEXT} $BOOLEANS"
8a3f41c
        rlSESearchRule "allow ping_t ping_t : icmp_socket { create }"
8a3f41c
    rlPhaseEnd
8a3f41c
    fi
8a3f41c
8a3f41c
    if ! rlIsRHEL 5 6 ; then
8a3f41c
    rlPhaseStartTest "bz#1596065"
8a3f41c
        rlSEMatchPathCon "/usr/bin/ping" "ping_exec_t"
8a3f41c
        rlSESearchRule "allow dhcpc_t ping_exec_t : file { map }"
8a3f41c
    rlPhaseEnd
8a3f41c
    fi
8a3f41c
8fb3f91
    if rlIsFedora '>=31' ; then
8fb3f91
    rlPhaseStartTest "bz#1803759 + bz#1810403 + bz#1848929"
8fb3f91
        # when unconfined users run ping, there is no transition to ping_t
8fb3f91
        rlSESearchRule "allow unconfined_t node_t : icmp_socket { node_bind } [ ]"
8fb3f91
        # when confined users run ping, a transition to ping_t happens
8fb3f91
        rlSESearchRule "allow ping_t node_t : icmp_socket { node_bind } [ ]"
8fb3f91
    rlPhaseEnd
8fb3f91
    fi
8fb3f91
8a3f41c
    # this phase is useful if you want to run some commands under confined users
8a3f41c
    rlPhaseStartTest "real scenario -- confined users"
0fdeb90
        rlSEConfigureSSH
0fdeb90
8a3f41c
        rlRun "setsebool ssh_sysadm_login on"
8a3f41c
        rlRun "setsebool user_ping on"
8a3f41c
        for SELINUX_USER in staff_u user_u sysadm_u unconfined_u ; do
8a3f41c
            USER_NAME="user${RANDOM}"
8a3f41c
            USER_SECRET="S3kr3t${RANDOM}"
8a3f41c
            rlRun "useradd -Z ${SELINUX_USER} ${USER_NAME}"
8a3f41c
            rlRun "echo ${USER_SECRET} | passwd --stdin ${USER_NAME}"
8a3f41c
            rlRun "./ssh.exp ${USER_NAME} ${USER_SECRET} localhost ${FILE_PATH} -w 10 127.0.0.1"
8fb3f91
            rlRun "./ssh.exp ${USER_NAME} ${USER_SECRET} localhost ${FILE_PATH} -c1 -W5 -I 0.0.0.0 127.0.0.1"
8a3f41c
            rlRun "userdel -rfZ ${USER_NAME}"
8a3f41c
        done
8a3f41c
        rlRun "setsebool user_ping off"
8a3f41c
        rlRun "setsebool ssh_sysadm_login off"
8a3f41c
    rlPhaseEnd
8a3f41c
8a3f41c
    # this phase is useful if there is a transition from initrc_t to ${PROCESS_CONTEXT} but there is no initscript or systemd unit file
8a3f41c
    rlPhaseStartTest "real scenario -- runcon under root"
8a3f41c
        rlRun "runcon system_u:system_r:initrc_t:s0 /bin/bash -c \"${FILE_PATH} -w 10 127.0.0.1\""
8fb3f91
        rlRun "runcon system_u:system_r:initrc_t:s0 /bin/bash -c \"${FILE_PATH} -c1 -W5 -I 0.0.0.0 127.0.0.1\""
8a3f41c
    rlPhaseEnd
8a3f41c
8a3f41c
    rlPhaseStartCleanup
8a3f41c
        sleep 2
8a3f41c
        rlSECheckAVC
8a3f41c
8a3f41c
        rlFileRestore
0fdeb90
        rlRun "service sshd restart"
8a3f41c
    rlPhaseEnd
8a3f41c
rlJournalPrintText
8a3f41c
rlJournalEnd
8a3f41c