Skip to content

Commit 33e752e

Browse files
kolyshkintklauser
authored andcommitted
syscall: ensure that Getwd returns absolute path
Since Linux kernel 2.6.36, the pathname returned by the getcwd() system call can be prefixed with the string "(unreachable)" in some cases [1]. Getcwd should return an absolute path, and doing otherwise is a conformance issue; it also can be dangerous, since the path returned can be an existing relative path. Fix by returning ENOENT in case the path is not absolute. This is essentially the same as what glibc does (since [2]). [1] https://man7.org/linux/man-pages/man2/getcwd.2.html#BUGS [2] https://sourceware.org/git/?p=glibc.git;a=commit;h=52a713fdd0a30e1bd79818e2e3c4ab44ddca1a94 Change-Id: I444c80eb3c836ff7d32c64c8b65d5112fa8c710f Reviewed-on: https://go-review.googlesource.com/c/go/+/387174 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Trust: Tobias Klauser <[email protected]> Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent a3fcc75 commit 33e752e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/syscall/syscall_linux.go

+7
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ func Getwd() (wd string, err error) {
253253
if n < 1 || n > len(buf) || buf[n-1] != 0 {
254254
return "", EINVAL
255255
}
256+
// In some cases, Linux can return a path that starts with the
257+
// "(unreachable)" prefix, which can potentially be a valid relative
258+
// path. To work around that, return ENOENT if path is not absolute.
259+
if buf[0] != '/' {
260+
return "", ENOENT
261+
}
262+
256263
return string(buf[0 : n-1]), nil
257264
}
258265

0 commit comments

Comments
 (0)