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

Re: [PATCH v3] merge: fix cache_entry use-after-free

From
René Scharfe <l.s.r@web.de>
Date
Oct 15, 2015, 03:35 UTC
Message-ID
<561F1EE5.5070300@web.de>
In-Reply-To
<1444860424-8361-1-git-send-email-dturner@twopensource.com>
Am 15.10.2015 um 00:07 schrieb David Turner:
Show 61 quoted lines
> From: Keith McGuigan <kmcguigan@twitter.com>
>
> During merges, we would previously free entries that we no longer need
> in the destination index.  But those entries might also be stored in
> the dir_entry cache, and when a later call to add_to_index found them,
> they would be used after being freed.
>
> To prevent this, add a ref count for struct cache_entry.  Whenever
> a cache entry is added to a data structure, the ref count is incremented;
> when it is removed from the data structure, it is decremented.  When
> it hits zero, the cache_entry is freed.
>
> Signed-off-by: Keith McGuigan <kmcguigan@twitter.com>
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>
> Fix type of ref_count (from unsigned int to int).
>
>
>   cache.h        | 27 +++++++++++++++++++++++++++
>   name-hash.c    |  7 ++++++-
>   read-cache.c   |  6 +++++-
>   split-index.c  | 13 ++++++++-----
>   unpack-trees.c |  6 ++++--
>   5 files changed, 50 insertions(+), 9 deletions(-)
>
> diff --git a/cache.h b/cache.h
> index 752031e..7906026 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -149,6 +149,7 @@ struct stat_data {
>
>   struct cache_entry {
>   	struct hashmap_entry ent;
> +	int ref_count; /* count the number of refs to this in dir_hash */
>   	struct stat_data ce_stat_data;
>   	unsigned int ce_mode;
>   	unsigned int ce_flags;
> @@ -213,6 +214,32 @@ struct cache_entry {
>   struct pathspec;
>
>   /*
> + * Increment the cache_entry reference count.  Should be called
> + * whenever a pointer to a cache_entry is retained in a data structure,
> + * thus marking it as alive.
> + */
> +static inline void add_ce_ref(struct cache_entry *ce)
> +{
> +	assert(ce != NULL && ce->ref_count >= 0);
> +	ce->ref_count++;
> +}
> +
> +/*
> + * Decrement the cache_entry reference count.  Should be called whenever
> + * a pointer to a cache_entry is dropped.  Once the counter drops to 0
> + * the cache_entry memory will be safely freed.
> + */
> +static inline void drop_ce_ref(struct cache_entry *ce)
> +{
> +	if (ce != NULL) {
> +		assert(ce->ref_count >= 0);
Shouldn't this be "> 0" to prevent double frees?
Show 5 quoted lines
> +		if (--ce->ref_count < 1) {
> +			free(ce);
> +		}
> +	}
> +}
Previous: David TurnerNext: David Turner
Message 2 of 9 in “merge: fix cache_entry use-after-free”
  1. merge: fix cache_entry use-after-freeDavid Turner, Oct 14, 2015
  2. René ScharfeOct 15, 2015
  3. David TurnerOct 15, 2015
  4. René ScharfeOct 15, 2015
  5. Junio C HamanoOct 15, 2015
  6. David TurnerOct 16, 2015
  7. Junio C HamanoOct 16, 2015
  8. David TurnerOct 19, 2015
  9. Junio C HamanoOct 20, 2015

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.