python/semanage: do not leak an audit fd per logger instance
Stephen Smalley
maintainerPATCH 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
@@ -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
Stephen Smalley
maintainerQuoted 8 lines · Stephen Smalley
semanageRecords.__init__()constructs alogger()for every instance, and semanage import constructs a fresh object for every line of the import file. With the audit-backed logger class, eachlogger()opens its own audit netlink socket viaaudit.audit_open()and never closes it, so a large export/import round-trip leaks one fd per line and can hitRLIMIT_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@@ -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