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

Re: [PATCH 2/4] reftable: fix allocation count on realloc error

From
Patrick Steinhardt <ps@pks.im>
Date
Dec 27, 2024, 10:33 UTC
Message-ID
<Z26ClnvVH1cg3CGQ@pks.im>
In-Reply-To
<33bbacc7-1727-4efd-9cbf-3c9abfa94d8c@web.de>
On Wed, Dec 25, 2024 at 07:38:35PM +0100, René Scharfe wrote:
Show 24 quoted lines
> When realloc(3) fails, it returns NULL and keeps the original allocation
> intact.  REFTABLE_ALLOC_GROW overwrites both the original pointer and
> the allocation count variable in that case, simultaneously leaking the
> original allocation and misrepresenting the number of storable items.
> 
> parse_names() avoids the leak by keeping the original pointer if
> reallocation fails, but still increase the allocation count in such a
> case as if it succeeded.  That's OK, because the error handling code
> just frees everything and doesn't look at names_cap anymore.
> 
> reftable_buf_add() does the same, but here it is a problem as it leaves
> the reftable_buf in a broken state, with ->alloc being roughly twice as
> big as the actually allocated memory, allowing out-of-bounds writes in
> subsequent calls.
> 
> Reimplement REFTABLE_ALLOC_GROW to avoid leaks, keep allocation counts
> in sync and still signal failures to callers while avoiding code
> duplication in callers.  Make it an expression that evaluates to 0 if no
> reallocation is needed or it succeeded and 1 on failure while keeping
> the original pointer and allocation counter values.
> 
> Adjust REFTABLE_ALLOC_GROW_OR_NULL to the new calling convention for
> REFTABLE_ALLOC_GROW, but keep its support for non-size_t alloc variables
> for now.

Okay, this change goes into the direction I was wondering about with the preceding commit, good.

Show 38 quoted lines
> diff --git a/reftable/basics.h b/reftable/basics.h
> index 259f4c274c..fa5d75868b 100644
> --- a/reftable/basics.h
> +++ b/reftable/basics.h
> @@ -120,22 +120,35 @@ char *reftable_strdup(const char *str);
>  #define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc)))
>  #define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))
>  #define REFTABLE_REALLOC_ARRAY(x, alloc) (x) = reftable_realloc((x), st_mult(sizeof(*(x)), (alloc)))
> -#define REFTABLE_ALLOC_GROW(x, nr, alloc) \
> -	do { \
> -		if ((nr) > alloc) { \
> -			alloc = 2 * (alloc) + 1; \
> -			if (alloc < (nr)) \
> -				alloc = (nr); \
> -			REFTABLE_REALLOC_ARRAY(x, alloc); \
> -		} \
> -	} while (0)
> +
> +static inline void *reftable_alloc_grow(void *p, size_t nelem, size_t elsize,
> +					size_t *allocp)
> +{
> +	void *new_p;
> +	size_t alloc = *allocp * 2 + 1;
> +	if (alloc < nelem)
> +		alloc = nelem;
> +	new_p = reftable_realloc(p, st_mult(elsize, alloc));
> +	if (!new_p)
> +		return p;
> +	*allocp = alloc;
> +	return new_p;
> +}
> +
> +#define REFTABLE_ALLOC_GROW(x, nr, alloc) ( \
> +	(nr) > (alloc) && ( \
> +		(x) = reftable_alloc_grow((x), (nr), sizeof(*(x)), &(alloc)), \
> +		(nr) > (alloc) \
> +	) \
> +)

Do we even need this macro? I don't really think it serves much of a purpose anymore.

Patrick
Previous: René ScharfeNext: René Scharfe
Message 8 of 25 in “reftable: fix realloc error handling”
  1. 0/4 reftable: fix realloc error handlingRené Scharfe, Dec 25, 2024
  2. 1/4 reftable: avoid leaks on realloc errorRené Scharfe, Dec 25, 2024
  3. Junio C HamanoDec 27, 2024
  4. Patrick SteinhardtDec 27, 2024
  5. René ScharfeDec 27, 2024
  6. Patrick SteinhardtDec 30, 2024
  7. 2/4 reftable: fix allocation count on realloc errorRené Scharfe, Dec 25, 2024
  8. Patrick SteinhardtDec 27, 2024
  9. René ScharfeDec 27, 2024
  10. René ScharfeDec 27, 2024
  11. 3/4 reftable: handle realloc error in parse_names()René Scharfe, Dec 25, 2024
  12. 4/4 t-reftable-merged: check realloc errorsRené Scharfe, Dec 25, 2024
  13. Junio C HamanoDec 27, 2024
  14. Patrick SteinhardtDec 27, 2024
  15. René ScharfeDec 27, 2024
  16. Patrick SteinhardtDec 27, 2024
  17. Junio C HamanoDec 27, 2024
  18. 0/4 reftable: fix realloc error handlingRené Scharfe, Dec 28, 2024
  19. 1/4 reftable: avoid leaks on realloc errorRené Scharfe, Dec 28, 2024
  20. Patrick SteinhardtDec 30, 2024
  21. 2/4 reftable: fix allocation count on realloc errorRené Scharfe, Dec 28, 2024
  22. 3/4 reftable: handle realloc error in parse_names()René Scharfe, Dec 28, 2024
  23. Patrick SteinhardtDec 30, 2024
  24. 4/4 t-reftable-merged: handle realloc errorsRené Scharfe, Dec 28, 2024
  25. Patrick SteinhardtDec 30, 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.