public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Ramsay Jones <ramsay@ramsayjones•plus.com>
To: David Turner <dturner@twopensource•com>,
	Nguyen Thai Ngoc Duy <pclouds@gmail•com>
Cc: Jeff King <peff@peff•net>, Junio C Hamano <gitster@pobox•com>,
	GIT Mailing-list <git@vger•kernel.org>
Subject: [PATCH 3/3] index-helper: take extra care with readlink
Date: Mon, 11 Apr 2016 00:03:39 +0100	[thread overview]
Message-ID: <570ADBCB.4040503@ramsayjones.plus.com> (raw)


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

             reply	other threads:[~2016-04-10 23:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-10 23:03 Ramsay Jones [this message]
2016-04-11  4:17 ` [PATCH 3/3] index-helper: take extra care with readlink Torsten Bögershausen
2016-04-11 21:53 ` David Turner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=570ADBCB.4040503@ramsayjones.plus.com \
    --to=ramsay@ramsayjones$(echo .)plus.com \
    --cc=dturner@twopensource$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=gitster@pobox$(echo .)com \
    --cc=pclouds@gmail$(echo .)com \
    --cc=peff@peff$(echo .)net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox