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

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