Skip to content

Commit

Permalink
Eliminate hardcoded GPG references from user visible messages
Browse files Browse the repository at this point in the history
Use the OpenPGP standard name or the configured+parsed signing command
in messages as appropriate. Also detect if we're specifically using
gpg and only set up its environment in that case to avoid bleeding
those messages to innocent bypassers.

Fixes: rpm-software-management#3274
  • Loading branch information
pmatilai committed Sep 5, 2024
1 parent 729ca53 commit 304feca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
42 changes: 25 additions & 17 deletions sign/rpmgensig.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,29 @@ static int runGPG(sigTarget sigt, const char *sigfile)
}

if (!(pid = fork())) {
const char *tty = ttyname(STDIN_FILENO);
const char *gpg_path = NULL;

if (!getenv("GPG_TTY") && (!tty || setenv("GPG_TTY", tty, 0)))
rpmlog(RPMLOG_WARNING, _("Could not set GPG_TTY to stdin: %m\n"));

gpg_path = rpmExpand("%{?_gpg_path}", NULL);
if (gpg_path && *gpg_path != '\0')
(void) setenv("GNUPGHOME", gpg_path, 1);
/* GnuPG needs extra setup, try to see if that's what we're running */
char *out = rpmExpand("%(", argv[0], " --version 2> /dev/null)", NULL);
int using_gpg = (strstr(out, "GnuPG") != NULL);
if (using_gpg) {
const char *tty = ttyname(STDIN_FILENO);
const char *gpg_path = NULL;

if (!getenv("GPG_TTY") && (!tty || setenv("GPG_TTY", tty, 0)))
rpmlog(RPMLOG_WARNING, _("Could not set GPG_TTY to stdin: %m\n"));

gpg_path = rpmExpand("%{?_gpg_path}", NULL);
if (gpg_path && *gpg_path != '\0')
(void) setenv("GNUPGHOME", gpg_path, 1);
}
free(out);

dup2(pipefd[0], STDIN_FILENO);
close(pipefd[1]);

rc = execve(argv[0], argv+1, environ);

rpmlog(RPMLOG_ERR, _("Could not exec %s: %s\n"), "gpg",
strerror(errno));
rpmlog(RPMLOG_ERR, _("Could not exec %s: %s\n"), argv[0],
strerror(errno));
_exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -298,9 +304,11 @@ static int runGPG(sigTarget sigt, const char *sigfile)
} while (reaped == -1 && errno == EINTR);

if (reaped == -1) {
rpmlog(RPMLOG_ERR, _("gpg waitpid failed (%s)\n"), strerror(errno));
rpmlog(RPMLOG_ERR, _("%s waitpid failed (%s)\n"), argv[0],
strerror(errno));
} else if (!WIFEXITED(status) || WEXITSTATUS(status)) {
rpmlog(RPMLOG_ERR, _("gpg exec failed (%d)\n"), WEXITSTATUS(status));
rpmlog(RPMLOG_ERR, _("%s exec failed (%d)\n"), argv[0],
WEXITSTATUS(status));
} else {
rc = 0;
}
Expand Down Expand Up @@ -331,13 +339,13 @@ static rpmtd makeGPGSignature(Header sigh, int ishdr, sigTarget sigt)
goto exit;

if (stat(sigfile, &st)) {
/* GPG failed to write signature */
rpmlog(RPMLOG_ERR, _("gpg failed to write signature\n"));
/* External command failed to write signature */
rpmlog(RPMLOG_ERR, _("failed to write signature\n"));
goto exit;
}

pktlen = st.st_size;
rpmlog(RPMLOG_DEBUG, "GPG sig size: %zd\n", pktlen);
rpmlog(RPMLOG_DEBUG, "OpenPGP sig size: %zd\n", pktlen);
pkt = (uint8_t *)xmalloc(pktlen);

{ FD_t fd;
Expand All @@ -354,7 +362,7 @@ static rpmtd makeGPGSignature(Header sigh, int ishdr, sigTarget sigt)
}
}

rpmlog(RPMLOG_DEBUG, "Got %zd bytes of GPG sig\n", pktlen);
rpmlog(RPMLOG_DEBUG, "Got %zd bytes of OpenPGP sig\n", pktlen);

/* Parse the signature, change signature tag as appropriate. */
sigtd = makeSigTag(sigh, ishdr, pkt, pktlen);
Expand Down
9 changes: 9 additions & 0 deletions tests/rpmsigdig.at
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,15 @@ run rpmsign --define "__gpg_sign_cmd mumble" --key-id 1964C5FC --addsign "${RPMT
[error: Invalid sign command: mumble
])

RPMTEST_CHECK([
run rpmsign --define "__gpg /gnus/not/here" --key-id 1964C5FC --addsign "${RPMTEST}"/tmp/hello-2.0-1.x86_64.rpm > /dev/null
],
[1],
[],
[error: Could not exec /gnus/not/here: No such file or directory
error: /gnus/not/here exec failed (1)
])

# rpmsign --addsign <signed>
RPMTEST_CHECK([
RPMDB_INIT
Expand Down

0 comments on commit 304feca

Please sign in to comment.