dcavalca / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone

Blame SOURCES/0006-rpm2extents-verify-package-signature-during-transcod.patch

629b27
From ea1177fcef609519f0c2377ebee236001d2a8fae Mon Sep 17 00:00:00 2001
629b27
From: chantra <chantr4@gmail.com>
629b27
Date: Fri, 28 Jan 2022 08:31:39 -0800
629b27
Subject: [PATCH 06/30] [rpm2extents] verify package signature during
629b27
 transcoding
629b27
629b27
---
629b27
 lib/rpmchecksig.c |  30 ++++++
629b27
 lib/rpmcli.h      |   9 ++
629b27
 rpm2extents.c     | 233 +++++++++++++++++++++++++++++++++++++++++-----
629b27
 3 files changed, 248 insertions(+), 24 deletions(-)
629b27
629b27
diff --git a/lib/rpmchecksig.c b/lib/rpmchecksig.c
629b27
index 40a3ab83f..1a6b95323 100644
629b27
--- a/lib/rpmchecksig.c
629b27
+++ b/lib/rpmchecksig.c
629b27
@@ -304,3 +304,33 @@ int rpmcliVerifySignatures(rpmts ts, ARGV_const_t argv)
629b27
     rpmKeyringFree(keyring);
629b27
     return res;
629b27
 }
629b27
+
629b27
+int rpmcliVerifySignaturesFD(rpmts ts, FD_t fdi)
629b27
+{
629b27
+    int res = 0;
629b27
+    rpmKeyring keyring = rpmtsGetKeyring(ts, 1);
629b27
+    rpmVSFlags vsflags = rpmtsVfyFlags(ts);
629b27
+    int vfylevel = rpmtsVfyLevel(ts);
629b27
+
629b27
+    vsflags |= rpmcliVSFlags;
629b27
+    if (rpmcliVfyLevelMask) {
629b27
+	vfylevel &= ~rpmcliVfyLevelMask;
629b27
+	rpmtsSetVfyLevel(ts, vfylevel);
629b27
+    }
629b27
+
629b27
+    FD_t fd = fdDup(Fileno(fdi));
629b27
+    if (fd == NULL || Ferror(fd)) {
629b27
+	rpmlog(RPMLOG_ERR, _("fdDup failed: %s\n"), Fstrerror(fd));
629b27
+	res++;
629b27
+    } else if (rpmpkgVerifySigs(keyring, vfylevel, vsflags, fd, "stdin")) {
629b27
+	res++;
629b27
+    }
629b27
+
629b27
+    lseek(Fileno(fd), SEEK_SET, 0);
629b27
+    Fclose(fd);
629b27
+    rpmsqPoll();
629b27
+
629b27
+    rpmKeyringFree(keyring);
629b27
+    return res;
629b27
+}
629b27
+
629b27
diff --git a/lib/rpmcli.h b/lib/rpmcli.h
629b27
index 906fe9951..52443e459 100644
629b27
--- a/lib/rpmcli.h
629b27
+++ b/lib/rpmcli.h
629b27
@@ -411,6 +411,15 @@ int rpmcliImportPubkeys(rpmts ts, ARGV_const_t argv);
629b27
  */
629b27
 int rpmcliVerifySignatures(rpmts ts, ARGV_const_t argv);
629b27
 
629b27
+
629b27
+/** \ingroup rpmcli
629b27
+ * Verify package signatures
629b27
+ * @param ts		transaction set
629b27
+ * @param fd		a file descriptor to verify
629b27
+ * @return		0 on success
629b27
+ */
629b27
+int rpmcliVerifySignaturesFD(rpmts ts, FD_t fd);
629b27
+
629b27
 #ifdef __cplusplus
629b27
 }
629b27
 #endif
629b27
diff --git a/rpm2extents.c b/rpm2extents.c
629b27
index c111be0a2..d8e582676 100644
629b27
--- a/rpm2extents.c
629b27
+++ b/rpm2extents.c
629b27
@@ -2,7 +2,9 @@
629b27
 
629b27
 #include "system.h"
629b27
 
629b27
+#include <rpm/rpmcli.h>
629b27
 #include <rpm/rpmlib.h>		/* rpmReadPackageFile .. */
629b27
+#include <rpm/rpmlog.h>
629b27
 #include <rpm/rpmfi.h>
629b27
 #include <rpm/rpmtag.h>
629b27
 #include <rpm/rpmio.h>
629b27
@@ -10,6 +12,7 @@
629b27
 
629b27
 #include <rpm/rpmts.h>
629b27
 #include "lib/rpmlead.h"
629b27
+#include "lib/rpmts.h"
629b27
 #include "lib/signature.h"
629b27
 #include "lib/header_internal.h"
629b27
 #include "rpmio/rpmio_internal.h"
629b27
@@ -51,6 +54,16 @@ rpm_loff_t pad_to(rpm_loff_t pos, rpm_loff_t unit)
629b27
     return (unit - (pos % unit)) % unit;
629b27
 }
629b27
 
629b27
+static struct poptOption optionsTable[] = {
629b27
+    { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
629b27
+    N_("Common options for all rpm modes and executables:"), NULL },
629b27
+
629b27
+    POPT_AUTOALIAS
629b27
+    POPT_AUTOHELP
629b27
+    POPT_TABLEEND
629b27
+};
629b27
+
629b27
+
629b27
 static int digestor(
629b27
     FD_t fdi,
629b27
     FD_t fdo,
629b27
@@ -120,7 +133,19 @@ exit:
629b27
     return rc;
629b27
 }
629b27
 
629b27
-static rpmRC process_package(FD_t fdi, FD_t validationi)
629b27
+static rpmRC validator(FD_t fdi){
629b27
+    rpmts ts = rpmtsCreate();
629b27
+    rpmtsSetRootDir(ts, rpmcliRootDir);
629b27
+    /* rpmlog prints NOTICE to stdout */
629b27
+    // rpmlogSetFile(stderr);
629b27
+    if(rpmcliVerifySignaturesFD(ts, fdi)){
629b27
+	fprintf(stderr, _("Error validating package\n"));
629b27
+	return RPMRC_FAIL;
629b27
+    }
629b27
+    return RPMRC_OK;
629b27
+}
629b27
+
629b27
+static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
 {
629b27
     uint32_t diglen;
629b27
     /* GNU C extension: can use diglen from outer context */
629b27
@@ -148,7 +173,7 @@ static rpmRC process_package(FD_t fdi, FD_t validationi)
629b27
     rpm_mode_t mode;
629b27
     char *rpmio_flags = NULL, *zeros;
629b27
     const unsigned char *digest;
629b27
-    rpm_loff_t pos, size, pad, validation_pos;
629b27
+    rpm_loff_t pos, size, pad, digest_pos, validation_pos;
629b27
     uint32_t offset_ix = 0;
629b27
     size_t len;
629b27
     int next = 0;
629b27
@@ -278,17 +303,26 @@ static rpmRC process_package(FD_t fdi, FD_t validationi)
629b27
 	    goto exit;
629b27
 	}
629b27
     }
629b27
-    validation_pos = (
629b27
+    digest_pos = (
629b27
 	pos + sizeof(offset_ix) + sizeof(diglen) +
629b27
 	offset_ix * (diglen + sizeof(rpm_loff_t))
629b27
     );
629b27
 
629b27
+    ssize_t digest_len = ufdCopy(digestori, fdo);
629b27
+    if (digest_len == -1) {
629b27
+	fprintf(stderr, _("digest table ufdCopy failed\n"));
629b27
+	rc = RPMRC_FAIL;
629b27
+	goto exit;
629b27
+    }
629b27
+
629b27
+    validation_pos = digest_pos + digest_len;
629b27
     ssize_t validation_len = ufdCopy(validationi, fdo);
629b27
     if (validation_len == -1) {
629b27
-	fprintf(stderr, _("digest table ufdCopy failed\n"));
629b27
+	fprintf(stderr, _("validation output ufdCopy failed\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
+
629b27
     /* add more padding so the last file can be cloned. It doesn't matter that
629b27
      * the table and validation etc are in this space. In fact, it's pretty
629b27
      * efficient if it is.
629b27
@@ -307,11 +341,16 @@ static rpmRC process_package(FD_t fdi, FD_t validationi)
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
-    if (Fwrite(&validation_pos, len, 1, fdo) != len) {
629b27
+    if (Fwrite(&digest_pos, len, 1, fdo) != len) {
629b27
 	fprintf(stderr, _("Unable to write offset of validation table\n"));
629b27
 	rc = RPMRC_FAIL;
629b27
 	goto exit;
629b27
     }
629b27
+    if (Fwrite(&validation_pos, len, 1, fdo) != len) {
629b27
+	fprintf(stderr, _("Unable to write offset of validation output\n"));
629b27
+	rc = RPMRC_FAIL;
629b27
+	goto exit;
629b27
+    }
629b27
     uint64_t magic = MAGIC;
629b27
     len = sizeof(magic);
629b27
     if (Fwrite(&magic, len, 1, fdo) != len) {
629b27
@@ -327,10 +366,156 @@ exit:
629b27
     return rc;
629b27
 }
629b27
 
629b27
+static off_t ufdTee(FD_t sfd, FD_t *fds, int len)
629b27
+{
629b27
+    char buf[BUFSIZ];
629b27
+    ssize_t rdbytes, wrbytes;
629b27
+    off_t total = 0;
629b27
+
629b27
+    while (1) {
629b27
+	rdbytes = Fread(buf, sizeof(buf[0]), sizeof(buf), sfd);
629b27
+
629b27
+	if (rdbytes > 0) {
629b27
+	    for(int i=0; i < len; i++) {
629b27
+		wrbytes = Fwrite(buf, sizeof(buf[0]), rdbytes, fds[i]);
629b27
+		if (wrbytes != rdbytes) {
629b27
+		    fprintf(stderr, "Error wriing to FD %d: %s\n", i, Fstrerror(fds[i]));
629b27
+		    total = -1;
629b27
+		    break;
629b27
+		}
629b27
+	    }
629b27
+	    if(total == -1){
629b27
+		break;
629b27
+	    }
629b27
+	    total += wrbytes;
629b27
+	} else {
629b27
+	    if (rdbytes < 0)
629b27
+		total = -1;
629b27
+	    break;
629b27
+	}
629b27
+    }
629b27
+
629b27
+    return total;
629b27
+}
629b27
+
629b27
+static int teeRpm(FD_t fdi, FD_t digestori) {
629b27
+    rpmRC rc;
629b27
+    off_t offt = -1;
629b27
+    int processorpipefd[2];
629b27
+    int validatorpipefd[2];
629b27
+    int rpmsignpipefd[2];
629b27
+    pid_t cpids[2], w;
629b27
+    int wstatus;
629b27
+    FD_t fds[2];
629b27
+
629b27
+     if (pipe(processorpipefd) == -1) {
629b27
+	fprintf(stderr, _("Processor pipe failure\n"));
629b27
+	return RPMRC_FAIL;
629b27
+    }
629b27
+
629b27
+    if (pipe(validatorpipefd) == -1) {
629b27
+	fprintf(stderr, _("Validator pipe failure\n"));
629b27
+	return RPMRC_FAIL;
629b27
+    }
629b27
+
629b27
+    if (pipe(rpmsignpipefd) == -1) {
629b27
+	fprintf(stderr, _("Validator pipe failure\n"));
629b27
+	return RPMRC_FAIL;
629b27
+    }
629b27
+
629b27
+    cpids[0] = fork();
629b27
+    if (cpids[0] == 0) {
629b27
+	/* child: validator */
629b27
+	close(processorpipefd[0]);
629b27
+	close(processorpipefd[1]);
629b27
+	close(validatorpipefd[1]);
629b27
+	close(rpmsignpipefd[0]);
629b27
+	FD_t fdi = fdDup(validatorpipefd[0]);
629b27
+	// redirect STDOUT to the pipe
629b27
+	close(STDOUT_FILENO);
629b27
+	FD_t fdo = fdDup(rpmsignpipefd[1]);
629b27
+	close(rpmsignpipefd[1]);
629b27
+	rc = validator(fdi);
629b27
+	if(rc != RPMRC_OK) {
629b27
+	    fprintf(stderr, _("Validator failed\n"));
629b27
+	}
629b27
+	Fclose(fdi);
629b27
+	Fclose(fdo);
629b27
+	if (rc != RPMRC_OK) {
629b27
+	    exit(EXIT_FAILURE);
629b27
+	}
629b27
+	exit(EXIT_SUCCESS);
629b27
+    } else {
629b27
+	/* parent: main program */
629b27
+	cpids[1] = fork();
629b27
+	if (cpids[1] == 0) {
629b27
+	    /* child: process_package */
629b27
+	    close(validatorpipefd[0]);
629b27
+	    close(validatorpipefd[1]);
629b27
+	    close(processorpipefd[1]);
629b27
+	    close(rpmsignpipefd[1]);
629b27
+	    FD_t fdi = fdDup(processorpipefd[0]);
629b27
+	    close(processorpipefd[0]);
629b27
+	    FD_t validatori = fdDup(rpmsignpipefd[0]);
629b27
+	    close(rpmsignpipefd[0]);
629b27
+
629b27
+	    rc = process_package(fdi, digestori, validatori);
629b27
+	    if(rc != RPMRC_OK) {
629b27
+		fprintf(stderr, _("Validator failed\n"));
629b27
+	    }
629b27
+	    Fclose(digestori);
629b27
+	    Fclose(validatori);
629b27
+	    /* fdi is normally closed through the stacked file gzdi in the
629b27
+	     * function
629b27
+	     */
629b27
+
629b27
+	    if (rc != RPMRC_OK) {
629b27
+		exit(EXIT_FAILURE);
629b27
+	    }
629b27
+	    exit(EXIT_SUCCESS);
629b27
+
629b27
+
629b27
+	} else {
629b27
+	    /* Actual parent. Read from fdi and write to both processes */
629b27
+	    close(processorpipefd[0]);
629b27
+	    close(validatorpipefd[0]);
629b27
+	    fds[0] = fdDup(processorpipefd[1]);
629b27
+	    fds[1] = fdDup(validatorpipefd[1]);
629b27
+	    close(validatorpipefd[1]);
629b27
+	    close(processorpipefd[1]);
629b27
+	    close(rpmsignpipefd[0]);
629b27
+	    close(rpmsignpipefd[1]);
629b27
+
629b27
+	    offt = ufdTee(fdi, fds, 2);
629b27
+	    if(offt == -1){
629b27
+		fprintf(stderr, _("Failed to tee RPM\n"));
629b27
+		rc = RPMRC_FAIL;
629b27
+	    }
629b27
+	    Fclose(fds[0]);
629b27
+	    Fclose(fds[1]);
629b27
+	    w = waitpid(cpids[0], &wstatus, 0);
629b27
+	    if (w == -1) {
629b27
+		fprintf(stderr, _("waitpid cpids[0] failed\n"));
629b27
+		rc = RPMRC_FAIL;
629b27
+	    }
629b27
+	    w = waitpid(cpids[1], &wstatus, 0);
629b27
+	    if (w == -1) {
629b27
+		fprintf(stderr, _("waitpid cpids[1] failed\n"));
629b27
+		rc = RPMRC_FAIL;
629b27
+	    }
629b27
+	}
629b27
+    }
629b27
+
629b27
+    return rc;
629b27
+}
629b27
+
629b27
 int main(int argc, char *argv[]) {
629b27
     rpmRC rc;
629b27
     int cprc = 0;
629b27
-    uint8_t algos[argc - 1];
629b27
+    poptContext optCon = NULL;
629b27
+    const char **args = NULL;
629b27
+    int nb_algos = 0;
629b27
+
629b27
     int mainpipefd[2];
629b27
     int metapipefd[2];
629b27
     pid_t cpid, w;
629b27
@@ -338,29 +523,30 @@ int main(int argc, char *argv[]) {
629b27
 
629b27
     xsetprogname(argv[0]);	/* Portability call -- see system.h */
629b27
     rpmReadConfigFiles(NULL, NULL);
629b27
+    optCon = rpmcliInit(argc, argv, optionsTable);
629b27
+    poptSetOtherOptionHelp(optCon, "[OPTIONS]* <DIGESTALGO>");
629b27
 
629b27
-    if (argc > 1 && (rstreq(argv[1], "-h") || rstreq(argv[1], "--help"))) {
629b27
-	fprintf(stderr, _("Usage: %s [DIGESTALGO]...\n"), argv[0]);
629b27
-	exit(EXIT_FAILURE);
629b27
-    }
629b27
-
629b27
-    if (argc == 1) {
629b27
+    if (poptPeekArg(optCon) == NULL) {
629b27
 	fprintf(stderr,
629b27
 		_("Need at least one DIGESTALGO parameter, e.g. 'SHA256'\n"));
629b27
+	poptPrintUsage(optCon, stderr, 0);
629b27
 	exit(EXIT_FAILURE);
629b27
     }
629b27
 
629b27
-    for (int x = 0; x < (argc - 1); x++) {
629b27
-	if (pgpStringVal(PGPVAL_HASHALGO, argv[x + 1], &algos[x]) != 0)
629b27
+    args = poptGetArgs(optCon);
629b27
+
629b27
+    for (nb_algos=0; args[nb_algos]; nb_algos++);
629b27
+    uint8_t algos[nb_algos];
629b27
+    for (int x = 0; x < nb_algos; x++) {
629b27
+	if (pgpStringVal(PGPVAL_HASHALGO, args[x], &algos[x]) != 0)
629b27
 	{
629b27
 	    fprintf(stderr,
629b27
 		    _("Unable to resolve '%s' as a digest algorithm, exiting\n"),
629b27
-		    argv[x + 1]);
629b27
+		    args[x]);
629b27
 	    exit(EXIT_FAILURE);
629b27
 	}
629b27
     }
629b27
 
629b27
-
629b27
     if (pipe(mainpipefd) == -1) {
629b27
 	fprintf(stderr, _("Main pipe failure\n"));
629b27
 	exit(EXIT_FAILURE);
629b27
@@ -369,6 +555,7 @@ int main(int argc, char *argv[]) {
629b27
 	fprintf(stderr, _("Meta pipe failure\n"));
629b27
 	exit(EXIT_FAILURE);
629b27
     }
629b27
+
629b27
     cpid = fork();
629b27
     if (cpid == 0) {
629b27
 	/* child: digestor */
629b27
@@ -377,7 +564,7 @@ int main(int argc, char *argv[]) {
629b27
 	FD_t fdi = fdDup(STDIN_FILENO);
629b27
 	FD_t fdo = fdDup(mainpipefd[1]);
629b27
 	FD_t validationo = fdDup(metapipefd[1]);
629b27
-	rc = digestor(fdi, fdo, validationo, algos, argc - 1);
629b27
+	rc = digestor(fdi, fdo, validationo, algos, nb_algos);
629b27
 	Fclose(validationo);
629b27
 	Fclose(fdo);
629b27
 	Fclose(fdi);
629b27
@@ -386,12 +573,10 @@ int main(int argc, char *argv[]) {
629b27
 	close(mainpipefd[1]);
629b27
 	close(metapipefd[1]);
629b27
 	FD_t fdi = fdDup(mainpipefd[0]);
629b27
-	FD_t validationi = fdDup(metapipefd[0]);
629b27
-	rc = process_package(fdi, validationi);
629b27
-	Fclose(validationi);
629b27
-	/* fdi is normally closed through the stacked file gzdi in the
629b27
-	 * function.
629b27
-	 * Wait for child process (digestor for stdin) to complete.
629b27
+	FD_t digestori = fdDup(metapipefd[0]);
629b27
+	rc = teeRpm(fdi, digestori);
629b27
+	Fclose(digestori);
629b27
+	/* Wait for child process (digestor for stdin) to complete.
629b27
 	 */
629b27
 	if (rc != RPMRC_OK) {
629b27
 	    if (kill(cpid, SIGTERM) != 0) {
629b27
@@ -402,7 +587,7 @@ int main(int argc, char *argv[]) {
629b27
 	}
629b27
 	w = waitpid(cpid, &wstatus, 0);
629b27
 	if (w == -1) {
629b27
-	    fprintf(stderr, _("waitpid failed\n"));
629b27
+	    fprintf(stderr, _("waitpid %d failed %s\n"), cpid, strerror(errno));
629b27
 	    cprc = EXIT_FAILURE;
629b27
 	} else if (WIFEXITED(wstatus)) {
629b27
 	    cprc = WEXITSTATUS(wstatus);
629b27
-- 
629b27
2.35.1
629b27