Skip to content

media: zoran: fix capture buffer size / geometry desync

Minseo Park linux-media lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest4 patches 1 replyIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4media: zoran: remove always-false buffer size checkdrivers/media/pci/zoran/zoran_driver.c+0 -70 replies
2/4media: zoran: do not update device state before the format is validateddrivers/media/pci/zoran/zoran_driver.c+3 -40 replies
3/4media: zoran: reject VIDIOC_S_FMT while the queue is busydrivers/media/pci/zoran/zoran_driver.c+3 -00 replies
4/4media: zoran: add KUnit tests for capture format negotiationdrivers/media/pci/zoran/Kconfig drivers/media/pci/zoran/zoran_driver.c drivers/media/pci/zoran/zoran_driver_test.c+141 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/4] media: zoran: fix capture buffer size / geometry desync

zoran_v4l_set_format() updates zr->buffer_size before it validates the line alignment, and zoran_s_fmt_vid_cap() updates zr->map_mode before it calls zoran_v4l_set_format() at all. When the alignment check rejects the format with -EINVAL both fields keep their new values, while zr->v4l_settings, which is only committed after every check has passed, still describes the format that was accepted before.

That matters because the two sides of the frame size contract come from different fields:

  zr_vb2_queue_setup()     -> zr->buffer_size
  zr36057_set_memgrab()    -> zr->v4l_settings
  zr_set_buf()             -> zr->v4l_settings

The ZR36057 has no transfer length register, so the geometry and stride registers alone bound the write, and nothing compares them against the size the buffers were allocated with. An accepted VIDIOC_S_FMT, a VIDIOC_S_FMT that fails the alignment check, and then VIDIOC_REQBUFS is enough to allocate buffers for the rejected format and have the chip DMA a frame of the accepted format into them. 768x576 BGR32 followed by a rejected 33x24 RGB555 leaves zr->buffer_size at 1584 bytes, so the queue allocates a single page and the chip writes 768 * 576 * 4 = 1769472 bytes into it.

zr_vb2_prepare() does not catch it, because it compares the plane size against the same zr->buffer_size that was used to allocate the plane.

Patch 1 removes the buffer size check in zoran_v4l_set_format(), which has not been able to fire since the assignment above it was introduced by commit 11c9c16f7cb5. The comparison used to be made against fh->buffers.buffer_size, the size of the already allocated buffers, and was a real bounds check.

Patch 2 defers the zr->buffer_size and zr->map_mode updates until every check has passed. This is what zoran_s_fmt_vid_out() already does for the JPEG path: it negotiates into a local struct zoran_jpg_settings, calls zoran_check_jpg_settings(), and only then commits.

Patch 3 rejects VIDIOC_S_FMT while the queue is busy, which the driver never checked.

Patch 4 adds KUnit tests for the above.

Verified on v7.0 with:

  ./tools/testing/kunit/kunit.py run --arch=x86_64 \
      --kconfig_add CONFIG_PCI=y --kconfig_add CONFIG_I2C=y \
      --kconfig_add '# CONFIG_I2C_HELPER_AUTO is not set' \
      --kconfig_add CONFIG_I2C_ALGOBIT=y \
      --kconfig_add CONFIG_MEDIA_SUPPORT=y \
      --kconfig_add '# CONFIG_MEDIA_SUPPORT_FILTER is not set' \
      --kconfig_add CONFIG_MEDIA_CAMERA_SUPPORT=y \
      --kconfig_add CONFIG_MEDIA_PCI_SUPPORT=y \
      --kconfig_add CONFIG_VIDEO_DEV=y --kconfig_add CONFIG_DEBUG_FS=y \
      --kconfig_add CONFIG_VIDEO_ZORAN=y \
      --kconfig_add CONFIG_VIDEO_ZORAN_KUNIT_TEST=y zoran

With only patch 4 applied:

# zoran_set_format_rejected_keeps_state: EXPECTATION FAILED at drivers/media/pci/zoran/zoran_driver_test.c:70 Expected 768u * 576 * 4 == zr->buffer_size, but

      768u * 576 * 4 == 1769472 (0x1b0000)
      zr->buffer_size == 1584 (0x630)

[FAILED] zoran_set_format_rejected_keeps_state [PASSED] zoran_set_format_accepted_is_consistent [PASSED] zoran_set_format_out_of_range
# Totals: pass:2 fail:1 skip:0 total:3

With the whole series applied:

[PASSED] zoran_set_format_rejected_keeps_state [PASSED] zoran_set_format_accepted_is_consistent [PASSED] zoran_set_format_out_of_range
# Totals: pass:3 fail:0 skip:0 total:3

A W=1 build of drivers/media/pci/zoran/ is warning free with the series applied.

I do not have the hardware, so the DMA overrun itself is derived from reading zr_set_buf() and zr36057_set_memgrab() rather than observed, and only the state desync is covered by the tests. Testing on a real card would be appreciated.

Disclosure: this series was prepared with AI assistance (Claude Opus 5). I reviewed every patch and take responsibility for the submission. I left the Assisted-by tag out of the commit messages because checkpatch.pl flags it as an error in v7.0.

Note also that VIDIOC_S_FMT rejects a misaligned width while zoran_try_fmt_vid_cap() adjusts it through v4l_bound_align_image(). Making VIDIOC_S_FMT adjust the width the same way would remove the failing path altogether, but it changes what existing applications get back, so I left it out of this series.

Minseo Park (4):
media: zoran: remove always-false buffer size check
media: zoran: do not update device state before the format is validated
media: zoran: reject VIDIOC_S_FMT while the queue is busy
media: zoran: add KUnit tests for capture format negotiation

 drivers/media/pci/zoran/Kconfig             |  14 ++++
 drivers/media/pci/zoran/zoran_driver.c      |  21 +++--
 drivers/media/pci/zoran/zoran_driver_test.c | 123 ++++++++++++++++++++++++++++
 3 files changed, 147 insertions(+), 11 deletions(-)
 create mode 100644 drivers/media/pci/zoran/zoran_driver_test.c