git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Fabian Stelzer <fs@gigacodes.de>
To: "René Scharfe" <l.s.r@web.de>, "Git List" <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH 1/2] gpg-interface: handle missing " with " gracefully in parse_ssh_output()
Date: Mon, 1 Nov 2021 09:40:18 +0100	[thread overview]
Message-ID: <dedf3971-adb1-c355-0b05-4e94ad92e1f7@gigacodes.de> (raw)
In-Reply-To: <f6fca7c0-079c-4337-23d9-cd970c79b8ad@web.de>

On 30.10.21 19:04, René Scharfe wrote:
> If the output of ssh-keygen starts with "Good \"git\" signature for ",
> but is not followed by " with " for some reason, then parse_ssh_output()
> uses -1 as the len parameter of xmemdupz(), which in turn will end the
> program.  Reject the signature and carry on instead in that case.
> 
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
> This code was added after v2.33.0.
> Patch formatted with --inter-hunk-context=2 for easier review.
> 
> Silly bonus question: What's the purpose of the "+ 1" and "- 1", which
> seem to cancel each other out?

They do. But only for the xmemdupz. It is important for the strstr() to
skip over already found strings (" with " could be in the principal name
as well - multiple times even). And doing strstr(line +1, ...) can be
problematic for the first iteration.

> 
>  gpg-interface.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/gpg-interface.c b/gpg-interface.c
> index 800d8caa67..62d340e78a 100644
> --- a/gpg-interface.c
> +++ b/gpg-interface.c
> @@ -387,17 +387,19 @@ static void parse_ssh_output(struct signature_check *sigc)
>  	line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
> 
>  	if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
> -		/* Valid signature and known principal */
> -		sigc->result = 'G';
> -		sigc->trust_level = TRUST_FULLY;
> -
>  		/* Search for the last "with" to get the full principal */
>  		principal = line;
>  		do {
>  			search = strstr(line, " with ");
>  			if (search)
>  				line = search + 1;
>  		} while (search != NULL);
> +		if (line == principal)
> +			goto cleanup;
> +
> +		/* Valid signature and known principal */
> +		sigc->result = 'G';
> +		sigc->trust_level = TRUST_FULLY;
>  		sigc->signer = xmemdupz(principal, line - principal - 1);
>  	} else if (skip_prefix(line, "Good \"git\" signature with ", &line)) {
>  		/* Valid signature, but key unknown */
> --
> 2.33.1
> 

The fix looks good otherwise.
Thanks!

  parent reply	other threads:[~2021-11-01  8:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-30 17:04 [PATCH 1/2] gpg-interface: handle missing " with " gracefully in parse_ssh_output() René Scharfe
2021-10-30 17:07 ` [PATCH 2/2] gpg-interface: avoid buffer overrun " René Scharfe
2021-11-01  6:07   ` Junio C Hamano
2021-11-01  8:33   ` Fabian Stelzer
2021-11-01 17:44     ` Junio C Hamano
2021-11-01  6:08 ` [PATCH 1/2] gpg-interface: handle missing " with " gracefully " Junio C Hamano
2021-11-01  8:40 ` Fabian Stelzer [this message]
2021-11-01 18:36   ` René Scharfe

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=dedf3971-adb1-c355-0b05-4e94ad92e1f7@gigacodes.de \
    --to=fs@gigacodes.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).