References: CVE-2026-16526
Description: libpcp, libpcp_pmda: set FD_CLOEXEC on AF_UNIX sockets (CWE-403)
 The __pmInitSocket() function returns early for AF_UNIX sockets,
 skipping all subsequent socket hardening including FD_CLOEXEC.
 This causes the pmdaroot Unix socket fd to be inherited by child
 processes spawned via popen()/fork(), enabling privilege escalation
 when combined with the linux_sockets command injection (vuln 3):
 an attacker's popen() child inherits the pmdaroot fd and can send
 a PDUROOT_STARTPMDA_REQ to execute commands as root.
 .
 Fix:
 - Set FD_CLOEXEC on AF_UNIX sockets in __pmInitSocket() before the
   early return, matching the behavior TCP sockets get via
   __pmConnectRestoreFlags()
 - Set FD_CLOEXEC on pmdarootfd in pmdaRootConnect() after connect()
   succeeds, as belt-and-suspenders for this critical fd
 - Add qa/src/check_cloexec.c and qa/2104 verifying FD_CLOEXEC is set
   on sockets created by __pmCreateUnixSocket()
 .
 Note: SO_PEERCRED peer credential verification on the pmdaroot server
 side is a separate hardening measure to be addressed as a follow-up.
 .
 Reported-by: Francisco Alisson Bezerra, TIM Security Red Team
 Reported-by: Lucas Gabriel Alves, TIM Security Red Team
 Reported-by: Massimiliano Brolli, TIM Security Red Team
 .
 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
 .
Author: Nathan Scott <nathans@redhat.com>
Origin: upstream, https://github.com/performancecopilot/pcp/commit/7e27614006ff6fc4925991edbedaf1eab6b14731 https://github.com/performancecopilot/pcp/commit/2a4bd9f81a3a59fcba4cf726cc615fd51d107054
Last-Update: 2026-07-25
 Backport-Note:
 Backported to PCP 6.3.8 (applies unchanged). No libpcp3 tree in 6.3.8, so that mirror hunk is dropped.


--- a/src/libpcp/src/auxconnect.c
+++ b/src/libpcp/src/auxconnect.c
@@ -516,8 +516,12 @@
     }
 
 #if defined(HAVE_STRUCT_SOCKADDR_UN)
-    if (family == AF_UNIX)
+    if (family == AF_UNIX) {
+	int	fdFlags;
+	if ((fdFlags = __pmGetFileDescriptorFlags(fd)) >= 0)
+	    __pmSetFileDescriptorFlags(fd, fdFlags | FD_CLOEXEC);
 	return fd;
+    }
 #endif
 
     /* Avoid 200 ms delay. This option is not supported for unix domain sockets. */
--- a/src/libpcp_pmda/src/root.c
+++ b/src/libpcp_pmda/src/root.c
@@ -32,7 +32,7 @@
     char		*tmpdir;
     char		socketpath[MAXPATHLEN];
     char		errmsg[PM_MAXERRMSGLEN];
-    int			fd, sts, version, features;
+    int			fd, sts, version, features, fdFlags;
 
     /* Initialize the socket address. */
     if ((addr = __pmSockAddrAlloc()) == NULL)
@@ -72,6 +72,9 @@
 	return sts;
     }
 
+    if ((fdFlags = __pmGetFileDescriptorFlags(fd)) >= 0)
+	__pmSetFileDescriptorFlags(fd, fdFlags | FD_CLOEXEC);
+
     /* Check server connection information */
     if ((sts = __pmdaRecvRootPDUInfo(fd, &version, &features)) < 0) {
 	pmNotifyErr(LOG_ERR,
--- a/src/pmdas/root/root.c
+++ b/src/pmdas/root/root.c
@@ -23,6 +23,9 @@
 #include "docker.h"
 #include "podman.h"
 #include "domain.h"
+#if defined(HAVE_PWD_H)
+#include <pwd.h>
+#endif
 
 #ifndef S_IRWXU
 /*
@@ -37,6 +40,7 @@
 static __pmSockAddr *socket_addr;
 static int socket_fd = -1;
 static int pmcd_fd = -1;
+static uid_t pcp_uid;
 
 static __pmFdSet connected_fds;
 int root_maximum_fd;
@@ -461,6 +465,42 @@
 	    exit(1);
 	}
     }
+#if defined(HAVE_STRUCT_UCRED)
+    {
+	struct ucred	cred;
+	__pmSockLen	len = sizeof(cred);
+
+	if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == 0) {
+	    if (cred.uid != 0 && cred.uid != pcp_uid) {
+		pmNotifyErr(LOG_ERR,
+		    "root_accept_client: rejected uid=%d (expected root or pcp[%d])\n",
+		    cred.uid, pcp_uid);
+		close(fd);
+		root_client[i].fd = -1;
+		root_delete_client(&root_client[i]);
+		return NULL;
+	    }
+	}
+    }
+#elif defined(HAVE_GETPEEREID)
+    {
+	uid_t		uid;
+	gid_t		gid;
+
+	if (getpeereid(fd, &uid, &gid) == 0) {
+	    if (uid != 0 && uid != pcp_uid) {
+		pmNotifyErr(LOG_ERR,
+		    "root_accept_client: rejected uid=%d (expected root or pcp[%d])\n",
+		    uid, pcp_uid);
+		close(fd);
+		root_client[i].fd = -1;
+		root_delete_client(&root_client[i]);
+		return NULL;
+	    }
+	}
+    }
+#endif
+
     if (fd > root_maximum_fd)
 	root_maximum_fd = fd;
     __pmFD_SET(fd, &connected_fds);
@@ -797,6 +837,19 @@
 }
 
 static void
+root_get_pcp_uid(void)
+{
+#if defined(HAVE_PWD_H)
+    char		*username;
+    struct passwd	*pw;
+
+    pmGetUsername(&username);
+    if ((pw = getpwnam(username)) != NULL)
+	pcp_uid = pw->pw_uid;
+#endif
+}
+
+static void
 root_check_user(void)
 {
 #ifdef HAVE_GETUID
@@ -815,6 +868,7 @@
 root_prep(void)
 {
     root_check_user();
+    root_get_pcp_uid();
     root_setup_socket();
     atexit(root_close_socket);
 }
