From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12369 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] make getcwd fail if it cannot obtain an absolute path Date: Fri, 12 Jan 2018 14:38:47 -0500 Message-ID: <20180112193847.GN1627@brightrain.aerifal.cx> References: <20180112151224.GA32321@altlinux.org> <20180112192927.GM1627@brightrain.aerifal.cx> 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 1515785834 13323 195.159.176.226 (12 Jan 2018 19:37:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 12 Jan 2018 19:37:14 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-12385-gllmg-musl=m.gmane.org@lists.openwall.com Fri Jan 12 20:37:10 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 1ea58M-0002Pi-Ob for gllmg-musl@m.gmane.org; Fri, 12 Jan 2018 20:36:58 +0100 Original-Received: (qmail 7849 invoked by uid 550); 12 Jan 2018 19:39:00 -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 7828 invoked from network); 12 Jan 2018 19:38:59 -0000 Content-Disposition: inline In-Reply-To: <20180112192927.GM1627@brightrain.aerifal.cx> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:12369 Archived-At: On Fri, Jan 12, 2018 at 02:29:27PM -0500, Rich Felker wrote: > On Fri, Jan 12, 2018 at 06:12:24PM +0300, Dmitry V. Levin wrote: > > 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 > > Looks ok. Can you provide any details on the circumstances under which > the kernel bug manifests? This would help users who may be affected > assess the severity of the situation. Ah, this answers it: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=v2.6.36-rc1~96%5e2~2 So it seems to be things like a no-longer-reachable working directory after chroot or in a container. Rich