drm/vboxvideo: validate VBVA ring buffer offsets and cursor dimensions
Subsystems (3)
PATCH 1/1 drm/vboxvideo: validate VBVA ring buffer offsets and cursor dimensions
In drivers/gpu/drm/vboxvideo/ (vbox_main.c, vbox_mode.c, vbva_base.c), validate VBVA ring buffer offsets against buffer_length and check cursor dimensions before copying cursor data into HGSMI buffers.
---
drivers/gpu/drm/vboxvideo/vbox_main.c +4 -0
@@ -115,6 +115,10 @@ int vbox_hw_init(struct vbox_private *vbox)DRM_INFO ("VRAM %08x\n" , vbox->full_vram_size); +if (vbox->full_vram_size <VBVA_ADAPTER_INFORMATION_SIZE + + vbox->num_crtcs *VBVA_MIN_BUFFER_SIZE ) +return -EINVAL ; + ret =pcim_request_region (pdev,0 ,"vboxvideo" );if (ret)return ret;
drivers/gpu/drm/vboxvideo/vbox_mode.c +5 -2
@@ -141,7 +141,8 @@ static bool vbox_set_up_input_mapping(struct vbox_private *vbox)if (!fb1) { fb1 = fb; -if (fb1 == vbox->ddev.fb_helper->fb) +if (vbox->ddev.fb_helper && + fb1 == vbox->ddev.fb_helper->fb)break ; }else if (fb != fb1) { single_framebuffer = false;
@@ -363,7 +364,8 @@ static int vbox_cursor_atomic_check(struct drm_plane *plane,return 0 ;if (width >VBOX_MAX_CURSOR_WIDTH || height >VBOX_MAX_CURSOR_HEIGHT || - width ==0 || height ==0 ) + width ==0 || height ==0 || + width != new_state->fb->width || height != new_state->fb->height)return -EINVAL ;return 0 ;
@@ -380,6 +382,7 @@ static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,size_t line_size = (width +7 ) /8 ;u32 i, j; +memset (dst,0 , mask_size);memcpy (dst + mask_size, src, width * height *4 );for (i =0 ; i < height; ++i)for (j =0 ; j < width; ++j)
drivers/gpu/drm/vboxvideo/vbva_base.c +32 -8
@@ -21,18 +21,35 @@static u32 vbva_buffer_available (const struct vbva_buffer *vbva) { -s32 diff = vbva->data_offset - vbva->free_offset; +u32 data_len =READ_ONCE (vbva->data_len); +u32 data_offset =READ_ONCE (vbva->data_offset); +u32 free_offset =READ_ONCE (vbva->free_offset); +s32 diff; -return diff >0 ? diff : vbva->data_len + diff; +if (!data_len || data_offset >= data_len || free_offset >= data_len) +return 0 ; + + diff = data_offset - free_offset; + +return diff >0 ? diff : data_len + diff; }static void vbva_buffer_place_data_at (struct vbva_buf_ctx *vbva_ctx,const void *p,u32 len,u32 offset) {struct vbva_buffer *vbva = vbva_ctx->vbva; -u32 bytes_till_boundary = vbva->data_len - offset; -u8 *dst = &vbva->data[offset]; -s32 diff = len - bytes_till_boundary; +u32 data_len =READ_ONCE (vbva->data_len); +u32 bytes_till_boundary; +u8 *dst; +s32 diff; + +if (data_len > vbva_ctx->buffer_length -sizeof (*vbva) || + offset >= data_len || len > data_len) +return ; + + bytes_till_boundary = data_len - offset; + dst = &vbva->data[offset]; + diff = len - bytes_till_boundary;if (diff <=0 ) {/* Chunk will not cross buffer boundary. */
@@ -93,6 +110,9 @@ bool vbva_write(struct vbva_buf_ctx *vbva_ctx, struct gen_pool *ctx,vbva_buffer_place_data_at (vbva_ctx, p, chunk, vbva->free_offset); +if (!vbva->data_len) +return false; + vbva->free_offset = (vbva->free_offset + chunk) % vbva->data_len; record->len_and_flags += chunk;
@@ -165,7 +185,7 @@ bool vbva_buffer_begin_update(struct vbva_buf_ctx *vbva_ctx, struct gen_pool *ctx) {struct vbva_record *record; -u32 next; +u32 free_idx, next;if (!vbva_ctx->vbva || !(vbva_ctx->vbva->host_flags.host_events &VBVA_F_MODE_ENABLED ))
@@ -173,7 +193,11 @@ bool vbva_buffer_begin_update(struct vbva_buf_ctx *vbva_ctx,WARN_ON (vbva_ctx->buffer_overflow || vbva_ctx->record); - next = (vbva_ctx->vbva->record_free_index +1 ) %VBVA_MAX_RECORDS ; + free_idx =READ_ONCE (vbva_ctx->vbva->record_free_index); +if (free_idx >=VBVA_MAX_RECORDS ) +return false; + + next = (free_idx +1 ) %VBVA_MAX_RECORDS ;/* Flush if all slots in the records queue are used */ if (next == vbva_ctx->vbva->record_first_index)
@@ -183,7 +207,7 @@ bool vbva_buffer_begin_update(struct vbva_buf_ctx *vbva_ctx,if (next == vbva_ctx->vbva->record_first_index)return false; - record = &vbva_ctx->vbva->records[vbva_ctx->vbva->record_free_index]; + record = &vbva_ctx->vbva->records[free_idx]; record->len_and_flags =VBVA_F_RECORD_PARTIAL ; vbva_ctx->vbva->record_free_index = next;/* Remember which record we are using. */
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] drm/vboxvideo: VRAM size validation uses uninitialized num_crtcs - [High] drm/vboxvideo: TOCTOU…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] drm/vboxvideo: VRAM size validation uses uninitialized num_crtcs
- [High] drm/vboxvideo: TOCTOU race on vbva->data_len allows divide-by-zero --
commit b1a081469cd7aae1113cfd3057268becc7a4f37a
Author: Hui Peng <[email protected]>
drm/vboxvideo: validate VBVA ring buffer offsets and cursor dimensions
In drivers/gpu/drm/vboxvideo/ (vbox_main.c, vbox_mode.c, vbva_base.c), validate VBVA ring buffer offsets against buffer_length and check cursor dimensions before copying cursor data into HGSMI buffers.
drivers/gpu/drm/vboxvideo/vbox_main.c int vbox_hw_init(struct vbox_private *vbox) · line 121
Quoted 12 lines
drivers/gpu/drm/vboxvideo/vbox_main.c +4
-0@@ -115,6 +115,10 @@ int vbox_hw_init(struct vbox_private *vbox)DRM_INFO ("VRAM %08x\n" , vbox->full_vram_size); +if (vbox->full_vram_size <VBVA_ADAPTER_INFORMATION_SIZE + + vbox->num_crtcs *VBVA_MIN_BUFFER_SIZE ) +return -EINVAL ; +
[Severity: High]
Does this code use an uninitialized value for vbox->num_crtcs?
Looking at vbox_hw_init(), it appears the memory for vbox is allocated with devm_drm_dev_alloc(), which zero-initializes the memory. Since vbox->num_crtcs is not populated until later in the function via hgsmi_query_conf(), is it strictly 0 at the time of this check?
Could this cause the validation to evaluate 0 * VBVA_MIN_BUFFER_SIZE, allowing an inadequate VRAM size to slip through and resulting in a potential out-of-bounds mapping later in vbox_accel_init()?
Quoted 3 lines
drivers/gpu/drm/vboxvideo/vbox_main.c +0
-0ret =pcim_request_region (pdev,0 ,"vboxvideo" );if (ret)return ret;
[ ... ]
Quoted 4 lines
drivers/gpu/drm/vboxvideo/vbva_base.c +0
-0
[ ... ]
drivers/gpu/drm/vboxvideo/vbva_base.c bool vbva_write(struct vbva_buf_ctx *vbva_ctx, struct gen_pool *ctx, · line 117
Quoted 9 lines
drivers/gpu/drm/vboxvideo/vbva_base.c +3
-0@@ -93,6 +110,9 @@ bool vbva_write(struct vbva_buf_ctx *vbva_ctx, struct gen_pool *ctx,vbva_buffer_place_data_at (vbva_ctx, p, chunk, vbva->free_offset); +if (!vbva->data_len) +return false; + vbva->free_offset = (vbva->free_offset + chunk) % vbva->data_len;
[Severity: High]
Could this check introduce a time-of-check to time-of-use vulnerability?
Because vbva resides in VRAM, which is shared memory accessible by the host, and the check does not use READ_ONCE() or store the length locally, might the compiler emit two distinct memory loads for vbva->data_len?
Can a buggy host hypervisor bypass this check by setting data_len to a valid non-zero value, then immediately flipping it to 0 before the modulo operation executes, potentially leading to a divide-by-zero kernel panic in the guest?
Quoted 1 line
drivers/gpu/drm/vboxvideo/vbva_base.c +0
-0record->len_and_flags += chunk;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1