Blame SOURCES/kvm-seccomp-prefer-SCMP_ACT_KILL_PROCESS-if-available.patch

ae23c9
From caa17bcb65ea65a9fa39e7d6117a87cc7cc9c0ce Mon Sep 17 00:00:00 2001
ae23c9
From: Eduardo Otubo <otubo@redhat.com>
ae23c9
Date: Fri, 28 Sep 2018 07:56:37 +0100
ae23c9
Subject: [PATCH 3/6] seccomp: prefer SCMP_ACT_KILL_PROCESS if available
ae23c9
MIME-Version: 1.0
ae23c9
Content-Type: text/plain; charset=UTF-8
ae23c9
Content-Transfer-Encoding: 8bit
ae23c9
ae23c9
RH-Author: Eduardo Otubo <otubo@redhat.com>
ae23c9
Message-id: <20180928075639.16746-4-otubo@redhat.com>
ae23c9
Patchwork-id: 82315
ae23c9
O-Subject: [RHEL-8 qemu-kvm PATCH 3/5] seccomp: prefer SCMP_ACT_KILL_PROCESS if available
ae23c9
Bugzilla: 1618356
ae23c9
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ae23c9
RH-Acked-by: Marc-André Lureau <marcandre.lureau@redhat.com>
ae23c9
RH-Acked-by: Thomas Huth <thuth@redhat.com>
ae23c9
ae23c9
From: Marc-André Lureau <marcandre.lureau@redhat.com>
ae23c9
ae23c9
commit bda08a5764d470f101fa38635d30b41179a313e1
ae23c9
Author: Marc-André Lureau <marcandre.lureau@redhat.com>
ae23c9
Date:   Wed Aug 22 19:02:48 2018 +0200
ae23c9
ae23c9
    seccomp: prefer SCMP_ACT_KILL_PROCESS if available
ae23c9
ae23c9
    The upcoming libseccomp release should have SCMP_ACT_KILL_PROCESS
ae23c9
    action (https://github.com/seccomp/libseccomp/issues/96).
ae23c9
ae23c9
    SCMP_ACT_KILL_PROCESS is preferable to immediately terminate the
ae23c9
    offending process, rather than having the SIGSYS handler running.
ae23c9
ae23c9
    Use SECCOMP_GET_ACTION_AVAIL to check availability of kernel support,
ae23c9
    as libseccomp will fallback on SCMP_ACT_KILL otherwise, and we still
ae23c9
    prefer SCMP_ACT_TRAP.
ae23c9
ae23c9
    Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
ae23c9
    Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
ae23c9
    Acked-by: Eduardo Otubo <otubo@redhat.com>
ae23c9
ae23c9
Signed-off-by: Eduardo Otubo <otubo@rehdat.com>
ae23c9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ae23c9
---
ae23c9
 qemu-seccomp.c | 31 ++++++++++++++++++++++++++++++-
ae23c9
 1 file changed, 30 insertions(+), 1 deletion(-)
ae23c9
ae23c9
diff --git a/qemu-seccomp.c b/qemu-seccomp.c
ae23c9
index b88fa05..10fcfa3 100644
ae23c9
--- a/qemu-seccomp.c
ae23c9
+++ b/qemu-seccomp.c
ae23c9
@@ -15,6 +15,7 @@
ae23c9
 #include "qemu/osdep.h"
ae23c9
 #include <seccomp.h>
ae23c9
 #include "sysemu/seccomp.h"
ae23c9
+#include <linux/seccomp.h>
ae23c9
 
ae23c9
 /* For some architectures (notably ARM) cacheflush is not supported until
ae23c9
  * libseccomp 2.2.3, but configure enforces that we are using a more recent
ae23c9
@@ -102,12 +103,40 @@ static const struct QemuSeccompSyscall blacklist[] = {
ae23c9
     { SCMP_SYS(sched_get_priority_min), QEMU_SECCOMP_SET_RESOURCECTL },
ae23c9
 };
ae23c9
 
ae23c9
+static inline __attribute__((unused)) int
ae23c9
+qemu_seccomp(unsigned int operation, unsigned int flags, void *args)
ae23c9
+{
ae23c9
+#ifdef __NR_seccomp
ae23c9
+    return syscall(__NR_seccomp, operation, flags, args);
ae23c9
+#else
ae23c9
+    errno = ENOSYS;
ae23c9
+    return -1;
ae23c9
+#endif
ae23c9
+}
ae23c9
+
ae23c9
+static uint32_t qemu_seccomp_get_kill_action(void)
ae23c9
+{
ae23c9
+#if defined(SECCOMP_GET_ACTION_AVAIL) && defined(SCMP_ACT_KILL_PROCESS) && \
ae23c9
+    defined(SECCOMP_RET_KILL_PROCESS)
ae23c9
+    {
ae23c9
+        uint32_t action = SECCOMP_RET_KILL_PROCESS;
ae23c9
+
ae23c9
+        if (qemu_seccomp(SECCOMP_GET_ACTION_AVAIL, 0, &action) == 0) {
ae23c9
+            return SCMP_ACT_KILL_PROCESS;
ae23c9
+        }
ae23c9
+    }
ae23c9
+#endif
ae23c9
+
ae23c9
+    return SCMP_ACT_TRAP;
ae23c9
+}
ae23c9
+
ae23c9
 
ae23c9
 int seccomp_start(uint32_t seccomp_opts)
ae23c9
 {
ae23c9
     int rc = 0;
ae23c9
     unsigned int i = 0;
ae23c9
     scmp_filter_ctx ctx;
ae23c9
+    uint32_t action = qemu_seccomp_get_kill_action();
ae23c9
 
ae23c9
     ctx = seccomp_init(SCMP_ACT_ALLOW);
ae23c9
     if (ctx == NULL) {
ae23c9
@@ -120,7 +149,7 @@ int seccomp_start(uint32_t seccomp_opts)
ae23c9
             continue;
ae23c9
         }
ae23c9
 
ae23c9
-        rc = seccomp_rule_add_array(ctx, SCMP_ACT_TRAP, blacklist[i].num,
ae23c9
+        rc = seccomp_rule_add_array(ctx, action, blacklist[i].num,
ae23c9
                                     blacklist[i].narg, blacklist[i].arg_cmp);
ae23c9
         if (rc < 0) {
ae23c9
             goto seccomp_return;
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9