Skip to content

HEVC decode - fix a whole lot of security holes and misc issues - #7780

Merged
pelwell merged 18 commits into
raspberrypi:rpi-6.18.yfrom
jc-kynesim:dev/6.18/hevc_d_sec_1
Nov 26, 2026
Merged

pelwell merged 18 commits into
raspberrypi:rpi-6.18.yfrom
jc-kynesim:dev/6.18/hevc_d_sec_1

Conversation

@jc-kynesim

Copy link
Copy Markdown
Contributor

I ran Mythos over hevc_d and it found quite a lot of stuff that previous Claude's hadn't found. This is the accumulated patch set. We aren't quite clean but I'm not worried by any of the remaining points it raised.
Time for Dom's Claude to argue with my Claude :-)

@popcornmix

Copy link
Copy Markdown
Collaborator

Review of #7780: HEVC decode security/misc fixes

Overall this looks good. I went through all 17 commits against rpi-6.18.y, along with the IRQ claim code (hevc_d_hw.c) and the vb2/mc-request core paths they rely on. I found no blocking problems. Some comments:

Correctness / design

  1. Ensure that a removed buffer is not in use: claiming only active2 is enough. Ref addresses are read from ctx->slots[] in phase2_claimed(), not in setup. A frame still in phase 1 that references the removed index sees the zeroed slot when it reaches phase 2 and falls back via find_slot(). The comment on hevc_d_h265_invalidate_slot() could say this, because at first sight it looks like it should be a full hevc_d_h265_sync(). Side effect: requeuing a CAPTURE dmabuf with a different fd now blocks QBUF until the current phase 2 frame finishes. That is probably fine, but worth knowing.

  2. Lock controls against change in phase 0: hdl.lock is taken after v4l2_ctrl_request_setup(). A non-request S_EXT_CTRLS that runs between the two can still overwrite the values the request just applied. The window is small and it needs badly behaved userspace, but the commit message implies it is closed. It can't simply be moved, because request_setup takes the same lock itself. A comment would be enough. (I checked for deadlocks: nothing inside hevc_d_h265_setup() calls v4l2_ctrl_find() or takes the control lock, and no device_run caller holds hdl.lock.)

  3. Remove dev_mutex: queue ioctls are still serialised because m2m_ctx->q_lock = ctx_mutex, and G/S_FMT now take it. These are left without the lock:

    • TRY_FMT (cap): reads ctx->src_fmt (copy_color) and the SPS without ctx_mutex or hdl.lock.
    • ENUM_FMT / G_FMT / S_FMT (cap): read the SPS via p_cur without hdl.lock.

    The worst outcome is an inconsistent format returned to userspace. Setup checks the plane sizes again, so I don't think it is a safety problem. I'm noting it because the lock that used to cover these is now gone.

  4. Fix behaviour if unbound whilst running: freeing in the vfd release is the right pattern (v4l2_dev.release is NULL, so v4l2_device_release() doesn't touch v4l2_dev after our kfree). Remaining issues:

    • A request fd (/dev/mediaN request) can outlive the last video fd. media_request_release() then uses req->mdev, which is inside the freed dev. This is the same for every m2m driver that frees in vfd release, so I'm not asking for a fix, but the commit message is a bit optimistic.
    • hevc_d_hw_remove() runs while the video node is still registered. That is safe as far as I can see (runtime PM is disabled, so pm_get fails, and m2m is suspended), but a short comment would help the next reader.
  5. Avoid unlikely race between decode & streamoff: I went through the p1out ordering for when the claim callback runs synchronously and fails (phase1_done before the increment). job_finish is still called exactly once because N ≤ P1BUF_COUNT-1 when device_run runs. OK.

  6. Ensure media requests are cleaned up correctly: OK. media_request_object_unbind() clears req_obj.req, so the unbind/put in __vb2_queue_cancel() is skipped and nothing is done twice. This depends on vb2 internals, so the comment could say "vb2 will skip its own unbind as req_obj.req is now NULL".

  7. Only start decode h/w after both ...: both queues now have min_queued_buffers = 0, so start_streaming_called follows STREAMON and the pairing logic is sound. hevc_d_h265_start() reads dst_fmt with ctx_mutex held (q->lock). OK.

  8. Checks (Tighten up SPS/PPS/SH, tiles in order, off by one): checked against the spec ranges (CtbLog2SizeY 4..6, MinTb < MinCb, PCM sizes/depths, sub_layers ≤ 6). Moving the tile check from > to >= is right because of col_bd[t_x + 1]. The cmds_check_space change keeps the same total and covers the case where the loop doesn't run.

Commit message nits

  • c0b1911: "the is a" → "there is a"
  • 12cdf35: "wait_for_competion" → "wait_for_completion"
  • 146fa40: title says min_queue_buffers; the field is min_queued_buffers
  • 64f901e: "Requests are manually completed fix cleanup code…" needs a full stop or comma
  • ccaab73: "Fixe these"
  • 63b438f: "just just", "v2_plane_size"
  • 0258f2a: "corrcetly"

Not build-tested or run on a Pi.

@jc-kynesim

Copy link
Copy Markdown
Contributor Author

All of these that suggest action except (3) come down to "it would be nice to add a comment" or "fix spelling". I had a look at (3) and it is mildly annoying to add all the locks required to make it go away. On the whole I believe that if the user is changing the SPS at the same time as poking the format that depends on it then they deserve everything they get - even if fixed you have a race as to what you get. So I propose to do nothing about (3) unless you have strong feelings about it. I'll fix spelling / add a few comments for the other points.

tile_entry_fill will not fulfil its stated guarantee to leave one
slot free if its loop never runs. Fix that.

Signed-off-by: John Cox <jc@kynesim.co.uk>
If there is a concurrent decode and streamoff request there is a
theoretical window in which the streamoff can complete before the
decode. Close it.

Signed-off-by: John Cox <jc@kynesim.co.uk>
frame_slot and rpi_currpoc are used in the error path before they
are set. Fix this.

Signed-off-by: John Cox <jc@kynesim.co.uk>
Typo after checking if PPS has changed. Fix it

Signed-off-by: John Cox <jc@kynesim.co.uk>
…e started

Starting the h/w and therefore keeping decode context based only on OUTPUT
streaming state allows CAPTURE formats to change without the context
changing. This allows for missized buffers. Fix so the h/w is shutdown if
either stream is off.

Signed-off-by: John Cox <jc@kynesim.co.uk>
Remove device serialization of G_FMT and S_FMT and use the per context
lock instead for these ops.

Signed-off-by: John Cox <jc@kynesim.co.uk>
Add code to invalidate a capture buffer slot if the buffer is removed
Also change sync to use wait_for_completion which is both neater and
safer than the existing wait_queue logic

Signed-off-by: John Cox <jc@kynesim.co.uk>
min_queued_buffers = 1 is unneeded and Claude asserts that it can cause
a (unlikely) race such that a job can be started before _start has
completed so remove it. (Hantro doesn't have this line)

Signed-off-by: John Cox <jc@kynesim.co.uk>
Requests are manually completed. Fix cleanup code to complete them.

Signed-off-by: John Cox <jc@kynesim.co.uk>
Take the control lock whilst h265_setup is in progress to avoid
inconsistent values whilst in use.

Signed-off-by: John Cox <jc@kynesim.co.uk>
Control setup errors ignored, v4l2_fh_exit missing on error path.
Fix these.

Signed-off-by: John Cox <jc@kynesim.co.uk>
Tighten up value checking so we cannot program wrong values into the h/w

Signed-off-by: John Cox <jc@kynesim.co.uk>
Tiles must turn up in the correct order. Enforce this.

Signed-off-by: John Cox <jc@kynesim.co.uk>
It is possible to remove all CAPTURE buffers whilst streaming. This
allows format change as that makes it no longer busy. Check for
streaming too.

Signed-off-by: John Cox <jc@kynesim.co.uk>
Setup checks the vb2_v4l2_buffer plane length member which may not be
correct for dmabufs as that is just what the user passed not the
actual size - use vb2_plane_size on the vb2_buf instead

Signed-off-by: John Cox <jc@kynesim.co.uk>
Separate unregister and device release functions so we behave correctly
if unbound whilst running.

Signed-off-by: John Cox <jc@kynesim.co.uk>
Pick a format that matches the SPS (if set) in hevc_d_try_fmt_vid_cap
if what the user set is unsupported or bad rather than leaving bad and
always fixing up to NV12MT_COL128

Signed-off-by: John Cox <jc@kynesim.co.uk>
@jc-kynesim
jc-kynesim force-pushed the dev/6.18/hevc_d_sec_1 branch from f5d96d4 to 9255d04 Compare November 25, 2026 17:50
@jc-kynesim

jc-kynesim commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Maybe that was what I meant to push but compare suggests a huge amount has changed when I've just fixed some comments and tweaked the removed buffer patch very slightly. Possibly because I rebased onto current rpi-6.18.y head?

…ration

It is possible to flood the log with warning messages by setting bad
values in SPS, PPS etc. It is valuable to have this info when
debugging so reduce the level to debug.

Reported-by: Roy <rongh7426@gmail.com>
Closes: raspberrypi#7796
Signed-off-by: John Cox <jc@kynesim.co.uk>
@jc-kynesim

Copy link
Copy Markdown
Contributor Author

Also added a commit to reduce the level of debug when misconfiguring controls. Now reliant on the debug level rather than potentially flooding the log.

@pelwell
pelwell merged commit 16ae264 into raspberrypi:rpi-6.18.y Nov 26, 2026
12 checks passed
@pelwell pelwell mentioned this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants