git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 3/3] index-helper: take extra care with readlink
@ 2016-04-10 23:03 Ramsay Jones
  2016-04-11  4:17 ` Torsten Bögershausen
  2016-04-11 21:53 ` David Turner
  0 siblings, 2 replies; 3+ messages in thread
From: Ramsay Jones @ 2016-04-10 23:03 UTC (permalink / raw)
  To: David Turner, Nguyen Thai Ngoc Duy
  Cc: Jeff King, Junio C Hamano, GIT Mailing-list


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;
 
-- 
2.8.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 3/3] index-helper: take extra care with readlink
  2016-04-10 23:03 [PATCH 3/3] index-helper: take extra care with readlink Ramsay Jones
@ 2016-04-11  4:17 ` Torsten Bögershausen
  2016-04-11 21:53 ` David Turner
  1 sibling, 0 replies; 3+ messages in thread
From: Torsten Bögershausen @ 2016-04-11  4:17 UTC (permalink / raw)
  To: Ramsay Jones, David Turner, Nguyen Thai Ngoc Duy
  Cc: Jeff King, Junio C Hamano, GIT Mailing-list

On 04/11/2016 01:03 AM, Ramsay Jones wrote:
> 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
>
Minor nit:
s/NULL/NUL/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 3/3] index-helper: take extra care with readlink
  2016-04-10 23:03 [PATCH 3/3] index-helper: take extra care with readlink Ramsay Jones
  2016-04-11  4:17 ` Torsten Bögershausen
@ 2016-04-11 21:53 ` David Turner
  1 sibling, 0 replies; 3+ messages in thread
From: David Turner @ 2016-04-11 21:53 UTC (permalink / raw)
  To: Ramsay Jones, Nguyen Thai Ngoc Duy
  Cc: Jeff King, Junio C Hamano, GIT Mailing-list

On Mon, 2016-04-11 at 00:03 +0100, Ramsay Jones wrote:
> 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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-04-11 21:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-10 23:03 [PATCH 3/3] index-helper: take extra care with readlink Ramsay Jones
2016-04-11  4:17 ` Torsten Bögershausen
2016-04-11 21:53 ` David Turner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).