daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
5d2ee9
From b39697a80ad388e2063c54e56333882f4307c1a1 Mon Sep 17 00:00:00 2001
5d2ee9
From: Jan Synacek <jsynacek@redhat.com>
5d2ee9
Date: Tue, 12 Nov 2019 13:27:49 +0100
5d2ee9
Subject: [PATCH] test: add test case for restrict_suid_sgid()
5d2ee9
5d2ee9
(cherry picked from commit 167fc10cb352b04d442c9010dab4f8dc24219749)
5d2ee9
Related: #1687512
5d2ee9
---
5d2ee9
 src/test/test-seccomp.c | 226 ++++++++++++++++++++++++++++++++++++++++
5d2ee9
 1 file changed, 226 insertions(+)
5d2ee9
5d2ee9
diff --git a/src/test/test-seccomp.c b/src/test/test-seccomp.c
5d2ee9
index d177515ac7..4021a06e0e 100644
5d2ee9
--- a/src/test/test-seccomp.c
5d2ee9
+++ b/src/test/test-seccomp.c
5d2ee9
@@ -17,9 +17,11 @@
5d2ee9
 #include "nsflags.h"
5d2ee9
 #include "process-util.h"
5d2ee9
 #include "raw-clone.h"
5d2ee9
+#include "rm-rf.h"
5d2ee9
 #include "seccomp-util.h"
5d2ee9
 #include "set.h"
5d2ee9
 #include "string-util.h"
5d2ee9
+#include "umask-util.h"
5d2ee9
 #include "util.h"
5d2ee9
 #include "virt.h"
5d2ee9
 
5d2ee9
@@ -666,6 +668,229 @@ static void test_filter_sets_ordered(void) {
5d2ee9
         }
5d2ee9
 }
5d2ee9
 
5d2ee9
+static int mkostemp_safe(char *pattern) {
5d2ee9
+        _unused_ _cleanup_umask_ mode_t u = umask(0077);
5d2ee9
+        int fd;
5d2ee9
+
5d2ee9
+        assert(pattern);
5d2ee9
+
5d2ee9
+        fd = mkostemp(pattern, O_CLOEXEC);
5d2ee9
+        if (fd < 0)
5d2ee9
+                return -errno;
5d2ee9
+
5d2ee9
+        return fd;
5d2ee9
+}
5d2ee9
+
5d2ee9
+static int real_open(const char *path, int flags, mode_t mode) {
5d2ee9
+        /* glibc internally calls openat() when open() is requested. Let's hence define our own wrapper for
5d2ee9
+         * testing purposes that calls the real syscall, on architectures where SYS_open is defined. On
5d2ee9
+         * other architectures, let's just fall back to the glibc call. */
5d2ee9
+
5d2ee9
+#ifdef SYS_open
5d2ee9
+        return (int) syscall(SYS_open, path, flags, mode);
5d2ee9
+#else
5d2ee9
+        return open(path, flags, mode);
5d2ee9
+#endif
5d2ee9
+}
5d2ee9
+
5d2ee9
+static void test_restrict_suid_sgid(void) {
5d2ee9
+        pid_t pid;
5d2ee9
+
5d2ee9
+        log_info("/* %s */", __func__);
5d2ee9
+
5d2ee9
+        if (!is_seccomp_available()) {
5d2ee9
+                log_notice("Seccomp not available, skipping %s", __func__);
5d2ee9
+                return;
5d2ee9
+        }
5d2ee9
+        if (geteuid() != 0) {
5d2ee9
+                log_notice("Not root, skipping %s", __func__);
5d2ee9
+                return;
5d2ee9
+        }
5d2ee9
+
5d2ee9
+        pid = fork();
5d2ee9
+        assert_se(pid >= 0);
5d2ee9
+
5d2ee9
+        if (pid == 0) {
5d2ee9
+                char path[] = "/tmp/suidsgidXXXXXX", dir[] = "/tmp/suidsgiddirXXXXXX";
5d2ee9
+                int fd = -1, k = -1;
5d2ee9
+                const char *z;
5d2ee9
+
5d2ee9
+                fd = mkostemp_safe(path);
5d2ee9
+                assert_se(fd >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mkdtemp(dir));
5d2ee9
+                z = strjoina(dir, "/test");
5d2ee9
+
5d2ee9
+                assert_se(chmod(path, 0755 | S_ISUID) >= 0);
5d2ee9
+                assert_se(chmod(path, 0755 | S_ISGID) >= 0);
5d2ee9
+                assert_se(chmod(path, 0755 | S_ISGID | S_ISUID) >= 0);
5d2ee9
+                assert_se(chmod(path, 0755) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(fchmod(fd, 0755 | S_ISUID) >= 0);
5d2ee9
+                assert_se(fchmod(fd, 0755 | S_ISGID) >= 0);
5d2ee9
+                assert_se(fchmod(fd, 0755 | S_ISGID | S_ISUID) >= 0);
5d2ee9
+                assert_se(fchmod(fd, 0755) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISUID, 0) >= 0);
5d2ee9
+                assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISGID, 0) >= 0);
5d2ee9
+                assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISGID | S_ISUID, 0) >= 0);
5d2ee9
+                assert_se(fchmodat(AT_FDCWD, path, 0755, 0) >= 0);
5d2ee9
+
5d2ee9
+                k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISGID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID | S_ISGID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = creat(z, 0644 | S_ISUID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = creat(z, 0644 | S_ISGID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = creat(z, 0644 | S_ISUID | S_ISGID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = creat(z, 0644);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISGID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID | S_ISGID);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mkdir(z, 0755 | S_ISUID) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+                assert_se(mkdir(z, 0755 | S_ISGID) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+                assert_se(mkdir(z, 0755 | S_ISUID | S_ISGID) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+                assert_se(mkdir(z, 0755) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISUID) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+                assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISGID) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+                assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISUID | S_ISGID) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+                assert_se(mkdirat(AT_FDCWD, z, 0755) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mknod(z, S_IFREG | 0755 | S_ISUID, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+                assert_se(mknod(z, S_IFREG | 0755 | S_ISGID, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+                assert_se(mknod(z, S_IFREG | 0755 | S_ISUID | S_ISGID, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+                assert_se(mknod(z, S_IFREG | 0755, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISUID, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+                assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISGID, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+                assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISUID | S_ISGID, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+                assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(seccomp_restrict_suid_sgid() >= 0);
5d2ee9
+
5d2ee9
+                assert_se(chmod(path, 0775 | S_ISUID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(chmod(path, 0775 | S_ISGID) < 0  && errno == EPERM);
5d2ee9
+                assert_se(chmod(path, 0775 | S_ISGID | S_ISUID) < 0  && errno == EPERM);
5d2ee9
+                assert_se(chmod(path, 0775) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(fchmod(fd, 0775 | S_ISUID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(fchmod(fd, 0775 | S_ISGID) < 0  && errno == EPERM);
5d2ee9
+                assert_se(fchmod(fd, 0775 | S_ISGID | S_ISUID) < 0  && errno == EPERM);
5d2ee9
+                assert_se(fchmod(fd, 0775) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISUID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISGID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(fchmodat(AT_FDCWD, path, 0755 | S_ISGID | S_ISUID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(fchmodat(AT_FDCWD, path, 0755, 0) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                k = real_open(z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(creat(z, 0644 | S_ISUID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(creat(z, 0644 | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(creat(z, 0644 | S_ISUID | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                k = creat(z, 0644);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644 | S_ISUID | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                k = openat(AT_FDCWD, z, O_CREAT|O_RDWR|O_CLOEXEC|O_EXCL, 0644);
5d2ee9
+                k = safe_close(k);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mkdir(z, 0755 | S_ISUID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mkdir(z, 0755 | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mkdir(z, 0755 | S_ISUID | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mkdir(z, 0755) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISUID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mkdirat(AT_FDCWD, z, 0755 | S_ISUID | S_ISGID) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mkdirat(AT_FDCWD, z, 0755) >= 0);
5d2ee9
+                assert_se(rmdir(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mknod(z, S_IFREG | 0755 | S_ISUID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mknod(z, S_IFREG | 0755 | S_ISGID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mknod(z, S_IFREG | 0755 | S_ISUID | S_ISGID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mknod(z, S_IFREG | 0755, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISUID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISGID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755 | S_ISUID | S_ISGID, 0) < 0 && errno == EPERM);
5d2ee9
+                assert_se(mknodat(AT_FDCWD, z, S_IFREG | 0755, 0) >= 0);
5d2ee9
+                assert_se(unlink(z) >= 0);
5d2ee9
+
5d2ee9
+                assert_se(unlink(path) >= 0);
5d2ee9
+                assert_se(rm_rf(dir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
5d2ee9
+
5d2ee9
+                _exit(EXIT_SUCCESS);
5d2ee9
+        }
5d2ee9
+
5d2ee9
+        assert_se(wait_for_terminate_and_check("suidsgidseccomp", pid, WAIT_LOG) == EXIT_SUCCESS);
5d2ee9
+}
5d2ee9
+
5d2ee9
 int main(int argc, char *argv[]) {
5d2ee9
 
5d2ee9
         log_set_max_level(LOG_DEBUG);
5d2ee9
@@ -684,6 +909,7 @@ int main(int argc, char *argv[]) {
5d2ee9
         test_load_syscall_filter_set_raw();
5d2ee9
         test_lock_personality();
5d2ee9
         test_filter_sets_ordered();
5d2ee9
+        test_restrict_suid_sgid();
5d2ee9
 
5d2ee9
         return 0;
5d2ee9
 }