fa6a06
From 96888e99c5103d9dea5230c917b946732de2d302 Mon Sep 17 00:00:00 2001
fa6a06
From: Panu Matilainen <pmatilai@redhat.com>
fa6a06
Date: Thu, 22 Sep 2022 11:54:47 +0300
fa6a06
Subject: [PATCH] Add a handler for libselinux log messages (RhBug:2123719,
fa6a06
 RhBug:2050774)
fa6a06
fa6a06
libselinux logs to stderr by default, which up to now has been just fine
fa6a06
with us. However somewhere around libselinux 3.2 it begun issuing
fa6a06
log messages for events discovered in selinux_status_updated().
fa6a06
We only call that to see whether the status *was* updated behind our
fa6a06
back and are not interested in these audit-style messages for our
fa6a06
functionality, but to suppress them while preserving actually relevant
fa6a06
errors and warnings, we need to have a log callback of our own. Might as
fa6a06
well forward them to rpmlog then.
fa6a06
fa6a06
SELINUX_ERROR and SELINUX_WARNING are pretty obvious, of SELINUX_AVC
fa6a06
selinux_set_callback(3) says it should be treated as SELINUX_ERROR if
fa6a06
not audited. The rest we suppress to debug messages, they may be handy
fa6a06
for diagnostics some day.
fa6a06
fa6a06
Note that this intentionally avoids explicit SELINUX_POLICYLOAD and
fa6a06
SELINUX_SETENFORCE cases in the switch: we don't want to introduce
fa6a06
libselinux >= 3.2 dependency just because of this silly thing.
fa6a06
---
fa6a06
 plugins/selinux.c | 30 ++++++++++++++++++++++++++++++
fa6a06
 1 file changed, 30 insertions(+)
fa6a06
fa6a06
diff --git a/plugins/selinux.c b/plugins/selinux.c
fa6a06
index 747f62d05..0f10331f0 100644
fa6a06
--- a/plugins/selinux.c
fa6a06
+++ b/plugins/selinux.c
fa6a06
@@ -18,6 +18,35 @@ static inline rpmlogLvl loglvl(int iserror)
fa6a06
     return iserror ? RPMLOG_ERR : RPMLOG_DEBUG;
fa6a06
 }
fa6a06
 
fa6a06
+static int logcb(int type, const char *fmt, ...)
fa6a06
+{
fa6a06
+    char *buf = NULL;
fa6a06
+    va_list ap;
fa6a06
+    int lvl;
fa6a06
+
fa6a06
+    switch (type) {
fa6a06
+    case SELINUX_ERROR:
fa6a06
+    case SELINUX_AVC:
fa6a06
+	lvl = RPMLOG_ERR;
fa6a06
+	break;
fa6a06
+    case SELINUX_WARNING:
fa6a06
+	lvl = RPMLOG_WARNING;
fa6a06
+	break;
fa6a06
+    default:
fa6a06
+	lvl = RPMLOG_DEBUG;
fa6a06
+	break;
fa6a06
+    }
fa6a06
+
fa6a06
+    va_start(ap, fmt);
fa6a06
+    rvasprintf(&buf, fmt, ap);
fa6a06
+    va_end(ap);
fa6a06
+
fa6a06
+    rpmlog(lvl, "libselinux: type %d: %s", type, buf);
fa6a06
+    free(buf);
fa6a06
+
fa6a06
+    return 0;
fa6a06
+}
fa6a06
+
fa6a06
 static void sehandle_fini(int close_status)
fa6a06
 {
fa6a06
     if (sehandle) {
fa6a06
@@ -44,6 +73,7 @@ static rpmRC sehandle_init(int open_status)
fa6a06
 	if (selinux_status_open(0) < 0) {
fa6a06
 	    return RPMRC_FAIL;
fa6a06
 	}
fa6a06
+	selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) &logcb);
fa6a06
     } else if (!selinux_status_updated() && sehandle) {
fa6a06
 	return RPMRC_OK;
fa6a06
     }
fa6a06
-- 
fa6a06
2.38.1
fa6a06