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

Re: [PATCH v2 00/19] Index-v5

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Jul 17, 2013, 08:04 UTC
Message-ID
<87zjtlsjcw.fsf@gmail.com>
In-Reply-To
<51E5B52C.7050603@ramsay1.demon.co.uk>
Ramsay Jones <ramsay@ramsay1.demon.co.uk> writes:
Show 14 quoted lines
> Thomas Gummerer wrote:
>> Hi,
>>
>> previous rounds (without api) are at $gmane/202752, $gmane/202923,
>> $gmane/203088 and $gmane/203517, the previous round with api was at
>> $gmane/229732.  Thanks to Junio, Duy and Eric for their comments on
>> the previous round.
>
> If I remember correctly, the original version of this series had the
> same problem as Michael's "Fix some reference-related races" series
> (now in master). In particular, you had introduced an 'index_changed()'
> function which does essentially the same job as 'stat_validity_check()'
> in the new reference handling API. I seem to remember advising you
> not to compare st_uid, st_gid and st_ino on __CYGWIN__.

Yes, you provided a patch that simply wrapped those checks in a #if !defined (__CYGWIN__), which is included in the new series too.

> I haven't had time to look at this version of your series yet, but it
> may be worth taking a look at stat_validity_check(). (although that is
> causing failures on cygwin at the moment! ;-)

I took a quick look, that function makes sense I think. I'll use it in the re-roll. It makes probably sense to wrap the uid, gid and ino fields as in the index_changed function.

Show 10 quoted lines
> Also, I can't recall if I mentioned it to you at the time, but your
> index reading code was (unnecessarily) calling munmap() twice on the
> same buffer (without an intervening mmap()). This causes problems for
> systems that have the NO_MMAP build variable set. In particular, the
> compat/mmap.c code will attempt to free() the allocated memory block
> twice, with unpredictable results.
>
> I wrote a patch to address this at the time (Hmm, seems to be built
> on v1.8.1), but didn't submit it since your patch didn't progress. :-D
> I have included the patch below.

I can't recall this either. From a quick check I don't call munmap() on a already unmapped mmap, so I think this is fine as it is and your patch is independent from it. Not sure if it makes sense as safeguard for future changes.

Show 126 quoted lines
> -- >8 --
> From: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
> Date: Sun, 9 Sep 2012 20:50:32 +0100
> Subject: [PATCH] mmap.c: Keep log of mmap() blocks to avoid double-delete bug
>
> When compiling with the NO_MMAP build variable set, the built-in
> 'git_mmap()' and 'git_munmap()' compatibility routines use simple
> memory allocation and file I/O to emulate the required behaviour.
> The current implementation is vulnerable to the "double-delete" bug
> (where the pointer returned by malloc() is passed to free() two or
> more times), should the mapped memory block address be passed to
> munmap() multiple times.
>
> In order to guard the implementation from such a calling sequence,
> we keep a list of mmap-block descriptors, which we then consult to
> determine the validity of the input pointer to munmap(). This then
> allows 'git_munmap()' to return -1 on error, as required, with
> errno set to EINVAL.
>
> Using a list in the log of mmap-ed blocks, along with the resulting
> linear search, means that the performance of the code is directly
> proportional to the number of concurrently active memory mapped
> file regions. The number of such regions is not expected to be
> excessive.
>
> Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
> ---
>  compat/mmap.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 56 insertions(+), 1 deletion(-)
>
> diff --git a/compat/mmap.c b/compat/mmap.c
> index c9d46d1..400e034 100644
> --- a/compat/mmap.c
> +++ b/compat/mmap.c
> @@ -1,14 +1,61 @@
>  #include "../git-compat-util.h"
>
> +struct mmbd {  /* memory mapped block descriptor */
> +	struct mmbd *next;  /* next in list */
> +	void   *start;      /* pointer to memory mapped block */
> +	size_t length;      /* length of memory mapped block */
> +};
> +
> +static struct mmbd *head;  /* head of mmb descriptor list */
> +
> +
> +static void add_desc(struct mmbd *desc, void *start, size_t length)
> +{
> +	desc->start = start;
> +	desc->length = length;
> +	desc->next = head;
> +	head = desc;
> +}
> +
> +static void free_desc(struct mmbd *desc)
> +{
> +	if (head == desc)
> +		head = head->next;
> +	else {
> +		struct mmbd *d = head;
> +		for (; d; d = d->next) {
> +			if (d->next == desc) {
> +				d->next = desc->next;
> +				break;
> +			}
> +		}
> +	}
> +	free(desc);
> +}
> +
> +static struct mmbd *find_desc(void *start)
> +{
> +	struct mmbd *d = head;
> +	for (; d; d = d->next) {
> +		if (d->start == start)
> +			return d;
> +	}
> +	return NULL;
> +}
> +
>  void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
>  {
>  	size_t n = 0;
> +	struct mmbd *desc = NULL;
>
>  	if (start != NULL || !(flags & MAP_PRIVATE))
>  		die("Invalid usage of mmap when built with NO_MMAP");
>
>  	start = xmalloc(length);
> -	if (start == NULL) {
> +	desc = xmalloc(sizeof(*desc));
> +	if (!start || !desc) {
> +		free(start);
> +		free(desc);
>  		errno = ENOMEM;
>  		return MAP_FAILED;
>  	}
> @@ -25,18 +72,26 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
>  			if (errno == EAGAIN || errno == EINTR)
>  				continue;
>  			free(start);
> +			free(desc);
>  			errno = EACCES;
>  			return MAP_FAILED;
>  		}
>
>  		n += count;
>  	}
> +	add_desc(desc, start, length);
>
>  	return start;
>  }
>
>  int git_munmap(void *start, size_t length)
>  {
> +	struct mmbd *d = find_desc(start);
> +	if (!d) {
> +		errno = EINVAL;
> +		return -1;
> +	}
> +	free_desc(d);
>  	free(start);
>  	return 0;
>  }
> --
> 1.8.3
Previous: Ramsay Jones
Message 51 of 51 in “Index-v5”
  1. 00/19 Index-v5Thomas Gummerer, Jul 12, 2013
  2. 01/19 t2104: Don't fail for index versions other than [23]Thomas Gummerer, Jul 12, 2013
  3. 02/19 read-cache: split index file version specific functionalityThomas Gummerer, Jul 12, 2013
  4. 03/19 read-cache: move index v2 specific functions to their own fileThomas Gummerer, Jul 12, 2013
  5. Duy NguyenJul 14, 2013
  6. Thomas GummererJul 19, 2013
  7. 04/19 read-cache: Re-read index if index file changedThomas Gummerer, Jul 12, 2013
  8. 05/19 Add documentation for the index apiThomas Gummerer, Jul 12, 2013
  9. 06/19 read-cache: add index reading apiThomas Gummerer, Jul 12, 2013
  10. Duy NguyenJul 14, 2013
  11. 07/19 make sure partially read index is not changedThomas Gummerer, Jul 12, 2013
  12. Duy NguyenJul 14, 2013
  13. Thomas GummererJul 17, 2013
  14. 08/19 grep.c: Use index apiThomas Gummerer, Jul 12, 2013
  15. Duy NguyenJul 14, 2013
  16. Thomas GummererJul 15, 2013
  17. 09/19 ls-files.c: use index apiThomas Gummerer, Jul 12, 2013
  18. Duy NguyenJul 14, 2013
  19. Thomas GummererJul 17, 2013
  20. 10/19 documentation: add documentation of the index-v5 file formatThomas Gummerer, Jul 12, 2013
  21. Duy NguyenJul 14, 2013
  22. Thomas GummererJul 17, 2013
  23. Duy NguyenAug 4, 2013
  24. Thomas GummererAug 4, 2013
  25. 11/19 read-cache: make in-memory format aware of stat_crcThomas Gummerer, Jul 12, 2013
  26. 12/19 read-cache: read index-v5Thomas Gummerer, Jul 12, 2013
  27. Duy NguyenJul 14, 2013
  28. Thomas GummererAug 7, 2013
  29. Duy NguyenJul 15, 2013
  30. Thomas GummererJul 17, 2013
  31. Duy NguyenAug 8, 2013
  32. Thomas GummererAug 8, 2013
  33. Thomas GummererAug 9, 2013
  34. Thomas GummererAug 7, 2013
  35. Duy NguyenAug 8, 2013
  36. 13/19 read-cache: read resolve-undo dataThomas Gummerer, Jul 12, 2013
  37. 14/19 read-cache: read cache-tree in index-v5Thomas Gummerer, Jul 12, 2013
  38. 15/19 read-cache: write index-v5Thomas Gummerer, Jul 12, 2013
  39. 16/19 read-cache: write index-v5 cache-tree dataThomas Gummerer, Jul 12, 2013
  40. 17/19 read-cache: write resolve-undo data for index-v5Thomas Gummerer, Jul 12, 2013
  41. 18/19 update-index.c: rewrite index when index-version is givenThomas Gummerer, Jul 12, 2013
  42. 19/19 p0003-index.sh: add perf test for the index formatsThomas Gummerer, Jul 12, 2013
  43. Duy NguyenJul 14, 2013
  44. Thomas GummererJul 15, 2013
  45. Duy NguyenJul 15, 2013
  46. Thomas GummererJul 17, 2013
  47. Junio C HamanoJul 17, 2013
  48. Thomas GummererJul 19, 2013
  49. Junio C HamanoJul 19, 2013
  50. Ramsay JonesJul 16, 2013
  51. Thomas GummererJul 17, 2013

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.