Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Fri, 12 Jan 2018 18:12:24 +0300
From: "Dmitry V. Levin" <ldv@...linux.org>
To: musl@...ts.openwall.com
Subject: [PATCH] make getcwd fail if it cannot obtain an absolute path

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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.