Re: [PATCH] packfile: avoid overflowing shift during decode
- From
- Jonathan Tan <jonathantanmy@google.com>
- Date
- Jan 12, 2022, 20:27 UTC
- Message-ID
- <20220112202708.324762-1-jonathantanmy@google.com>
- In-Reply-To
- <xmqq8rvku3bq.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
Show 29 quoted lines
> Marc Strapetz <marc.strapetz@syntevo.com> writes:
>
> > On 11/11/2021 02:58, Junio C Hamano wrote:
> >> Jonathan Tan <jonathantanmy@google.com> writes:
> >>
> >>> diff --git a/packfile.c b/packfile.c
> >>> index 89402cfc69..972c327e29 100644
> >>> --- a/packfile.c
> >>> +++ b/packfile.c
> >>> @@ -1068,7 +1068,7 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
> >>> size = c & 15;
> >>> shift = 4;
> >>> while (c & 0x80) {
> >>> - if (len <= used || bitsizeof(long) <= shift) {
> >>> + if (len <= used || (bitsizeof(long) - 7) <= shift) {
> >
> > This seems to cause troubles now for 32-bit systems (in my case Git
> > for Windows 32-Bit): `shift` will go through 4, 11, 18 and for 25 it
> > finally errors out. This means that objects >= 32MB can't be processed
> > anymore. The condition should probably be changed to:
> >
> > + if (len <= used || (bitsizeof(long) - 7) < shift) {
> >
> > This still ensures that the shift can never overflow and on 32-bit
> > systems restores the maximum size of 4G with a final shift of 127<<25
> > (the old condition `bitsizeof(long) <= shift` was perfectly valid for
> > 32-bit systems).
>
> Jonathan?This analysis makes sense - not sure how I missed that. 0x7f (the number being shifted) is 7 bits, so it can safely be shifted 25 bits.
The original condition of `bitsizeof(long) <= shift` works for 32-bit but not for 64-bit (4, 11, 18, 25, 32, 39, 46, 53, 60) since shifting 0x7f, a 7-bit value, by 60 bits would result in overflow, so we still need to subtract 7. I agree that the inequality should be `<`, not `<=`.