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

[PATCH v2 3/6] shallow.c: make paint_alloc slightly more robust

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 6, 2016, 12:53 UTC
Message-ID
<20161206125339.16803-4-pclouds@gmail.com>
In-Reply-To
<20161206125339.16803-1-pclouds@gmail.com>

paint_alloc() allocates a big block of memory and splits it into smaller, fixed size, chunks of memory whenever it's called. Each chunk contains enough bits to present all "new refs" [1] in a fetch from a shallow repository.

We do not check if the new "big block" is smaller than the requested memory chunk though. If it happens, we'll happily pass back a memory region smaller than expected. Which will lead to problems eventually.

A normal fetch may add/update a dozen new refs. Let's stay on the "reasonably extreme" side and say we need 16k refs (or bits from paint_alloc's perspective). Each chunk of memory would be 2k, much smaller than the memory pool (512k).

So, normally, the under-allocation situation should never happen. A bad guy, however, could make a fetch that adds more than 4m new/updated refs to this code which results in a memory chunk larger than pool size. Check this case and abort.

Noticed-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
[1] Details are in commit message of 58babff (shallow.c: the 8 steps to
    select new commits for .git/shallow - 2013-12-05), step 6.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 shallow.c | 3 +++
 1 file changed, 3 insertions(+)
diff --git a/shallow.c b/shallow.c
index 2512ed3..75e1702 100644
--- a/shallow.c
+++ b/shallow.c
@@ -447,6 +447,9 @@ static uint32_t *paint_alloc(struct paint_info *info)
 	unsigned size = nr * sizeof(uint32_t);
 	void *p;
 	if (!info->pool_count || info->free + size > info->end) {
+		if (size > POOL_SIZE)
+			die("BUG: pool size too small for %d in paint_alloc()",
+			    size);
 		info->pool_count++;
 		REALLOC_ARRAY(info->pools, info->pool_count);
 		info->free = xmalloc(POOL_SIZE);
-- 
2.8.2.524.g6ff3d78
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 16 of 20 in “shallow.c: make paint_alloc slightly more robust”
  1. 1/4 shallow.c: make paint_alloc slightly more robustRasmus Villemoes, Dec 2, 2016
  2. 2/4 shallow.c: avoid theoretical pointer wrap-aroundRasmus Villemoes, Dec 2, 2016
  3. Jeff KingDec 3, 2016
  4. 3/4 shallow.c: bit manipulation tweaksRasmus Villemoes, Dec 2, 2016
  5. Jeff KingDec 3, 2016
  6. 4/4 shallow.c: remove useless testRasmus Villemoes, Dec 2, 2016
  7. Jeff KingDec 3, 2016
  8. Duy NguyenDec 5, 2016
  9. Jeff KingDec 3, 2016
  10. Duy NguyenDec 5, 2016
  11. 0/6 shallow.c improvementsNguyễn Thái Ngọc Duy, Dec 6, 2016
  12. 1/6 shallow.c: rename fields in paint_info to better express their purposesNguyễn Thái Ngọc Duy, Dec 6, 2016
  13. 5/6 shallow.c: bit manipulation tweaksNguyễn Thái Ngọc Duy, Dec 6, 2016
  14. 4/6 shallow.c: avoid theoretical pointer wrap-aroundNguyễn Thái Ngọc Duy, Dec 6, 2016
  15. 6/6 shallow.c: remove useless codeNguyễn Thái Ngọc Duy, Dec 6, 2016
  16. 3/6 shallow.c: make paint_alloc slightly more robustNguyễn Thái Ngọc Duy, Dec 6, 2016
  17. 2/6 shallow.c: stop abusing COMMIT_SLAB_SIZE for paint_info's memory poolsNguyễn Thái Ngọc Duy, Dec 6, 2016
  18. Jeff KingDec 6, 2016
  19. Duy NguyenDec 6, 2016
  20. Junio C HamanoDec 7, 2016

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.