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

Re: [PATCH 3/3] index-helper: take extra care with readlink

From
David Turner <dturner@twopensource.com>
Date
Apr 11, 2016, 21:53 UTC
Message-ID
<1460411585.5540.21.camel@twopensource.com>
In-Reply-To
<570ADBCB.4040503@ramsayjones.plus.com>
On Mon, 2016-04-11 at 00:03 +0100, Ramsay Jones wrote:
Show 48 quoted lines
> It took me a few minutes to convince myself that, if index-helper is
> the only writer for the symlink, that the call to readlink would
> result in a properly NULL terminated string. This relies on the
> initialization of the address variable setting each byte of the
> 'struct sockaddr_un' to zero and the contents of the symlink being
> no more than 'sizeof(address.sun_path) - 1' bytes long.
> 
> In order to make the call easier to reason about, introduce a wrapper
> function, read_link(), which copes with overlong symlinks and ensures
> correct NULL termination.
> 
> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
> ---
>  read-cache.c | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/read-cache.c b/read-cache.c
> index c7053d8..39330a1 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -1722,12 +1722,23 @@ static void post_read_index_from(struct
> index_state *istate)
>  	tweak_untracked_cache(istate);
>  }
>  
> +static int read_link(const char *path, char *buf, size_t bufsize)
> +{
> +	int len;
> +
> +	len = readlink(path, buf, bufsize);
> +	if (len < 0 || len >= bufsize)
> +		return -1;
> +	buf[len] = '\0';
> +	return 0;
> +}
> +
>  int connect_to_index_helper(void)
>  {
>  	struct sockaddr_un address = {0};
>  	int fd;
>  
> -	if (readlink(git_path("index-helper.path"),
> address.sun_path,
> +	if (read_link(git_path("index-helper.path"),
> address.sun_path,
>  		     sizeof(address.sun_path)) < 0)
>  		return -1;
>  
Will squash.
Previous: Torsten Bögershausen
Message 3 of 3 in “index-helper: take extra care with readlink”
  1. 3/3 index-helper: take extra care with readlinkRamsay Jones, Apr 10, 2016
  2. Torsten BögershausenApr 11, 2016
  3. David TurnerApr 11, 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.