Skip to contentSkip to conversation

userspace: use openat2 if defined

RFC PATCH Superseded Stephen Smalley 1 participant 5 messages Jun 11 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

cover userspace: use openat2 if defined

This series is only an RFC (targeted for merging after the 3.11 final release). It updates libselinux, restorecond, and sandbox/seunshare to use openat2() if defined to provide symlink-safe full pathname lookup in a single system call rather than requiring per-component lookups (as in safe_open() in libselinux and restorecond) or following intermediate symlinks (as in seunshare, although this is done with the caller's fsuid and thus is not a concern). If openat2 is not defined or if it is not supported by the kernel, the code continues to fall back to the old logic and thus preserves compatibility. If we care, we could save the result to avoid repeatedly trying openat2() on pre-5.6 kernels but I am not sure we do.

Stephen Smalley (3):
libselinux: selinux_restorecon: use openat2 if defined
restorecond: use openat2 if defined sandbox/seunshare: pin_dir: use openat2 if available

 libselinux/src/selinux_restorecon.c | 23 +++++++++++++++++++++++
 restorecond/watch.c                 | 23 +++++++++++++++++++++++
 sandbox/seunshare.c                 | 17 +++++++++++++++--
 3 files changed, 61 insertions(+), 2 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

RFC PATCH 1/3 libselinux: selinux_restorecon: use openat2 if defined

Update selinux_restorecon()'s safe_open() helper to use openat2() if defined to optimize the lookup of the initial pathname. Fall back to the existing per-component lookup on ENOSYS/EINVAL so that pre-5.6 and seccomp-filtered environments are unaffected.

---
 libselinux/src/selinux_restorecon.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
libselinux/src/selinux_restorecon.c +23 -0
index 0159fb7f..ee78eab0 100644
@@ -25,7 +25,11 @@
 #include <sys/vfs.h>
 #include <sys/statvfs.h>
 #include <sys/utsname.h>
+#include <sys/syscall.h>
 #include <linux/magic.h>
+#ifdef __NR_openat2
+#include <linux/openat2.h>
+#endif
 #include <libgen.h>
 #include <syslog.h>
 #include <assert.h>
@@ -1114,6 +1118,25 @@ static int safe_open(const char *path, struct stat *sb)
 		return -1;
 	}
 
+#ifdef __NR_openat2
+	struct open_how how = {
+		.flags = O_PATH | O_NOFOLLOW | O_CLOEXEC,
+		.resolve = RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS,
+	};
+
+	nfd = syscall(__NR_openat2, AT_FDCWD, path, &how, sizeof(how));
+	if (nfd >= 0) {
+		if (fstat(nfd, sb) < 0) {
+			close(nfd);
+			return -1;
+		}
+		return nfd;
+	}
+
+	if (errno != ENOSYS && errno != EINVAL)
+		return -1;
+#endif
+
 	copy = strdup(path);
 	if (!copy)
 		return -1;
Signature · 1 line
2.54.0
author maintainer
#2 lore raw

RFC PATCH 2/3 restorecond: use openat2 if defined

In reply to Stephen Smalley

Update restorecond's safe_open() helper to use openat2() if defined to optimize the lookup of the initial pathname. Fall back to the existing per-component lookup on ENOSYS/EINVAL so that pre-5.6 and seccomp-filtered environments are unaffected.

---
 restorecond/watch.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
restorecond/watch.c +23 -0
index 25918fd5..18bd90db 100644
@@ -12,6 +12,10 @@
 #include <glob.h>
 #include <libgen.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
+#ifdef __NR_openat2
+#include <linux/openat2.h>
+#endif
 #include <string.h>
 #include <stdio.h>
 #include <fcntl.h>
@@ -72,6 +76,25 @@ static int safe_open(const char *path, struct stat *sb)
 		return -1;
 	}
 
+#ifdef __NR_openat2
+	struct open_how how = {
+		.flags = O_PATH | O_NOFOLLOW | O_CLOEXEC,
+		.resolve = RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS,
+	};
+
+	nfd = syscall(__NR_openat2, AT_FDCWD, path, &how, sizeof(how));
+	if (nfd >= 0) {
+		if (fstat(nfd, sb) < 0) {
+			close(nfd);
+			return -1;
+		}
+		return nfd;
+	}
+
+	if (errno != ENOSYS && errno != EINVAL)
+		return -1;
+#endif
+
 	copy = strdup(path);
 	if (!copy)
 		return -1;
Signature · 1 line
2.54.0
author maintainer
#3 lore raw

RFC PATCH 3/3 sandbox/seunshare: pin_dir: use openat2 if available

In reply to Stephen Smalley

Update seunshare's pin_dir() helper to use openat2() if defined to optimize the lookup of the directory and prevent following any intermediate symlinks. Fall back to the existing open() on ENOSYS/EINVAL so that pre-5.6 and seccomp-filtered environments are unaffected.

---
 sandbox/seunshare.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
sandbox/seunshare.c +15 -2
index 58baa7ff..829c6289 100644
@@ -10,8 +10,12 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/syscall.h>
 #include <syslog.h>
 #include <sys/mount.h>
+#ifdef __NR_openat2
+#include <linux/openat2.h>
+#endif
 #include <glob.h>
 #include <pwd.h>
 #include <sched.h>
@@ -219,10 +223,19 @@ static int check_owner_gid(gid_t gid, const char *file, struct stat *st)
  */
 static int pin_dir(const char *dir, struct stat *st_out)
 {
-	int fd;
+	int fd = -1;
 	struct stat sb;
 
-	fd = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
+#ifdef __NR_openat2
+	struct open_how how = {
+		.flags = O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC,
+		.resolve = RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS,
+	};
+
+	fd = syscall(__NR_openat2, AT_FDCWD, dir, &how, sizeof(how));
+	if (fd < 0 && (errno == ENOSYS || errno == EINVAL))
+#endif
+		fd = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
 	if (fd < 0) {
 		fprintf(stderr, _("Failed to open %s: %m\n"), dir);
 		return -1;
Signature · 1 line
2.54.0
author maintainer
#4 lore raw

PATCH 1/1 sandbox/seunshare: pin_dir: use openat2 if available

In reply to Stephen Smalley

Update seunshare's pin_dir() helper to use openat2() if defined to optimize the lookup of the directory and prevent following any intermediate symlinks. Fall back to the existing open() on ENOSYS/EINVAL so that pre-5.6 and seccomp-filtered environments are unaffected.

---
 sandbox/seunshare.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
sandbox/seunshare.c +15 -2
index 58baa7ff..829c6289 100644
@@ -10,8 +10,12 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/syscall.h>
 #include <syslog.h>
 #include <sys/mount.h>
+#ifdef __NR_openat2
+#include <linux/openat2.h>
+#endif
 #include <glob.h>
 #include <pwd.h>
 #include <sched.h>
@@ -219,10 +223,19 @@ static int check_owner_gid(gid_t gid, const char *file, struct stat *st)
  */
 static int pin_dir(const char *dir, struct stat *st_out)
 {
-	int fd;
+	int fd = -1;
 	struct stat sb;
 
-	fd = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
+#ifdef __NR_openat2
+	struct open_how how = {
+		.flags = O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC,
+		.resolve = RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS,
+	};
+
+	fd = syscall(__NR_openat2, AT_FDCWD, dir, &how, sizeof(how));
+	if (fd < 0 && (errno == ENOSYS || errno == EINVAL))
+#endif
+		fd = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
 	if (fd < 0) {
 		fprintf(stderr, _("Failed to open %s: %m\n"), dir);
 		return -1;
Signature · 1 line
2.54.0