21255d
From 8cc497e735104080f6830a8f468b2724ae372990 Mon Sep 17 00:00:00 2001
21255d
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
21255d
Date: Wed, 3 Apr 2019 13:11:00 +0200
21255d
Subject: [PATCH] seccomp: rework how the S[UG]ID filter is installed
21255d
21255d
If we know that a syscall is undefined on the given architecture, don't
21255d
even try to add it.
21255d
21255d
Try to install the filter even if some syscalls fail. Also use a helper
21255d
function to make the whole a bit less magic.
21255d
21255d
This allows the S[UG]ID test to pass on arm64.
21255d
21255d
(cherry picked from commit da4dc9a6748797e804b6bc92ad513d509abf581c)
21255d
21255d
Resolves: #1860374
21255d
---
21255d
 src/shared/seccomp-util.c | 244 +++++++++++++++++++++-----------------
21255d
 1 file changed, 138 insertions(+), 106 deletions(-)
21255d
21255d
diff --git a/src/shared/seccomp-util.c b/src/shared/seccomp-util.c
21255d
index fd46b9f88d..d91fb4e269 100644
21255d
--- a/src/shared/seccomp-util.c
21255d
+++ b/src/shared/seccomp-util.c
21255d
@@ -1750,9 +1750,139 @@ int seccomp_lock_personality(unsigned long personality) {
21255d
         return 0;
21255d
 }
21255d
 
21255d
+static int seccomp_restrict_sxid(scmp_filter_ctx seccomp, mode_t m) {
21255d
+        /* Checks the mode_t parameter of the following system calls:
21255d
+         *
21255d
+         *       → chmod() + fchmod() + fchmodat()
21255d
+         *       → open() + creat() + openat()
21255d
+         *       → mkdir() + mkdirat()
21255d
+         *       → mknod() + mknodat()
21255d
+         *
21255d
+         * Returns error if *everything* failed, and 0 otherwise.
21255d
+         */
21255d
+        int r = 0;
21255d
+        bool any = false;
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(chmod),
21255d
+                        1,
21255d
+                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for chmod: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(fchmod),
21255d
+                        1,
21255d
+                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for fchmod: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(fchmodat),
21255d
+                        1,
21255d
+                        SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for fchmodat: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(mkdir),
21255d
+                        1,
21255d
+                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for mkdir: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(mkdirat),
21255d
+                        1,
21255d
+                        SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for mkdirat: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(mknod),
21255d
+                        1,
21255d
+                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for mknod: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(mknodat),
21255d
+                        1,
21255d
+                        SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for mknodat: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+#if SCMP_SYS(open) > 0
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(open),
21255d
+                        2,
21255d
+                        SCMP_A1(SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT),
21255d
+                        SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for open: %m");
21255d
+        else
21255d
+                any = true;
21255d
+#endif
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(openat),
21255d
+                        2,
21255d
+                        SCMP_A2(SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT),
21255d
+                        SCMP_A3(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for openat: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+        r = seccomp_rule_add_exact(
21255d
+                        seccomp,
21255d
+                        SCMP_ACT_ERRNO(EPERM),
21255d
+                        SCMP_SYS(creat),
21255d
+                        1,
21255d
+                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
+        if (r < 0)
21255d
+                log_debug_errno(r, "Failed to add filter for creat: %m");
21255d
+        else
21255d
+                any = true;
21255d
+
21255d
+        return any ? 0 : r;
21255d
+}
21255d
+
21255d
 int seccomp_restrict_suid_sgid(void) {
21255d
         uint32_t arch;
21255d
-        int r;
21255d
+        int r, k;
21255d
 
21255d
         SECCOMP_FOREACH_LOCAL_ARCH(arch) {
21255d
                 _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
21255d
@@ -1761,114 +1891,16 @@ int seccomp_restrict_suid_sgid(void) {
21255d
                 if (r < 0)
21255d
                         return r;
21255d
 
21255d
-                /* Checks the mode_t parameter of the following system calls:
21255d
-                 *
21255d
-                 *       → chmod() + fchmod() + fchmodat()
21255d
-                 *       → open() + creat() + openat()
21255d
-                 *       → mkdir() + mkdirat()
21255d
-                 *       → mknod() + mknodat()
21255d
-                 */
21255d
-
21255d
-                for (unsigned bit = 0; bit < 2; bit ++) {
21255d
-                        /* Block S_ISUID in the first iteration, S_ISGID in the second */
21255d
-                        mode_t m = bit == 0 ? S_ISUID : S_ISGID;
21255d
-
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(chmod),
21255d
-                                        1,
21255d
-                                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
-
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(fchmod),
21255d
-                                        1,
21255d
-                                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
-
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(fchmodat),
21255d
-                                        1,
21255d
-                                        SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
-
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(mkdir),
21255d
-                                        1,
21255d
-                                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
-
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(mkdirat),
21255d
-                                        1,
21255d
-                                        SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
-
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(mknod),
21255d
-                                        1,
21255d
-                                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
-
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(mknodat),
21255d
-                                        1,
21255d
-                                        SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
-
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(open),
21255d
-                                        2,
21255d
-                                        SCMP_A1(SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT),
21255d
-                                        SCMP_A2(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
+                r = seccomp_restrict_sxid(seccomp, S_ISUID);
21255d
+                if (r < 0)
21255d
+                        log_debug_errno(r, "Failed to add suid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch));
21255d
 
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(openat),
21255d
-                                        2,
21255d
-                                        SCMP_A2(SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT),
21255d
-                                        SCMP_A3(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
+                k = seccomp_restrict_sxid(seccomp, S_ISGID);
21255d
+                if (k < 0)
21255d
+                        log_debug_errno(r, "Failed to add sgid rule for architecture %s, ignoring: %m", seccomp_arch_to_string(arch));
21255d
 
21255d
-                        r = seccomp_rule_add_exact(
21255d
-                                        seccomp,
21255d
-                                        SCMP_ACT_ERRNO(EPERM),
21255d
-                                        SCMP_SYS(creat),
21255d
-                                        1,
21255d
-                                        SCMP_A1(SCMP_CMP_MASKED_EQ, m, m));
21255d
-                        if (r < 0)
21255d
-                                break;
21255d
-                }
21255d
-                if (r < 0) {
21255d
-                        log_debug_errno(r, "Failed to add suid/sgid rule for architecture %s, skipping: %m", seccomp_arch_to_string(arch));
21255d
+                if (r < 0 && k < 0)
21255d
                         continue;
21255d
-                }
21255d
 
21255d
                 r = seccomp_load(seccomp);
21255d
                 if (IN_SET(r, -EPERM, -EACCES))