Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20201229225518.3677-1-ericonr@disroot.org>
Date: Tue, 29 Dec 2020 19:55:18 -0300
From: Érico Nogueira <ericonr@...root.org>
To: musl@...ts.openwall.com
Cc: Érico Rolim <ericonr@...root.org>
Subject: [PATCH] use openat syscall in open() to avoid the spurious fcntl call in modern kernels to apply O_CLOEXEC

From: Érico Rolim <ericonr@...root.org>

openat appears to have been introduced after the O_CLOEXEC flag was
added to the kernel, so its presence can be used as a proxy for the
O_CLOEXEC flag definitely taking effect
---

The commit message is unsure about the timeline between O_CLOEXEC and
openat, because I couldn't find any definitive sources. It's certainly
the assumption in the current openat() implementation, and glibc seems
to use openat for open() since the first commit I could find
(a63c7fa1856d6d4ef6573111e5700ac01b0bf6b2, from 2011). bionic also
always uses openat, but I didn't look through its history.

I believe this will generate a tiny amount of dead code in archs which
don't have SYS_open (so __sys_open_cp already uses SYS_openat), since
they should never return ENOSYS, and if they do (funky seccomp filters?)
the second attempt will be useless, since it will use the exact same
values. The block could be guarded by an ifdef, but I'm not sure it's
desired.

 src/fcntl/open.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/fcntl/open.c b/src/fcntl/open.c
index 1d817a2d..75c4b919 100644
--- a/src/fcntl/open.c
+++ b/src/fcntl/open.c
@@ -13,9 +13,12 @@ int open(const char *filename, int flags, ...)
 		va_end(ap);
 	}
 
-	int fd = __sys_open_cp(filename, flags, mode);
-	if (fd>=0 && (flags & O_CLOEXEC))
-		__syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
+	int fd = __syscall(SYS_openat, AT_FDCWD, filename, flags|O_LARGEFILE, mode);
+	if (fd==-ENOSYS) {
+		fd = __sys_open_cp(filename, flags, mode);
+		if (fd>=0 && (flags & O_CLOEXEC))
+			__syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
+	}
 
 	return __syscall_ret(fd);
 }
-- 
2.29.2

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.