Git development
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Junio C Hamano <gitster@pobox.com>
Cc: Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>,
	 git@vger.kernel.org, Phillip Wood <phillip.wood123@gmail.com>,
	 Patrick Steinhardt <ps@pks.im>,
	Harald Nordgren <haraldnordgren@gmail.com>,
	 Simon Richter <Simon.Richter@hogyros.de>
Subject: Re: [PATCH v2 2/5] http: use `size_t` for byte position in fwrite_sha1_file
Date: Wed, 25 Nov 2026 15:04:57 +0100 (CET)	[thread overview]
Message-ID: <7e0e2cfd-e189-cb75-6252-3fed5ccb093e@gmx.de> (raw)
In-Reply-To: <xmqqfqwqt8sl.fsf@gitster.g>

Hi Junio,

On Tue, 24 Nov 2026, Junio C Hamano wrote:

> "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> >
> > The `fwrite_sha1_file()` function tracks the number of bytes written via
> > an `int` variable, but the total size to write is of type `size_t`. When
> > the HTTP response body exceeds INT_MAX (~2GB), this causes an overflow
> > and a heap under-read, then writes that data to the local object file.
> >
> > The hash check afterward would reject the corrupt object (so no bad data
> > enters the object store), but the under-read itself could leak adjacent
> > heap contents to the temporary file. In practice this is unlikely
> > because curl delivers data in small chunks (typically 16KB per callback
> > invocation), so in practice, the total size is far below INT_MAX in a
> > single call. However, the function interface accepts arbitrary sizes,
> > and the accumulation across the loop means that the `int` variable grows
> > across calls within a single invocation, not across callbacks.
> 
> The second paragraph also repeats "In practice ... so in practice".
> 
> I had to read the two paragraphs a few times to convince myself that
> they are not delivering contradictory messages.  In fwrite_sha1_file(),
> 'posn' is a local variable reset to 0 on every invocation, so a
> multi-gigabyte HTTP response body delivered in normal chunks will never
> overflow 'posn'.  Only a single invocation whose eltsize * nmemb is
> larger than INT_MAX would overflow it, as the second paragraph notes.
> 
> Perhaps shorten the thing (while removing the repetition) along the
> lines of something like...
> 
>     The fwrite_sha1_file() function tracks the number of bytes written
>     during a single invocation in an int variable ('posn'), even though
>     the buffer length size = eltsize * nmemb has type size_t.  If a
>     caller or callback ever passed more than INT_MAX bytes in a single
>     call, 'posn' could overflow and wrap negative, causing (char *)ptr +
>     posn to under-read the buffer.  While curl delivers data in small
>     chunks in practice, matching the variable type to 'size' avoids the
>     defect.
> 
> perhaps?

I'll use that.

Thanks!
Johannes

> 
> > diff --git a/http.c b/http.c
> > index d79678d01f..2879eb3874 100644
> > --- a/http.c
> > +++ b/http.c
> > @@ -2848,7 +2848,7 @@ static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
> >  {
> >  	unsigned char expn[4096];
> >  	size_t size = eltsize * nmemb;
> > -	int posn = 0;
> > +	size_t posn = 0;
> >  	struct http_object_request *freq = data;
> >  	struct active_request_slot *slot = freq->slot;
> 
> And of course, this single line change itself is good.
> 
> xwrite() clamps per-call I/O to MAX_IO_SIZE, and posn/eltsize division
> avoids signed-to-unsigned promotion on the value this function returns.
> 
> Looking good.
> 
> 

  reply	other threads:[~2026-11-25 14:05 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  6:09 [PATCH 0/5] Address overflows pointed out by Coverity Johannes Schindelin via GitGitGadget
2026-10-14  6:09 ` [PATCH 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-10-14  6:09 ` [PATCH 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-10-16  8:38   ` Patrick Steinhardt
2026-10-16 14:30     ` Junio C Hamano
2026-10-17  0:18       ` Simon Richter
2026-10-17  4:10         ` Junio C Hamano
2026-10-17  4:11         ` Junio C Hamano
2026-10-14  6:09 ` [PATCH 3/5] wrapper: reject insanely large byte counts when reading/writing Johannes Schindelin via GitGitGadget
2026-10-14 11:37   ` Phillip Wood
2026-11-15 19:24     ` Johannes Schindelin
2026-10-14  6:09 ` [PATCH 4/5] git-compat-util: add `signed_mult_overflows` and `mult_overflows` macros Johannes Schindelin via GitGitGadget
2026-10-15  9:27   ` Phillip Wood
2026-11-15 19:29     ` Johannes Schindelin
2026-10-14  6:09 ` [PATCH 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-10-14 21:44   ` Junio C Hamano
2026-10-15  9:28   ` Phillip Wood
2026-11-15 19:35     ` Johannes Schindelin
2026-10-16  8:43 ` [PATCH 0/5] Address overflows pointed out by Coverity Harald Nordgren
2026-11-07 22:36 ` Junio C Hamano
2026-11-23 22:13 ` [PATCH v2 " Johannes Schindelin via GitGitGadget
2026-11-23 22:13   ` [PATCH v2 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-11-24 17:34     ` Junio C Hamano
2026-11-23 22:13   ` [PATCH v2 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-11-24 17:54     ` Junio C Hamano
2026-11-25 14:04       ` Johannes Schindelin [this message]
2026-11-23 22:13   ` [PATCH v2 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-11-24 14:37     ` Phillip Wood
2026-11-25 14:20       ` Johannes Schindelin
2026-11-23 22:13   ` [PATCH v2 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-11-24 14:24     ` Junio C Hamano
2026-11-23 22:13   ` [PATCH v2 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-11-24 14:44     ` Phillip Wood
2026-11-24 18:04   ` [PATCH v2 0/5] Address overflows pointed out by Coverity Junio C Hamano
2026-11-25 20:55 ` [PATCH v3 " Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 1/5] archive-tar: guard get_path_prefix against zero-length input Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 2/5] http: use `size_t` for byte position in fwrite_sha1_file Johannes Schindelin via GitGitGadget
2026-11-25 20:55   ` [PATCH v3 3/5] wrapper: avoid byte-count overflow in write_in_full() Johannes Schindelin via GitGitGadget
2026-11-25 23:47     ` Junio C Hamano
2026-11-26 15:14       ` Phillip Wood
2026-11-25 20:55   ` [PATCH v3 4/5] wrapper: add read_in_full_or_error() Johannes Schindelin via GitGitGadget
2026-11-25 23:57     ` Junio C Hamano
2026-11-25 20:55   ` [PATCH v3 5/5] date: avoid overflow in approxidate relative-time multiplication Johannes Schindelin via GitGitGadget
2026-11-25 22:08     ` Junio C Hamano
2026-11-26 14:08     ` Aniket Gupta

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=7e0e2cfd-e189-cb75-6252-3fed5ccb093e@gmx.de \
    --to=johannes.schindelin@gmx.de \
    --cc=Simon.Richter@hogyros.de \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=gitster@pobox.com \
    --cc=haraldnordgren@gmail.com \
    --cc=phillip.wood123@gmail.com \
    --cc=ps@pks.im \
    /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