dcavalca / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone

Blame SOURCES/rpm-4.16.1.3-fix-IMA-sig-len-assumed-const.patch

137a59
From b66422161d68ed7f7b1cb30e4db900bf42bed146 Mon Sep 17 00:00:00 2001
3f3ddd
From: Panu Matilainen <pmatilai@redhat.com>
3f3ddd
Date: Mon, 29 Nov 2021 14:01:39 +0200
137a59
Subject: [PATCH 1/4] Add Python bindings for rpmfilesFSignature()
137a59
137a59
Only, use more descriptive names than the C-side counterparts.
137a59
Python has nice facilities for dealing with binary data so return it
137a59
as such rather than converting to hex.
137a59
137a59
Backported for 4.16.1.3 (removed rpmfilesVSignature()).
137a59
---
137a59
 python/rpmfiles-py.c | 18 ++++++++++++++++++
137a59
 1 file changed, 18 insertions(+)
137a59
137a59
diff --git a/python/rpmfiles-py.c b/python/rpmfiles-py.c
137a59
index 27666021d..48189a0ac 100644
137a59
--- a/python/rpmfiles-py.c
137a59
+++ b/python/rpmfiles-py.c
137a59
@@ -152,6 +152,22 @@ static PyObject *rpmfile_digest(rpmfileObject *s)
137a59
     Py_RETURN_NONE;
137a59
 }
137a59
 
137a59
+static PyObject *bytebuf(const unsigned char *buf, size_t len)
137a59
+{
137a59
+    if (buf) {
137a59
+	PyObject *o = PyBytes_FromStringAndSize((const char *)buf, len);
137a59
+	return o;
137a59
+    }
137a59
+    Py_RETURN_NONE;
137a59
+}
137a59
+
137a59
+static PyObject *rpmfile_imasig(rpmfileObject *s)
137a59
+{
137a59
+    size_t len = 0;
137a59
+    const unsigned char *sig = rpmfilesFSignature(s->files, s->ix, &len;;
137a59
+    return bytebuf(sig, len);
137a59
+}
137a59
+
137a59
 static PyObject *rpmfile_class(rpmfileObject *s)
137a59
 {
137a59
     return utf8FromString(rpmfilesFClass(s->files, s->ix));
137a59
@@ -278,6 +294,8 @@ static PyGetSetDef rpmfile_getseters[] = {
137a59
       "language the file provides (typically for doc files)" },
137a59
     { "caps",		(getter) rpmfile_caps,		NULL,
137a59
       "file capabilities" },
137a59
+    { "imasig",	(getter) rpmfile_imasig,	NULL,
137a59
+      "IMA signature" },
137a59
     { NULL, NULL, NULL, NULL }
137a59
 };
137a59
 
137a59
-- 
137a59
2.35.1
137a59
137a59
From 9c4622998d3d0666edbea3ed1ae518502c3ed987 Mon Sep 17 00:00:00 2001
137a59
From: Panu Matilainen <pmatilai@redhat.com>
137a59
Date: Mon, 7 Feb 2022 11:52:55 +0200
137a59
Subject: [PATCH 2/4] Add a testcase for --dump query
137a59
137a59
---
137a59
 tests/rpmquery.at | 18 ++++++++++++++++++
137a59
 1 file changed, 18 insertions(+)
137a59
137a59
diff --git a/tests/rpmquery.at b/tests/rpmquery.at
137a59
index 9a4f1cb76..9bd391ac5 100644
137a59
--- a/tests/rpmquery.at
137a59
+++ b/tests/rpmquery.at
137a59
@@ -83,6 +83,24 @@ hello.spec
137a59
 [ignore])
137a59
 AT_CLEANUP
137a59
 
137a59
+AT_SETUP([rpm -qp --dump])
137a59
+AT_KEYWORDS([query])
137a59
+AT_CHECK([
137a59
+RPMDB_INIT
137a59
+runroot rpm \
137a59
+  -qp --dump \
137a59
+  /data/RPMS/hello-2.0-1.x86_64.rpm
137a59
+],
137a59
+[0],
137a59
+[/usr/bin/hello 7120 1489670606 c89fa87aeb1143969c0b6be9334b21d932f77f74e8f60120b5de316406369cf0 0100751 root root 0 0 0 X
137a59
+/usr/share/doc/hello-2.0 4096 1489670606 0000000000000000000000000000000000000000000000000000000000000000 040755 root root 0 0 0 X
137a59
+/usr/share/doc/hello-2.0/COPYING 48 908894882 fac3b28492ecdc16da172a6f1a432ceed356ca4d9248157b2a962b395e37b3b0 0100644 root root 0 1 0 X
137a59
+/usr/share/doc/hello-2.0/FAQ 36 908895030 678b87e217a415f05e43460e2c7b668245b412e2b4f18a75aa7399d9774ed0b4 0100644 root root 0 1 0 X
137a59
+/usr/share/doc/hello-2.0/README 39 908884468 d63fdc6c986106f57230f217d36b2395d83ecf491d2b7187af714dc8db9629e9 0100644 root root 0 1 0 X
137a59
+],
137a59
+[])
137a59
+AT_CLEANUP
137a59
+
137a59
 # ------------------------------
137a59
 AT_SETUP([rpmspec -q])
137a59
 AT_KEYWORDS([query])
137a59
-- 
137a59
2.35.1
137a59
137a59
From 9b2bc10881db7691439005fd74ea53d75b15ac76 Mon Sep 17 00:00:00 2001
137a59
From: Panu Matilainen <pmatilai@redhat.com>
137a59
Date: Thu, 10 Feb 2022 11:15:04 +0200
137a59
Subject: [PATCH 3/4] Ensure sane string lengths for file digests from header
137a59
137a59
---
137a59
 lib/rpmfi.c | 4 ++++
137a59
 1 file changed, 4 insertions(+)
137a59
137a59
diff --git a/lib/rpmfi.c b/lib/rpmfi.c
137a59
index af428468c..2dffab3aa 100644
137a59
--- a/lib/rpmfi.c
137a59
+++ b/lib/rpmfi.c
137a59
@@ -1501,6 +1501,10 @@ static uint8_t *hex2bin(Header h, rpmTagVal tag, rpm_count_t num, size_t len)
137a59
 		t += len;
137a59
 		continue;
137a59
 	    }
137a59
+	    if (strlen(s) != len * 2) {
137a59
+		bin = rfree(bin);
137a59
+		break;
137a59
+	    }
137a59
 	    for (int j = 0; j < len; j++, t++, s += 2)
137a59
 		*t = (rnibble(s[0]) << 4) | rnibble(s[1]);
137a59
 	}
137a59
-- 
137a59
2.35.1
137a59
137a59
From ddfed9e1842a1b60a8c40de3a18add6f6d68c515 Mon Sep 17 00:00:00 2001
137a59
From: Panu Matilainen <pmatilai@redhat.com>
137a59
Date: Mon, 29 Nov 2021 14:01:39 +0200
137a59
Subject: [PATCH 4/4] Fix IMA signature fubar, take III (#1833, RhBug:2018937)
3f3ddd
3f3ddd
At least ECDSA and RSA signatures can vary in length, but the IMA code
3f3ddd
assumes constant lengths and thus may either place invalid signatures on
3f3ddd
disk from either truncating or overshooting, and segfault if the stars are
3f3ddd
just so.
3f3ddd
137a59
As we can't assume static lengths and attempts to use maximum length
137a59
have proven problematic for other reasons, use a data structure that
137a59
can actually handle variable length data properly: store offsets into
137a59
the decoded binary blob and use them to calculate lengths when needed,
137a59
empty data is simply consequtive identical offsets. This avoids a whole
137a59
class of silly overflow issues with multiplying, makes zero-length data
137a59
actually presentable in the data structure and saves memory too.
137a59
137a59
Add tests to show behavior with variable length signatures and missing
137a59
signatures.
3f3ddd
3f3ddd
Additionally update the signing code to store the largest IMA signature
3f3ddd
length rather than what happened to be last to be on the safe side.
3f3ddd
We can't rely on this value due to invalid packages being out there,
3f3ddd
but then we need to calculate the lengths on rpmfiles populate so there's
3f3ddd
not a lot to gain anyhow.
3f3ddd
3f3ddd
Fixes: #1833
3f3ddd
137a59
Backported for 4.16.1.3.  Note that the test case has been removed due
137a59
to it including a binary file (test package) for which we'd have to use
137a59
-Sgit with %autopatch and thus depend on git-core at build time.
137a59
Nevertheless, we do have this BZ covered in our internal test suite, so
137a59
no need for it anyway.
3f3ddd
---
137a59
 lib/rpmfi.c         | 61 +++++++++++++++++++++++++++++++++++++++------
137a59
 sign/rpmsignfiles.c |  5 +++-
137a59
 2 files changed, 58 insertions(+), 8 deletions(-)
3f3ddd
3f3ddd
diff --git a/lib/rpmfi.c b/lib/rpmfi.c
137a59
index 2dffab3aa..77e73442c 100644
3f3ddd
--- a/lib/rpmfi.c
3f3ddd
+++ b/lib/rpmfi.c
137a59
@@ -115,7 +115,7 @@ struct rpmfiles_s {
3f3ddd
     struct fingerPrint_s * fps;	/*!< File fingerprint(s). */
3f3ddd
 
3f3ddd
     int digestalgo;		/*!< File digest algorithm */
3f3ddd
-    int signaturelength;	/*!< File signature length */
137a59
+    uint32_t *signatureoffs;	/*!< File signature offsets */
3f3ddd
     unsigned char * digests;	/*!< File digests in binary. */
3f3ddd
     unsigned char * signatures; /*!< File signatures in binary. */
3f3ddd
 
137a59
@@ -574,10 +574,15 @@ const unsigned char * rpmfilesFSignature(rpmfiles fi, int ix, size_t *len)
137a59
     const unsigned char *signature = NULL;
3f3ddd
 
3f3ddd
     if (fi != NULL && ix >= 0 && ix < rpmfilesFC(fi)) {
137a59
-	if (fi->signatures != NULL)
3f3ddd
-	    signature = fi->signatures + (fi->signaturelength * ix);
137a59
+	size_t slen = 0;
137a59
+	if (fi->signatures != NULL && fi->signatureoffs != NULL) {
137a59
+	    uint32_t off = fi->signatureoffs[ix];
137a59
+	    slen = fi->signatureoffs[ix+1] - off;
137a59
+	    if (slen > 0)
137a59
+		signature = fi->signatures + off;
137a59
+	}
3f3ddd
 	if (len)
3f3ddd
-	    *len = fi->signaturelength;
137a59
+	    *len = slen;
3f3ddd
     }
3f3ddd
     return signature;
3f3ddd
 }
137a59
@@ -1257,6 +1262,7 @@ rpmfiles rpmfilesFree(rpmfiles fi)
3f3ddd
 	fi->flangs = _free(fi->flangs);
3f3ddd
 	fi->digests = _free(fi->digests);
3f3ddd
 	fi->signatures = _free(fi->signatures);
137a59
+	fi->signatureoffs = _free(fi->signatureoffs);
3f3ddd
 	fi->fcaps = _free(fi->fcaps);
3f3ddd
 
3f3ddd
 	fi->cdict = _free(fi->cdict);
137a59
@@ -1485,6 +1491,48 @@ err:
137a59
     return;
3f3ddd
 }
3f3ddd
 
137a59
+/*
137a59
+ * Convert a tag of variable len hex strings to binary presentation,
137a59
+ * accessed via offsets to a contiguous binary blob. Empty values
137a59
+ * are represented by identical consequtive offsets. The offsets array
137a59
+ * always has one extra element to allow calculating the size of the
137a59
+ * last element.
137a59
+ */
137a59
+static uint8_t *hex2binv(Header h, rpmTagVal tag, rpm_count_t num,
137a59
+			uint32_t **offsetp)
137a59
+{
137a59
+    struct rpmtd_s td;
137a59
+    uint8_t *bin = NULL;
137a59
+    uint32_t *offs = NULL;
3f3ddd
+
137a59
+    if (headerGet(h, tag, &td, HEADERGET_MINMEM) && rpmtdCount(&td) == num) {
137a59
+	const char *s;
137a59
+	int i = 0;
137a59
+	uint8_t *t = bin = xmalloc(((rpmtdSize(&td) / 2) + 1));
137a59
+	offs = xmalloc((num + 1) * sizeof(*offs));
3f3ddd
+
137a59
+	while ((s = rpmtdNextString(&td))) {
137a59
+	    uint32_t slen = strlen(s);
137a59
+	    uint32_t len = slen / 2;
137a59
+	    if (slen % 2) {
137a59
+		bin = rfree(bin);
137a59
+		offs = rfree(offs);
137a59
+		goto exit;
3f3ddd
+	    }
137a59
+	    offs[i] = t - bin;
137a59
+	    for (int j = 0; j < len; j++, t++, s += 2)
137a59
+		*t = (rnibble(s[0]) << 4) | rnibble(s[1]);
137a59
+	    i++;
137a59
+	}
137a59
+	offs[i] = t - bin;
137a59
+	*offsetp = offs;
137a59
+    }
3f3ddd
+
137a59
+exit:
137a59
+    rpmtdFreeData(&td);
137a59
+    return bin;
137a59
+}
3f3ddd
+
137a59
 /* Convert a tag of hex strings to binary presentation */
137a59
 static uint8_t *hex2bin(Header h, rpmTagVal tag, rpm_count_t num, size_t len)
137a59
 {
137a59
@@ -1580,9 +1628,8 @@ static int rpmfilesPopulate(rpmfiles fi, Header h, rpmfiFlags flags)
3f3ddd
     fi->signatures = NULL;
3f3ddd
     /* grab hex signatures from header and store in binary format */
3f3ddd
     if (!(flags & RPMFI_NOFILESIGNATURES)) {
3f3ddd
-	fi->signaturelength = headerGetNumber(h, RPMTAG_FILESIGNATURELENGTH);
3f3ddd
-	fi->signatures = hex2bin(h, RPMTAG_FILESIGNATURES,
3f3ddd
-				 totalfc, fi->signaturelength);
137a59
+	fi->signatures = hex2binv(h, RPMTAG_FILESIGNATURES,
137a59
+				 totalfc, &fi->signatureoffs);
3f3ddd
     }
3f3ddd
 
3f3ddd
     /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes */
3f3ddd
diff --git a/sign/rpmsignfiles.c b/sign/rpmsignfiles.c
137a59
index b143c5b9b..372ba634c 100644
3f3ddd
--- a/sign/rpmsignfiles.c
3f3ddd
+++ b/sign/rpmsignfiles.c
3f3ddd
@@ -98,8 +98,9 @@ rpmRC rpmSignFiles(Header sigh, Header h, const char *key, char *keypass)
3f3ddd
     td.count = 1;
3f3ddd
 
3f3ddd
     while (rpmfiNext(fi) >= 0) {
137a59
+	uint32_t slen = 0;
3f3ddd
 	digest = rpmfiFDigest(fi, NULL, NULL);
3f3ddd
-	signature = signFile(algoname, digest, diglen, key, keypass, &siglen);
3f3ddd
+	signature = signFile(algoname, digest, diglen, key, keypass, &slen);
3f3ddd
 	if (!signature) {
3f3ddd
 	    rpmlog(RPMLOG_ERR, _("signFile failed\n"));
3f3ddd
 	    goto exit;
3f3ddd
@@ -110,6 +111,8 @@ rpmRC rpmSignFiles(Header sigh, Header h, const char *key, char *keypass)
3f3ddd
 	    goto exit;
3f3ddd
 	}
3f3ddd
 	signature = _free(signature);
3f3ddd
+	if (slen > siglen)
3f3ddd
+	    siglen = slen;
3f3ddd
     }
3f3ddd
 
3f3ddd
     if (siglen > 0) {
3f3ddd
-- 
137a59
2.35.1
3f3ddd