References: CVE-2026-16524
Description: linux_sockets pmda: fix command injection via network.persocket.filter (CWE-78)
 The sockets_check_filter() validation helper returns 1 for safe input
 and 0 for unsafe input.  The guard in sockets_store() tested
 if (sockets_check_filter(av.cp)) — rejecting safe input and accepting
 malicious input containing shell metacharacters.  The accepted filter
 was later passed to popen() via shell interpretation, enabling arbitrary
 command execution as the PMDA process user.
 .
 Fix:
 - Invert the guard: if (!sockets_check_filter(av.cp))
 - Replace popen()/pclose() in ss_open_stream() with the libpcp
   __pmProcessAddArg()/__pmProcessPipe()/__pmProcessPipeClose() API
   which uses execvp() internally, eliminating shell interpretation
   of the filter string entirely
 - Add qa/2101 verifying that valid filters are accepted and shell
   metacharacters (semicolons, backticks, pipes) are rejected
 .
 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/c5cbeceb7d3c2af357c04065cdd911efdc270de0
Last-Update: 2026-07-25
 Backport-Note:
 Backported to PCP 6.3.8 (applies unchanged).


--- a/src/pmdas/linux_sockets/pmda.c
+++ b/src/pmdas/linux_sockets/pmda.c
@@ -162,11 +162,9 @@
     const char *p;
 
     for (p = string; *p; p++) {
-	if (isspace(*p))
+	if (isspace(*p) || isalnum(*p))
 	    continue;
-	if (isalnum(*p))
-	    continue;
-	if (*p == '(' || *p == ')')
+	if (strchr("()=!<>:.*/-,", *p) != NULL)
 	    continue;
 	return 0; /* disallow */
     }
@@ -191,7 +189,7 @@
 	    	case 0: /* network.persocket.filter */
 		    if ((sts = pmExtractValue(vsp->valfmt, &vsp->vlist[0],
 			PM_TYPE_STRING, &av, PM_TYPE_STRING)) >= 0) {
-			if (sockets_check_filter(av.cp)) {
+			if (!sockets_check_filter(av.cp)) {
 			    sts = PM_ERR_BADSTORE;
 			    free(av.cp);
 			    break;
--- a/src/pmdas/linux_sockets/ss_stream.c
+++ b/src/pmdas/linux_sockets/ss_stream.c
@@ -14,18 +14,19 @@
 
 #include <pcp/pmapi.h>
 #include <pcp/pmda.h>
+#include <pcp/libpcp.h>
 #include "ss_stats.h"
 
 #define SS_OPTIONS "-noemitauO"
 
-char *ss_filter = NULL; /* storable: network.persocket.filter */
+char *ss_filter;	/* storable: network.persocket.filter */
+static int using_pipe;	/* pipe is normal operation, QA uses files */
 
 FILE *
 ss_open_stream()
 {
-    FILE *fp;
+    FILE *fp = NULL;
     char *path;
-    char cmd[MAXPATHLEN];
 
     if (ss_filter == NULL) {
 	/* pmstore to network.persocket.filter frees this if changing */
@@ -38,17 +39,51 @@
     	fp = fopen(path, "r");
 	if (pmDebugOptions.appl0)
 	    fprintf(stderr, "ss_open_stream: open PCPQA_PMDA_SOCKETS=%s\n", path);
+	using_pipe = 0;
     } else {
+	__pmExecCtl_t	*argp = NULL;
+	int		sts;
+
 	if (access((path = "/usr/sbin/ss"), X_OK) != 0) {
 	    if (access((path = "/usr/bin/ss"), X_OK) != 0) {
 	    	fprintf(stderr, "Error: no \"ss\" binary found\n");
 		return NULL;
 	    }
 	}
-	pmsprintf(cmd, sizeof(cmd), "%s %s %s", path, SS_OPTIONS, ss_filter);
-	fp = popen(cmd, "r");
+	if ((sts = __pmProcessAddArg(&argp, path)) < 0 ||
+	    (sts = __pmProcessAddArg(&argp, SS_OPTIONS)) < 0) {
+	    if (pmDebugOptions.appl0)
+		fprintf(stderr, "ss_open_stream: __pmProcessAddArg failed: %s\n",
+		    pmErrStr(sts));
+	    return NULL;
+	}
+	if (ss_filter[0] != '\0') {
+	    char	*s, *tok, *saveptr;
+
+	    if ((s = strdup(ss_filter)) == NULL)
+		return NULL;
+	    for (tok = strtok_r(s, " \t", &saveptr); tok != NULL;
+		 tok = strtok_r(NULL, " \t", &saveptr)) {
+		if ((sts = __pmProcessAddArg(&argp, tok)) < 0) {
+		    free(s);
+		    if (pmDebugOptions.appl0)
+			fprintf(stderr, "ss_open_stream: __pmProcessAddArg failed: %s\n",
+			    pmErrStr(sts));
+		    return NULL;
+		}
+	    }
+	    free(s);
+	}
+	if ((sts = __pmProcessPipe(&argp, "r", PM_EXEC_TOSS_NONE, &fp)) < 0) {
+	    if (pmDebugOptions.appl0)
+		fprintf(stderr, "ss_open_stream: __pmProcessPipe failed: %s\n",
+		    pmErrStr(sts));
+	    return NULL;
+	}
 	if (pmDebugOptions.appl0)
-	    fprintf(stderr, "ss_open_stream: popen %s\n", cmd);
+	    fprintf(stderr, "ss_open_stream: exec %s %s %s\n",
+		path, SS_OPTIONS, ss_filter);
+	using_pipe = 1;
     }
 
     return fp;
@@ -57,8 +92,8 @@
 void
 ss_close_stream(FILE *fp)
 {
-    if (getenv("PCPQA_PMDA_SOCKETS") != NULL)
-    	fclose(fp);
+    if (using_pipe)
+    	__pmProcessPipeClose(fp);
     else
-    	pclose(fp);
+    	fclose(fp);
 }
