|
|
95ab14 |
From 4011007b445e8f8da9b0cc45eccd793b94f6b5ce Mon Sep 17 00:00:00 2001
|
|
|
95ab14 |
From: Sergio Correia <scorreia@redhat.com>
|
|
|
95ab14 |
Date: Thu, 29 Jul 2021 19:25:43 -0300
|
|
|
95ab14 |
Subject: [PATCH] Add ausysrulevalidate
|
|
|
95ab14 |
|
|
|
95ab14 |
---
|
|
|
95ab14 |
contrib/ausysrulevalidate | 198 ++++++++++++++++++++++++++++++++++++++
|
|
|
95ab14 |
1 file changed, 198 insertions(+)
|
|
|
95ab14 |
create mode 100755 contrib/ausysrulevalidate
|
|
|
95ab14 |
|
|
|
95ab14 |
diff --git a/contrib/ausysrulevalidate b/contrib/ausysrulevalidate
|
|
|
95ab14 |
new file mode 100755
|
|
|
95ab14 |
index 0000000..a251b2c
|
|
|
95ab14 |
--- /dev/null
|
|
|
95ab14 |
+++ b/contrib/ausysrulevalidate
|
|
|
95ab14 |
@@ -0,0 +1,198 @@
|
|
|
95ab14 |
+#!/usr/bin/env python3
|
|
|
95ab14 |
+# -*- coding: utf-8 -*-
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+# ausysrulevalidate - A program that lets you validate the syscalls
|
|
|
95ab14 |
+# in audit rules.
|
|
|
95ab14 |
+# Copyright (c) 2021 Red Hat Inc., Durham, North Carolina.
|
|
|
95ab14 |
+# All Rights Reserved.
|
|
|
95ab14 |
+#
|
|
|
95ab14 |
+# This software may be freely redistributed and/or modified under the
|
|
|
95ab14 |
+# terms of the GNU General Public License as published by the Free
|
|
|
95ab14 |
+# Software Foundation; either version 2, or (at your option) any
|
|
|
95ab14 |
+# later version.
|
|
|
95ab14 |
+#
|
|
|
95ab14 |
+# This program is distributed in the hope that it will be useful,
|
|
|
95ab14 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
95ab14 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
95ab14 |
+# GNU General Public License for more details.
|
|
|
95ab14 |
+#
|
|
|
95ab14 |
+# You should have received a copy of the GNU General Public License
|
|
|
95ab14 |
+# along with this program; see the file COPYING. If not, write to the
|
|
|
95ab14 |
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
|
|
|
95ab14 |
+# Boston, MA 02110-1335, USA.
|
|
|
95ab14 |
+#
|
|
|
95ab14 |
+# Authors:
|
|
|
95ab14 |
+# Sergio Correia <scorreia@redhat.com>
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+""" This program lets you validate syscalls in audit rules. """
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+import argparse
|
|
|
95ab14 |
+import os.path
|
|
|
95ab14 |
+import sys
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+import audit
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+class AuSyscallRuleValidate:
|
|
|
95ab14 |
+ """AuSyscallRuleValidate validates syscalls in audit rules."""
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ def __init__(self):
|
|
|
95ab14 |
+ self.syscalls_table = {}
|
|
|
95ab14 |
+ self.invalid_syscalls = {}
|
|
|
95ab14 |
+ self.machines = {
|
|
|
95ab14 |
+ "b32": audit.audit_determine_machine("b32"),
|
|
|
95ab14 |
+ "b64": audit.audit_determine_machine("b64"),
|
|
|
95ab14 |
+ }
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if self.machines["b32"] == -1 or self.machines["b64"] == -1:
|
|
|
95ab14 |
+ sys.stderr.write("ERROR: Unable to determine machine type\n")
|
|
|
95ab14 |
+ sys.exit(1)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ def validate_syscall(self, arch, syscall):
|
|
|
95ab14 |
+ """Validates a single syscall."""
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if syscall == "all":
|
|
|
95ab14 |
+ return True
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ lookup = "{0}:{1}".format(arch, syscall)
|
|
|
95ab14 |
+ if lookup in self.syscalls_table:
|
|
|
95ab14 |
+ return self.syscalls_table[lookup]
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ ret = audit.audit_name_to_syscall(syscall, self.machines[arch])
|
|
|
95ab14 |
+ self.syscalls_table[lookup] = ret != -1
|
|
|
95ab14 |
+ if not self.syscalls_table[lookup]:
|
|
|
95ab14 |
+ self.invalid_syscalls[lookup] = lookup
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ return self.syscalls_table[lookup]
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ def process_syscalls(self, arch, syscalls):
|
|
|
95ab14 |
+ """Processes a group of syscalls, validating them individually."""
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ scalls = syscalls.split(",")
|
|
|
95ab14 |
+ processed = []
|
|
|
95ab14 |
+ for syscall in scalls:
|
|
|
95ab14 |
+ if self.validate_syscall(arch, syscall):
|
|
|
95ab14 |
+ processed.append(syscall)
|
|
|
95ab14 |
+ return ",".join(processed)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ def parse_line(self, line):
|
|
|
95ab14 |
+ """Processes a single line from the audit rules file, and returns the
|
|
|
95ab14 |
+ same line adjusted, if required, by removing invalid syscalls, or even
|
|
|
95ab14 |
+ removing the rule altogether, if no valid syscall remain after
|
|
|
95ab14 |
+ validation."""
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if line.lstrip().startswith("#") or "-S" not in line:
|
|
|
95ab14 |
+ return line
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ # We do have a rule specifying syscalls, so let's validate them.
|
|
|
95ab14 |
+ tokens = line.split()
|
|
|
95ab14 |
+ processed = []
|
|
|
95ab14 |
+ is_syscall = False
|
|
|
95ab14 |
+ arch = None
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ for val in tokens:
|
|
|
95ab14 |
+ if not is_syscall:
|
|
|
95ab14 |
+ processed.append(val)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if val.startswith("arch="):
|
|
|
95ab14 |
+ archs = val.split("=")
|
|
|
95ab14 |
+ if len(archs) == 2:
|
|
|
95ab14 |
+ arch = val.split("=")[1]
|
|
|
95ab14 |
+ if arch not in self.machines:
|
|
|
95ab14 |
+ sys.stderr.write("ERROR: unexpected arch '{0}'\n".format(arch))
|
|
|
95ab14 |
+ continue
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if val == "-S":
|
|
|
95ab14 |
+ is_syscall = True
|
|
|
95ab14 |
+ continue
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if is_syscall:
|
|
|
95ab14 |
+ is_syscall = False
|
|
|
95ab14 |
+ scalls = self.process_syscalls(arch, val)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if len(scalls) == 0:
|
|
|
95ab14 |
+ processed = processed[:-1]
|
|
|
95ab14 |
+ continue
|
|
|
95ab14 |
+ processed.append(scalls)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if "-S" not in processed:
|
|
|
95ab14 |
+ # Removing rule altogether, as we have no valid syscalls remaining.
|
|
|
95ab14 |
+ return None
|
|
|
95ab14 |
+ return " ".join(processed)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ def process_rules(self, rules_file):
|
|
|
95ab14 |
+ """Reads a file with audit rules and returns the rules after
|
|
|
95ab14 |
+ validation of syscalls/architecture. Invalid syscalls will be removed
|
|
|
95ab14 |
+ and, if there are no valid remaining syscalls, the rule itself is
|
|
|
95ab14 |
+ removed."""
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if not os.path.isfile(rules_file):
|
|
|
95ab14 |
+ sys.stderr.write("ERROR: rules file '{0}' not found\n".format(rules_file))
|
|
|
95ab14 |
+ sys.exit(1)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ with open(rules_file) as rules:
|
|
|
95ab14 |
+ content = rules.readlines()
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ processed = []
|
|
|
95ab14 |
+ changed = False
|
|
|
95ab14 |
+ for line in content:
|
|
|
95ab14 |
+ validated = self.parse_line(line)
|
|
|
95ab14 |
+ if validated is None:
|
|
|
95ab14 |
+ changed = True
|
|
|
95ab14 |
+ continue
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if validated.rstrip("\r\n") != line.rstrip("\r\n"):
|
|
|
95ab14 |
+ changed = True
|
|
|
95ab14 |
+ processed.append(validated.rstrip("\r\n"))
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ invalid_syscalls = []
|
|
|
95ab14 |
+ for invalid in self.invalid_syscalls:
|
|
|
95ab14 |
+ invalid_syscalls.append(invalid)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ return (processed, changed, invalid_syscalls)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ def update_rules(self, rules_file):
|
|
|
95ab14 |
+ """Reads a file with audit rules and updates it after validation of
|
|
|
95ab14 |
+ syscalls/architecture. Invalid syscalls will be removed and, if
|
|
|
95ab14 |
+ there are no valid remaining syscalls, the rule itself is removed."""
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ new_rules, changed, invalid_syscalls = self.process_rules(rules_file)
|
|
|
95ab14 |
+ if changed:
|
|
|
95ab14 |
+ with open(rules_file, "w") as rules:
|
|
|
95ab14 |
+ for line in new_rules:
|
|
|
95ab14 |
+ rules.write("{0}\n".format(line))
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ return (new_rules, changed, invalid_syscalls)
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+if __name__ == "__main__":
|
|
|
95ab14 |
+ parser = argparse.ArgumentParser(description="ausysrulevalidate")
|
|
|
95ab14 |
+ parser.add_argument(
|
|
|
95ab14 |
+ "-u", "--update", help="Update rules file if required", action="store_true"
|
|
|
95ab14 |
+ )
|
|
|
95ab14 |
+ parser.add_argument(
|
|
|
95ab14 |
+ "-v", "--verbose", help="Show the resulting rules file", action="store_true"
|
|
|
95ab14 |
+ )
|
|
|
95ab14 |
+ required_named = parser.add_argument_group("required named arguments")
|
|
|
95ab14 |
+ required_named.add_argument(
|
|
|
95ab14 |
+ "-r", "--rules-file", help="Rules file name", required=True
|
|
|
95ab14 |
+ )
|
|
|
95ab14 |
+ args = parser.parse_args()
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ validator = AuSyscallRuleValidate()
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ action = validator.process_rules
|
|
|
95ab14 |
+ if args.update:
|
|
|
95ab14 |
+ action = validator.update_rules
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ data, changed, invalid = action(args.rules_file)
|
|
|
95ab14 |
+ if changed:
|
|
|
95ab14 |
+ verb = "require"
|
|
|
95ab14 |
+ if args.update:
|
|
|
95ab14 |
+ verb += "d"
|
|
|
95ab14 |
+ sys.stderr.write("Rules in '{0}' {1} changes\n".format(args.rules_file, verb))
|
|
|
95ab14 |
+ if len(invalid) > 0:
|
|
|
95ab14 |
+ sys.stderr.write("Invalid syscalls: {0}\n".format(", ".join(invalid)))
|
|
|
95ab14 |
+
|
|
|
95ab14 |
+ if args.verbose:
|
|
|
95ab14 |
+ print(*data, sep="\n")
|
|
|
95ab14 |
--
|
|
|
95ab14 |
2.31.1
|
|
|
95ab14 |
|