thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-virtiofsd-validate-input-buffer-sizes-in-do_write_bu.patch

ddf19c
From 6877a6c456178d6c1ca9a0ffaabaa7e51105b2ac Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:22 +0100
ddf19c
Subject: [PATCH 051/116] virtiofsd: validate input buffer sizes in
ddf19c
 do_write_buf()
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-48-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93501
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 047/112] virtiofsd: validate input buffer sizes in do_write_buf()
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
There is a small change in behavior: if fuse_write_in->size doesn't
ddf19c
match the input buffer size then the request is failed.  Previously
ddf19c
write requests with 1 fuse_buf element would truncate to
ddf19c
fuse_write_in->size.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Sergio Lopez <slp@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 0ba8c3c6fce8fe949d59c1fd84d98d220ef9e759)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/fuse_lowlevel.c | 49 +++++++++++++++++++++++++----------------
ddf19c
 1 file changed, 30 insertions(+), 19 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
index 7e10995..611e8b0 100644
ddf19c
--- a/tools/virtiofsd/fuse_lowlevel.c
ddf19c
+++ b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
@@ -1003,8 +1003,8 @@ static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
ddf19c
     }
ddf19c
 }
ddf19c
 
ddf19c
-static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
ddf19c
-                         struct fuse_bufvec *ibufv)
ddf19c
+static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid,
ddf19c
+                         struct fuse_mbuf_iter *iter, struct fuse_bufvec *ibufv)
ddf19c
 {
ddf19c
     struct fuse_session *se = req->se;
ddf19c
     struct fuse_bufvec *pbufv = ibufv;
ddf19c
@@ -1012,28 +1012,27 @@ static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
ddf19c
         .buf[0] = ibufv->buf[0],
ddf19c
         .count = 1,
ddf19c
     };
ddf19c
-    struct fuse_write_in *arg = (struct fuse_write_in *)inarg;
ddf19c
+    struct fuse_write_in *arg;
ddf19c
+    size_t arg_size = sizeof(*arg);
ddf19c
     struct fuse_file_info fi;
ddf19c
 
ddf19c
     memset(&fi, 0, sizeof(fi));
ddf19c
+
ddf19c
+    arg = fuse_mbuf_iter_advance(iter, arg_size);
ddf19c
+    if (!arg) {
ddf19c
+        fuse_reply_err(req, EINVAL);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    fi.lock_owner = arg->lock_owner;
ddf19c
+    fi.flags = arg->flags;
ddf19c
     fi.fh = arg->fh;
ddf19c
     fi.writepage = arg->write_flags & FUSE_WRITE_CACHE;
ddf19c
 
ddf19c
     if (ibufv->count == 1) {
ddf19c
-        fi.lock_owner = arg->lock_owner;
ddf19c
-        fi.flags = arg->flags;
ddf19c
-        if (!(tmpbufv.buf[0].flags & FUSE_BUF_IS_FD)) {
ddf19c
-            tmpbufv.buf[0].mem = PARAM(arg);
ddf19c
-        }
ddf19c
-        tmpbufv.buf[0].size -=
ddf19c
-            sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in);
ddf19c
-        if (tmpbufv.buf[0].size < arg->size) {
ddf19c
-            fuse_log(FUSE_LOG_ERR,
ddf19c
-                     "fuse: do_write_buf: buffer size too small\n");
ddf19c
-            fuse_reply_err(req, EIO);
ddf19c
-            return;
ddf19c
-        }
ddf19c
-        tmpbufv.buf[0].size = arg->size;
ddf19c
+        assert(!(tmpbufv.buf[0].flags & FUSE_BUF_IS_FD));
ddf19c
+        tmpbufv.buf[0].mem = ((char *)arg) + arg_size;
ddf19c
+        tmpbufv.buf[0].size -= sizeof(struct fuse_in_header) + arg_size;
ddf19c
         pbufv = &tmpbufv;
ddf19c
     } else {
ddf19c
         /*
ddf19c
@@ -1043,6 +1042,13 @@ static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
ddf19c
         ibufv->buf[0].size = 0;
ddf19c
     }
ddf19c
 
ddf19c
+    if (fuse_buf_size(pbufv) != arg->size) {
ddf19c
+        fuse_log(FUSE_LOG_ERR,
ddf19c
+                 "fuse: do_write_buf: buffer size doesn't match arg->size\n");
ddf19c
+        fuse_reply_err(req, EIO);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
     se->op.write_buf(req, nodeid, pbufv, arg->offset, &fi);
ddf19c
 }
ddf19c
 
ddf19c
@@ -2052,12 +2058,17 @@ void fuse_session_process_buf_int(struct fuse_session *se,
ddf19c
                                   struct fuse_chan *ch)
ddf19c
 {
ddf19c
     const struct fuse_buf *buf = bufv->buf;
ddf19c
+    struct fuse_mbuf_iter iter = FUSE_MBUF_ITER_INIT(buf);
ddf19c
     struct fuse_in_header *in;
ddf19c
     const void *inarg;
ddf19c
     struct fuse_req *req;
ddf19c
     int err;
ddf19c
 
ddf19c
-    in = buf->mem;
ddf19c
+    /* The first buffer must be a memory buffer */
ddf19c
+    assert(!(buf->flags & FUSE_BUF_IS_FD));
ddf19c
+
ddf19c
+    in = fuse_mbuf_iter_advance(&iter, sizeof(*in));
ddf19c
+    assert(in); /* caller guarantees the input buffer is large enough */
ddf19c
 
ddf19c
     if (se->debug) {
ddf19c
         fuse_log(FUSE_LOG_DEBUG,
ddf19c
@@ -2129,7 +2140,7 @@ void fuse_session_process_buf_int(struct fuse_session *se,
ddf19c
 
ddf19c
     inarg = (void *)&in[1];
ddf19c
     if (in->opcode == FUSE_WRITE && se->op.write_buf) {
ddf19c
-        do_write_buf(req, in->nodeid, inarg, bufv);
ddf19c
+        do_write_buf(req, in->nodeid, &iter, bufv);
ddf19c
     } else {
ddf19c
         fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
ddf19c
     }
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c