xperms support in audit2allow
cover xperms support in audit2allow
Hi,
this patchset is the result of my bachelor's thesis. First two patches fix bugs in sepolgen and are not related to the third patch. The third patch provides xperms support to audit2allow.
Jan Zarsky (3):
python/sepolgen: print all AV rules correctly python/sepolgen: fix access vector initialization
python: add xperms support to audit2allow
python/audit2allow/audit2allow | 6 + python/audit2allow/audit2allow.1 | 3 + python/audit2allow/test.log | 1 + python/audit2allow/test_audit2allow.py | 9 ++ python/sepolgen/src/sepolgen/access.py | 54 +++++---- python/sepolgen/src/sepolgen/audit.py | 28 +++-- python/sepolgen/src/sepolgen/policygen.py | 125 +++++++++++-------- python/sepolgen/src/sepolgen/refpolicy.py | 125 ++++++++++++++++++- python/sepolgen/tests/test_access.py | 129 ++++++++++++++++++++ python/sepolgen/tests/test_audit.py | 50 ++++++++ python/sepolgen/tests/test_policygen.py | 112 ++++++++++++++++- python/sepolgen/tests/test_refpolicy.py | 139 ++++++++++++++++++++++ 12 files changed, 700 insertions(+), 81 deletions(-)
PATCH 1/3 python/sepolgen: print all AV rules correctly
In refpolicy module, in AVRule.__rule_type_str() method, self.rule_type was not checked for value self.NEVERALLOW so that string "neverallow" was never returned. Fix this by checking all four possible values and returning correct strings.
--- python/sepolgen/src/sepolgen/refpolicy.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
python/sepolgen/src/sepolgen/refpolicy.py +3 -1
@@ -472,8 +472,10 @@ class AVRule(Leaf): return "allow" elif self.rule_type == self.DONTAUDIT: return "dontaudit" - else: + elif self.rule_type == self.AUDITALLOW: return "auditallow" + elif self.rule_type == self.NEVERALLOW: + return "neverallow" def from_av(self, av): """Add the access from an access vector to this allow
Signature · 1 line
2.14.4
PATCH 2/3 python/sepolgen: fix access vector initialization
In reply to Jan ZarskyIn access module, in AccessVector.__init__() method, when init_list is not None, the access vector is initialized by from_list() method. However, this method does not assign attributes self.audit_msgs, self.type, and self.data. Fix this by assigning these attributes in __init__() method.
--- python/sepolgen/src/sepolgen/access.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
python/sepolgen/src/sepolgen/access.py +4 -3
@@ -87,9 +87,10 @@ class AccessVector(util.Comparison): self.tgt_type = None self.obj_class = None self.perms = refpolicy.IdSet() - self.audit_msgs = [] - self.type = audit2why.TERULE - self.data = [] + + self.audit_msgs = [] + self.type = audit2why.TERULE + self.data = [] # when implementing __eq__ also __hash__ is needed on py2 # if object is muttable __hash__ should be None self.__hash__ = None
Signature · 1 line
2.14.4
PATCH 3/3 python: add xperms support to audit2allow
In reply to Jan ZarskyAdd support for extended permissions to audit2allow. Extend AuditParser to parse the 'ioctlcmd' field in AVC message. Extend PolicyGenerator to generate allowxperm rules. Add the '-x'/'--xperms' option to audit2allow to turn on generating of extended permission AV rules.
AVCMessage parses the ioctlcmd field in AVC messages. AuditParser converts the ioctlcmd values into generic representation of extended permissions that is stored in access vectors.
Extended permissions are represented by operations (currently only 'ioctl') and values associated to the operations. Values (for example '~{ 0x42 1234 23-34 }') are stored in the XpermSet class.
PolicyGenerator contains new method to turn on generating of xperms. When turned on, for each access vector, standard AV rule and possibly several xperm AV rules are generated. Xperm AV rules are represented by the AVExtRule class.
With xperm generating turned off, PolicyGenerator provides comments about extended permissions in certain situations. When the AVC message contains the ioctlcmd field and the access would be allowed according to the policy, PolicyGenerator warns about xperm rules being the possible cause of the denial.
--- python/audit2allow/audit2allow | 6 ++ python/audit2allow/audit2allow.1 | 3 + python/audit2allow/test.log | 1 + python/audit2allow/test_audit2allow.py | 9 ++ python/sepolgen/src/sepolgen/access.py | 47 ++++++---- python/sepolgen/src/sepolgen/audit.py | 28 ++++-- python/sepolgen/src/sepolgen/policygen.py | 125 ++++++++++++++++----------- python/sepolgen/src/sepolgen/refpolicy.py | 121 ++++++++++++++++++++++++++ python/sepolgen/tests/test_access.py | 129 +++++++++++++++++++++++++++ python/sepolgen/tests/test_audit.py | 50 +++++++++++ python/sepolgen/tests/test_policygen.py | 112 +++++++++++++++++++++++- python/sepolgen/tests/test_refpolicy.py | 139 ++++++++++++++++++++++++++++++ 12 files changed, 693 insertions(+), 77 deletions(-)
python/audit2allow/audit2allow +6 -0
@@ -86,6 +86,8 @@ class AuditToPolicy: dest="type") parser.add_option("--perm-map", dest="perm_map", help="file name of perm map") parser.add_option("--interface-info", dest="interface_info", help="file name of interface information") + parser.add_option("-x", "--xperms", action="store_true", dest="xperms", + default=False, help="generate extended permission rules") parser.add_option("--debug", dest="debug", action="store_true", default=False, help="leave generated modules for -M") parser.add_option("-w", "--why", dest="audit2why", action="store_true", default=(os.path.basename(sys.argv[0]) == "audit2why"),
@@ -314,6 +316,10 @@ class AuditToPolicy: ifs, perm_maps = self.__load_interface_info() g.set_gen_refpol(ifs, perm_maps) + # Extended permissions + if self.__options.xperms: + g.set_gen_xperms(True) + # Explanation if self.__options.verbose: g.set_gen_explain(policygen.SHORT_EXPLANATION)
python/audit2allow/audit2allow.1 +3 -0
@@ -85,6 +85,9 @@ This is the default behavior. Generate reference policy using installed macros. This attempts to match denials against interfaces and may be inaccurate. .TP +.B "\-x" | "\-\-xperms" +Generate extended permission access vector rules +.TP .B "\-w" | "\-\-why" Translates SELinux audit messages into a description of why the access was denied
python/audit2allow/test.log +1 -0
@@ -34,3 +34,4 @@ node=mary.example.com type=AVC msg=audit(1166023021.373:910): avc: denied { re node=lilly.example.com type=AVC_PATH msg=audit(1164783469.561:109): path="/linuxtest/LVT/lvt/log.current" node=lilly.example.com type=SYSCALL msg=audit(1164783469.561:109): arch=14 syscall=11 success=yes exit=0 a0=10120520 a1=10120a78 a2=10120970 a3=118 items=0 ppid=8310 pid=8311 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) comm="smbd" exe="/usr/sbin/smbd" subj=root:system_r:smbd_t:s0 key=(null) node=lilly.example.com type=AVC msg=audit(1164783469.561:109): avc: denied { append } for pid=8311 comm="smbd" name="log.current" dev=dm-0 ino=130930 scontext=root:system_r:smbd_t:s0 tcontext=root:object_r:default_t:s0 tclass=dir +node=lilly.example.com type=AVC msg=audit(1164783469.561:109): avc: denied { ioctl } for pid=8311 comm="smbd" name="log.current" ioctlcmd=0x2a scontext=root:system_r:smbd_t:s0 tcontext=root:object_r:default_t:s0 tclass=tcp_socket
python/audit2allow/test_audit2allow.py +9 -0
@@ -47,5 +47,14 @@ class Audit2allowTests(unittest.TestCase): print(out, err) self.assertSuccess("audit2why", p.returncode, err) + def test_xperms(self): + "Verify that xperms generation works" + p = Popen(['python', './audit2allow', "-x", "-i", "test.log"], stdout=PIPE) + out, err = p.communicate() + if err: + print(out, err) + self.assertTrue(b"allowxperm" in out) + self.assertSuccess("xperms", p.returncode, err) + if __name__ == "__main__": unittest.main()
python/sepolgen/src/sepolgen/access.py +29 -18
75 lines not on this page · raw message
python/sepolgen/src/sepolgen/audit.py +21 -7
@@ -152,6 +152,7 @@ class AVCMessage(AuditMessage): access - list of accesses that were allowed or denied denial - boolean indicating whether this was a denial (True) or granted (False) message. + ioctlcmd - ioctl 'request' parameter An example audit message generated from the audit daemon looks like (line breaks added):
@@ -178,6 +179,7 @@ class AVCMessage(AuditMessage): self.name = "" self.accesses = [] self.denial = True + self.ioctlcmd = None self.type = audit2why.TERULE def __parse_access(self, recs, start):
@@ -237,6 +239,11 @@ class AVCMessage(AuditMessage): self.exe = fields[1][1:-1] elif fields[0] == "name": self.name = fields[1][1:-1] + elif fields[0] == "ioctlcmd": + try: + self.ioctlcmd = int(fields[1], 16) + except: + pass if not found_src or not found_tgt or not found_class or not found_access: raise ValueError("AVC message in invalid format [%s]\n" % self.message)
@@ -522,13 +529,20 @@ class AuditParser: for avc in self.avc_msgs: if avc.denial != True and only_denials: continue - if avc_filter: - if avc_filter.filter(avc): - av_set.add(avc.scontext.type, avc.tcontext.type, avc.tclass, - avc.accesses, avc, avc_type=avc.type, data=avc.data) - else: - av_set.add(avc.scontext.type, avc.tcontext.type, avc.tclass, - avc.accesses, avc, avc_type=avc.type, data=avc.data) + + if not avc_filter or avc_filter.filter(avc): + av = access.AccessVector([avc.scontext.type, avc.tcontext.type, + avc.tclass] + avc.accesses) + av.data = avc.data + av.type = avc.type + + if avc.ioctlcmd: + xperm_set = refpolicy.XpermSet() + xperm_set.add(avc.ioctlcmd) + av.xperms["ioctl"] = xperm_set + + av_set.add_av(av, audit_msg=avc) + return av_set class AVCTypeFilter:
python/sepolgen/src/sepolgen/policygen.py +77 -48
157 lines not on this page · raw message
python/sepolgen/src/sepolgen/refpolicy.py +121 -0
139 lines not on this page · raw message
python/sepolgen/tests/test_access.py +129 -0
151 lines not on this page · raw message
python/sepolgen/tests/test_audit.py +50 -0
74 lines not on this page · raw message
python/sepolgen/tests/test_policygen.py +108 -4
121 lines not on this page · raw message
python/sepolgen/tests/test_refpolicy.py +139 -0
157 lines not on this page · raw message
Signature · 1 line
2.14.4
Re: [PATCH 3/3] python: add xperms support to audit2allow
2018-06-05 16:34 GMT+02:00 Jan Zarsky <[email protected]>:
Quoted 18 lines
Add support for extended permissions to audit2allow. Extend AuditParser to parse the 'ioctlcmd' field in AVC message. Extend PolicyGenerator to generate allowxperm rules. Add the '-x'/'--xperms' option to audit2allow to turn on generating of extended permission AV rules.
AVCMessage parses the ioctlcmd field in AVC messages. AuditParser converts the ioctlcmd values into generic representation of extended permissions that is stored in access vectors.
Extended permissions are represented by operations (currently only 'ioctl') and values associated to the operations. Values (for example '~{
0x421234 23-34 }') are stored in the XpermSet class.PolicyGenerator contains new method to turn on generating of xperms. When turned on, for each access vector, standard AV rule and possibly several xperm AV rules are generated. Xperm AV rules are represented by the AVExtRule class.
Quoted 7 lines
With xperm generating turned off, PolicyGenerator provides comments about extended permissions in certain situations. When the AVC message contains the ioctlcmd field and the access would be allowed according to the policy, PolicyGenerator warns about xperm rules being the possible cause of the denial.
Signed-off-by Jan Zarsky <[email protected]>
Hello,
Thanks for these patches. The first two look good to me and I will merge them if nobody else has a comment on them. On the third one, it is really nice it introduces more tests :) I have successfully run them.
On the code itself, please find some comments below (one Python syntax and two minor whitespace issues).
python/sepolgen/src/sepolgen/audit.py class AVCMessage(AuditMessage): · line 246
Quoted 13 lines
python/sepolgen/src/sepolgen/audit.py +5
-0@@ -237,6 +239,11 @@ class AVCMessage(AuditMessage): self.exe = fields[1][1:-1] elif fields[0] == "name": self.name = fields[1][1:-1] + elif fields[0] == "ioctlcmd": + try: + self.ioctlcmd = int(fields[1], 16) + except: + pass
Please do not introduce new bare "except". It also intercepts KeyboardInterrupt which may cause issues when the command takes too much time. It is quite difficult to change the existing ones in the code because there may be some tricky exceptions which are legitimately caught, but here, a "except ValueError" is better.
Quoted 4 lines
python/sepolgen/tests/test_access.py +0
-0
[...]
python/sepolgen/tests/test_access.py class TestAccessVectorSet(unittest.TestCase): · line 391
Quoted 5 lines
python/sepolgen/tests/test_access.py +5
-0+ s.add_av = test_add_av + + s.add("foo", "bar", "file", refpolicy.IdSet(["read"]), + audit_msg='test message', avc_type=42, data='test data') +
There is a stray end of line here. My git reports:
<stdin>:159: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
Quoted 4 lines
python/sepolgen/tests/test_refpolicy.py +0
-0
[...]
python/sepolgen/tests/test_refpolicy.py class TestAVRule(unittest.TestCase): · line 193
Quoted 12 lines
python/sepolgen/tests/test_refpolicy.py +12
-0+class TestAVExtRule(unittest.TestCase): + def test_init(self): + """ Test initialization of attributes """ + a = refpolicy.AVExtRule() + self.assertEqual(a.rule_type, a.ALLOWXPERM) + self.assertIsInstance(a.src_types, set) + self.assertIsInstance(a.tgt_types, set) + self.assertIsInstance(a.obj_classes, set) + self.assertIsNone(a.operation) + self.assertIsInstance(a.xperms, refpolicy.XpermSet) + + def test_rule_type_str(self):
There is a empty line with whitespaces right before this function. My git reports:
<stdin>:102: trailing whitespace.
warning: 1 line adds whitespace errors.
If you send a new patch which fixes these comments, I will gladly merge it.
Thanks!
Nicolas
PATCH v2 3/3 python: add xperms support to audit2allow
Add support for extended permissions to audit2allow. Extend AuditParser to parse the 'ioctlcmd' field in AVC message. Extend PolicyGenerator to generate allowxperm rules. Add the '-x'/'--xperms' option to audit2allow to turn on generating of extended permission AV rules.
AVCMessage parses the ioctlcmd field in AVC messages. AuditParser converts the ioctlcmd values into generic representation of extended permissions that is stored in access vectors.
Extended permissions are represented by operations (currently only 'ioctl') and values associated to the operations. Values (for example '~{ 0x42 1234 23-34 }') are stored in the XpermSet class.
PolicyGenerator contains new method to turn on generating of xperms. When turned on, for each access vector, standard AV rule and possibly several xperm AV rules are generated. Xperm AV rules are represented by the AVExtRule class.
With xperm generating turned off, PolicyGenerator provides comments about extended permissions in certain situations. When the AVC message contains the ioctlcmd field and the access would be allowed according to the policy, PolicyGenerator warns about xperm rules being the possible cause of the denial.
---
V2 fixes two whitespace issues, in audit.py uses 'except ValueError' instead of bare except, and fixes typo in error message in policygen.py
python/audit2allow/audit2allow | 6 ++ python/audit2allow/audit2allow.1 | 3 + python/audit2allow/test.log | 1 + python/audit2allow/test_audit2allow.py | 9 ++ python/sepolgen/src/sepolgen/access.py | 47 ++++++---- python/sepolgen/src/sepolgen/audit.py | 28 ++++-- python/sepolgen/src/sepolgen/policygen.py | 125 ++++++++++++++++----------- python/sepolgen/src/sepolgen/refpolicy.py | 121 ++++++++++++++++++++++++++ python/sepolgen/tests/test_access.py | 128 +++++++++++++++++++++++++++ python/sepolgen/tests/test_audit.py | 50 +++++++++++ python/sepolgen/tests/test_policygen.py | 112 +++++++++++++++++++++++- python/sepolgen/tests/test_refpolicy.py | 139 ++++++++++++++++++++++++++++++ 12 files changed, 692 insertions(+), 77 deletions(-)
python/audit2allow/audit2allow +6 -0
@@ -86,6 +86,8 @@ class AuditToPolicy: dest="type") parser.add_option("--perm-map", dest="perm_map", help="file name of perm map") parser.add_option("--interface-info", dest="interface_info", help="file name of interface information") + parser.add_option("-x", "--xperms", action="store_true", dest="xperms", + default=False, help="generate extended permission rules") parser.add_option("--debug", dest="debug", action="store_true", default=False, help="leave generated modules for -M") parser.add_option("-w", "--why", dest="audit2why", action="store_true", default=(os.path.basename(sys.argv[0]) == "audit2why"),
@@ -314,6 +316,10 @@ class AuditToPolicy: ifs, perm_maps = self.__load_interface_info() g.set_gen_refpol(ifs, perm_maps) + # Extended permissions + if self.__options.xperms: + g.set_gen_xperms(True) + # Explanation if self.__options.verbose: g.set_gen_explain(policygen.SHORT_EXPLANATION)
python/audit2allow/audit2allow.1 +3 -0
@@ -85,6 +85,9 @@ This is the default behavior. Generate reference policy using installed macros. This attempts to match denials against interfaces and may be inaccurate. .TP +.B "\-x" | "\-\-xperms" +Generate extended permission access vector rules +.TP .B "\-w" | "\-\-why" Translates SELinux audit messages into a description of why the access was denied
python/audit2allow/test.log +1 -0
@@ -34,3 +34,4 @@ node=mary.example.com type=AVC msg=audit(1166023021.373:910): avc: denied { re node=lilly.example.com type=AVC_PATH msg=audit(1164783469.561:109): path="/linuxtest/LVT/lvt/log.current" node=lilly.example.com type=SYSCALL msg=audit(1164783469.561:109): arch=14 syscall=11 success=yes exit=0 a0=10120520 a1=10120a78 a2=10120970 a3=118 items=0 ppid=8310 pid=8311 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) comm="smbd" exe="/usr/sbin/smbd" subj=root:system_r:smbd_t:s0 key=(null) node=lilly.example.com type=AVC msg=audit(1164783469.561:109): avc: denied { append } for pid=8311 comm="smbd" name="log.current" dev=dm-0 ino=130930 scontext=root:system_r:smbd_t:s0 tcontext=root:object_r:default_t:s0 tclass=dir +node=lilly.example.com type=AVC msg=audit(1164783469.561:109): avc: denied { ioctl } for pid=8311 comm="smbd" name="log.current" ioctlcmd=0x2a scontext=root:system_r:smbd_t:s0 tcontext=root:object_r:default_t:s0 tclass=tcp_socket
python/audit2allow/test_audit2allow.py +9 -0
@@ -47,5 +47,14 @@ class Audit2allowTests(unittest.TestCase): print(out, err) self.assertSuccess("audit2why", p.returncode, err) + def test_xperms(self): + "Verify that xperms generation works" + p = Popen(['python', './audit2allow', "-x", "-i", "test.log"], stdout=PIPE) + out, err = p.communicate() + if err: + print(out, err) + self.assertTrue(b"allowxperm" in out) + self.assertSuccess("xperms", p.returncode, err) + if __name__ == "__main__": unittest.main()
python/sepolgen/src/sepolgen/access.py +29 -18
75 lines not on this page · raw message
python/sepolgen/src/sepolgen/audit.py +21 -7
@@ -152,6 +152,7 @@ class AVCMessage(AuditMessage): access - list of accesses that were allowed or denied denial - boolean indicating whether this was a denial (True) or granted (False) message. + ioctlcmd - ioctl 'request' parameter An example audit message generated from the audit daemon looks like (line breaks added):
@@ -178,6 +179,7 @@ class AVCMessage(AuditMessage): self.name = "" self.accesses = [] self.denial = True + self.ioctlcmd = None self.type = audit2why.TERULE def __parse_access(self, recs, start):
@@ -237,6 +239,11 @@ class AVCMessage(AuditMessage): self.exe = fields[1][1:-1] elif fields[0] == "name": self.name = fields[1][1:-1] + elif fields[0] == "ioctlcmd": + try: + self.ioctlcmd = int(fields[1], 16) + except ValueError: + pass if not found_src or not found_tgt or not found_class or not found_access: raise ValueError("AVC message in invalid format [%s]\n" % self.message)
@@ -522,13 +529,20 @@ class AuditParser: for avc in self.avc_msgs: if avc.denial != True and only_denials: continue - if avc_filter: - if avc_filter.filter(avc): - av_set.add(avc.scontext.type, avc.tcontext.type, avc.tclass, - avc.accesses, avc, avc_type=avc.type, data=avc.data) - else: - av_set.add(avc.scontext.type, avc.tcontext.type, avc.tclass, - avc.accesses, avc, avc_type=avc.type, data=avc.data) + + if not avc_filter or avc_filter.filter(avc): + av = access.AccessVector([avc.scontext.type, avc.tcontext.type, + avc.tclass] + avc.accesses) + av.data = avc.data + av.type = avc.type + + if avc.ioctlcmd: + xperm_set = refpolicy.XpermSet() + xperm_set.add(avc.ioctlcmd) + av.xperms["ioctl"] = xperm_set + + av_set.add_av(av, audit_msg=avc) + return av_set class AVCTypeFilter:
python/sepolgen/src/sepolgen/policygen.py +77 -48
157 lines not on this page · raw message
python/sepolgen/src/sepolgen/refpolicy.py +121 -0
139 lines not on this page · raw message
python/sepolgen/tests/test_access.py +128 -0
150 lines not on this page · raw message
python/sepolgen/tests/test_audit.py +50 -0
74 lines not on this page · raw message
python/sepolgen/tests/test_policygen.py +108 -4
121 lines not on this page · raw message
python/sepolgen/tests/test_refpolicy.py +139 -0
157 lines not on this page · raw message
Signature · 1 line
2.14.4
Re: [PATCH V2 3/3] python: add xperms support to audit2allow
2018-06-12 11:06 GMT+02:00 Jan Zarsky <[email protected]>:
Quoted 24 lines
Add support for extended permissions to audit2allow. Extend AuditParser to parse the 'ioctlcmd' field in AVC message. Extend PolicyGenerator to generate allowxperm rules. Add the '-x'/'--xperms' option to audit2allow to turn on generating of extended permission AV rules.
AVCMessage parses the ioctlcmd field in AVC messages. AuditParser converts the ioctlcmd values into generic representation of extended permissions that is stored in access vectors.
Extended permissions are represented by operations (currently only 'ioctl') and values associated to the operations. Values (for example '~{
0x421234 23-34 }') are stored in the XpermSet class.PolicyGenerator contains new method to turn on generating of xperms. When turned on, for each access vector, standard AV rule and possibly several xperm AV rules are generated. Xperm AV rules are represented by the AVExtRule class.
With xperm generating turned off, PolicyGenerator provides comments about extended permissions in certain situations. When the AVC message contains the ioctlcmd field and the access would be allowed according to the policy, PolicyGenerator warns about xperm rules being the possible cause of the denial.
Quoted 5 lines
Signed-off-by Jan Zarsky <[email protected]>---
V2 fixes two whitespace issues, in
audit.pyuses 'except ValueError' instead of bare except, and fixes typo in error message inpolicygen.py
Thanks. I have applied your three patches.
Nicolas