thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-virtiofsd-Drop-CAP_FSETID-if-client-asked-for-it.patch

902636
From e217ab392e0d4c770ec18dbfbe986771773cb557 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:33 +0100
902636
Subject: [PATCH 062/116] virtiofsd: Drop CAP_FSETID if client asked for it
902636
MIME-Version: 1.0
902636
Content-Type: text/plain; charset=UTF-8
902636
Content-Transfer-Encoding: 8bit
902636
902636
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Message-id: <20200127190227.40942-59-dgilbert@redhat.com>
902636
Patchwork-id: 93513
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 058/112] virtiofsd: Drop CAP_FSETID if client asked for it
902636
Bugzilla: 1694164
902636
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
902636
From: Vivek Goyal <vgoyal@redhat.com>
902636
902636
If client requested killing setuid/setgid bits on file being written, drop
902636
CAP_FSETID capability so that setuid/setgid bits are cleared upon write
902636
automatically.
902636
902636
pjdfstest chown/12.t needs this.
902636
902636
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
902636
  dgilbert: reworked for libcap-ng
902636
Reviewed-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
902636
Reviewed-by: Sergio Lopez <slp@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit ee88465224b3aed2596049caa28f86cbe0d5a3d0)
902636
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/passthrough_ll.c | 105 +++++++++++++++++++++++++++++++++++++++
902636
 1 file changed, 105 insertions(+)
902636
902636
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
902636
index 97e7c75..d53cb1e 100644
902636
--- a/tools/virtiofsd/passthrough_ll.c
902636
+++ b/tools/virtiofsd/passthrough_ll.c
902636
@@ -201,6 +201,91 @@ static int load_capng(void)
902636
     return 0;
902636
 }
902636
 
902636
+/*
902636
+ * Helpers for dropping and regaining effective capabilities. Returns 0
902636
+ * on success, error otherwise
902636
+ */
902636
+static int drop_effective_cap(const char *cap_name, bool *cap_dropped)
902636
+{
902636
+    int cap, ret;
902636
+
902636
+    cap = capng_name_to_capability(cap_name);
902636
+    if (cap < 0) {
902636
+        ret = errno;
902636
+        fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
902636
+                 cap_name, strerror(errno));
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (load_capng()) {
902636
+        ret = errno;
902636
+        fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
902636
+        goto out;
902636
+    }
902636
+
902636
+    /* We dont have this capability in effective set already. */
902636
+    if (!capng_have_capability(CAPNG_EFFECTIVE, cap)) {
902636
+        ret = 0;
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, cap)) {
902636
+        ret = errno;
902636
+        fuse_log(FUSE_LOG_ERR, "capng_update(DROP,) failed\n");
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (capng_apply(CAPNG_SELECT_CAPS)) {
902636
+        ret = errno;
902636
+        fuse_log(FUSE_LOG_ERR, "drop:capng_apply() failed\n");
902636
+        goto out;
902636
+    }
902636
+
902636
+    ret = 0;
902636
+    if (cap_dropped) {
902636
+        *cap_dropped = true;
902636
+    }
902636
+
902636
+out:
902636
+    return ret;
902636
+}
902636
+
902636
+static int gain_effective_cap(const char *cap_name)
902636
+{
902636
+    int cap;
902636
+    int ret = 0;
902636
+
902636
+    cap = capng_name_to_capability(cap_name);
902636
+    if (cap < 0) {
902636
+        ret = errno;
902636
+        fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
902636
+                 cap_name, strerror(errno));
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (load_capng()) {
902636
+        ret = errno;
902636
+        fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, cap)) {
902636
+        ret = errno;
902636
+        fuse_log(FUSE_LOG_ERR, "capng_update(ADD,) failed\n");
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (capng_apply(CAPNG_SELECT_CAPS)) {
902636
+        ret = errno;
902636
+        fuse_log(FUSE_LOG_ERR, "gain:capng_apply() failed\n");
902636
+        goto out;
902636
+    }
902636
+    ret = 0;
902636
+
902636
+out:
902636
+    return ret;
902636
+}
902636
+
902636
 static void lo_map_init(struct lo_map *map)
902636
 {
902636
     map->elems = NULL;
902636
@@ -1577,6 +1662,7 @@ static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
902636
     (void)ino;
902636
     ssize_t res;
902636
     struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
902636
+    bool cap_fsetid_dropped = false;
902636
 
902636
     out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
902636
     out_buf.buf[0].fd = lo_fi_fd(req, fi);
902636
@@ -1588,12 +1674,31 @@ static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
902636
                  out_buf.buf[0].size, (unsigned long)off);
902636
     }
902636
 
902636
+    /*
902636
+     * If kill_priv is set, drop CAP_FSETID which should lead to kernel
902636
+     * clearing setuid/setgid on file.
902636
+     */
902636
+    if (fi->kill_priv) {
902636
+        res = drop_effective_cap("FSETID", &cap_fsetid_dropped);
902636
+        if (res != 0) {
902636
+            fuse_reply_err(req, res);
902636
+            return;
902636
+        }
902636
+    }
902636
+
902636
     res = fuse_buf_copy(&out_buf, in_buf);
902636
     if (res < 0) {
902636
         fuse_reply_err(req, -res);
902636
     } else {
902636
         fuse_reply_write(req, (size_t)res);
902636
     }
902636
+
902636
+    if (cap_fsetid_dropped) {
902636
+        res = gain_effective_cap("FSETID");
902636
+        if (res) {
902636
+            fuse_log(FUSE_LOG_ERR, "Failed to gain CAP_FSETID\n");
902636
+        }
902636
+    }
902636
 }
902636
 
902636
 static void lo_statfs(fuse_req_t req, fuse_ino_t ino)
902636
-- 
902636
1.8.3.1
902636