virtio: restore elem->in/out_sg after iov_discard_front/back()
v2 cover virtio: restore elem->in/out_sg after iov_discard_front/back()
v2:
* Add missing undo in virtio-blk write zeroes error path [Li Qiang]
Both virtio-blk and virtio-crypto use destructive iov_discard_front/back() operations on elem->in/out_sg. virtqueue_push() calls dma_memory_unmap() on the modified iovec arrays. The memory addresses may not match those originally mapped with dma_memory_map().
This raises several issues:
1. MemoryRegion references can be leaked.
2. Dirty memory may not be tracked.
3. The non-RAM bounce buffer can be leaked.
This patch series solves the issue in two ways:
1. virtio-blk uses a new iov_discard_undo() API to restore iovec arrays.
2. virtio-crypto uses g_memdup() to avoid modifying the original iovec arrays.
The g_memdup() approach is slower than iov_discard_undo() but less complex/fragile. I am less familiar with the virtio-crypto code and it uses more complex sequences of iov_discard_front/back() calls than virtio-blk. If anyone feels like optimizing virtio-crypto, please go ahead.
The virtio-blk bug was found by Alexander Bulekov's fuzzing effort. I found the virtio-crypto bug through code inspection.
Stefan Hajnoczi (3):
util/iov: add iov_discard_undo()
virtio-blk: undo destructive iov_discard_*() operations
virtio-crypto: don't modify elem->in/out_sg
include/hw/virtio/virtio-blk.h | 2 + include/qemu/iov.h | 23 +++++ hw/block/virtio-blk.c | 11 ++- hw/virtio/virtio-crypto.c | 17 +++- tests/test-iov.c | 165 +++++++++++++++++++++++++++++++++ util/iov.c | 50 +++++++++- 6 files changed, 259 insertions(+), 9 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/3 util/iov: add iov_discard_undo()
The iov_discard_front/back() operations are useful for parsing iovecs but they modify the array elements. If the original array is needed after parsing finishes there is currently no way to restore it.
Although g_memdup() can be used before performing destructive iov_discard_front/back() operations, this is inefficient.
Introduce iov_discard_undo() to restore the array to the state prior to an iov_discard_front/back() operation.
--- include/qemu/iov.h | 23 +++++++ tests/test-iov.c | 165 +++++++++++++++++++++++++++++++++++++++++++++ util/iov.c | 50 ++++++++++++-- 3 files changed, 234 insertions(+), 4 deletions(-)
include/qemu/iov.h +23 -0
@@ -130,6 +130,29 @@ size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,size_t iov_discard_back (struct iovec *iov,unsigned int *iov_cnt,size_t bytes); +/* Information needed to undo an iov_discard_*() operation */ +typedef struct { +struct iovec *modified_iov; +struct iovec orig; +}IOVDiscardUndo ; + +/* +* Undo an iov_discard_front_undoable() or iov_discard_back_undoable() +* operation. If multiple operations are made then each one needs a separate +* IOVDiscardUndo and iov_discard_undo() must be called in the reverse order +* that the operations were made. +*/ +void iov_discard_undo (IOVDiscardUndo *undo); + +/* +* Undoable versions of iov_discard_front() and iov_discard_back(). Use +* iov_discard_undo() to reset to the state before the discard operations. +*/ +size_t iov_discard_front_undoable (struct iovec **iov,unsigned int *iov_cnt, +size_t bytes,IOVDiscardUndo *undo); +size_t iov_discard_back_undoable (struct iovec *iov,unsigned int *iov_cnt, +size_t bytes,IOVDiscardUndo *undo); + typedef struct QEMUIOVector { struct iovec *iov; int niov;
tests/test-iov.c +165 -0
188 lines not on this page · raw message
util/iov.c +46 -4
84 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH v2 2/3 virtio-blk: undo destructive iov_discard_*() operations
In reply to Stefan HajnocziFuzzing discovered that virtqueue_unmap_sg() is being called on modified req->in/out_sg iovecs. This means dma_memory_map() and dma_memory_unmap() calls do not have matching memory addresses.
Fuzzing discovered that non-RAM addresses trigger a bug:
void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
bool is_write, hwaddr access_len)
{
if (buffer != bounce.buffer) {
^^^^^^^^^^^^^^^^^^^^^^^A modified iov->iov_base is no longer recognized as a bounce buffer and the wrong branch is taken.
There are more potential bugs: dirty memory is not tracked correctly and MemoryRegion refcounts can be leaked.
Use the new iov_discard_undo() API to restore elem->in/out_sg before virtqueue_push() is called.
--- include/hw/virtio/virtio-blk.h | 2 ++ hw/block/virtio-blk.c | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-)
include/hw/virtio/virtio-blk.h +2 -0
@@ -72,6 +72,8 @@ typedef struct VirtIOBlockReq {int64_t sector_num;VirtIOBlock *dev;VirtQueue *vq; +IOVDiscardUndo inhdr_undo; +IOVDiscardUndo outhdr_undo;struct virtio_blk_inhdr *in;struct virtio_blk_outhdr out;QEMUIOVector qiov;
hw/block/virtio-blk.c +9 -2
@@ -80,6 +80,8 @@ static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)trace_virtio_blk_req_complete (vdev, req, status);stb_p (&req->in->status, status); +iov_discard_undo (&req->inhdr_undo); +iov_discard_undo (&req->outhdr_undo);virtqueue_push (req->vq, &req->elem, req->in_len);if (s->dataplane_started && !s->dataplane_disabled) {virtio_blk_data_plane_notify (s->dataplane, req->vq);
@@ -632,10 +634,12 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)return -1 ; } -iov_discard_front (&out_iov, &out_num,sizeof (req->out)); +iov_discard_front_undoable (&out_iov, &out_num,sizeof (req->out), + &req->outhdr_undo);if (in_iov[in_num -1 ].iov_len <sizeof (struct virtio_blk_inhdr )) {virtio_error (vdev,"virtio-blk request inhdr too short" ); +iov_discard_undo (&req->outhdr_undo);return -1 ; }
@@ -644,7 +648,8 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb) req->in = (void *)in_iov[in_num -1 ].iov_base + in_iov[in_num -1 ].iov_len -sizeof (struct virtio_blk_inhdr ); -iov_discard_back (in_iov, &in_num,sizeof (struct virtio_blk_inhdr )); +iov_discard_back_undoable (in_iov, &in_num,sizeof (struct virtio_blk_inhdr ), + &req->inhdr_undo); type =virtio_ldl_p (vdev, &req->out.type);
@@ -739,6 +744,8 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)if (unlikely (iov_to_buf (out_iov, out_num,0 , &dwz_hdr,sizeof (dwz_hdr)) !=sizeof (dwz_hdr))) { +iov_discard_undo (&req->inhdr_undo); +iov_discard_undo (&req->outhdr_undo);virtio_error (vdev,"virtio-blk discard/write_zeroes header" " too short" );return -1 ;
Signature · 1 line
2.26.2
Re: [PATCH v2 2/3] virtio-blk: undo destructive iov_discard_*() operations
Quoted 19 lines · Stefan Hajnoczi <[email protected]> 于2020年9月17日周四 下午5:47写道:
Fuzzing discovered that
virtqueue_unmap_sg()is being called on modified req->in/out_sg iovecs. This meansdma_memory_map()anddma_memory_unmap()calls do not have matching memory addresses.Fuzzing discovered that non-RAM addresses trigger a bug:
void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, bool is_write, hwaddr access_len) { if (buffer != bounce.buffer) { ^^^^^^^^^^^^^^^^^^^^^^^A modified
iov->iov_baseis no longer recognized as a bounce buffer and the wrong branch is taken.There are more potential bugs: dirty memory is not tracked correctly and MemoryRegion refcounts can be leaked.
Quoted 7 lines
Use the new
iov_discard_undo()API to restore elem->in/out_sg beforevirtqueue_push()is called.Reported-by Alexander Bulekov <[email protected]>Fixes 827805a2492c1bbf1c0712ed18ee069b4ebf3dd6 ("virtio-blk: Convert VirtIOBlockReq.out to structrue")Signed-off-by Stefan Hajnoczi <[email protected]>
Quoted 67 lines
--- include/hw/virtio/virtio-blk.h | 2 ++ hw/block/virtio-blk.c | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-)include/hw/virtio/virtio-blk.h +2
-0@@ -72,6 +72,8 @@ typedef struct VirtIOBlockReq {int64_t sector_num;VirtIOBlock *dev;VirtQueue *vq; +IOVDiscardUndo inhdr_undo; +IOVDiscardUndo outhdr_undo;struct virtio_blk_inhdr *in;struct virtio_blk_outhdr out;QEMUIOVector qiov;hw/block/virtio-blk.c +9
-3@@ -80,6 +80,8 @@ static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)trace_virtio_blk_req_complete (vdev, req, status);stb_p (&req->in->status, status); +iov_discard_undo (&req->inhdr_undo); +iov_discard_undo (&req->outhdr_undo);virtqueue_push (req->vq, &req->elem, req->in_len);if (s->dataplane_started && !s->dataplane_disabled) {virtio_blk_data_plane_notify (s->dataplane, req->vq);@@ -632,10 +634,12 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)return -1 ; } -iov_discard_front (&out_iov, &out_num,sizeof (req->out)); +iov_discard_front_undoable (&out_iov, &out_num,sizeof (req->out), + &req->outhdr_undo);if (in_iov[in_num -1 ].iov_len <sizeof (struct virtio_blk_inhdr )) {virtio_error (vdev,"virtio-blk request inhdr too short" ); +iov_discard_undo (&req->outhdr_undo);return -1 ; }@@ -644,7 +648,8 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb) req->in = (void *)in_iov[in_num -1 ].iov_base + in_iov[in_num -1 ].iov_len -sizeof (struct virtio_blk_inhdr ); -iov_discard_back (in_iov, &in_num,sizeof (struct virtio_blk_inhdr )); +iov_discard_back_undoable (in_iov, &in_num,sizeof (struct virtio_blk_inhdr ), + &req->inhdr_undo); type =virtio_ldl_p (vdev, &req->out.type);@@ -739,6 +744,8 @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)if (unlikely (iov_to_buf (out_iov, out_num,0 , &dwz_hdr,sizeof (dwz_hdr)) !=sizeof (dwz_hdr))) { +iov_discard_undo (&req->inhdr_undo); +iov_discard_undo (&req->outhdr_undo);virtio_error (vdev,"virtio-blk discard/write_zeroes header" " too short" );return -1 ; --2.26.2
PATCH v2 3/3 virtio-crypto: don't modify elem->in/out_sg
In reply to Stefan HajnocziA number of iov_discard_front/back() operations are made by virtio-crypto. The elem->in/out_sg iovec arrays are modified by these operations, resulting virtqueue_unmap_sg() calls on different addresses than were originally mapped.
This is problematic because dirty memory may not be logged correctly, MemoryRegion refcounts may be leaked, and the non-RAM bounce buffer can be leaked.
Take a copy of the elem->in/out_sg arrays so that the originals are preserved. The iov_discard_undo() API could be used instead (with better performance) but requires careful auditing of the code, so do the simple thing instead.
--- hw/virtio/virtio-crypto.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
hw/virtio/virtio-crypto.c +14 -3
@@ -228,6 +228,8 @@ static void virtio_crypto_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)size_t s;for (;;) { + g_autofree struct iovec *out_iov_copy = NULL; + elem =virtqueue_pop (vq,sizeof (VirtQueueElement));if (!elem) {break ;
@@ -240,9 +242,12 @@ static void virtio_crypto_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) } out_num = elem->out_num; - out_iov = elem->out_sg; + out_iov_copy =g_memdup (elem->out_sg,sizeof (out_iov[0 ]) * out_num); + out_iov = out_iov_copy; + in_num = elem->in_num; in_iov = elem->in_sg; +if (unlikely (iov_to_buf (out_iov, out_num,0 , &ctrl,sizeof (ctrl)) !=sizeof (ctrl))) {virtio_error (vdev,"virtio-crypto request ctrl_hdr too short" );
@@ -582,6 +587,8 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request)int queue_index =virtio_crypto_vq2q (virtio_get_queue_index (request->vq));struct virtio_crypto_op_data_req req;int ret; + g_autofree struct iovec *in_iov_copy = NULL; + g_autofree struct iovec *out_iov_copy = NULL;struct iovec *in_iov;struct iovec *out_iov;unsigned in_num;
@@ -598,9 +605,13 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) } out_num = elem->out_num; - out_iov = elem->out_sg; + out_iov_copy =g_memdup (elem->out_sg,sizeof (out_iov[0 ]) * out_num); + out_iov = out_iov_copy; + in_num = elem->in_num; - in_iov = elem->in_sg; + in_iov_copy =g_memdup (elem->in_sg,sizeof (in_iov[0 ]) * in_num); + in_iov = in_iov_copy; +if (unlikely (iov_to_buf (out_iov, out_num,0 , &req,sizeof (req)) !=sizeof (req))) {virtio_error (vdev,"virtio-crypto request outhdr too short" );
Signature · 1 line
2.26.2
Stefan Hajnoczi
AppliedQuoted 30 lines · Stefan Hajnoczi
v2:
* Add missing undo in virtio-blk write zeroes error path [Li Qiang]Both virtio-blk and virtio-crypto use destructive iov_discard_front/back() operations on elem->in/out_sg.
virtqueue_push()callsdma_memory_unmap()on the modified iovec arrays. The memory addresses may not match those originally mapped withdma_memory_map().This raises several issues:
1. MemoryRegion references can be leaked.
2. Dirty memory may not be tracked.
3. The non-RAM bounce buffer can be leaked.This patch series solves the issue in two ways:
1. virtio-blk uses a newiov_discard_undo()API to restore iovec arrays.
2. virtio-crypto usesg_memdup()to avoid modifying the original iovec arrays.The
g_memdup()approach is slower thaniov_discard_undo()but less complex/fragile. I am less familiar with the virtio-crypto code and it uses more complex sequences of iov_discard_front/back() calls than virtio-blk. If anyone feels like optimizing virtio-crypto, please go ahead.The virtio-blk bug was found by Alexander Bulekov's fuzzing effort. I found the virtio-crypto bug through code inspection.
Stefan Hajnoczi (3):
util/iov: addiov_discard_undo()
virtio-blk: undo destructive iov_discard_*() operations
virtio-crypto: don't modify elem->in/out_sg
Quoted 11 lines
include/hw/virtio/virtio-blk.h | 2 + include/qemu/iov.h | 23 +++++ hw/block/virtio-blk.c | 11 ++- hw/virtio/virtio-crypto.c | 17 +++- tests/test-iov.c | 165 +++++++++++++++++++++++++++++++++ util/iov.c | 50 +++++++++- 6 files changed, 259 insertions(+), 9 deletions(-)Signature · 1 line
2.26.2
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan