Skip to contentSkip to conversation

python/semanage: do not leak an audit fd per logger instance

PATCH In review Stephen Smalley 1 participant 2 messages Jun 16 – Jun 17 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v4 1/1 python/semanage: do not leak an audit fd per logger instance

semanageRecords.__init__() constructs a logger() for every instance, and semanage import constructs a fresh object for every line of the import file. With the audit-backed logger class, each logger() opens its own audit netlink socket via audit.audit_open() and never closes it, so a large export/import round-trip leaks one fd per line and can hit RLIMIT_NOFILE.

Open the audit socket once and store it as a class attribute, the same way semanageRecords already caches the semanage handle.

---
v4: it helps to actually commit the change before sending.

 python/semanage/seobject.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
python/semanage/seobject.py +4 -1
index 9ef99be3..5914ef7e 100644
@@ -106,9 +106,12 @@ try:
     audit.audit_close(audit.audit_open())
 
     class logger:
+        audit_fd = None
 
         def __init__(self):
-            self.audit_fd = audit.audit_open()
+            if logger.audit_fd is None:
+                logger.audit_fd = audit.audit_open()
+
             self.log_list = []
             self.log_change_list = []
 
Signature · 1 line
2.54.0
author maintainer
#1 lore raw
Quoted 8 lines · Stephen Smalley

semanageRecords.__init__() constructs a logger() for every instance, and semanage import constructs a fresh object for every line of the import file. With the audit-backed logger class, each logger() opens its own audit netlink socket via audit.audit_open() and never closes it, so a large export/import round-trip leaks one fd per line and can hit RLIMIT_NOFILE.

Quoted 5 lines

Open the audit socket once and store it as a class attribute, the same way semanageRecords already caches the semanage handle.

Merged.

Quoted 27 lines

---
v4: it helps to actually commit the change before sending.

 python/semanage/seobject.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
python/semanage/seobject.py +4 -2
index 9ef99be3..5914ef7e 100644
@@ -106,9 +106,12 @@ try:
     audit.audit_close(audit.audit_open())
 
     class logger:
+        audit_fd = None
 
         def __init__(self):
-            self.audit_fd = audit.audit_open()
+            if logger.audit_fd is None:
+                logger.audit_fd = audit.audit_open()
+
             self.log_list = []
             self.log_change_list = []
 
--

2.54.0