Blame SOURCES/rsyslog-8.2102.0-rhbz2127404-libcap-ng.patch

1fd806
diff -up rsyslog-8.2102.0/configure.ac.orig rsyslog-8.2102.0/configure.ac
1fd806
--- rsyslog-8.2102.0/configure.ac.orig	2022-11-21 11:39:40.717183684 +0100
1fd806
+++ rsyslog-8.2102.0/configure.ac	2022-11-21 11:40:18.697206706 +0100
1fd806
@@ -387,6 +387,28 @@ if test "$enable_fmhash_xxhash" = "yes";
1fd806
 	])
1fd806
 fi
1fd806
 
1fd806
+AC_ARG_ENABLE(libcap-ng,
1fd806
+        [AS_HELP_STRING([--enable-libcap-ng],[Enable dropping capabilities to only the necessary set @<:@default=no@:>@])],
1fd806
+        [case "${enableval}" in
1fd806
+         yes) enable_libcapng="yes" ;;
1fd806
+          no) enable_libcapng="no" ;;
1fd806
+           *) AC_MSG_ERROR(bad value ${enableval} for --enable_libcapng) ;;
1fd806
+         esac],
1fd806
+        [enable_libcapng=no]
1fd806
+)
1fd806
+
1fd806
+if test "$enable_libcapng" = "yes"; then
1fd806
+        PKG_CHECK_MODULES(
1fd806
+                [LIBCAPNG],
1fd806
+                [libcap-ng >= 0.8.2],
1fd806
+                [AC_DEFINE([ENABLE_LIBCAPNG], [1], [Indicator that libcap-ng is present])],
1fd806
+                [AC_MSG_ERROR(libcap-ng is not present.)]
1fd806
+        )
1fd806
+        CFLAGS="$CFLAGS $LIBCAPNG_CFLAGS"
1fd806
+        LIBS="$LIBS $LIBCAPNG_LIBS"
1fd806
+fi
1fd806
+
1fd806
+
1fd806
 
1fd806
 #gssapi
1fd806
 AC_ARG_ENABLE(gssapi_krb5,
1fd806
@@ -2688,6 +2710,7 @@ echo "    liblogging-stdlog support enab
1fd806
 echo "    libsystemd enabled:                       $enable_libsystemd"
1fd806
 echo "    kafka static linking enabled:             $enable_kafka_static"
1fd806
 echo "    atomic operations enabled:                $enable_atomic_operations"
1fd806
+echo "    libcap-ng support enabled:                $enable_libcapng"
1fd806
 echo
1fd806
 echo "---{ input plugins }---"
1fd806
 if test "$unamestr" != "AIX"; then
1fd806
diff -up rsyslog-8.2102.0/runtime/rsconf.c.orig rsyslog-8.2102.0/runtime/rsconf.c
1fd806
--- rsyslog-8.2102.0/runtime/rsconf.c.orig	2022-11-21 11:40:31.926214720 +0100
1fd806
+++ rsyslog-8.2102.0/runtime/rsconf.c	2022-11-21 11:44:26.742356979 +0100
1fd806
@@ -33,6 +33,9 @@
1fd806
 #include <sys/resource.h>
1fd806
 #include <sys/types.h>
1fd806
 #include <sys/stat.h>
1fd806
+#ifdef ENABLE_LIBCAPNG
1fd806
+	#include <cap-ng.h>
1fd806
+#endif
1fd806
 
1fd806
 #include "rsyslog.h"
1fd806
 #include "obj.h"
1fd806
@@ -546,6 +549,7 @@ rsRetVal doDropPrivGid(void)
1fd806
 	uchar szBuf[1024];
1fd806
 	DEFiRet;
1fd806
 
1fd806
+#ifndef ENABLE_LIBCAPNG
1fd806
 	if(!ourConf->globals.gidDropPrivKeepSupplemental) {
1fd806
 		res = setgroups(0, NULL); /* remove all supplemental group IDs */
1fd806
 		if(res) {
1fd806
@@ -560,9 +564,19 @@ rsRetVal doDropPrivGid(void)
1fd806
 	if(res) {
1fd806
 		rs_strerror_r(errno, (char*)szBuf, sizeof(szBuf));
1fd806
 		LogError(0, RS_RET_ERR_DROP_PRIV,
1fd806
-				"could not set requested group id: %s", szBuf);
1fd806
+				"could not set requested group id: %s via setgid()", szBuf);
1fd806
 		ABORT_FINALIZE(RS_RET_ERR_DROP_PRIV);
1fd806
 	}
1fd806
+#else
1fd806
+	int capng_flags = ourConf->globals.gidDropPrivKeepSupplemental ? CAPNG_NO_FLAG : CAPNG_DROP_SUPP_GRP;
1fd806
+	res = capng_change_id(-1, ourConf->globals.gidDropPriv, capng_flags);
1fd806
+	if (res) {
1fd806
+		LogError(0, RS_RET_LIBCAPNG_ERR,
1fd806
+				"could not set requested group id %d via capng_change_id()", ourConf->globals.gidDropPriv);
1fd806
+		ABORT_FINALIZE(RS_RET_LIBCAPNG_ERR);
1fd806
+	}
1fd806
+#endif
1fd806
+
1fd806
 	DBGPRINTF("setgid(%d): %d\n", ourConf->globals.gidDropPriv, res);
1fd806
 	snprintf((char*)szBuf, sizeof(szBuf), "rsyslogd's groupid changed to %d",
1fd806
 		 ourConf->globals.gidDropPriv);
1fd806
@@ -599,7 +613,14 @@ static void doDropPrivUid(int iUid)
1fd806
 				iUid, szBuf);
1fd806
 	}
1fd806
 
1fd806
+#ifndef ENABLE_LIBCAPNG
1fd806
 	res = setuid(iUid);
1fd806
+	// res = setuid(cnf->globals.uidDropPriv);
1fd806
+#else
1fd806
+	int capng_flags = ourConf->globals.gidDropPrivKeepSupplemental ? CAPNG_NO_FLAG : CAPNG_DROP_SUPP_GRP;
1fd806
+	res = capng_change_id(iUid, -1, capng_flags);
1fd806
+#endif
1fd806
+
1fd806
 	if(res) {
1fd806
 		/* if we can not set the userid, this is fatal, so let's unconditionally abort */
1fd806
 		perror("could not set requested userid");
1fd806
diff -up rsyslog-8.2102.0/runtime/rsyslog.h.orig rsyslog-8.2102.0/runtime/rsyslog.h
1fd806
--- rsyslog-8.2102.0/runtime/rsyslog.h.orig	2022-11-21 11:45:09.007382588 +0100
1fd806
+++ rsyslog-8.2102.0/runtime/rsyslog.h	2022-11-21 11:45:31.333396112 +0100
1fd806
@@ -582,6 +582,7 @@ enum rsRetVal_				/** return value. All
1fd806
 	RS_RET_RABBITMQ_CHANNEL_ERR = -2449, /**< RabbitMQ Connection error */
1fd806
 	RS_RET_NO_WRKDIR_SET = -2450, /**< working directory not set, but desired by functionality */
1fd806
 	RS_RET_ERR_QUEUE_FN_DUP = -2451, /**< duplicate queue file name */
1fd806
+	RS_RET_LIBCAPNG_ERR = -2455, /**< error during dropping the capabilities */
1fd806
 
1fd806
 	/* RainerScript error messages (range 1000.. 1999) */
1fd806
 	RS_RET_SYSVAR_NOT_FOUND = 1001, /**< system variable could not be found (maybe misspelled) */
1fd806
diff -up rsyslog-8.2102.0/tools/rsyslogd.c.orig rsyslog-8.2102.0/tools/rsyslogd.c
1fd806
--- rsyslog-8.2102.0/tools/rsyslogd.c.orig	2022-11-21 11:45:17.587387786 +0100
1fd806
+++ rsyslog-8.2102.0/tools/rsyslogd.c	2022-11-21 11:46:19.509425295 +0100
1fd806
@@ -38,6 +38,10 @@
1fd806
 #	include <systemd/sd-daemon.h>
1fd806
 #endif
1fd806
 
1fd806
+#ifdef ENABLE_LIBCAPNG
1fd806
+	#include <cap-ng.h>
1fd806
+#endif
1fd806
+
1fd806
 #include "rsyslog.h"
1fd806
 #include "wti.h"
1fd806
 #include "ratelimit.h"
1fd806
@@ -321,7 +325,7 @@ checkStartupOK(void)
1fd806
 		fprintf(stderr, "rsyslogd: error reading pid file, cannot start up\n");
1fd806
 		ABORT_FINALIZE(RS_RET_ERR);
1fd806
 	}
1fd806
-	
1fd806
+
1fd806
 	/* ok, we got a pid, let's check if the process is running */
1fd806
 	const pid_t pid = (pid_t) pf_pid;
1fd806
 	if(kill(pid, 0) == 0 || errno != ESRCH) {
1fd806
@@ -1594,7 +1598,7 @@ initAll(int argc, char **argv)
1fd806
 		localRet = RS_RET_OK;
1fd806
 	}
1fd806
 	CHKiRet(localRet);
1fd806
-	
1fd806
+
1fd806
 	CHKiRet(rsyslogd_InitStdRatelimiters());
1fd806
 
1fd806
 	if(bChDirRoot) {
1fd806
@@ -2019,7 +2023,7 @@ deinitAll(void)
1fd806
 	/* close the inputs */
1fd806
 	DBGPRINTF("Terminating input threads...\n");
1fd806
 	glbl.SetGlobalInputTermination();
1fd806
-	
1fd806
+
1fd806
 	thrdTerminateAll();
1fd806
 
1fd806
 	/* and THEN send the termination log message (see long comment above) */
1fd806
@@ -2142,6 +2146,45 @@ main(int argc, char **argv)
1fd806
 	if(log_dflt != NULL && !strcmp(log_dflt, "1"))
1fd806
 		bProcessInternalMessages = 1;
1fd806
 	dbgClassInit();
1fd806
+
1fd806
+#ifdef ENABLE_LIBCAPNG
1fd806
+	/*
1fd806
+	 * Drop capabilities to the necessary set
1fd806
+	 */
1fd806
+	int capng_rc;
1fd806
+	capng_clear(CAPNG_SELECT_BOTH);
1fd806
+
1fd806
+	if ((capng_rc = capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
1fd806
+		CAP_BLOCK_SUSPEND,
1fd806
+		CAP_CHOWN,
1fd806
+		CAP_IPC_LOCK,
1fd806
+		CAP_LEASE,
1fd806
+		CAP_NET_ADMIN,
1fd806
+		CAP_NET_BIND_SERVICE,
1fd806
+		CAP_PERFMON,
1fd806
+		CAP_SETGID,
1fd806
+		CAP_SETUID,
1fd806
+		CAP_SYS_ADMIN,
1fd806
+		CAP_SYS_CHROOT,
1fd806
+		CAP_SYS_RESOURCE,
1fd806
+		CAP_SYSLOG,
1fd806
+		-1
1fd806
+	)) != 0) {
1fd806
+		LogError(0, RS_RET_LIBCAPNG_ERR,
1fd806
+				"could not update the internal posix capabilities settings "
1fd806
+				"based on the options passed to it, capng_updatev=%d\n", capng_rc);
1fd806
+		exit(-1);
1fd806
+	}
1fd806
+
1fd806
+	if ((capng_rc = capng_apply(CAPNG_SELECT_BOTH)) != 0) {
1fd806
+		LogError(0, RS_RET_LIBCAPNG_ERR,
1fd806
+			"could not transfer  the  specified  internal posix  capabilities "
1fd806
+			"settings to the kernel, capng_apply=%d\n", capng_rc);
1fd806
+		exit(-1);
1fd806
+	}
1fd806
+	DBGPRINTF("Capabilities were dropped successfully\n");
1fd806
+#endif
1fd806
+
1fd806
 	initAll(argc, argv);
1fd806
 #ifdef HAVE_LIBSYSTEMD
1fd806
 	sd_notify(0, "READY=1");