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