render / rpms / qemu

Forked from rpms/qemu 9 months ago
Clone

Blame 0064-virtiofsd-Drop-CAP_FSETID-if-client-asked-for-it.patch

1d442b
From: Vivek Goyal <vgoyal@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:01:33 +0000
1d442b
Subject: [PATCH] virtiofsd: Drop CAP_FSETID if client asked for it
1d442b
1d442b
If client requested killing setuid/setgid bits on file being written, drop
1d442b
CAP_FSETID capability so that setuid/setgid bits are cleared upon write
1d442b
automatically.
1d442b
1d442b
pjdfstest chown/12.t needs this.
1d442b
1d442b
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
1d442b
  dgilbert: reworked for libcap-ng
1d442b
Reviewed-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
1d442b
Reviewed-by: Sergio Lopez <slp@redhat.com>
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
(cherry picked from commit ee88465224b3aed2596049caa28f86cbe0d5a3d0)
1d442b
---
1d442b
 tools/virtiofsd/passthrough_ll.c | 105 +++++++++++++++++++++++++++++++
1d442b
 1 file changed, 105 insertions(+)
1d442b
1d442b
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
1d442b
index 97e7c75667..d53cb1e005 100644
1d442b
--- a/tools/virtiofsd/passthrough_ll.c
1d442b
+++ b/tools/virtiofsd/passthrough_ll.c
1d442b
@@ -201,6 +201,91 @@ static int load_capng(void)
1d442b
     return 0;
1d442b
 }
1d442b
 
1d442b
+/*
1d442b
+ * Helpers for dropping and regaining effective capabilities. Returns 0
1d442b
+ * on success, error otherwise
1d442b
+ */
1d442b
+static int drop_effective_cap(const char *cap_name, bool *cap_dropped)
1d442b
+{
1d442b
+    int cap, ret;
1d442b
+
1d442b
+    cap = capng_name_to_capability(cap_name);
1d442b
+    if (cap < 0) {
1d442b
+        ret = errno;
1d442b
+        fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
1d442b
+                 cap_name, strerror(errno));
1d442b
+        goto out;
1d442b
+    }
1d442b
+
1d442b
+    if (load_capng()) {
1d442b
+        ret = errno;
1d442b
+        fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
1d442b
+        goto out;
1d442b
+    }
1d442b
+
1d442b
+    /* We dont have this capability in effective set already. */
1d442b
+    if (!capng_have_capability(CAPNG_EFFECTIVE, cap)) {
1d442b
+        ret = 0;
1d442b
+        goto out;
1d442b
+    }
1d442b
+
1d442b
+    if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, cap)) {
1d442b
+        ret = errno;
1d442b
+        fuse_log(FUSE_LOG_ERR, "capng_update(DROP,) failed\n");
1d442b
+        goto out;
1d442b
+    }
1d442b
+
1d442b
+    if (capng_apply(CAPNG_SELECT_CAPS)) {
1d442b
+        ret = errno;
1d442b
+        fuse_log(FUSE_LOG_ERR, "drop:capng_apply() failed\n");
1d442b
+        goto out;
1d442b
+    }
1d442b
+
1d442b
+    ret = 0;
1d442b
+    if (cap_dropped) {
1d442b
+        *cap_dropped = true;
1d442b
+    }
1d442b
+
1d442b
+out:
1d442b
+    return ret;
1d442b
+}
1d442b
+
1d442b
+static int gain_effective_cap(const char *cap_name)
1d442b
+{
1d442b
+    int cap;
1d442b
+    int ret = 0;
1d442b
+
1d442b
+    cap = capng_name_to_capability(cap_name);
1d442b
+    if (cap < 0) {
1d442b
+        ret = errno;
1d442b
+        fuse_log(FUSE_LOG_ERR, "capng_name_to_capability(%s) failed:%s\n",
1d442b
+                 cap_name, strerror(errno));
1d442b
+        goto out;
1d442b
+    }
1d442b
+
1d442b
+    if (load_capng()) {
1d442b
+        ret = errno;
1d442b
+        fuse_log(FUSE_LOG_ERR, "load_capng() failed\n");
1d442b
+        goto out;
1d442b
+    }
1d442b
+
1d442b
+    if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, cap)) {
1d442b
+        ret = errno;
1d442b
+        fuse_log(FUSE_LOG_ERR, "capng_update(ADD,) failed\n");
1d442b
+        goto out;
1d442b
+    }
1d442b
+
1d442b
+    if (capng_apply(CAPNG_SELECT_CAPS)) {
1d442b
+        ret = errno;
1d442b
+        fuse_log(FUSE_LOG_ERR, "gain:capng_apply() failed\n");
1d442b
+        goto out;
1d442b
+    }
1d442b
+    ret = 0;
1d442b
+
1d442b
+out:
1d442b
+    return ret;
1d442b
+}
1d442b
+
1d442b
 static void lo_map_init(struct lo_map *map)
1d442b
 {
1d442b
     map->elems = NULL;
1d442b
@@ -1577,6 +1662,7 @@ static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
1d442b
     (void)ino;
1d442b
     ssize_t res;
1d442b
     struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
1d442b
+    bool cap_fsetid_dropped = false;
1d442b
 
1d442b
     out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
1d442b
     out_buf.buf[0].fd = lo_fi_fd(req, fi);
1d442b
@@ -1588,12 +1674,31 @@ static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
1d442b
                  out_buf.buf[0].size, (unsigned long)off);
1d442b
     }
1d442b
 
1d442b
+    /*
1d442b
+     * If kill_priv is set, drop CAP_FSETID which should lead to kernel
1d442b
+     * clearing setuid/setgid on file.
1d442b
+     */
1d442b
+    if (fi->kill_priv) {
1d442b
+        res = drop_effective_cap("FSETID", &cap_fsetid_dropped);
1d442b
+        if (res != 0) {
1d442b
+            fuse_reply_err(req, res);
1d442b
+            return;
1d442b
+        }
1d442b
+    }
1d442b
+
1d442b
     res = fuse_buf_copy(&out_buf, in_buf);
1d442b
     if (res < 0) {
1d442b
         fuse_reply_err(req, -res);
1d442b
     } else {
1d442b
         fuse_reply_write(req, (size_t)res);
1d442b
     }
1d442b
+
1d442b
+    if (cap_fsetid_dropped) {
1d442b
+        res = gain_effective_cap("FSETID");
1d442b
+        if (res) {
1d442b
+            fuse_log(FUSE_LOG_ERR, "Failed to gain CAP_FSETID\n");
1d442b
+        }
1d442b
+    }
1d442b
 }
1d442b
 
1d442b
 static void lo_statfs(fuse_req_t req, fuse_ino_t ino)