git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] pkt-line: don't check string length in packet_length()

From
René Scharfe <l.s.r@web.de>
Date
Jul 5, 2023, 21:11 UTC
Message-ID
<6f984150-5a98-b2b8-1791-b101f5953b7a@web.de>
In-Reply-To
<xmqq4jmiyu7s.fsf@gitster.g>
Am 05.07.23 um 22:33 schrieb Junio C Hamano:
Show 10 quoted lines
> René Scharfe <l.s.r@web.de> writes:
>
>>> I do like the resulting code, but I feel a bit uneasy to sell this
>>> change as "the code becomes more streamlined without losing safety".
>>> It looks more like "this change is safe for our two callers; those
>>> adding more callers in the future are better be very careful", no?
>>
>> With no way to enforce passing an array of a certain size to a function
>> the only safe options I see are keeping the length check, using a macro
>> or inlining the calculation.  Hmm.

Three more ideas: Wrap the buffer in a custom struct, pass the four bytes as a uint32_t or as four separate char parameters. I better stop now..

Show 5 quoted lines
> We keep repeating "length check" because that is what the comment in
> the function says, but even if the caller has 4-byte, that 4-byte
> substring at the beginning is what it read from the untrusted side
> over the network.  We should be checking if we have 4 hexadecimal
> length even if we are not running beyond the end of the buffer, no?

Sure, that's done. If any of the four characters is not a hexadecimal digit then packet_length() returns a negative value, before and after the change.

> So it may be that the comment needs to be fixed more than the code.

Which comment specifically? The one in pkt-line.h doesn't mention what happens if you pass in a short buffer -- leaving it undefined is OK, I think. And in and around pkt-line.c::packet_length() I don't see any comment?

René
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 8 in “pkt-line: don't check string length in packet_length()”
  1. pkt-line: don't check string length in packet_length()René Scharfe, Jul 1, 2023
  2. Junio C HamanoJul 5, 2023
  3. René ScharfeJul 5, 2023
  4. Junio C HamanoJul 5, 2023
  5. René ScharfeJul 5, 2023
  6. Junio C HamanoJul 5, 2023
  7. René ScharfeJul 6, 2023
  8. pkt-line: add size parameter to packet_length()René Scharfe, Jul 7, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.