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

Re: [PATCH] commit.c: ensure strchrnul() doesn't scan beyond range

From
Jeff King <peff@peff.net>
Date
Feb 8, 2024, 01:00 UTC
Message-ID
<20240208010040.GB1059751@coredump.intra.peff.net>
In-Reply-To
<ce83bd09-dbd2-4c9e-8197-6e4800935523@web.de>
On Mon, Feb 05, 2024 at 08:57:46PM +0100, René Scharfe wrote:
Show 5 quoted lines
> If you want to make the code work with buffers that lack a terminating
> NUL then you need to replace the strchrnul() call with something that
> respects buffer lengths.  You could e.g. call memchr().  Don't forget
> to check for NUL to preserve the original behavior.  Or you could roll
> your own custom replacement, perhaps like this:

I'm not sure it is worth retaining the check for NUL. The original function added by me in fe6eb7f2c5 (commit: provide a function to find a header in a buffer, 2014-08-27) just took a NUL-terminated string, so we certainly were not expecting embedded NULs.

In cfc5cf428b (receive-pack.c: consolidate find header logic, 2022-01-06) we switched to taking the "len" parameter, but the new caller just passes strlen(msg) anyway.

I guess you could argue that before that commit, receive-pack.c's find_header() which took a length was buggy to use strchrnul(). It gets fed with a push-cert buffer. I guess it's possible for there to be an embedded NUL there, but in practice there shouldn't be. If we are thinking of malformed or malicious input, it's not clear which behavior (finding or not finding a header past a NUL) is more harmful. So all things being equal, I would try to reduce the number of special cases here by not worrying about NULs.

(Though if somebody really wants to dig, it's possible there's a clever dual-parser attack here where "\nfoo\0bar baz" finds the header "bar baz" in one parser but not in another).

-Peff
Previous: Junio C HamanoNext: René Scharfe
Message 4 of 13 in “commit.c: ensure strchrnul() doesn't scan beyond range”
  1. commit.c: ensure strchrnul() doesn't scan beyond rangeChandra Pratap via GitGitGadget, Feb 5, 2024
  2. René ScharfeFeb 5, 2024
  3. Junio C HamanoFeb 6, 2024
  4. Jeff KingFeb 8, 2024
  5. René ScharfeFeb 8, 2024
  6. Junio C HamanoFeb 8, 2024
  7. Kyle LippincottFeb 8, 2024
  8. Jeff KingFeb 8, 2024
  9. Junio C HamanoFeb 8, 2024
  10. Kyle LippincottFeb 6, 2024
  11. commit.c: ensure find_header_mem() doesn't scan beyond given rangeChandra Pratap via GitGitGadget, Feb 7, 2024
  12. René ScharfeFeb 7, 2024
  13. Junio C HamanoFeb 7, 2024

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.