Skip to content

Commit 0d2b664

Browse files
peffgitster
authored andcommitted
verify_signed_buffer: use pipe_command
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 96335bc commit 0d2b664

File tree

1 file changed

+3
-19
lines changed

1 file changed

+3
-19
lines changed

gpg-interface.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,29 +229,13 @@ int verify_signed_buffer(const char *payload, size_t payload_size,
229229
"--status-fd=1",
230230
"--verify", temp.filename.buf, "-",
231231
NULL);
232-
gpg.in = -1;
233-
gpg.out = -1;
234-
if (gpg_output)
235-
gpg.err = -1;
236-
if (start_command(&gpg)) {
237-
delete_tempfile(&temp);
238-
return error(_("could not run gpg."));
239-
}
240232

241-
sigchain_push(SIGPIPE, SIG_IGN);
242-
write_in_full(gpg.in, payload, payload_size);
243-
close(gpg.in);
244-
245-
if (gpg_output) {
246-
strbuf_read(gpg_output, gpg.err, 0);
247-
close(gpg.err);
248-
}
249233
if (!gpg_status)
250234
gpg_status = &buf;
251-
strbuf_read(gpg_status, gpg.out, 0);
252-
close(gpg.out);
253235

254-
ret = finish_command(&gpg);
236+
sigchain_push(SIGPIPE, SIG_IGN);
237+
ret = pipe_command(&gpg, payload, payload_size,
238+
gpg_status, 0, gpg_output, 0);
255239
sigchain_pop(SIGPIPE);
256240

257241
delete_tempfile(&temp);

0 commit comments

Comments
 (0)