#!/bin/bash
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#
#   Copyright (c) 2018 Red Hat, Inc.
#
#   This program is free software: you can redistribute it and/or
#   modify it under the terms of the GNU General Public License as
#   published by the Free Software Foundation, either version 2 of
#   the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#   PURPOSE.  See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see http://www.gnu.org/licenses/.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1

PACKAGE="glibc"
TESTPROG1="exploit"
TESTPROG2="exploit2"
rlJournalStart
    rlPhaseStartSetup
        rlAssertRpm $PACKAGE
        rlRun -l "gcc --version"
        rlRun "TESTTMPDIR=$(mktemp -d)"
        rlRun -c "cp ${TESTPROG1}.c  ${TESTPROG2}.c ${TESTTMPDIR}/"
        rlRun "pushd $TESTTMPDIR"
    rlPhaseEnd

    for T in $TESTPROG1 $TESTPROG2
    do

        rlPhaseStartTest $T
            rlRun -c "gcc ${T}.c -o ${T}"
            RS=`./${T}`
            rlLog "Return code: $?"
            if [ "$RS" == 'FAIL' ]
            then
                Log "$T exploit wasn't prevented: FAIL"
                rlFail $T FAIL
            else
                rlLog "$T exploit was prevented: PASS"
                rlPass $T PASS
            fi
        rlPhaseEnd
    done


    rlPhaseStartCleanup
        rlRun "popd"
        rlRun "rm -r $TESTTMPDIR"
    rlPhaseEnd
rlJournalPrintText
rlJournalEnd
