virtio: fix reachable assertion due to stale value of cached region size
PATCH 1/1 virtio: fix reachable assertion due to stale value of cached region size
In virtqueue_{split,packed}_get_avail_bytes() descriptors are read in a loop via MemoryRegionCache regions and calls to vring_{split,packed}_desc_read() - these take a region cache and the index of the descriptor to be read.
For direct descriptors we use a cache provided by the caller, whose size matches that of the virtqueue vring. We limit the number of descriptors we can read by the size of that vring:
max = vq->vring.num;
...
MemoryRegionCache *desc_cache = &caches->desc;For indirect descriptors, we initialize a new cache and limit the number of descriptors by the size of the intermediate descriptor:
len = address_space_cache_init(&indirect_desc_cache,
vdev->dma_as,
desc.addr, desc.len, false);
desc_cache = &indirect_desc_cache;
...
max = desc.len / sizeof(VRingDesc);However, the first initialization of max is done outside the loop where we process guest descriptors, while the second one is done inside. This means that a sequence of an indirect descriptor followed by a direct one will leave a stale value in max. If the second descriptor's next field is smaller than the stale value, but greater than the size of the virtqueue ring (and thus the cached region), a failed assertion will be triggered in address_space_read_cached() down the call chain.
Fix this by initializing max inside the loop in both functions.
--- hw/virtio/virtio.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
hw/virtio/virtio.c +6 -7
@@ -1071,6 +1071,7 @@ static void virtqueue_split_get_avail_bytes(VirtQueue *vq,VirtIODevice *vdev = vq->vdev;unsigned int max, idx;unsigned int total_bufs, in_total, out_total; +MemoryRegionCache *desc_cache;MemoryRegionCache indirect_desc_cache =MEMORY_REGION_CACHE_INVALID ;int64_t len =0 ;int rc;
@@ -1078,15 +1079,13 @@ static void virtqueue_split_get_avail_bytes(VirtQueue *vq, idx = vq->last_avail_idx; total_bufs = in_total = out_total =0 ; - max = vq->vring.num; -while ((rc =virtqueue_num_heads (vq, idx)) >0 ) { -MemoryRegionCache *desc_cache = &caches->desc; -unsigned int num_bufs; +unsigned int num_bufs = total_bufs;VRingDesc desc;unsigned int i; - num_bufs = total_bufs; + desc_cache = &caches->desc; + max = vq->vring.num;if (!virtqueue_get_head (vq, idx++, &i)) { goto err;
@@ -1218,14 +1217,14 @@ static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, wrap_counter = vq->last_avail_wrap_counter; total_bufs = in_total = out_total =0 ; - max = vq->vring.num; -for (;;) {unsigned int num_bufs = total_bufs;unsigned int i = idx;int rc; desc_cache = &caches->desc; + max = vq->vring.num; +vring_packed_desc_read (vdev, &desc, desc_cache, idx, true);if (!is_desc_avail (desc.flags, wrap_counter)) {break ;
Signature · 1 line
2.35.3
Quoted 45 lines · Carlos López
In virtqueue_{split,packed}_get_avail_bytes() descriptors are read in a loop via MemoryRegionCache regions and calls to vring_{split,packed}_desc_read() - these take a region cache and the index of the descriptor to be read.
For direct descriptors we use a cache provided by the caller, whose size matches that of the virtqueue vring. We limit the number of descriptors we can read by the size of that vring:
max = vq->vring.num; ... MemoryRegionCache *desc_cache = &caches->desc;For indirect descriptors, we initialize a new cache and limit the number of descriptors by the size of the intermediate descriptor:
len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as, desc.addr, desc.len, false); desc_cache = &indirect_desc_cache; ... max = desc.len / sizeof(VRingDesc);However, the first initialization of
maxis done outside the loop where we process guest descriptors, while the second one is done inside. This means that a sequence of an indirect descriptor followed by a direct one will leave a stale value inmax. If the second descriptor'snextfield is smaller than the stale value, but greater than the size of the virtqueue ring (and thus the cached region), a failed assertion will be triggered inaddress_space_read_cached()down the call chain.Fix this by initializing
maxinside the loop in both functions.Fixes 9796d0ac8fb0 ("virtio: use address_space_map/unmap to access descriptors")Signed-off-by Carlos López <[email protected]>--- hw/virtio/virtio.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)hw/virtio/virtio.c +0
-0
hw/virtio/virtio.c static void virtqueue_split_get_avail_bytes(VirtQueue *vq, · line 1074
Quoted 4 lines
hw/virtio/virtio.c +1
-0@@ -1071,6 +1071,7 @@ static void virtqueue_split_get_avail_bytes(VirtQueue *vq,VirtIODevice *vdev = vq->vdev;unsigned int max, idx;unsigned int total_bufs, in_total, out_total; +MemoryRegionCache *desc_cache;
why are you moving desc_cache here?
Quoted 7 lines
hw/virtio/virtio.c +0
-0MemoryRegionCache indirect_desc_cache =MEMORY_REGION_CACHE_INVALID ;int64_t len =0 ;int rc;@@ -1078,15 +1079,13 @@ static void virtqueue_split_get_avail_bytes(VirtQueue *vq, idx = vq->last_avail_idx; total_bufs = in_total = out_total =0 ;
hw/virtio/virtio.c static void virtqueue_split_get_avail_bytes(VirtQueue *vq, · line 1089
Quoted 10 lines
hw/virtio/virtio.c +1
-5- max = vq->vring.num; -while ((rc =virtqueue_num_heads (vq, idx)) >0 ) { -MemoryRegionCache *desc_cache = &caches->desc; -unsigned int num_bufs; +unsigned int num_bufs = total_bufs;VRingDesc desc;unsigned int i; - num_bufs = total_bufs;
nice cleanup but not a bugfix. Keep cleanups separate from fixes pls.
hw/virtio/virtio.c static void virtqueue_split_get_avail_bytes(VirtQueue *vq, · line 1087
Quoted 1 line
hw/virtio/virtio.c +1
-0+ desc_cache = &caches->desc;
init as part of declaration seems cleaner.
hw/virtio/virtio.c static void virtqueue_split_get_avail_bytes(VirtQueue *vq, · line 1089
Quoted 2 lines
hw/virtio/virtio.c +1
-0+ max = vq->vring.num;
can we move declaration of max here within the loop? will make sure the problem does not recur.
Quoted 6 lines
hw/virtio/virtio.c +0
-0if (!virtqueue_get_head (vq, idx++, &i)) { goto err;@@ -1218,14 +1217,14 @@ static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, wrap_counter = vq->last_avail_wrap_counter; total_bufs = in_total = out_total =0 ;
hw/virtio/virtio.c static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, · line 1227
Quoted 10 lines
hw/virtio/virtio.c +2
-2- max = vq->vring.num; -for (;;) {unsigned int num_bufs = total_bufs;unsigned int i = idx;int rc; desc_cache = &caches->desc; + max = vq->vring.num; +
same question can we move declaration into the loop?
Quoted 5 lines
hw/virtio/virtio.c +0
-0vring_packed_desc_read (vdev, &desc, desc_cache, idx, true);if (!is_desc_avail (desc.flags, wrap_counter)) {break ;Signature · 1 line
2.35.3
Hi,
On 1/3/23 23:03, Michael S. Tsirkin wrote:>> diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
Quoted 10 lines
hw/virtio/virtio.c static void virtqueue_split_get_avail_bytes(VirtQueue *vq, · line 1074
Quoted 8 lines
index
f35178f5fc..db70c4976e100644hw/virtio/virtio.c +1
-0@@ -1071,6 +1071,7 @@ static void virtqueue_split_get_avail_bytes(VirtQueue *vq,VirtIODevice *vdev = vq->vdev;unsigned int max, idx;unsigned int total_bufs, in_total, out_total; +MemoryRegionCache *desc_cache;why are you moving
desc_cachehere?
Just to make virtqueue_split_get_avail_bytes() and virtqueue_packed_get_avail_bytes() symmetrical, but I'll keep it out of v2.
Quoted 16 lines
Quoted 16 lines
hw/virtio/virtio.c +1
-4MemoryRegionCache indirect_desc_cache =MEMORY_REGION_CACHE_INVALID ;int64_t len =0 ;int rc;@@ -1078,15 +1079,13 @@ static void virtqueue_split_get_avail_bytes(VirtQueue *vq, idx = vq->last_avail_idx; total_bufs = in_total = out_total =0 ; - max = vq->vring.num; -while ((rc =virtqueue_num_heads (vq, idx)) >0 ) { -MemoryRegionCache *desc_cache = &caches->desc; -unsigned int num_bufs; +unsigned int num_bufs = total_bufs;VRingDesc desc;unsigned int i;
Quoted 3 lines
hw/virtio/virtio.c static void virtqueue_split_get_avail_bytes(VirtQueue *vq, · line 1089
Quoted 1 line
hw/virtio/virtio.c +0
-1- num_bufs = total_bufs;nice cleanup but not a bugfix. Keep cleanups separate from fixes pls.
Ok makes sense.
Quoted 9 lines
hw/virtio/virtio.c static void virtqueue_split_get_avail_bytes(VirtQueue *vq, · line 1087
Quoted 1 line
hw/virtio/virtio.c +1
-0+ desc_cache = &caches->desc;init as part of declaration seems cleaner.
hw/virtio/virtio.c static void virtqueue_split_get_avail_bytes(VirtQueue *vq, · line 1089
Quoted 2 lines
hw/virtio/virtio.c +1
-0+ max = vq->vring.num;can we move declaration of max here within the loop? will make sure the problem does not recur.
Yes, sounds good.
Quoted 13 lines
Quoted 13 lines
hw/virtio/virtio.c +0
-2if (!virtqueue_get_head (vq, idx++, &i)) { goto err;@@ -1218,14 +1217,14 @@ static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, wrap_counter = vq->last_avail_wrap_counter; total_bufs = in_total = out_total =0 ; - max = vq->vring.num; -for (;;) {unsigned int num_bufs = total_bufs;unsigned int i = idx;int rc;
Quoted 6 lines
hw/virtio/virtio.c static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, · line 1227
Quoted 3 lines
hw/virtio/virtio.c +2
-0desc_cache = &caches->desc; + max = vq->vring.num; +same question can we move declaration into the loop?
Same as above.
Signature · 3 lines
Carlos López Security Engineer SUSE Software Solutions
Quoted 14 lines · Carlos López
In virtqueue_{split,packed}_get_avail_bytes() descriptors are read in a loop via MemoryRegionCache regions and calls to vring_{split,packed}_desc_read() - these take a region cache and the index of the descriptor to be read.
For direct descriptors we use a cache provided by the caller, whose size matches that of the virtqueue vring. We limit the number of descriptors we can read by the size of that vring:
max = vq->vring.num; ... MemoryRegionCache *desc_cache = &caches->desc;
Quoted 6 lines
For indirect descriptors, we initialize a new cache and limit the number of descriptors by the size of the intermediate descriptor:
len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as, desc.addr, desc.len, false);
So desc.addr and desc.len are under the control of the driver. A malicious driver can choose to do a trick there. Should we sanitize them here?
Thanks
Quoted 73 lines
desc_cache = &indirect_desc_cache; ... max = desc.len / sizeof(VRingDesc);However, the first initialization of
maxis done outside the loop where we process guest descriptors, while the second one is done inside. This means that a sequence of an indirect descriptor followed by a direct one will leave a stale value inmax. If the second descriptor'snextfield is smaller than the stale value, but greater than the size of the virtqueue ring (and thus the cached region), a failed assertion will be triggered inaddress_space_read_cached()down the call chain.Fix this by initializing
maxinside the loop in both functions.Fixes 9796d0ac8fb0 ("virtio: use address_space_map/unmap to access descriptors")Signed-off-by Carlos López <[email protected]>--- hw/virtio/virtio.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)hw/virtio/virtio.c +6
-8@@ -1071,6 +1071,7 @@ static void virtqueue_split_get_avail_bytes(VirtQueue *vq,VirtIODevice *vdev = vq->vdev;unsigned int max, idx;unsigned int total_bufs, in_total, out_total; +MemoryRegionCache *desc_cache;MemoryRegionCache indirect_desc_cache =MEMORY_REGION_CACHE_INVALID ;int64_t len =0 ;int rc;@@ -1078,15 +1079,13 @@ static void virtqueue_split_get_avail_bytes(VirtQueue *vq, idx = vq->last_avail_idx; total_bufs = in_total = out_total =0 ; - max = vq->vring.num; -while ((rc =virtqueue_num_heads (vq, idx)) >0 ) { -MemoryRegionCache *desc_cache = &caches->desc; -unsigned int num_bufs; +unsigned int num_bufs = total_bufs;VRingDesc desc;unsigned int i; - num_bufs = total_bufs; + desc_cache = &caches->desc; + max = vq->vring.num;if (!virtqueue_get_head (vq, idx++, &i)) { goto err;@@ -1218,14 +1217,14 @@ static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, wrap_counter = vq->last_avail_wrap_counter; total_bufs = in_total = out_total =0 ; - max = vq->vring.num; -for (;;) {unsigned int num_bufs = total_bufs;unsigned int i = idx;int rc; desc_cache = &caches->desc; + max = vq->vring.num; +vring_packed_desc_read (vdev, &desc, desc_cache, idx, true);if (!is_desc_avail (desc.flags, wrap_counter)) {break ; --2.35.3