From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12362 Path: news.gmane.org!.POSTED!not-for-mail From: "Dmitry V. Levin" Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] make getcwd fail if it cannot obtain an absolute path Date: Fri, 12 Jan 2018 18:12:24 +0300 Message-ID: <20180112151224.GA32321@altlinux.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1515769844 11301 195.159.176.226 (12 Jan 2018 15:10:44 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 12 Jan 2018 15:10:44 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-12378-gllmg-musl=m.gmane.org@lists.openwall.com Fri Jan 12 16:10:40 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1ea0yZ-0002Mr-Rd for gllmg-musl@m.gmane.org; Fri, 12 Jan 2018 16:10:35 +0100 Original-Received: (qmail 19576 invoked by uid 550); 12 Jan 2018 15:12:36 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 19537 invoked from network); 12 Jan 2018 15:12:35 -0000 Content-Disposition: inline Xref: news.gmane.org gmane.linux.lib.musl.general:12362 Archived-At: 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