ddf19c
From 1b0edd3d0a2ee5c097bcf3501c1dfa937f02e473 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:21 +0100
ddf19c
Subject: [PATCH 050/116] virtiofsd: add fuse_mbuf_iter API
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-47-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93502
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 046/112] virtiofsd: add fuse_mbuf_iter API
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
Introduce an API for consuming bytes from a buffer with size checks.
ddf19c
All FUSE operations will be converted to use this safe API instead of
ddf19c
void *inarg.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit dad157e880416ab3a0e45beaa0e81977516568bc)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/buffer.c      | 28 +++++++++++++++++++++++++
ddf19c
 tools/virtiofsd/fuse_common.h | 49 ++++++++++++++++++++++++++++++++++++++++++-
ddf19c
 2 files changed, 76 insertions(+), 1 deletion(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/buffer.c b/tools/virtiofsd/buffer.c
ddf19c
index 772efa9..42a608f 100644
ddf19c
--- a/tools/virtiofsd/buffer.c
ddf19c
+++ b/tools/virtiofsd/buffer.c
ddf19c
@@ -267,3 +267,31 @@ ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv)
ddf19c
 
ddf19c
     return copied;
ddf19c
 }
ddf19c
+
ddf19c
+void *fuse_mbuf_iter_advance(struct fuse_mbuf_iter *iter, size_t len)
ddf19c
+{
ddf19c
+    void *ptr;
ddf19c
+
ddf19c
+    if (len > iter->size - iter->pos) {
ddf19c
+        return NULL;
ddf19c
+    }
ddf19c
+
ddf19c
+    ptr = iter->mem + iter->pos;
ddf19c
+    iter->pos += len;
ddf19c
+    return ptr;
ddf19c
+}
ddf19c
+
ddf19c
+const char *fuse_mbuf_iter_advance_str(struct fuse_mbuf_iter *iter)
ddf19c
+{
ddf19c
+    const char *str = iter->mem + iter->pos;
ddf19c
+    size_t remaining = iter->size - iter->pos;
ddf19c
+    size_t i;
ddf19c
+
ddf19c
+    for (i = 0; i < remaining; i++) {
ddf19c
+        if (str[i] == '\0') {
ddf19c
+            iter->pos += i + 1;
ddf19c
+            return str;
ddf19c
+        }
ddf19c
+    }
ddf19c
+    return NULL;
ddf19c
+}
ddf19c
diff --git a/tools/virtiofsd/fuse_common.h b/tools/virtiofsd/fuse_common.h
ddf19c
index 0cb33ac..f8f6433 100644
ddf19c
--- a/tools/virtiofsd/fuse_common.h
ddf19c
+++ b/tools/virtiofsd/fuse_common.h
ddf19c
@@ -703,10 +703,57 @@ size_t fuse_buf_size(const struct fuse_bufvec *bufv);
ddf19c
  */
ddf19c
 ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src);
ddf19c
 
ddf19c
+/**
ddf19c
+ * Memory buffer iterator
ddf19c
+ *
ddf19c
+ */
ddf19c
+struct fuse_mbuf_iter {
ddf19c
+    /**
ddf19c
+     * Data pointer
ddf19c
+     */
ddf19c
+    void *mem;
ddf19c
+
ddf19c
+    /**
ddf19c
+     * Total length, in bytes
ddf19c
+     */
ddf19c
+    size_t size;
ddf19c
+
ddf19c
+    /**
ddf19c
+     * Offset from start of buffer
ddf19c
+     */
ddf19c
+    size_t pos;
ddf19c
+};
ddf19c
+
ddf19c
+/* Initialize memory buffer iterator from a fuse_buf */
ddf19c
+#define FUSE_MBUF_ITER_INIT(fbuf) \
ddf19c
+    ((struct fuse_mbuf_iter){     \
ddf19c
+        .mem = fbuf->mem,         \
ddf19c
+        .size = fbuf->size,       \
ddf19c
+        .pos = 0,                 \
ddf19c
+    })
ddf19c
+
ddf19c
+/**
ddf19c
+ * Consume bytes from a memory buffer iterator
ddf19c
+ *
ddf19c
+ * @param iter memory buffer iterator
ddf19c
+ * @param len number of bytes to consume
ddf19c
+ * @return pointer to start of consumed bytes or
ddf19c
+ *         NULL if advancing beyond end of buffer
ddf19c
+ */
ddf19c
+void *fuse_mbuf_iter_advance(struct fuse_mbuf_iter *iter, size_t len);
ddf19c
+
ddf19c
+/**
ddf19c
+ * Consume a NUL-terminated string from a memory buffer iterator
ddf19c
+ *
ddf19c
+ * @param iter memory buffer iterator
ddf19c
+ * @return pointer to the string or
ddf19c
+ *         NULL if advancing beyond end of buffer or there is no NUL-terminator
ddf19c
+ */
ddf19c
+const char *fuse_mbuf_iter_advance_str(struct fuse_mbuf_iter *iter);
ddf19c
+
ddf19c
 /*
ddf19c
  * Signal handling
ddf19c
  */
ddf19c
-
ddf19c
 /**
ddf19c
  * Exit session on HUP, TERM and INT signals and ignore PIPE signal
ddf19c
  *
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c