peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0044-virtiofsd-passthrough_ll-add-lo_map-for-ino-fh-indir.patch

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