cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-passthrough_ll-add-lo_map-for-ino-fh-indir.patch

ddf19c
From d56651e227bae83ee0cceb12bd91e3e9f6045ab3 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:13 +0100
ddf19c
Subject: [PATCH 042/116] virtiofsd: passthrough_ll: add lo_map for ino/fh
ddf19c
 indirection
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-39-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93492
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 038/112] virtiofsd: passthrough_ll: add lo_map for ino/fh indirection
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: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
A layer of indirection is needed because passthrough_ll cannot expose
ddf19c
pointers or file descriptor numbers to untrusted clients.  Malicious
ddf19c
clients could send invalid pointers or file descriptors in order to
ddf19c
crash or exploit the file system daemon.
ddf19c
ddf19c
lo_map provides an integer key->value mapping.  This will be used for
ddf19c
ino and fh fields in the patches that follow.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 25c135727b08dca90f00094e522a69170b13dfac)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/passthrough_ll.c | 124 +++++++++++++++++++++++++++++++++++++++
ddf19c
 1 file changed, 124 insertions(+)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
ddf19c
index 5e06179..e83a976 100644
ddf19c
--- a/tools/virtiofsd/passthrough_ll.c
ddf19c
+++ b/tools/virtiofsd/passthrough_ll.c
ddf19c
@@ -74,6 +74,21 @@ struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct {
ddf19c
 };
ddf19c
 #endif
ddf19c
 
ddf19c
+struct lo_map_elem {
ddf19c
+    union {
ddf19c
+        /* Element values will go here... */
ddf19c
+        ssize_t freelist;
ddf19c
+    };
ddf19c
+    bool in_use;
ddf19c
+};
ddf19c
+
ddf19c
+/* Maps FUSE fh or ino values to internal objects */
ddf19c
+struct lo_map {
ddf19c
+    struct lo_map_elem *elems;
ddf19c
+    size_t nelems;
ddf19c
+    ssize_t freelist;
ddf19c
+};
ddf19c
+
ddf19c
 struct lo_inode {
ddf19c
     struct lo_inode *next; /* protected by lo->mutex */
ddf19c
     struct lo_inode *prev; /* protected by lo->mutex */
ddf19c
@@ -130,6 +145,115 @@ static struct lo_data *lo_data(fuse_req_t req)
ddf19c
     return (struct lo_data *)fuse_req_userdata(req);
ddf19c
 }
ddf19c
 
ddf19c
+__attribute__((unused)) static void lo_map_init(struct lo_map *map)
ddf19c
+{
ddf19c
+    map->elems = NULL;
ddf19c
+    map->nelems = 0;
ddf19c
+    map->freelist = -1;
ddf19c
+}
ddf19c
+
ddf19c
+__attribute__((unused)) static void lo_map_destroy(struct lo_map *map)
ddf19c
+{
ddf19c
+    free(map->elems);
ddf19c
+}
ddf19c
+
ddf19c
+static int lo_map_grow(struct lo_map *map, size_t new_nelems)
ddf19c
+{
ddf19c
+    struct lo_map_elem *new_elems;
ddf19c
+    size_t i;
ddf19c
+
ddf19c
+    if (new_nelems <= map->nelems) {
ddf19c
+        return 1;
ddf19c
+    }
ddf19c
+
ddf19c
+    new_elems = realloc(map->elems, sizeof(map->elems[0]) * new_nelems);
ddf19c
+    if (!new_elems) {
ddf19c
+        return 0;
ddf19c
+    }
ddf19c
+
ddf19c
+    for (i = map->nelems; i < new_nelems; i++) {
ddf19c
+        new_elems[i].freelist = i + 1;
ddf19c
+        new_elems[i].in_use = false;
ddf19c
+    }
ddf19c
+    new_elems[new_nelems - 1].freelist = -1;
ddf19c
+
ddf19c
+    map->elems = new_elems;
ddf19c
+    map->freelist = map->nelems;
ddf19c
+    map->nelems = new_nelems;
ddf19c
+    return 1;
ddf19c
+}
ddf19c
+
ddf19c
+__attribute__((unused)) static struct lo_map_elem *
ddf19c
+lo_map_alloc_elem(struct lo_map *map)
ddf19c
+{
ddf19c
+    struct lo_map_elem *elem;
ddf19c
+
ddf19c
+    if (map->freelist == -1 && !lo_map_grow(map, map->nelems + 256)) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    elem = &map->elems[map->freelist];
ddf19c
+    map->freelist = elem->freelist;
ddf19c
+
ddf19c
+    elem->in_use = true;
ddf19c
+
ddf19c
+    return elem;
ddf19c
+}
ddf19c
+
ddf19c
+__attribute__((unused)) static struct lo_map_elem *
ddf19c
+lo_map_reserve(struct lo_map *map, size_t key)
ddf19c
+{
ddf19c
+    ssize_t *prev;
ddf19c
+
ddf19c
+    if (!lo_map_grow(map, key + 1)) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    for (prev = &map->freelist; *prev != -1;
ddf19c
+         prev = &map->elems[*prev].freelist) {
ddf19c
+        if (*prev == key) {
ddf19c
+            struct lo_map_elem *elem = &map->elems[key];
ddf19c
+
ddf19c
+            *prev = elem->freelist;
ddf19c
+            elem->in_use = true;
ddf19c
+            return elem;
ddf19c
+        }
ddf19c
+    }
ddf19c
+    return NULL;
ddf19c
+}
ddf19c
+
ddf19c
+__attribute__((unused)) static struct lo_map_elem *
ddf19c
+lo_map_get(struct lo_map *map, size_t key)
ddf19c
+{
ddf19c
+    if (key >= map->nelems) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+    if (!map->elems[key].in_use) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+    return &map->elems[key];
ddf19c
+}
ddf19c
+
ddf19c
+__attribute__((unused)) static void lo_map_remove(struct lo_map *map,
ddf19c
+                                                  size_t key)
ddf19c
+{
ddf19c
+    struct lo_map_elem *elem;
ddf19c
+
ddf19c
+    if (key >= map->nelems) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    elem = &map->elems[key];
ddf19c
+    if (!elem->in_use) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    elem->in_use = false;
ddf19c
+
ddf19c
+    elem->freelist = map->freelist;
ddf19c
+    map->freelist = key;
ddf19c
+}
ddf19c
+
ddf19c
 static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
ddf19c
 {
ddf19c
     if (ino == FUSE_ROOT_ID) {
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c