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

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