References: CVE-2026-16530
Description: libpcp: fix arbitrary pointer deref in __pmLogLoadInDom (CWE-125/822)
 The bounds check on string indices (idx > max_idx) in __pmLogLoadInDom()
 was guarded by if (acp != NULL), making it unreachable from the streaming
 path used by pmproxy (which passes acp=NULL).  An attacker could submit
 a TYPE_INDOM record with an out-of-range stridx value via POST
 /logger/meta, causing namelist[i] to point to arbitrary heap memory.
 .
 Fix:
 - Add minimum rlen checks before reading fixed fields, using macros
   derived from the on-disk struct sizes (INDOM_V3_MINRLEN, INDOM_V2_MINRLEN)
 - Validate numinst against rlen before using it in arithmetic, preventing
   integer overflow in the max_idx computation
 - Make max_idx computation and idx bounds check unconditional (remove the
   acp != NULL guard) so they protect both archive and streaming paths
 - Extend qa/src/pducrash.c with decode_log_indom() exercising all four
   failure modes via __pmLogLoadInDom(NULL, ...)
 .
 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/ec81e2b35c7dc19a69406d2712d1b9904ac22112 https://github.com/performancecopilot/pcp/commit/b72da5135df7fb08ca432db1e2db24478f43aea2 https://github.com/performancecopilot/pcp/commit/edfd909edac8ad4762aa150e330f92211df98c1c
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/e_indom.c
+++ b/src/libpcp/src/e_indom.c
@@ -51,6 +51,10 @@
     				/* will be expanded if numinst > 0 */
 } __pmInDom_v2;
 
+/* Minimum rlen (record body without len+type header) to read fixed fields */
+#define INDOM_V3_MINRLEN	(sizeof(__pmInDom_v3) - 2 * sizeof(__int32_t))
+#define INDOM_V2_MINRLEN	(sizeof(__pmInDom_v2) - 2 * sizeof(__int32_t))
+
 /*
  * pack an indom into a physical metadata record
  * - lcp required to provide archive version (else NULL)
@@ -258,33 +262,55 @@
 
     if (type == TYPE_INDOM || type == TYPE_INDOM_DELTA) {
 	__pmInDom_v3	*v3;
+	if (rlen < (int)INDOM_V3_MINRLEN) {
+	    if (pmDebugOptions.logmeta)
+		fprintf(stderr, "__pmLogLoadInDom: v3 rlen=%d too small (min=%d)\n",
+		    rlen, (int)INDOM_V3_MINRLEN);
+	    goto bad;
+	}
 	v3 = (__pmInDom_v3 *)&lbuf[-2];	/* len+type not in buf */
 	__pmLoadTimestamp(&v3->sec[0], &lidp->stamp);
 	k = (sizeof(v3->sec)+sizeof(v3->nsec))/sizeof(__int32_t);
 	lidp->indom = __ntohpmInDom(v3->indom);
 	k++;
 	lidp->numinst = ntohl(v3->numinst);
+	if (lidp->numinst < 0 ||
+	    lidp->numinst > (rlen - (int)INDOM_V3_MINRLEN) / (2 * (int)sizeof(__int32_t))) {
+	    if (pmDebugOptions.logmeta)
+		fprintf(stderr, "__pmLogLoadInDom: v3 numinst=%d not consistent with rlen=%d\n",
+		    lidp->numinst, rlen);
+	    goto bad;
+	}
 	k++;
 	lidp->instlist = (int *)&v3->data;
-	if (acp != NULL) {
-	    /* rlen minus fixed fields (plus len+type), minus instlist[], minus strindex[] */
-	    max_idx = rlen - 5*sizeof(__int32_t) - 2*lidp->numinst*sizeof(__int32_t);
-	}
+	/* rlen minus fixed fields (plus len+type), minus instlist[], minus strindex[] */
+	max_idx = rlen - (int)INDOM_V3_MINRLEN - 2 * lidp->numinst * (int)sizeof(__int32_t);
     }
     else if (type == TYPE_INDOM_V2) {
 	__pmInDom_v2	*v2;
+	if (rlen < (int)INDOM_V2_MINRLEN) {
+	    if (pmDebugOptions.logmeta)
+		fprintf(stderr, "__pmLogLoadInDom: v2 rlen=%d too small (min=%d)\n",
+		    rlen, (int)INDOM_V2_MINRLEN);
+	    goto bad;
+	}
 	v2 = (__pmInDom_v2 *)&lbuf[-2];	/* len+type not in lbuf */
 	__pmLoadTimeval(&v2->sec, &lidp->stamp);
 	k = (sizeof(v2->sec)+sizeof(v2->usec))/sizeof(__int32_t);
 	lidp->indom = __ntohpmInDom(v2->indom);
 	k++;
 	lidp->numinst = ntohl(v2->numinst);
+	if (lidp->numinst < 0 ||
+	    lidp->numinst > (rlen - (int)INDOM_V2_MINRLEN) / (2 * (int)sizeof(__int32_t))) {
+	    if (pmDebugOptions.logmeta)
+		fprintf(stderr, "__pmLogLoadInDom: v2 numinst=%d not consistent with rlen=%d\n",
+		    lidp->numinst, rlen);
+	    goto bad;
+	}
 	k++;
 	lidp->instlist = (int *)&v2->data;
-	if (acp != NULL) {
-	    /* rlen minus fixed fields (plus len+type), minus instlist[], minus strindex[] */
-	    max_idx = rlen - 4*sizeof(__int32_t) - 2*lidp->numinst*sizeof(__int32_t);
-	}
+	/* rlen minus fixed fields (plus len+type), minus instlist[], minus strindex[] */
+	max_idx = rlen - (int)INDOM_V2_MINRLEN - 2 * lidp->numinst * (int)sizeof(__int32_t);
     }
     else {
 	if (pmDebugOptions.logmeta)
@@ -327,21 +353,14 @@
 	    }
 	    idx = ntohl(stridx[i]);
 	    if (idx >= 0) {
-		if (acp != NULL) {
-		    /*
-		     * crude sanity check ... if the index points to the
-		     * start of the name that is past the end of the input
-		     * record, the record is corrupted
-		     */
-		    if (idx > max_idx) {
-			if (pmDebugOptions.logmeta) {
-			    char	strbuf[20];
-			    fprintf(stderr, "__pmLogLoadInDom: InDom: %s instance[%d]: bad string index (%d) > max index based on record length (%d)\n",
-				pmInDomStr_r(lidp->indom, strbuf, sizeof(strbuf)),
-				i, idx, max_idx);
-			}
-			goto bad;
+		if (idx > max_idx) {
+		    if (pmDebugOptions.logmeta) {
+			char	strbuf[20];
+			fprintf(stderr, "__pmLogLoadInDom: InDom: %s instance[%d]: bad string index (%d) > max index based on record length (%d)\n",
+			    pmInDomStr_r(lidp->indom, strbuf, sizeof(strbuf)),
+			    i, idx, max_idx);
 		    }
+		    goto bad;
 		}
 		lidp->namelist[i] = &namebase[idx];
 		if (pmDebugOptions.logmeta && pmDebugOptions.desperate)
--- a/src/pmlogextract/pmlogextract.c
+++ b/src/pmlogextract/pmlogextract.c
@@ -1300,7 +1300,7 @@
 		    memcpy(buf, rec->pdu, rlen);
 
 		    ibuf = &buf[2];
-		    sts = __pmLogLoadInDom(NULL, 0, type, &lid, &ibuf);
+		    sts = __pmLogLoadInDom(NULL, rlen, type, &lid, &ibuf);
 		    if (sts < 0) {
 			fprintf(stderr, "write_rec: __pmLogLoadInDom(type=%s (%d)): failed: %s\n", __pmLogMetaTypeStr(type), type, pmErrStr(sts));
 		    }
--- a/src/pmlogrewrite/indom.c
+++ b/src/pmlogrewrite/indom.c
@@ -223,9 +223,10 @@
     }
     else {
 	__int32_t	*buf;
+	int		len = htonl(hdr->len);
 	/* buffer for __pmLogLoadInDom has to start AFTER the header */
 	buf = &recbuf[2];
-	sts = __pmLogLoadInDom(NULL, 0, type, lidp, &buf);
+	sts = __pmLogLoadInDom(NULL, len, type, lidp, &buf);
 	if (sts < 0) {
 	    fprintf(stderr, "_pmUnpackInDom: __pmLogLoadInDom(type=%d): failed: %s\n", type, pmErrStr(sts));
 	    abandon();
