summaryrefslogtreecommitdiffstats
path: root/recipes-mac/smack/smack-test
diff options
context:
space:
mode:
authorArmin Kuster <akuster808@gmail.com>2019-05-06 11:36:58 -0700
committerArmin Kuster <akuster808@gmail.com>2019-05-09 17:45:13 -0700
commit8eee8727cb09a9fc14e899b4058fcd108f44a0eb (patch)
treee6cd90e7b08c098e425a8d0efbb29fd88fc9ee2e /recipes-mac/smack/smack-test
parent5d37937f2e495147fd2a756d22c09f49773ac8ae (diff)
downloadmeta-security-8eee8727cb09a9fc14e899b4058fcd108f44a0eb.tar.gz
smack-test: add smack tests from meta-intel-iot-security
ported over smack tests Signed-off-by: Armin Kuster <akuster808@gmail.com>
Diffstat (limited to 'recipes-mac/smack/smack-test')
-rw-r--r--recipes-mac/smack/smack-test/notroot.py33
-rw-r--r--recipes-mac/smack/smack-test/smack_test_file_access.sh54
-rw-r--r--recipes-mac/smack/smack-test/test_privileged_change_self_label.sh18
-rw-r--r--recipes-mac/smack/smack-test/test_smack_onlycap.sh27
4 files changed, 132 insertions, 0 deletions
diff --git a/recipes-mac/smack/smack-test/notroot.py b/recipes-mac/smack/smack-test/notroot.py
new file mode 100644
index 0000000..f0eb0b5
--- /dev/null
+++ b/recipes-mac/smack/smack-test/notroot.py
@@ -0,0 +1,33 @@
1#!/usr/bin/env python
2#
3# Script used for running executables with custom labels, as well as custom uid/gid
4# Process label is changed by writing to /proc/self/attr/curent
5#
6# Script expects user id and group id to exist, and be the same.
7#
8# From adduser manual:
9# """By default, each user in Debian GNU/Linux is given a corresponding group
10# with the same name. """
11#
12# Usage: root@desk:~# python notroot.py <uid> <label> <full_path_to_executable> [arguments ..]
13# eg: python notroot.py 1000 User::Label /bin/ping -c 3 192.168.1.1
14#
15# Author: Alexandru Cornea <alexandru.cornea@intel.com>
16import os
17import sys
18
19try:
20 uid = int(sys.argv[1])
21 sys.argv.pop(1)
22 label = sys.argv[1]
23 sys.argv.pop(1)
24 open("/proc/self/attr/current", "w").write(label)
25 path=sys.argv[1]
26 sys.argv.pop(0)
27 os.setgid(uid)
28 os.setuid(uid)
29 os.execv(path,sys.argv)
30
31except Exception,e:
32 print e.message
33 sys.exit(1)
diff --git a/recipes-mac/smack/smack-test/smack_test_file_access.sh b/recipes-mac/smack/smack-test/smack_test_file_access.sh
new file mode 100644
index 0000000..5a0ce84
--- /dev/null
+++ b/recipes-mac/smack/smack-test/smack_test_file_access.sh
@@ -0,0 +1,54 @@
1#!/bin/sh
2
3SMACK_PATH=`grep smack /proc/mounts | awk '{print $2}' `
4RC=0
5TMP="/tmp"
6test_file=$TMP/smack_test_access_file
7CAT=`which cat`
8ECHO=`which echo`
9uid=1000
10initial_label=`cat /proc/self/attr/current`
11python $TMP/notroot.py $uid "TheOther" $ECHO 'TEST' > $test_file
12chsmack -a "TheOther" $test_file
13
14# 12345678901234567890123456789012345678901234567890123456
15delrule="TheOne TheOther -----"
16rule_ro="TheOne TheOther r----"
17
18# Remove pre-existent rules for "TheOne TheOther <access>"
19echo -n "$delrule" > $SMACK_PATH/load
20python $TMP/notroot.py $uid "TheOne" $CAT $test_file 2>&1 1>/dev/null | grep -q "Permission denied" || RC=$?
21if [ $RC -ne 0 ]; then
22 echo "Process with different label than the test file and no read access on it can read it"
23 exit $RC
24fi
25
26# adding read access
27echo -n "$rule_ro" > $SMACK_PATH/load
28python $TMP/notroot.py $uid "TheOne" $CAT $test_file | grep -q "TEST" || RC=$?
29if [ $RC -ne 0 ]; then
30 echo "Process with different label than the test file but with read access on it cannot read it"
31 exit $RC
32fi
33
34# Remove pre-existent rules for "TheOne TheOther <access>"
35echo -n "$delrule" > $SMACK_PATH/load
36# changing label of test file to *
37# according to SMACK documentation, read access on a * object is always permitted
38chsmack -a '*' $test_file
39python $TMP/notroot.py $uid "TheOne" $CAT $test_file | grep -q "TEST" || RC=$?
40if [ $RC -ne 0 ]; then
41 echo "Process cannot read file with * label"
42 exit $RC
43fi
44
45# changing subject label to *
46# according to SMACK documentation, every access requested by a star labeled subject is rejected
47TOUCH=`which touch`
48python $TMP/notroot.py $uid '*' $TOUCH $TMP/test_file_2
49ls -la $TMP/test_file_2 2>&1 | grep -q 'No such file or directory' || RC=$?
50if [ $RC -ne 0 ];then
51 echo "Process with label '*' should not have any access"
52 exit $RC
53fi
54exit 0
diff --git a/recipes-mac/smack/smack-test/test_privileged_change_self_label.sh b/recipes-mac/smack/smack-test/test_privileged_change_self_label.sh
new file mode 100644
index 0000000..26d9e9d
--- /dev/null
+++ b/recipes-mac/smack/smack-test/test_privileged_change_self_label.sh
@@ -0,0 +1,18 @@
1#!/bin/sh
2
3initial_label=`cat /proc/self/attr/current 2>/dev/null`
4modified_label="test_label"
5
6echo "$modified_label" >/proc/self/attr/current 2>/dev/null
7
8new_label=`cat /proc/self/attr/current 2>/dev/null`
9
10if [ "$new_label" != "$modified_label" ]; then
11 # restore proper label
12 echo $initial_label >/proc/self/attr/current
13 echo "Privileged process could not change its label"
14 exit 1
15fi
16
17echo "$initial_label" >/proc/self/attr/current 2>/dev/null
18exit 0 \ No newline at end of file
diff --git a/recipes-mac/smack/smack-test/test_smack_onlycap.sh b/recipes-mac/smack/smack-test/test_smack_onlycap.sh
new file mode 100644
index 0000000..1c4a93a
--- /dev/null
+++ b/recipes-mac/smack/smack-test/test_smack_onlycap.sh
@@ -0,0 +1,27 @@
1#!/bin/sh
2RC=0
3SMACK_PATH=`grep smack /proc/mounts | awk '{print $2}'`
4test_label="test_label"
5onlycap_initial=`cat $SMACK_PATH/onlycap`
6smack_initial=`cat /proc/self/attr/current`
7
8# need to set out label to be the same as onlycap, otherwise we lose our smack privileges
9# even if we are root
10echo "$test_label" > /proc/self/attr/current
11
12echo "$test_label" > $SMACK_PATH/onlycap || RC=$?
13if [ $RC -ne 0 ]; then
14 echo "Onlycap label could not be set"
15 return $RC
16fi
17
18if [ `cat $SMACK_PATH/onlycap` != "$test_label" ]; then
19 echo "Onlycap label was not set correctly."
20 return 1
21fi
22
23# resetting original onlycap label
24echo "$onlycap_initial" > $SMACK_PATH/onlycap 2>/dev/null
25
26# resetting our initial's process label
27echo "$smack_initial" > /proc/self/attr/current