cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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