All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: Michael J Gruber <git@drmicha.warpmail.net>,
	Junio C Hamano <gitster@pobox.com>
Subject: [PATCH 5/7] verify_signed_buffer: use pipe_command
Date: Thu, 16 Jun 2016 05:38:52 -0400	[thread overview]
Message-ID: <20160616093852.GE15851@sigill.intra.peff.net> (raw)
In-Reply-To: <20160616093248.GA15130@sigill.intra.peff.net>

This is shorter and should make the function easier to
follow. But more importantly, it removes the possibility of
any deadlocks based on reading or writing to gpg.

It's not clear if such a deadlock is possible in practice.

We do write the whole payload before reading anything, so we
could deadlock there. However, in practice gpg will need to
read our whole input to verify the signature, so it will
drain our payload first. It could write an error to stderr
before reading, but it's unlikely that such an error
wouldn't be followed by it immediately exiting, or that the
error would actually be larger than a pipe buffer.

On the writing side, we drain stderr (with the
human-readable output) in its entirety before reading stdout
(with the status-fd data). Running strace on "gpg --verify"
does show interleaved output on the two descriptors:

  write(2, "gpg: ", 5)                    = 5
  write(2, "Signature made Thu 16 Jun 2016 0"..., 73) = 73
  write(1, "[GNUPG:] SIG_ID tQw8KGcs9rBfLvAj"..., 66) = 66
  write(1, "[GNUPG:] GOODSIG 69808639F9430ED"..., 60) = 60
  write(2, "gpg: ", 5)                    = 5
  write(2, "Good signature from \"Jeff King <"..., 47) = 47
  write(2, "\n", 1)                       = 1
  write(2, "gpg: ", 5)                    = 5
  write(2, "                aka \"Jeff King <"..., 49) = 49
  write(2, "\n", 1)                       = 1
  write(1, "[GNUPG:] VALIDSIG C49CE24156AF08"..., 135) = 135
  write(1, "[GNUPG:] TRUST_ULTIMATE\n", 24) = 24

The second line written to stdout there contains the
signer's UID, which can be arbitrarily long. If it fills the
pipe buffer, then gpg would block writing to its stdout,
while we are blocked trying to read its stderr.

In practice, GPG seems to limit UIDs to 2048 bytes, so
unless your pipe buffer size is quite small, or unless gpg
does not enforce the limit under some conditions, this seems
unlikely in practice.

Still, it is not hard for us to be cautious and just use
pipe_command.

Signed-off-by: Jeff King <peff@peff.net>
---
 gpg-interface.c | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)

diff --git a/gpg-interface.c b/gpg-interface.c
index 854c1e5..c98035d 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -229,29 +229,13 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
 			 "--status-fd=1",
 			 "--verify", temp.filename.buf, "-",
 			 NULL);
-	gpg.in = -1;
-	gpg.out = -1;
-	if (gpg_output)
-		gpg.err = -1;
-	if (start_command(&gpg)) {
-		delete_tempfile(&temp);
-		return error(_("could not run gpg."));
-	}
 
-	sigchain_push(SIGPIPE, SIG_IGN);
-	write_in_full(gpg.in, payload, payload_size);
-	close(gpg.in);
-
-	if (gpg_output) {
-		strbuf_read(gpg_output, gpg.err, 0);
-		close(gpg.err);
-	}
 	if (!gpg_status)
 		gpg_status = &buf;
-	strbuf_read(gpg_status, gpg.out, 0);
-	close(gpg.out);
 
-	ret = finish_command(&gpg);
+	sigchain_push(SIGPIPE, SIG_IGN);
+	ret = pipe_command(&gpg, payload, payload_size,
+			   gpg_status, 0, gpg_output, 0);
 	sigchain_pop(SIGPIPE);
 
 	delete_tempfile(&temp);
-- 
2.9.0.160.g4984cba


  parent reply	other threads:[~2016-06-16  9:38 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-16  9:32 [PATCH 0/7] gpg-interface cleanups Jeff King
2016-06-16  9:33 ` [PATCH 1/7] gpg-interface: use child_process.args Jeff King
2016-06-16  9:34 ` [PATCH 2/7] verify_signed_buffer: drop pbuf variable Jeff King
2016-06-16  9:35 ` [PATCH 3/7] verify_signed_buffer: use tempfile object Jeff King
2016-06-16  9:37 ` [PATCH 4/7] run-command: add pipe_command helper Jeff King
2016-06-17 20:03   ` Eric Sunshine
2016-06-17 23:28     ` Jeff King
2016-06-17 23:31       ` Eric Sunshine
2016-06-16  9:38 ` Jeff King [this message]
2016-06-16  9:39 ` [PATCH 6/7] sign_buffer: use pipe_command Jeff King
2016-06-16  9:42 ` [PATCH 7/7] gpg-interface: check gpg signature creation status Jeff King
2016-06-16 18:20 ` [PATCH 0/7] gpg-interface cleanups Junio C Hamano
2016-06-17  9:20   ` Michael J Gruber
2016-06-17  9:21     ` Jeff King
2016-06-17 23:38 ` [PATCH v2 " Jeff King
2016-06-17 23:38   ` [PATCH v2 1/7] gpg-interface: use child_process.args Jeff King
2016-06-17 23:38   ` [PATCH v2 2/7] verify_signed_buffer: drop pbuf variable Jeff King
2016-06-17 23:38   ` [PATCH v2 3/7] verify_signed_buffer: use tempfile object Jeff King
2016-06-17 23:38   ` [PATCH v2 4/7] run-command: add pipe_command helper Jeff King
2016-06-17 23:38   ` [PATCH v2 5/7] verify_signed_buffer: use pipe_command Jeff King
2016-06-17 23:38   ` [PATCH v2 6/7] sign_buffer: " Jeff King
2016-06-17 23:38   ` [PATCH v2 7/7] gpg-interface: check gpg signature creation status Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160616093852.GE15851@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@drmicha.warpmail.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.