2020-05-09 14:15:00 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# Copyright (C) 2020 Christian Boltz <apparmor@cboltz.de>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of version 2 of the GNU General Public
|
|
|
|
# License as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
from collections import namedtuple
|
|
|
|
from common_test import AATest, setup_all_loops
|
|
|
|
|
2022-08-07 20:32:07 -04:00
|
|
|
from apparmor.common import AppArmorBug, AppArmorException
|
|
|
|
from apparmor.rule.abi import AbiRule, AbiRuleset
|
2020-05-09 14:15:00 +02:00
|
|
|
from apparmor.translations import init_translation
|
2022-08-07 20:32:07 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
_ = init_translation()
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
exp = namedtuple(
|
|
|
|
'exp', ( # 'audit', 'allow_keyword', 'deny',
|
2024-05-17 13:53:42 +02:00
|
|
|
'comment', 'path', 'ifexists', 'ismagic'))
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
# --- tests for single AbiRule --- #
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class AbiTest(AATest):
|
|
|
|
def _compare_obj(self, obj, expected):
|
|
|
|
self.assertEqual(False, obj.allow_keyword) # not supported in abi rules, expected to be always False
|
|
|
|
self.assertEqual(False, obj.audit) # not supported in abi rules, expected to be always False
|
|
|
|
self.assertEqual(False, obj.deny) # not supported in abi rules, expected to be always False
|
|
|
|
self.assertEqual(expected.comment, obj.comment)
|
|
|
|
|
|
|
|
self.assertEqual(expected.path, obj.path)
|
|
|
|
self.assertEqual(False, expected.ifexists) # tests bug - should always expect ifexists==False
|
|
|
|
self.assertEqual(False, obj.ifexists) # not supported in abi rules, expected to be always False
|
|
|
|
self.assertEqual(expected.ismagic, obj.ismagic)
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class AbiTestParse(AbiTest):
|
2022-06-18 14:30:49 -04:00
|
|
|
tests = (
|
2022-08-07 12:26:24 -04:00
|
|
|
# AbiRule object comment path if exists ismagic
|
|
|
|
('abi <abstractions/base>,', exp('', 'abstractions/base', False, True)), # magic path
|
|
|
|
('abi <abstractions/base>, # comment', exp(' # comment', 'abstractions/base', False, True)),
|
|
|
|
('abi<abstractions/base>,#comment', exp(' #comment', 'abstractions/base', False, True)),
|
|
|
|
(' abi <abstractions/base> , ', exp('', 'abstractions/base', False, True)),
|
|
|
|
('abi "/foo/bar",', exp('', '/foo/bar', False, False)), # absolute path
|
|
|
|
('abi "/foo/bar", # comment', exp(' # comment', '/foo/bar', False, False)),
|
|
|
|
('abi "/foo/bar",#comment', exp(' #comment', '/foo/bar', False, False)),
|
|
|
|
(' abi "/foo/bar" , ', exp('', '/foo/bar', False, False)),
|
2022-06-18 14:30:49 -04:00
|
|
|
)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
def _run_test(self, rawrule, expected):
|
|
|
|
self.assertTrue(AbiRule.match(rawrule))
|
2022-09-10 19:45:22 -04:00
|
|
|
obj = AbiRule.create_instance(rawrule)
|
2020-05-09 14:15:00 +02:00
|
|
|
self.assertEqual(rawrule.strip(), obj.raw_rule)
|
|
|
|
self._compare_obj(obj, expected)
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class AbiTestParseInvalid(AbiTest):
|
2022-08-07 12:26:24 -04:00
|
|
|
# tests = (
|
|
|
|
# (' some abi <abstractions/base>', AppArmorException),
|
|
|
|
# (' /etc/fstab r,', AppArmorException),
|
|
|
|
# ('/usr/abi r,', AppArmorException),
|
|
|
|
# ('/abi r,', AppArmorException),
|
|
|
|
# )
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
def _run_test(self, rawrule, expected):
|
|
|
|
self.assertTrue(AbiRule.match(rawrule)) # the above invalid rules still match the main regex!
|
|
|
|
with self.assertRaises(expected):
|
2022-09-10 19:45:22 -04:00
|
|
|
AbiRule.create_instance(rawrule)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
# class AbiTestParseFromLog(AbiTest): # we'll never have log events for abi
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class AbiFromInit(AbiTest):
|
2022-06-18 14:30:49 -04:00
|
|
|
tests = (
|
2022-08-07 12:26:24 -04:00
|
|
|
# AbiRule object ifexists ismagic comment path ifexists ismagic
|
|
|
|
(AbiRule('abi/4.19', False, False), exp('', 'abi/4.19', False, False)),
|
|
|
|
(AbiRule('foo', False, False), exp('', 'foo', False, False)),
|
|
|
|
(AbiRule('bar', False, True), exp('', 'bar', False, True)),
|
|
|
|
(AbiRule('comment', False, False, comment='# cmt'), exp('# cmt', 'comment', False, False)),
|
2022-06-18 14:30:49 -04:00
|
|
|
)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
def _run_test(self, obj, expected):
|
|
|
|
self._compare_obj(obj, expected)
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class InvalidAbiInit(AATest):
|
2022-06-18 14:30:49 -04:00
|
|
|
tests = (
|
2022-08-07 12:26:24 -04:00
|
|
|
# init params expected exception
|
|
|
|
((False, False, False), AppArmorBug), # wrong type for path
|
|
|
|
(('', False, False), AppArmorBug), # empty path
|
|
|
|
((None, False, False), AppArmorBug), # wrong type for path
|
|
|
|
# ((' ', False, False), AppArmorBug), # whitespace-only path
|
|
|
|
(('foo', None, False), AppArmorBug), # wrong type for ifexists
|
|
|
|
(('foo', '', False), AppArmorBug), # wrong type for ifexists
|
|
|
|
(('foo', False, None), AppArmorBug), # wrong type for ismagic
|
|
|
|
(('foo', False, ''), AppArmorBug), # wrong type for ismagic
|
|
|
|
(('', True, False), AppArmorBug), # ifexists set
|
2022-06-18 14:30:49 -04:00
|
|
|
)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
def _run_test(self, params, expected):
|
|
|
|
with self.assertRaises(expected):
|
2022-06-18 14:30:49 -04:00
|
|
|
AbiRule(*params)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
def test_missing_params_1(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
AbiRule()
|
|
|
|
|
|
|
|
def test_missing_params_2(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
AbiRule('foo')
|
|
|
|
|
|
|
|
def test_missing_params_3(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
AbiRule('foo', False)
|
|
|
|
|
|
|
|
def test_audit_true(self):
|
|
|
|
with self.assertRaises(AppArmorBug):
|
|
|
|
AbiRule('foo', False, False, audit=True)
|
|
|
|
|
|
|
|
def test_deny_true(self):
|
|
|
|
with self.assertRaises(AppArmorBug):
|
|
|
|
AbiRule('foo', False, False, deny=True)
|
|
|
|
|
|
|
|
def test_ifexists_true(self):
|
|
|
|
with self.assertRaises(AppArmorBug):
|
|
|
|
AbiRule('foo', True, False)
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class InvalidAbiTest(AATest):
|
2022-08-07 12:26:24 -04:00
|
|
|
def _check_invalid_rawrule(self, rawrule, matches_regex=False):
|
2020-05-09 14:15:00 +02:00
|
|
|
obj = None
|
|
|
|
self.assertEqual(AbiRule.match(rawrule), matches_regex)
|
|
|
|
with self.assertRaises(AppArmorException):
|
2022-09-10 19:45:22 -04:00
|
|
|
obj = AbiRule.create_instance(rawrule)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
self.assertIsNone(obj, 'AbiRule handed back an object unexpectedly')
|
|
|
|
|
|
|
|
def test_invalid_abi_missing_path(self):
|
|
|
|
self._check_invalid_rawrule('abi ,', matches_regex=True) # missing path
|
|
|
|
|
|
|
|
def test_invalid_non_AbiRule(self):
|
|
|
|
self._check_invalid_rawrule('dbus,') # not a abi rule
|
|
|
|
|
|
|
|
# def test_empty_data_1(self):
|
|
|
|
# obj = AbiRule('foo', False, False)
|
|
|
|
# obj.path = ''
|
|
|
|
# # no path set
|
|
|
|
# with self.assertRaises(AppArmorBug):
|
|
|
|
# obj.get_clean(1)
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class WriteAbiTestAATest(AATest):
|
|
|
|
def _run_test(self, rawrule, expected):
|
|
|
|
self.assertTrue(AbiRule.match(rawrule))
|
2022-09-10 19:45:22 -04:00
|
|
|
obj = AbiRule.create_instance(rawrule)
|
2020-05-09 14:15:00 +02:00
|
|
|
clean = obj.get_clean()
|
|
|
|
raw = obj.get_raw()
|
|
|
|
|
|
|
|
self.assertEqual(expected.strip(), clean, 'unexpected clean rule')
|
|
|
|
self.assertEqual(rawrule.strip(), raw, 'unexpected raw rule')
|
|
|
|
|
2022-06-18 14:30:49 -04:00
|
|
|
tests = (
|
2022-08-07 12:26:24 -04:00
|
|
|
# raw rule clean rule
|
|
|
|
(' abi <foo> , ', 'abi <foo>,'),
|
|
|
|
(' abi foo , ', 'abi "foo",'),
|
|
|
|
(' abi "foo" , ', 'abi "foo",'),
|
|
|
|
(' abi /foo , ', 'abi "/foo",'),
|
|
|
|
(' abi "/foo" , ', 'abi "/foo",'),
|
|
|
|
|
|
|
|
(' abi <foo>, # bar ', 'abi <foo>, # bar'),
|
|
|
|
(' abi foo , # bar ', 'abi "foo", # bar'),
|
|
|
|
(' abi "foo", # bar ', 'abi "foo", # bar'),
|
|
|
|
(' abi /foo, # bar ', 'abi "/foo", # bar'),
|
|
|
|
(' abi "/foo", # bar ', 'abi "/foo", # bar'),
|
2022-06-18 14:30:49 -04:00
|
|
|
)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
def test_write_manually(self):
|
|
|
|
obj = AbiRule('abs/foo', False, True, comment=' # cmt')
|
|
|
|
|
|
|
|
expected = ' abi <abs/foo>, # cmt'
|
|
|
|
|
|
|
|
self.assertEqual(expected, obj.get_clean(2), 'unexpected clean rule')
|
|
|
|
self.assertEqual(expected, obj.get_raw(2), 'unexpected raw rule')
|
|
|
|
|
|
|
|
|
|
|
|
class AbiCoveredTest(AATest):
|
|
|
|
def _run_test(self, param, expected):
|
2022-09-10 19:45:22 -04:00
|
|
|
obj = AbiRule.create_instance(self.rule)
|
|
|
|
check_obj = AbiRule.create_instance(param)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
self.assertTrue(AbiRule.match(param))
|
|
|
|
|
2023-02-19 16:26:14 -05:00
|
|
|
self.assertEqual(obj.is_equal(check_obj), expected[0], 'Mismatch in is_equal, expected {}'.format(expected[0]))
|
|
|
|
self.assertEqual(obj.is_equal(check_obj, True), expected[1], 'Mismatch in is_equal/strict, expected {}'.format(expected[1]))
|
2020-05-09 14:15:00 +02:00
|
|
|
|
2023-02-19 16:26:14 -05:00
|
|
|
self.assertEqual(obj.is_covered(check_obj), expected[2], 'Mismatch in is_covered, expected {}'.format(expected[2]))
|
|
|
|
self.assertEqual(obj.is_covered(check_obj, True, True), expected[3], 'Mismatch in is_covered/exact, expected {}'.format(expected[3]))
|
2020-05-09 14:15:00 +02:00
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class AbiCoveredTest_01(AbiCoveredTest):
|
|
|
|
rule = 'abi <foo>,'
|
|
|
|
|
2022-06-18 14:30:49 -04:00
|
|
|
tests = (
|
2022-08-07 12:26:24 -04:00
|
|
|
# rule equal strict equal covered covered exact
|
|
|
|
('abi <foo>,', (True, True, True, True)),
|
|
|
|
('abi "foo",', (False, False, False, False)),
|
|
|
|
('abi <foobar>,', (False, False, False, False)),
|
|
|
|
('abi "foo",', (False, False, False, False)),
|
2022-06-18 14:30:49 -04:00
|
|
|
)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class AbiCoveredTest_02(AbiCoveredTest):
|
|
|
|
rule = 'abi "foo",'
|
|
|
|
|
2022-06-18 14:30:49 -04:00
|
|
|
tests = (
|
2022-08-07 12:26:24 -04:00
|
|
|
# rule equal strict equal covered covered exact
|
|
|
|
('abi <foo>,', (False, False, False, False)),
|
|
|
|
('abi "foo",', (True, True, True, True)),
|
|
|
|
('abi "foobar",', (False, False, False, False)),
|
|
|
|
('abi foo,', (True, False, True, True)),
|
2022-06-18 14:30:49 -04:00
|
|
|
)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
# class AbiCoveredTest_Invalid(AATest):
|
|
|
|
# def test_borked_obj_is_covered_1(self):
|
2022-09-10 19:45:22 -04:00
|
|
|
# obj = AbiRule.create_instance('abi <foo>')
|
2022-08-07 12:26:24 -04:00
|
|
|
#
|
|
|
|
# testobj = AbiRule('foo', True, True)
|
|
|
|
# testobj.path = ''
|
|
|
|
#
|
|
|
|
# with self.assertRaises(AppArmorBug):
|
|
|
|
# obj.is_covered(testobj)
|
|
|
|
#
|
|
|
|
# def test_borked_obj_is_covered_2(self):
|
2022-09-10 19:45:22 -04:00
|
|
|
# obj = AbiRule.create_instance('abi send set=quit peer=/foo,')
|
2022-08-07 12:26:24 -04:00
|
|
|
#
|
|
|
|
# testobj = AbiRule('send', 'quit', '/foo')
|
|
|
|
# testobj.abi = ''
|
|
|
|
#
|
|
|
|
# with self.assertRaises(AppArmorBug):
|
|
|
|
# obj.is_covered(testobj)
|
|
|
|
#
|
|
|
|
# def test_borked_obj_is_covered_3(self):
|
2022-09-10 19:45:22 -04:00
|
|
|
# obj = AbiRule.create_instance('abi send set=quit peer=/foo,')
|
2022-08-07 12:26:24 -04:00
|
|
|
#
|
|
|
|
# testobj = AbiRule('send', 'quit', '/foo')
|
|
|
|
# testobj.peer = ''
|
|
|
|
#
|
|
|
|
# with self.assertRaises(AppArmorBug):
|
|
|
|
# obj.is_covered(testobj)
|
|
|
|
#
|
|
|
|
# def test_invalid_is_covered(self):
|
2022-09-27 22:14:31 -04:00
|
|
|
# raw_rule = 'abi send,'
|
|
|
|
# class SomeOtherClass(AbiRule):
|
|
|
|
# pass
|
2022-08-07 12:26:24 -04:00
|
|
|
#
|
2022-09-27 22:14:31 -04:00
|
|
|
# obj = AbiRule.create_instance(raw_rule)
|
|
|
|
# testobj = SomeOtherClass.create_instance(raw_rule) # different type
|
2022-08-07 12:26:24 -04:00
|
|
|
# with self.assertRaises(AppArmorBug):
|
|
|
|
# obj.is_covered(testobj)
|
|
|
|
#
|
|
|
|
# def test_invalid_is_equal(self):
|
2022-09-27 22:14:31 -04:00
|
|
|
# raw_rule = 'abi send,'
|
|
|
|
# class SomeOtherClass(AbiRule):
|
|
|
|
# pass
|
2022-08-07 12:26:24 -04:00
|
|
|
#
|
2022-09-27 22:14:31 -04:00
|
|
|
# obj = AbiRule.create_instance(raw_rule)
|
|
|
|
# testobj = SomeOtherClass.create_instance(raw_rule) # different type
|
2022-08-07 12:26:24 -04:00
|
|
|
# with self.assertRaises(AppArmorBug):
|
|
|
|
# obj.is_equal(testobj)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AbiLogprofHeaderTest(AATest):
|
2022-06-18 14:30:49 -04:00
|
|
|
tests = (
|
2022-08-07 12:26:24 -04:00
|
|
|
('abi <abi/3.0>,', [_('Abi'), 'abi <abi/3.0>,']),
|
|
|
|
('abi "/foo/bar",', [_('Abi'), 'abi "/foo/bar",']),
|
2022-06-18 14:30:49 -04:00
|
|
|
)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
def _run_test(self, params, expected):
|
2022-09-10 19:45:22 -04:00
|
|
|
obj = AbiRule.create_instance(params)
|
2020-05-09 14:15:00 +02:00
|
|
|
self.assertEqual(obj.logprof_header(), expected)
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2024-05-17 13:53:42 +02:00
|
|
|
# --- tests for AbiRuleset --- #
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
class AbiRulesTest(AATest):
|
|
|
|
def test_empty_ruleset(self):
|
|
|
|
ruleset = AbiRuleset()
|
|
|
|
ruleset_2 = AbiRuleset()
|
|
|
|
self.assertEqual([], ruleset.get_raw(2))
|
|
|
|
self.assertEqual([], ruleset.get_clean(2))
|
|
|
|
self.assertEqual([], ruleset_2.get_raw(2))
|
|
|
|
self.assertEqual([], ruleset_2.get_clean(2))
|
|
|
|
self.assertEqual([], ruleset_2.get_clean_unsorted(2))
|
|
|
|
|
|
|
|
def test_ruleset_1(self):
|
|
|
|
ruleset = AbiRuleset()
|
2022-06-18 14:30:49 -04:00
|
|
|
rules = (
|
2020-05-09 14:15:00 +02:00
|
|
|
' abi <foo> ,',
|
|
|
|
' abi "/bar", ',
|
2022-06-18 14:30:49 -04:00
|
|
|
)
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
expected_raw = [
|
|
|
|
'abi <foo> ,',
|
|
|
|
'abi "/bar",',
|
|
|
|
'',
|
|
|
|
]
|
|
|
|
|
|
|
|
expected_clean = [
|
|
|
|
'abi "/bar",',
|
|
|
|
'abi <foo>,',
|
|
|
|
'',
|
|
|
|
]
|
|
|
|
|
|
|
|
expected_clean_unsorted = [
|
|
|
|
'abi <foo>,',
|
|
|
|
'abi "/bar",',
|
|
|
|
'',
|
|
|
|
]
|
|
|
|
|
|
|
|
for rule in rules:
|
2022-09-10 19:45:22 -04:00
|
|
|
ruleset.add(AbiRule.create_instance(rule))
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
self.assertEqual(expected_raw, ruleset.get_raw())
|
|
|
|
self.assertEqual(expected_clean, ruleset.get_clean())
|
|
|
|
self.assertEqual(expected_clean_unsorted, ruleset.get_clean_unsorted())
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
class AbiGlobTestAATest(AATest):
|
|
|
|
def setUp(self):
|
|
|
|
self.maxDiff = None
|
|
|
|
self.ruleset = AbiRuleset()
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
# def test_glob(self):
|
|
|
|
# with self.assertRaises(NotImplementedError):
|
|
|
|
# # get_glob_ext is not available for include rules
|
|
|
|
# self.ruleset.get_glob('include send set=int,')
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
def test_glob_ext(self):
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
# get_glob_ext is not available for include rules
|
|
|
|
self.ruleset.get_glob_ext('include send set=int,')
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
|
|
|
# class AbiDeleteTestAATest(AATest):
|
|
|
|
# pass
|
|
|
|
|
2020-05-09 14:15:00 +02:00
|
|
|
|
|
|
|
setup_all_loops(__name__)
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=1)
|