dcavalca / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
41043c
commit d6a86b5e69e46cc283b1e06c92343319beb42e21
41043c
Author: Panu Matilainen <pmatilai@redhat.com>
41043c
Date:   Thu Mar 4 13:21:19 2021 +0200
41043c
41043c
    Be much more careful about copying data from the signature header
41043c
    
41043c
    Only look for known tags, and ensure correct type and size where known
41043c
    before copying over. Bump the old arbitrary 16k count limit to 16M limit
41043c
    though, it's not inconceivable that a package could have that many files.
41043c
    While at it, ensure none of these tags exist in the main header,
41043c
    which would confuse us greatly.
41043c
    
41043c
    This is optimized for backporting ease, upstream can remove redundancies
41043c
    and further improve checking later.
41043c
    
41043c
    Reported and initial patches by Demi Marie Obenour.
41043c
    
41043c
    Fixes: RhBug:1935049, RhBug:1933867, RhBug:1935035, RhBug:1934125, ...
41043c
    
41043c
    Fixes: CVE-2021-3421, CVE-2021-20271
41043c
41043c
    Combined with e2f1f1931c5ccf3ecbe4e1e12cacb1e17a277776 and backported into
41043c
    4.14.3
41043c
41043c
diff -up rpm-4.14.3/lib/package.c.orig rpm-4.14.3/lib/package.c
41043c
--- rpm-4.14.3/lib/package.c.orig	2021-05-31 12:32:49.970393976 +0200
41043c
+++ rpm-4.14.3/lib/package.c	2021-05-31 13:53:58.250673275 +0200
41043c
@@ -31,76 +31,72 @@ struct pkgdata_s {
41043c
     rpmRC rc;
41043c
 };
41043c
 
41043c
+struct taglate_s {
41043c
+    rpmTagVal stag;
41043c
+    rpmTagVal xtag;
41043c
+    rpm_count_t count;
41043c
+    int quirk;
41043c
+} const xlateTags[] = {
41043c
+    { RPMSIGTAG_SIZE, RPMTAG_SIGSIZE, 1, 0 },
41043c
+    { RPMSIGTAG_PGP, RPMTAG_SIGPGP, 0, 0 },
41043c
+    { RPMSIGTAG_MD5, RPMTAG_SIGMD5, 16, 0 },
41043c
+    { RPMSIGTAG_GPG, RPMTAG_SIGGPG, 0, 0 },
41043c
+    /* { RPMSIGTAG_PGP5, RPMTAG_SIGPGP5, 0, 0 }, */ /* long obsolete, dont use */
41043c
+    { RPMSIGTAG_PAYLOADSIZE, RPMTAG_ARCHIVESIZE, 1, 1 },
41043c
+    { RPMSIGTAG_SHA1, RPMTAG_SHA1HEADER, 1, 0 },
41043c
+    { RPMSIGTAG_SHA256, RPMTAG_SHA256HEADER, 1, 0 },
41043c
+    { RPMSIGTAG_DSA, RPMTAG_DSAHEADER, 0, 0 },
41043c
+    { RPMSIGTAG_RSA, RPMTAG_RSAHEADER, 0, 0 },
41043c
+    { RPMSIGTAG_LONGSIZE, RPMTAG_LONGSIGSIZE, 1, 0 },
41043c
+    { RPMSIGTAG_LONGARCHIVESIZE, RPMTAG_LONGARCHIVESIZE, 1, 0 },
41043c
+    { 0 }
41043c
+};
41043c
+
41043c
 /** \ingroup header
41043c
  * Translate and merge legacy signature tags into header.
41043c
  * @param h		header (dest)
41043c
  * @param sigh		signature header (src)
41043c
+ * @return		failing tag number, 0 on success
41043c
  */
41043c
 static
41043c
-void headerMergeLegacySigs(Header h, Header sigh)
41043c
+rpmTagVal headerMergeLegacySigs(Header h, Header sigh, char **msg)
41043c
 {
41043c
-    HeaderIterator hi;
41043c
+    const struct taglate_s *xl;
41043c
     struct rpmtd_s td;
41043c
 
41043c
-    hi = headerInitIterator(sigh);
41043c
-    for (; headerNext(hi, &td); rpmtdFreeData(&td))
41043c
-    {
41043c
-	switch (td.tag) {
41043c
-	/* XXX Translate legacy signature tag values. */
41043c
-	case RPMSIGTAG_SIZE:
41043c
-	    td.tag = RPMTAG_SIGSIZE;
41043c
-	    break;
41043c
-	case RPMSIGTAG_PGP:
41043c
-	    td.tag = RPMTAG_SIGPGP;
41043c
-	    break;
41043c
-	case RPMSIGTAG_MD5:
41043c
-	    td.tag = RPMTAG_SIGMD5;
41043c
-	    break;
41043c
-	case RPMSIGTAG_GPG:
41043c
-	    td.tag = RPMTAG_SIGGPG;
41043c
-	    break;
41043c
-	case RPMSIGTAG_PGP5:
41043c
-	    td.tag = RPMTAG_SIGPGP5;
41043c
-	    break;
41043c
-	case RPMSIGTAG_PAYLOADSIZE:
41043c
-	    td.tag = RPMTAG_ARCHIVESIZE;
41043c
-	    break;
41043c
-	case RPMSIGTAG_SHA1:
41043c
-	case RPMSIGTAG_SHA256:
41043c
-	case RPMSIGTAG_DSA:
41043c
-	case RPMSIGTAG_RSA:
41043c
-	default:
41043c
-	    if (!(td.tag >= HEADER_SIGBASE && td.tag < HEADER_TAGBASE))
41043c
+    rpmtdReset(&td);
41043c
+    for (xl = xlateTags; xl->stag; xl++) {
41043c
+	/* There mustn't be one in the main header */
41043c
+	if (headerIsEntry(h, xl->xtag)) {
41043c
+	    /* Some tags may exist in either header, but never both */
41043c
+	    if (xl->quirk && !headerIsEntry(sigh, xl->stag))
41043c
 		continue;
41043c
 	    break;
41043c
 	}
41043c
-	if (!headerIsEntry(h, td.tag)) {
41043c
-	    switch (td.type) {
41043c
-	    case RPM_NULL_TYPE:
41043c
-		continue;
41043c
+	if (headerGet(sigh, xl->stag, &td, HEADERGET_RAW|HEADERGET_MINMEM)) {
41043c
+	    /* Translate legacy tags */
41043c
+	    if (xl->stag != xl->xtag)
41043c
+		td.tag = xl->xtag;
41043c
+	    /* Ensure type and tag size match expectations */
41043c
+	    if (td.type != rpmTagGetTagType(td.tag))
41043c
 		break;
41043c
-	    case RPM_CHAR_TYPE:
41043c
-	    case RPM_INT8_TYPE:
41043c
-	    case RPM_INT16_TYPE:
41043c
-	    case RPM_INT32_TYPE:
41043c
-	    case RPM_INT64_TYPE:
41043c
-		if (td.count != 1)
41043c
-		    continue;
41043c
+	    if (td.count < 1 || td.count > 16*1024*1024)
41043c
 		break;
41043c
-	    case RPM_STRING_TYPE:
41043c
-	    case RPM_BIN_TYPE:
41043c
-		if (td.count >= 16*1024)
41043c
-		    continue;
41043c
+	    if (xl->count && td.count != xl->count)
41043c
 		break;
41043c
-	    case RPM_STRING_ARRAY_TYPE:
41043c
-	    case RPM_I18NSTRING_TYPE:
41043c
-		continue;
41043c
+	    if (!headerPut(h, &td, HEADERPUT_DEFAULT))
41043c
 		break;
41043c
-	    }
41043c
-	    (void) headerPut(h, &td, HEADERPUT_DEFAULT);
41043c
+	    rpmtdFreeData(&td);
41043c
 	}
41043c
     }
41043c
-    headerFreeIterator(hi);
41043c
+    rpmtdFreeData(&td);
41043c
+
41043c
+    if (xl->stag) {
41043c
+	rasprintf(msg, "invalid signature tag %s (%d)",
41043c
+			rpmTagGetName(xl->xtag), xl->xtag);
41043c
+    }
41043c
+
41043c
+    return xl->stag;
41043c
 }
41043c
 
41043c
 /**
41043c
@@ -363,7 +359,8 @@ rpmRC rpmReadPackageFile(rpmts ts, FD_t
41043c
 		goto exit;
41043c
 
41043c
 	    /* Append (and remap) signature tags to the metadata. */
41043c
-	    headerMergeLegacySigs(h, sigh);
41043c
+	    if (headerMergeLegacySigs(h, sigh, &msg))
41043c
+		goto exit;
41043c
 	    applyRetrofits(h);
41043c
 
41043c
 	    /* Bump reference count for return. */