michal-grzedzicki / rpms / rpm

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