peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0053-virtiofsd-validate-input-buffer-sizes-in-do_write_bu.patch

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