mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Dmitry V. Levin" <ldv@altlinux.org>
To: musl@lists.openwall.com
Subject: [PATCH] make getcwd fail if it cannot obtain an absolute path
Date: Fri, 12 Jan 2018 18:12:24 +0300	[thread overview]
Message-ID: <20180112151224.GA32321@altlinux.org> (raw)

Currently getcwd(3) can succeed without returning an absolute path
because the underlying getcwd syscall, starting with linux commit
v2.6.36-rc1~96^2~2, may succeed without returning an absolute path.

This is a conformance issue because "The getcwd() function shall
place an absolute pathname of the current working directory
in the array pointed to by buf, and return buf".

Fix this by checking the path returned by syscall and failing with
ENOENT if the path is not absolute.  The error code is chosen for
consistency with the case when the current directory is unlinked.

Similar issue was fixed in glibc recently, see
https://sourceware.org/bugzilla/show_bug.cgi?id=22679
---
 src/unistd/getcwd.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/unistd/getcwd.c b/src/unistd/getcwd.c
index a7b925d..103fbbb 100644
--- a/src/unistd/getcwd.c
+++ b/src/unistd/getcwd.c
@@ -14,6 +14,12 @@ char *getcwd(char *buf, size_t size)
 		errno = EINVAL;
 		return 0;
 	}
-	if (syscall(SYS_getcwd, buf, size) < 0) return 0;
+	long ret = syscall(SYS_getcwd, buf, size);
+	if (ret < 0)
+		return 0;
+	if (ret == 0 || buf[0] != '/') {
+		errno = ENOENT;
+		return 0;
+	}
 	return buf == tmp ? strdup(buf) : buf;
 }
-- 
ldv


             reply	other threads:[~2018-01-12 15:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-12 15:12 Dmitry V. Levin [this message]
2018-01-12 19:29 ` Rich Felker
2018-01-12 19:38   ` Rich Felker

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=20180112151224.GA32321@altlinux.org \
    --to=ldv@altlinux.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).