vga patch queue
Hi,
vga patch queue brings some security fixes for vmware vga and live migration support (in 2d mode) for virtio-gpu.
v2: add qemu-stable to cc for the security fixes.
please pull,
Gerd
The following changes since commit 2c107d7684f9e3c4db4780d0756bbf35b06aec07:
Merge remote-tracking branch 'remotes/jasowang/tags/net-pull-request' into staging (2016-06-02 14:26:57 +0100)
are available in the git repository at:
git://git.kraxel.org/qemu tags/pull-vga-20160606-1
for you to fetch changes up to 0c244e50ee12311037efd507ee37df0e846e4a18:
virtio-gpu: add live migration support (2016-06-06 09:04:34 +0200)
----------------------------------------------------------------
virtio-gpu: scanout fix, live migration support
vmsvga: security fixes
----------------------------------------------------------------
Gerd Hoffmann (6):
virtio-gpu: fix scanout rectangles
vmsvga: move fifo sanity checks to vmsvga_fifo_length
vmsvga: add more fifo checks
vmsvga: shadow fifo registers
vmsvga: don't process more than 1024 fifo commands at once
virtio-gpu: add live migration support
hw/display/virtio-gpu-3d.c | 2 +- hw/display/virtio-gpu.c | 202 +++++++++++++++++++++++++++++++++++++++-- hw/display/virtio-vga.c | 12 +++ hw/display/vmware_vga.c | 78 ++++++++-------- include/hw/virtio/virtio-gpu.h | 4 +- 5 files changed, 252 insertions(+), 46 deletions(-)
[Qemu-devel] [PULL 4/6] vmsvga: shadow fifo registers
The fifo is normal ram. So kvm vcpu threads and qemu iothread can access the fifo in parallel without syncronization. Which in turn implies we can't use the fifo pointers in-place because the guest can try changing them underneath us. So add shadows for them, to make sure the guest can't modify them after we've applied sanity checks.
Message-id: [email protected]
--- hw/display/vmware_vga.c | 57 ++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 29 deletions(-)
hw/display/vmware_vga.c +28 -29
@@ -66,17 +66,11 @@ struct vmsvga_state_s {uint8_t *fifo_ptr;unsigned int fifo_size; -union { -uint32_t *fifo; -struct QEMU_PACKED { -uint32_t min; -uint32_t max; -uint32_t next_cmd; -uint32_t stop; -/* Add registers here when adding capabilities. */ -uint32_t fifo[0 ]; - } *cmd; - }; +uint32_t *fifo; +uint32_t fifo_min; +uint32_t fifo_max; +uint32_t fifo_next; +uint32_t fifo_stop;#define REDRAW_FIFO_LEN 512struct vmsvga_rect_s {
@@ -198,7 +192,7 @@ enum {*/ SVGA_FIFO_MIN =0 ,SVGA_FIFO_MAX ,/* The distance from MIN to MAX must be at least 10K */ -SVGA_FIFO_NEXT_CMD , +SVGA_FIFO_NEXT , SVGA_FIFO_STOP,/*
@@ -546,8 +540,6 @@ static inline void vmsvga_cursor_define(struct vmsvga_state_s *s, }#endif -#define CMD (f) le32_to_cpu(s->cmd->f) -static inline int vmsvga_fifo_length (struct vmsvga_state_s *s) {int num;
@@ -556,38 +548,44 @@ static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)return 0 ; } + s->fifo_min =le32_to_cpu (s->fifo[SVGA_FIFO_MIN ]); + s->fifo_max =le32_to_cpu (s->fifo[SVGA_FIFO_MAX ]); + s->fifo_next =le32_to_cpu (s->fifo[SVGA_FIFO_NEXT ]); + s->fifo_stop =le32_to_cpu (s->fifo[SVGA_FIFO_STOP ]); +/* Check range and alignment. */ -if ((CMD (min) |CMD (max) |CMD (next_cmd) |CMD (stop)) &3 ) { +if ((s->fifo_min | s->fifo_max | s->fifo_next | s->fifo_stop) &3 ) {return 0 ; } -if (CMD (min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo) { +if (s->fifo_min <sizeof (uint32_t ) *4 ) {return 0 ; } -if (CMD (max) >SVGA_FIFO_SIZE || -CMD (min) >=SVGA_FIFO_SIZE || -CMD (stop) >=SVGA_FIFO_SIZE || -CMD (next_cmd) >=SVGA_FIFO_SIZE ) { +if (s->fifo_max >SVGA_FIFO_SIZE || + s->fifo_min >=SVGA_FIFO_SIZE || + s->fifo_stop >=SVGA_FIFO_SIZE || + s->fifo_next >=SVGA_FIFO_SIZE ) {return 0 ; } -if (CMD (max) <CMD (min) +10 *1024 ) { +if (s->fifo_max < s->fifo_min +10 *1024 ) {return 0 ; } - num =CMD (next_cmd) -CMD (stop); + num = s->fifo_next - s->fifo_stop;if (num <0 ) { - num +=CMD (max) -CMD (min); + num += s->fifo_max - s->fifo_min; }return num >>2 ; }static inline uint32_t vmsvga_fifo_read_raw (struct vmsvga_state_s *s) { -uint32_t cmd = s->fifo[CMD (stop) >>2 ]; +uint32_t cmd = s->fifo[s->fifo_stop >>2 ]; - s->cmd->stop =cpu_to_le32 (CMD (stop) +4 ); -if (CMD (stop) >=CMD (max)) { - s->cmd->stop = s->cmd->min; + s->fifo_stop +=4 ; +if (s->fifo_stop >= s->fifo_max) { + s->fifo_stop = s->fifo_min; } + s->fifo[SVGA_FIFO_STOP ] =cpu_to_le32 (s->fifo_stop);return cmd; }
@@ -607,7 +605,7 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s) len =vmsvga_fifo_length (s);while (len >0 ) {/* May need to go back to the start of the command if incomplete */ - cmd_start = s->cmd->stop; + cmd_start = s->fifo_stop;switch (cmd =vmsvga_fifo_read (s)) {case SVGA_CMD_UPDATE :
@@ -766,7 +764,8 @@ static void vmsvga_fifo_run(struct vmsvga_state_s *s)break ; rewind: - s->cmd->stop = cmd_start; + s->fifo_stop = cmd_start; + s->fifo[SVGA_FIFO_STOP ] =cpu_to_le32 (s->fifo_stop);break ; } }
Signature · 1 line
1.8.3.1
[Qemu-devel] [PULL 3/6] vmsvga: add more fifo checks
In reply to Gerd HoffmannMake sure all fifo ptrs are within range.
Message-id: [email protected]
--- hw/display/vmware_vga.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
hw/display/vmware_vga.c +4 -1
@@ -563,7 +563,10 @@ static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)if (CMD (min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo) {return 0 ; } -if (CMD (max) >SVGA_FIFO_SIZE ) { +if (CMD (max) >SVGA_FIFO_SIZE || +CMD (min) >=SVGA_FIFO_SIZE || +CMD (stop) >=SVGA_FIFO_SIZE || +CMD (next_cmd) >=SVGA_FIFO_SIZE ) {return 0 ; }if (CMD (max) <CMD (min) +10 *1024 ) {
Signature · 1 line
1.8.3.1
[Qemu-devel] [PULL 2/6] vmsvga: move fifo sanity checks to vmsvga_fifo_length
In reply to Gerd HoffmannSanity checks are applied when the fifo is enabled by the guest (SVGA_REG_CONFIG_DONE write). Which doesn't help much if the guest changes the fifo registers afterwards. Move the checks to vmsvga_fifo_length so they are done each time qemu is about to read from the fifo.
Message-id: [email protected]
--- hw/display/vmware_vga.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-)
hw/display/vmware_vga.c +15 -13
@@ -555,6 +555,21 @@ static inline int vmsvga_fifo_length(struct vmsvga_state_s *s)if (!s->config || !s->enable) {return 0 ; } + +/* Check range and alignment. */ +if ((CMD (min) |CMD (max) |CMD (next_cmd) |CMD (stop)) &3 ) { +return 0 ; + } +if (CMD (min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo) { +return 0 ; + } +if (CMD (max) >SVGA_FIFO_SIZE ) { +return 0 ; + } +if (CMD (max) <CMD (min) +10 *1024 ) { +return 0 ; + } + num =CMD (next_cmd) -CMD (stop);if (num <0 ) { num +=CMD (max) -CMD (min);
@@ -1005,19 +1020,6 @@ static void vmsvga_value_write(void *opaque, uint32_t address, uint32_t value)case SVGA_REG_CONFIG_DONE :if (value) { s->fifo = (uint32_t *) s->fifo_ptr; -/* Check range and alignment. */ -if ((CMD (min) |CMD (max) |CMD (next_cmd) |CMD (stop)) &3 ) { -break ; - } -if (CMD (min) < (uint8_t *) s->cmd->fifo - (uint8_t *) s->fifo) { -break ; - } -if (CMD (max) >SVGA_FIFO_SIZE ) { -break ; - } -if (CMD (max) <CMD (min) +10 *1024 ) { -break ; - }vga_dirty_log_stop (&s->vga); } s->config = !!value;
Signature · 1 line
1.8.3.1
[Qemu-devel] [PULL 5/6] vmsvga: don't process more than 1024 fifo commands at once
In reply to Gerd Hoffmannvmsvga_fifo_run is called in regular intervals (on each display update) and will resume where it left off. So we can simply exit the loop, without having to worry about how processing will continue.
Message-id: [email protected]
--- hw/display/vmware_vga.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/vmware_vga.c +2 -2
@@ -597,13 +597,13 @@ static inline uint32_t vmsvga_fifo_read(struct vmsvga_state_s *s)static void vmsvga_fifo_run (struct vmsvga_state_s *s) {uint32_t cmd, colour; -int args, len; +int args, len, maxloop =1024 ;int x, y, dx, dy, width, height;struct vmsvga_cursor_definition_s cursor;uint32_t cmd_start; len =vmsvga_fifo_length (s); -while (len >0 ) { +while (len >0 && --maxloop >0 ) {/* May need to go back to the start of the command if incomplete */ cmd_start = s->fifo_stop;
Signature · 1 line
1.8.3.1
[Qemu-devel] [PULL 6/6] virtio-gpu: add live migration support
In reply to Gerd HoffmannStore some additional state for cursor and resource backing storage, so we can write out and reload things. Implement vmsave+vmload for 2d mode. Continue blocking live migration in 3d/virgl mode.
Message-id: [email protected]
--- hw/display/virtio-gpu-3d.c | 2 +- hw/display/virtio-gpu.c | 188 +++++++++++++++++++++++++++++++++++++++-- hw/display/virtio-vga.c | 12 +++ include/hw/virtio/virtio-gpu.h | 4 +- 4 files changed, 198 insertions(+), 8 deletions(-)
hw/display/virtio-gpu-3d.c +1 -1
@@ -284,7 +284,7 @@ static void virgl_resource_attach_backing(VirtIOGPU *g,VIRTIO_GPU_FILL_CMD (att_rb);trace_virtio_gpu_cmd_res_back_attach (att_rb.resource_id); - ret =virtio_gpu_create_mapping_iov (&att_rb, cmd, &res_iovs); + ret =virtio_gpu_create_mapping_iov (&att_rb, cmd,NULL , &res_iovs);if (ret !=0 ) { cmd->error =VIRTIO_GPU_RESP_ERR_UNSPEC ;return ;
hw/display/virtio-gpu.c +182 -6
262 lines not on this page · raw message
hw/display/virtio-vga.c +12 -0
@@ -84,6 +84,17 @@ static const GraphicHwOps virtio_vga_ops = { .gl_block = virtio_vga_gl_block, }; +static const VMStateDescription vmstate_virtio_vga = { + .name ="virtio-vga" , + .version_id =2 , + .minimum_version_id =2 , + .fields = (VMStateField []) { +/* no pci stuff here, saving the virtio device will handle that */ +VMSTATE_STRUCT (vga, VirtIOVGA,0 , vmstate_vga_common, VGACommonState), +VMSTATE_END_OF_LIST () + } +}; +/* VGA device wrapper around PCI device around virtio GPU */ static void virtio_vga_realize (VirtIOPCIProxy *vpci_dev,Error **errp) {
@@ -168,6 +179,7 @@ static void virtio_vga_class_init(ObjectClass *klass, void *data)set_bit (DEVICE_CATEGORY_DISPLAY , dc->categories); dc->props = virtio_vga_properties; dc->reset = virtio_vga_reset; + dc->vmsd = &vmstate_virtio_vga; dc->hotpluggable = false; k->realize = virtio_vga_realize;
include/hw/virtio/virtio-gpu.h +3 -1
@@ -32,6 +32,7 @@ struct virtio_gpu_simple_resource {uint32_t width;uint32_t height;uint32_t format; +uint64_t *addrs;struct iovec *iov;unsigned int iov_cnt;uint32_t scanout_bitmask;
@@ -46,6 +47,7 @@ struct virtio_gpu_scanout {int x, y;int invalidate;uint32_t resource_id; +struct virtio_gpu_update_cursor cursor;QEMUCursor *current_cursor; };
@@ -150,7 +152,7 @@ void virtio_gpu_get_display_info(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd);int virtio_gpu_create_mapping_iov (struct virtio_gpu_resource_attach_backing *ab,struct virtio_gpu_ctrl_command *cmd, -struct iovec **iov); +uint64_t **addr,struct iovec **iov);void virtio_gpu_cleanup_mapping_iov (struct iovec *iov,uint32_t count);void virtio_gpu_process_cmdq (VirtIOGPU *g);
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PULL 6/6] virtio-gpu: add live migration support
hw/display/virtio-gpu.c const GraphicHwOps virtio_gpu_ops = { · line 1048
Quoted 12 lines · Gerd Hoffmann
hw/display/virtio-gpu.c +12
-0+while (resource_id !=0 ) { + res = g_new0(struct virtio_gpu_simple_resource, 1); + res->resource_id = resource_id; + res->width =qemu_get_be32 (f); + res->height =qemu_get_be32 (f); + res->format =qemu_get_be32 (f); + res->iov_cnt =qemu_get_be32 (f); + +/* allocate */ + pformat =get_pixman_format (res->format); +if (!pformat) { +return -EINVAL ;
Coverity says, all the "return" statements in this loop leak res.
Paolo
Quoted 32 lines
hw/display/virtio-gpu.c +32
-0+ } + res->image =pixman_image_create_bits (pformat, + res->width, res->height, +NULL ,0 ); +if (!res->image) { +return -EINVAL ; + } + + res->addrs =g_new (uint64_t, res->iov_cnt); + res->iov = g_new(struct iovec, res->iov_cnt); + +/* read data */ +for (i =0 ; i < res->iov_cnt; i++) { + res->addrs[i] =qemu_get_be64 (f); + res->iov[i].iov_len =qemu_get_be32 (f); + } +qemu_get_buffer (f, (void *)pixman_image_get_data (res->image), +pixman_image_get_stride (res->image) * res->height); + +/* restore mapping */ +for (i =0 ; i < res->iov_cnt; i++) { +hwaddr len = res->iov[i].iov_len; + res->iov[i].iov_base = +cpu_physical_memory_map (res->addrs[i], &len,1 ); +if (!res->iov[i].iov_base || len != res->iov[i].iov_len) { +return -EINVAL ; + } + } + +QTAILQ_INSERT_HEAD (&g->reslist, res, next); + + resource_id =qemu_get_be32 (f);
[Qemu-devel] [PULL 1/6] virtio-gpu: fix scanout rectangles
In reply to Gerd HoffmannCommit "ca58b45 ui/virtio-gpu: add and use qemu_create_displaysurface_pixman" breaks scanouts which use a region of the underlying resource only.
So, we need another way to handle the underlying issue. Lets create a new pixman image, grab a reference on the pixman providing the underlying storage, hook up a destroy callback which releases the reference. That way regions work again and releasing the backing storage should still be impossible thanks to the extra reference we are holding.
Message-id: [email protected]
--- hw/display/virtio-gpu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
hw/display/virtio-gpu.c +13 -1
@@ -495,6 +495,11 @@ static void virtio_gpu_resource_flush(VirtIOGPU *g,pixman_region_fini (&flush_region); } +static void virtio_unref_resource (pixman_image_t *image,void *data) +{ +pixman_image_unref (data); +} +static void virtio_gpu_set_scanout (VirtIOGPU *g,struct virtio_gpu_ctrl_command *cmd) {
@@ -571,8 +576,15 @@ static void virtio_gpu_set_scanout(VirtIOGPU *g, != ((uint8_t *)pixman_image_get_data(res->image) + offset) || scanout->width != ss.r.width || scanout->height != ss.r.height) { + pixman_image_t *rect; +void *ptr = (uint8_t *)pixman_image_get_data (res->image) + offset; + rect =pixman_image_create_bits (format, ss.r.width, ss.r.height, ptr, +pixman_image_get_stride (res->image)); +pixman_image_ref (res->image); +pixman_image_set_destroy_function (rect, virtio_unref_resource, + res->image);/* realloc the surface ptr */ - scanout->ds =qemu_create_displaysurface_pixman (res->image); + scanout->ds =qemu_create_displaysurface_pixman (rect);if (!scanout->ds) { cmd->error =VIRTIO_GPU_RESP_ERR_UNSPEC ;return ;
Signature · 1 line
1.8.3.1
Peter Maydell
AppliedQuoted 21 lines · Gerd Hoffmann
Hi,
vga patch queue brings some security fixes for vmware vga and live migration support (in 2d mode) for virtio-gpu.
v2: add qemu-stable to cc for the security fixes.
please pull,
GerdThe following changes since commit
2c107d7684f9e3c4db4780d0756bbf35b06aec07:Merge remote-tracking branch 'remotes/jasowang/tags/net-pull-request' into staging (2016-06-02 14:26:57 +0100)
are available in the git repository at:
git://git.kraxel.org/qemutags/pull-vga-20160606-1for you to fetch changes up to
0c244e50ee12311037efd507ee37df0e846e4a18:
Quoted 7 lines
virtio-gpu: add live migration support (2016-06-06 09:04:34 +0200)
----------------------------------------------------------------
virtio-gpu: scanout fix, live migration support
vmsvga: security fixes----------------------------------------------------------------
Applied, thanks.
-- PMM