userspace: use openat2 if defined
Stephen Smalley
maintainercover 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
Stephen Smalley
maintainerRFC 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
@@ -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
Stephen Smalley
maintainerRFC PATCH 2/3 restorecond: use openat2 if defined
In reply to Stephen SmalleyUpdate 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
@@ -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
Stephen Smalley
maintainerRFC PATCH 3/3 sandbox/seunshare: pin_dir: use openat2 if available
In reply to Stephen SmalleyUpdate 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
@@ -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
Stephen Smalley
maintainerPATCH 1/1 sandbox/seunshare: pin_dir: use openat2 if available
In reply to Stephen SmalleyUpdate 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
@@ -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