dcavalca / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone

Blame SOURCES/0011-rpm2extents-Perform-digest-computation-within-the-va.patch

629b27
From 86776bf17f1644c76fdf8b87042645cf77bd3873 Mon Sep 17 00:00:00 2001
629b27
From: chantra <chantr4@gmail.com>
629b27
Date: Wed, 2 Feb 2022 13:34:28 -0800
629b27
Subject: [PATCH 11/30] [rpm2extents] Perform digest computation within the
629b27
 validator
629b27
629b27
The validator calls `rpmcliVerifySignaturesFD` which under the hood
629b27
performs `Fread`. Digests are computed/updated for each `Fread`.
629b27
629b27
This diffs takes advantage of that by initializing the digest before
629b27
calling `rpmcliVerifySignaturesFD`. Once `rpmcliVerifySignaturesFD` as
629b27
returned and the file has been read, the digests are available.
629b27
629b27
This saves us from spawning a `digestor` process, as well as performing
629b27
an extra file read within it.
629b27
---
629b27
 rpm2extents.c | 234 +++++++++++++++++++++++---------------------------
629b27
 1 file changed, 106 insertions(+), 128 deletions(-)
629b27
629b27
diff --git a/rpm2extents.c b/rpm2extents.c
629b27
index 065a00306..e316a2834 100644
629b27
--- a/rpm2extents.c
629b27
+++ b/rpm2extents.c
629b27
@@ -64,38 +64,37 @@ static struct poptOption optionsTable[] = {
629b27
 };
629b27
 
629b27
 
629b27
-static int digestor(
629b27
+static void FDDigestInit(FD_t fdi, uint8_t algos[], uint32_t algos_len){
629b27
+    int algo;
629b27
+
629b27
+    for (algo = 0; algo < algos_len; algo++) {
629b27
+	fdInitDigest(fdi, algos[algo], 0);
629b27
+    }
629b27
+}
629b27
+
629b27
+static int FDWriteDigests(
629b27
     FD_t fdi,
629b27
     FD_t fdo,
629b27
-    FD_t validationo,
629b27
     uint8_t algos[],
629b27
-    uint32_t algos_len
629b27
-)
629b27
+    uint32_t algos_len)
629b27
 {
629b27
-    ssize_t fdilength;
629b27
     const char *filedigest, *algo_name;
629b27
     size_t filedigest_len, len;
629b27
     uint32_t algo_name_len, algo_digest_len;
629b27
     int algo;
629b27
     rpmRC rc = RPMRC_FAIL;
629b27
 
629b27
-    for (algo = 0; algo < algos_len; algo++) {
629b27
-	fdInitDigest(fdi, algos[algo], 0);
629b27
-    }
629b27
-    fdilength = ufdCopy(fdi, fdo);
629b27
-    if (fdilength == -1) {
629b27
-	fprintf(stderr, _("digest cat failed\n"));
629b27
-	goto exit;
629b27
-    }
629b27
+    ssize_t fdilength = fdOp(fdi, FDSTAT_READ)->bytes;
629b27
 
629b27
     len = sizeof(fdilength);
629b27
-    if (Fwrite(&fdilength, len, 1, validationo) != len) {
629b27
+    if (Fwrite(&fdilength, len, 1, fdo) != len) {
629b27
 	fprintf(stderr, _("Unable to write input length %zd\n"), fdilength);
629b27
 	goto exit;
629b27
     }
629b27
     len = sizeof(algos_len);
629b27
-    if (Fwrite(&algos_len, len, 1, validationo) != len) {
629b27
-	fprintf(stderr, _("Unable to write number of validation digests\n"));
629b27
+    if (Fwrite(&algos_len, len, 1, fdo) != len) {
629b27
+	algo_digest_len = (uint32_t)filedigest_len;
629b27
+	fprintf(stderr, _("Unable to write number of digests\n"));
629b27
 	goto exit;
629b27
     }
629b27
     for (algo = 0; algo < algos_len; algo++) {
629b27
@@ -106,24 +105,24 @@ static int digestor(
629b27
 	algo_digest_len = (uint32_t)filedigest_len;
629b27
 
629b27
 	len = sizeof(algo_name_len);
629b27
-	if (Fwrite(&algo_name_len, len, 1, validationo) != len) {
629b27
+	if (Fwrite(&algo_name_len, len, 1, fdo) != len) {
629b27
 	    fprintf(stderr,
629b27
-		    _("Unable to write validation algo name length\n"));
629b27
+		    _("Unable to write digest algo name length\n"));
629b27
 	    goto exit;
629b27
 	}
629b27
 	len = sizeof(algo_digest_len);
629b27
-	if (Fwrite(&algo_digest_len, len, 1, validationo) != len) {
629b27
+	if (Fwrite(&algo_digest_len, len, 1, fdo) != len) {
629b27
 	    fprintf(stderr,
629b27
-		    _("Unable to write number of bytes for validation digest\n"));
629b27
+		    _("Unable to write number of bytes for digest\n"));
629b27
 	     goto exit;
629b27
 	}
629b27
-	if (Fwrite(algo_name, algo_name_len, 1, validationo) != algo_name_len) {
629b27
-	    fprintf(stderr, _("Unable to write validation algo name\n"));
629b27
+	if (Fwrite(algo_name, algo_name_len, 1, fdo) != algo_name_len) {
629b27
+	    fprintf(stderr, _("Unable to write digest algo name\n"));
629b27
 	    goto exit;
629b27
 	}
629b27
-	if (Fwrite(filedigest, algo_digest_len, 1, validationo ) != algo_digest_len) {
629b27
+	if (Fwrite(filedigest, algo_digest_len, 1, fdo ) != algo_digest_len) {
629b27
 	    fprintf(stderr,
629b27
-		    _("Unable to write validation digest value %u, %zu\n"),
629b27
+		    _("Unable to write digest value %u, %zu\n"),
629b27
 		    algo_digest_len, filedigest_len);
629b27
 	    goto exit;
629b27
 	}
629b27
@@ -133,38 +132,66 @@ exit:
629b27
     return rc;
629b27
 }
629b27
 
629b27
-static rpmRC validator(FD_t fdi, FD_t fdo){
629b27
-    int rc;
629b27
-    char *msg = NULL;
629b27
-    rpmts ts = rpmtsCreate();
629b27
+static rpmRC FDWriteSignaturesValidation(FD_t fdo, int rpmvsrc, char *msg) {
629b27
     size_t len;
629b27
+    rpmRC rc = RPMRC_FAIL;
629b27
 
629b27
-    rpmtsSetRootDir(ts, rpmcliRootDir);
629b27
-    rc = rpmcliVerifySignaturesFD(ts, fdi, &msg;;
629b27
-    if(rc){
629b27
-	fprintf(stderr, _("Error validating package\n"));
629b27
+    if(rpmvsrc){
629b27
+	fprintf(stderr, _("Error verifying package signatures\n"));
629b27
     }
629b27
-    len = sizeof(rc);
629b27
-    if (Fwrite(&rc, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write validator RC code %d\n"), rc);
629b27
+
629b27
+    len = sizeof(rpmvsrc);
629b27
+    if (Fwrite(&rpmvsrc, len, 1, fdo) != len) {
629b27
+	fprintf(stderr, _("Unable to write signature verification RC code %d\n"), rpmvsrc);
629b27
+	goto exit;
629b27
+    }
629b27
+    size_t content_len = msg ? strlen(msg) : 0;
629b27
+    len = sizeof(content_len);
629b27
+    if (Fwrite(&content_len, len, 1, fdo) != len) {
629b27
+	fprintf(stderr, _("Unable to write signature verification output length %zd\n"), content_len);
629b27
 	goto exit;
629b27
     }
629b27
-    size_t validator_len = msg ? strlen(msg) : 0;
629b27
-    len = sizeof(validator_len);
629b27
-    if (Fwrite(&validator_len, len, 1, fdo) != len) {
629b27
-	fprintf(stderr, _("Unable to write validator output length code %zd\n"), validator_len);
629b27
+    if (Fwrite(msg, content_len, 1, fdo) != content_len) {
629b27
+	fprintf(stderr, _("Unable to write signature verification output %s\n"), msg);
629b27
 	goto exit;
629b27
     }
629b27
-    if (Fwrite(msg, validator_len, 1, fdo) != validator_len) {
629b27
-	fprintf(stderr, _("Unable to write validator output %s\n"), msg);
629b27
+
629b27
+    rc = RPMRC_OK;
629b27
+exit:
629b27
+
629b27
+    return rc;
629b27
+}
629b27
+
629b27
+static rpmRC validator(FD_t fdi, FD_t digesto, FD_t sigo,
629b27
+	uint8_t algos[],
629b27
+	uint32_t algos_len){
629b27
+    int rpmvsrc;
629b27
+    rpmRC rc = RPMRC_FAIL;
629b27
+    char *msg = NULL;
629b27
+    rpmts ts = rpmtsCreate();
629b27
+
629b27
+    rpmtsSetRootDir(ts, rpmcliRootDir);
629b27
+
629b27
+    FDDigestInit(fdi, algos, algos_len);
629b27
+
629b27
+    rpmvsrc = rpmcliVerifySignaturesFD(ts, fdi, &msg;;
629b27
+
629b27
+    // Write result of digest computation
629b27
+    if(FDWriteDigests(fdi, digesto, algos, algos_len) != RPMRC_OK) {
629b27
+	fprintf(stderr, _("Failed to write digests"));
629b27
 	goto exit;
629b27
     }
629b27
 
629b27
+    // Write result of signature validation.
629b27
+    if(FDWriteSignaturesValidation(sigo, rpmvsrc, msg)) {
629b27
+	goto exit;
629b27
+    }
629b27
+    rc = RPMRC_OK;
629b27
 exit:
629b27
     if(msg) {
629b27
 	free(msg);
629b27
     }
629b27
-    return rc ? RPMRC_FAIL : RPMRC_OK;
629b27
+    return rc;
629b27
 }
629b27
 
629b27
 static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
629b27
@@ -422,12 +449,16 @@ static off_t ufdTee(FD_t sfd, FD_t *fds, int len)
629b27
     return total;
629b27
 }
629b27
 
629b27
-static int teeRpm(FD_t fdi, FD_t digestori) {
629b27
-    rpmRC rc;
629b27
+static rpmRC teeRpm(FD_t fdi, uint8_t algos[], uint32_t algos_len) {
629b27
+    rpmRC rc = RPMRC_FAIL;
629b27
     off_t offt = -1;
629b27
+    // tee-ed stdin
629b27
     int processorpipefd[2];
629b27
     int validatorpipefd[2];
629b27
-    int rpmsignpipefd[2];
629b27
+    // metadata
629b27
+    int meta_digestpipefd[2];
629b27
+    int meta_rpmsignpipefd[2];
629b27
+
629b27
     pid_t cpids[2], w;
629b27
     int wstatus;
629b27
     FD_t fds[2];
629b27
@@ -442,8 +473,13 @@ static int teeRpm(FD_t fdi, FD_t digestori) {
629b27
 	return RPMRC_FAIL;
629b27
     }
629b27
 
629b27
-    if (pipe(rpmsignpipefd) == -1) {
629b27
-	fprintf(stderr, _("Validator pipe failure\n"));
629b27
+    if (pipe(meta_digestpipefd) == -1) {
629b27
+	fprintf(stderr, _("Meta digest pipe failure\n"));
629b27
+	return RPMRC_FAIL;
629b27
+    }
629b27
+
629b27
+    if (pipe(meta_rpmsignpipefd) == -1) {
629b27
+	fprintf(stderr, _("Meta rpm signature pipe failure\n"));
629b27
 	return RPMRC_FAIL;
629b27
     }
629b27
 
629b27
@@ -453,16 +489,20 @@ static int teeRpm(FD_t fdi, FD_t digestori) {
629b27
 	close(processorpipefd[0]);
629b27
 	close(processorpipefd[1]);
629b27
 	close(validatorpipefd[1]);
629b27
-	close(rpmsignpipefd[0]);
629b27
+	close(meta_digestpipefd[0]);
629b27
+	close(meta_rpmsignpipefd[0]);
629b27
 	FD_t fdi = fdDup(validatorpipefd[0]);
629b27
-	FD_t fdo = fdDup(rpmsignpipefd[1]);
629b27
-	close(rpmsignpipefd[1]);
629b27
-	rc = validator(fdi, fdo);
629b27
+	FD_t digesto = fdDup(meta_digestpipefd[1]);
629b27
+	FD_t sigo = fdDup(meta_rpmsignpipefd[1]);
629b27
+	close(meta_digestpipefd[1]);
629b27
+	close(meta_rpmsignpipefd[1]);
629b27
+	rc = validator(fdi, digesto, sigo, algos, algos_len);
629b27
 	if(rc != RPMRC_OK) {
629b27
 	    fprintf(stderr, _("Validator failed\n"));
629b27
 	}
629b27
 	Fclose(fdi);
629b27
-	Fclose(fdo);
629b27
+	Fclose(digesto);
629b27
+	Fclose(sigo);
629b27
 	if (rc != RPMRC_OK) {
629b27
 	    exit(EXIT_FAILURE);
629b27
 	}
629b27
@@ -475,18 +515,21 @@ static int teeRpm(FD_t fdi, FD_t digestori) {
629b27
 	    close(validatorpipefd[0]);
629b27
 	    close(validatorpipefd[1]);
629b27
 	    close(processorpipefd[1]);
629b27
-	    close(rpmsignpipefd[1]);
629b27
+	    close(meta_digestpipefd[1]);
629b27
+	    close(meta_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
+	    FD_t sigi = fdDup(meta_rpmsignpipefd[0]);
629b27
+	    close(meta_rpmsignpipefd[0]);
629b27
+	    FD_t digestori = fdDup(meta_digestpipefd[0]);
629b27
+	    close(meta_digestpipefd[0]);
629b27
 
629b27
-	    rc = process_package(fdi, digestori, validatori);
629b27
+	    rc = process_package(fdi, digestori, sigi);
629b27
 	    if(rc != RPMRC_OK) {
629b27
 		fprintf(stderr, _("Validator failed\n"));
629b27
 	    }
629b27
 	    Fclose(digestori);
629b27
-	    Fclose(validatori);
629b27
+	    Fclose(sigi);
629b27
 	    /* fdi is normally closed through the stacked file gzdi in the
629b27
 	     * function
629b27
 	     */
629b27
@@ -505,8 +548,10 @@ static int teeRpm(FD_t fdi, FD_t digestori) {
629b27
 	    fds[1] = fdDup(validatorpipefd[1]);
629b27
 	    close(validatorpipefd[1]);
629b27
 	    close(processorpipefd[1]);
629b27
-	    close(rpmsignpipefd[0]);
629b27
-	    close(rpmsignpipefd[1]);
629b27
+	    close(meta_digestpipefd[0]);
629b27
+	    close(meta_digestpipefd[1]);
629b27
+	    close(meta_rpmsignpipefd[0]);
629b27
+	    close(meta_rpmsignpipefd[1]);
629b27
 
629b27
 	    rc = RPMRC_OK;
629b27
 	    offt = ufdTee(fdi, fds, 2);
629b27
@@ -534,16 +579,10 @@ static int teeRpm(FD_t fdi, FD_t digestori) {
629b27
 
629b27
 int main(int argc, char *argv[]) {
629b27
     rpmRC rc;
629b27
-    int cprc = 0;
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
-    int wstatus;
629b27
-
629b27
     xsetprogname(argv[0]);	/* Portability call -- see system.h */
629b27
     rpmReadConfigFiles(NULL, NULL);
629b27
     optCon = rpmcliInit(argc, argv, optionsTable);
629b27
@@ -570,69 +609,8 @@ int main(int argc, char *argv[]) {
629b27
 	}
629b27
     }
629b27
 
629b27
-    if (pipe(mainpipefd) == -1) {
629b27
-	fprintf(stderr, _("Main pipe failure\n"));
629b27
-	exit(EXIT_FAILURE);
629b27
-    }
629b27
-    if (pipe(metapipefd) == -1) {
629b27
-	fprintf(stderr, _("Meta pipe failure\n"));
629b27
-	exit(EXIT_FAILURE);
629b27
-    }
629b27
-
629b27
-    cpid = fork();
629b27
-    if (cpid == 0) {
629b27
-	/* child: digestor */
629b27
-	close(mainpipefd[0]);
629b27
-	close(metapipefd[0]);
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, nb_algos);
629b27
-	Fclose(validationo);
629b27
-	Fclose(fdo);
629b27
-	Fclose(fdi);
629b27
-    } else {
629b27
-	/* parent: main program */
629b27
-	close(mainpipefd[1]);
629b27
-	close(metapipefd[1]);
629b27
-	FD_t fdi = fdDup(mainpipefd[0]);
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
-		fprintf(stderr,
629b27
-		        _("Failed to kill digest process when main process failed: %s\n"),
629b27
-			strerror(errno));
629b27
-	    }
629b27
-	}
629b27
-	w = waitpid(cpid, &wstatus, 0);
629b27
-	if (w == -1) {
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
-	    if (cprc != 0) {
629b27
-		fprintf(stderr,
629b27
-			_("Digest process non-zero exit code %d\n"),
629b27
-			cprc);
629b27
-	    }
629b27
-	} else if (WIFSIGNALED(wstatus)) {
629b27
-	    fprintf(stderr,
629b27
-		    _("Digest process was terminated with a signal: %d\n"),
629b27
-		    WTERMSIG(wstatus));
629b27
-	    cprc = EXIT_FAILURE;
629b27
-	} else {
629b27
-	    /* Don't think this can happen, but covering all bases */
629b27
-	    fprintf(stderr, _("Unhandled circumstance in waitpid\n"));
629b27
-	    cprc = EXIT_FAILURE;
629b27
-	}
629b27
-	if (cprc != EXIT_SUCCESS) {
629b27
-	    rc = RPMRC_FAIL;
629b27
-	}
629b27
-    }
629b27
+    FD_t fdi = fdDup(STDIN_FILENO);
629b27
+    rc = teeRpm(fdi, algos, nb_algos);
629b27
     if (rc != RPMRC_OK) {
629b27
 	/* translate rpmRC into generic failure return code. */
629b27
 	return EXIT_FAILURE;
629b27
-- 
629b27
2.35.1
629b27