]> git.hungrycats.org Git - bees/commitdiff
openat2: create a weak syscall wrapper for it
authorZygo Blaxell <bees@furryterror.org>
Thu, 9 Jan 2025 06:16:02 +0000 (01:16 -0500)
committerZygo Blaxell <bees@furryterror.org>
Thu, 9 Jan 2025 06:36:39 +0000 (01:36 -0500)
openat2 allows closing more TOCTOU holes, but we can only use it when
the kernel supports it.

This should disappear seamlessly when libc implements the function.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
include/crucible/openat2.h [new file with mode: 0644]
lib/Makefile
lib/openat2.cc [new file with mode: 0644]

diff --git a/include/crucible/openat2.h b/include/crucible/openat2.h
new file mode 100644 (file)
index 0000000..45f2c7f
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef CRUCIBLE_OPENAT2_H
+#define CRUCIBLE_OPENAT2_H
+
+#include <linux/openat2.h>
+
+#include <fcntl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+extern "C" {
+
+/// Weak symbol to support libc with no syscall wrapper
+int openat2(int dirfd, const char *pathname, struct open_how *how, size_t size) throw();
+
+};
+
+#endif // CRUCIBLE_OPENAT2_H
index cddea6ff3091a7ea8f28b402e8836e06a9d9a2b5..939f84475a9d5dd4ee513d62b4c4334adb6f0af2 100644 (file)
@@ -14,6 +14,7 @@ CRUCIBLE_OBJS = \
        fs.o \
        multilock.o \
        ntoa.o \
+       openat2.o \
        path.o \
        process.o \
        string.o \
diff --git a/lib/openat2.cc b/lib/openat2.cc
new file mode 100644 (file)
index 0000000..95c9eb3
--- /dev/null
@@ -0,0 +1,13 @@
+#include "crucible/openat2.h"
+
+extern "C" {
+
+int
+__attribute__((weak))
+openat2(int const dirfd, const char *const pathname, struct open_how *const how, size_t const size)
+throw()
+{
+       return syscall(SYS_openat2, dirfd, pathname, how, size);
+}
+
+};