Skip to contentSkip to conversation

media: rkvdec: hevc: cap EXT SPS RPS control counts before descriptor assembly

PATCH Superseded Michael Bommarito 3 participants 11 messages May 13 – Jul 15

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: rkvdec: hevc: cap EXT SPS RPS control counts before descriptor assembly

V4L2_CID_STATELESS_HEVC_EXT_SPS_ST_RPS and V4L2_CID_STATELESS_HEVC_EXT_SPS_LT_RPS are registered as dynamic-size controls with a per-control element cap of 65. The V4L2 control core enforces only the payload-element cap. It does not bound the spec-derived count fields that the rkvdec HEVC helper later uses to walk fixed hardware descriptor tables and temporary helper arrays:

- struct rkvdec_rps::refs[32]
- struct rkvdec_rps::short_term_ref_sets[64]
- struct calculated_rps_st_set::delta_poc_s0[16] / delta_poc_s1[16]

A userspace V4L2 client that can open the Rockchip RKVDEC m2m decoder node may submit SPS/RPS controls whose counts exceed those capacities or whose prediction reference index underflows. rkvdec_hevc_assemble_hw_rps() then walks past the descriptor table or temporary-array bounds.

KASAN under a small KUnit harness wrapping the real helper reports slab-out-of-bounds in all of:

- num_short_term_ref_pic_sets = 65 (write past short_term_ref_sets[64])
- num_long_term_ref_pics_sps = 33 (write past refs[32], intra-struct)
- ext_sps_st_rps[i].num_negative_pics or num_positive_pics > 16 (write past delta_poc_s0[16] inside calculated_rps_st_set)
- INTER_REF_PIC_SET_PRED with delta_idx_minus1 + 1 > i
(u8 ref_rps_idx underflow then OOB read on calculated_rps_st_sets)

Validate the SPS/RPS counts before calling the assembly helpers. The cap values match both the HEVC spec ranges (num_short_term_ref_pic_sets <= 64, num_long_term_ref_pics_sps <= 32) and the fixed driver descriptor and helper-array capacities. Reject controls whose counts exceed those, and reject prediction entries whose reference index would underflow.

Fixes c9a59dc2acc7 ("media: rkvdec: Add HEVC support for the VDPU381 variant")
Assisted-by Claude:claude-opus-4-7

---

I don't have a RK3588 / RK3576 board to confirm this through a real /dev/videoN request path yet, but was convinced enough by:

1. Static reach: registration of EXT_SPS_ST_RPS / EXT_SPS_LT_RPS with .dims = { 65 } at drivers/media/platform/rockchip/rkvdec/rkvdec.c :239-287, the SPS-count-driven loops in rkvdec-hevc-common.c :213-225 and :228-251, and v4l2-ctrls-core.c :1213-1277, which validates only EXT RPS flags and not the spec-derived count fields.

2. A KUnit harness (separate, not in this patch) that allocates one struct rkvdec_rps + a single calculated_rps_st_sets element via kunit_kzalloc / kzalloc and calls the real rkvdec_hevc_assemble_hw_rps() helper. Under UML + KASAN_GENERIC with the kasan_multi_shot boot param, on a stock tree it produces these reports:

BUG: KASAN: slab-out-of-bounds in
rkvdec_hevc_assemble_hw_rps+0xb0c/0x1080 (num_short_term_ref_pic_sets = 65, write of size 36 0 bytes past the rps allocation)

BUG: KASAN: slab-out-of-bounds in
rkvdec_hevc_assemble_hw_rps (num_negative_pics = 64, write past the single-element kzalloc'd calculated_rps_st_sets buffer)

BUG: KASAN: slab-use-after-free / slab-out-of-bounds reads via
u8 ref_rps_idx underflow at calculated_rps_st_sets[255] (INTER_REF_PIC_SET_PRED with delta_idx_minus1 = 0, idx = 0)

The num_long_term_ref_pics_sps = 33 case is invisible to KASAN (the OOB write lands inside struct rkvdec_rps) but corrupts short_term_ref_sets[0]; the harness asserts that case explicitly.

3. Same harness on the patched tree: all five cases (four adversarial plus a legitimate-limit regression with ST=64, LT=32, num_neg=1) pass clean, no KASAN reports.

If hardware-side validation actually does reject these counts before rkvdec_hevc_assemble_hw_rps() runs and this patch is unnecessary, please say so and I will withdraw it. If it is reachable, I will follow up with a runtime hardware splat once the Orange Pi board I bought arrives.

Let me know if you want a patch set with the KUnit harnesses too.

checkpatch.pl: 0 errors / 0 warnings.

 .../rockchip/rkvdec/rkvdec-hevc-common.c      | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)
drivers/media/platform/rockchip/rkvdec/rkvdec-hevc-common.c +49 -0
index 3119f3bc9f98..895fb16bc572 100644
@@ -408,9 +408,58 @@ static void rkvdec_hevc_prepare_hw_st_rps(struct rkvdec_hevc_run *run, struct rk
 	memcpy(cache, run->ext_sps_st_rps, sizeof(struct v4l2_ctrl_hevc_ext_sps_st_rps));
 }
 
+/*
+ * V4L2 caps the EXT_SPS RPS payload length but not the SPS-derived counts
+ * that the helpers walk. Caps match the HEVC spec ranges.
+ */
+#define RKVDEC_HEVC_MAX_SHORT_TERM_REF_PIC_SETS	64
+#define RKVDEC_HEVC_MAX_LONG_TERM_REF_PICS_SPS	32
+#define RKVDEC_HEVC_MAX_RPS_NEG_POS_PICS	16
+
+static int rkvdec_hevc_validate_rps_ctrls(struct rkvdec_hevc_run *run)
+{
+	const struct v4l2_ctrl_hevc_sps *sps = run->sps;
+
+	if (run->ext_sps_lt_rps &&
+	    sps->num_long_term_ref_pics_sps >
+	    RKVDEC_HEVC_MAX_LONG_TERM_REF_PICS_SPS)
+		return -EINVAL;
+
+	if (run->ext_sps_st_rps) {
+		unsigned int i;
+
+		if (sps->num_short_term_ref_pic_sets >
+		    RKVDEC_HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
+			return -EINVAL;
+
+		for (i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
+			const struct v4l2_ctrl_hevc_ext_sps_st_rps *r =
+				&run->ext_sps_st_rps[i];
+
+			if (r->num_negative_pics >
+			    RKVDEC_HEVC_MAX_RPS_NEG_POS_PICS ||
+			    r->num_positive_pics >
+			    RKVDEC_HEVC_MAX_RPS_NEG_POS_PICS)
+				return -EINVAL;
+
+			if ((r->flags &
+			     V4L2_HEVC_EXT_SPS_ST_RPS_FLAG_INTER_REF_PIC_SET_PRED) &&
+			    (unsigned int)r->delta_idx_minus1 + 1 > i)
+				return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
 void rkvdec_hevc_assemble_hw_rps(struct rkvdec_hevc_run *run, struct rkvdec_rps *rps,
 				 struct v4l2_ctrl_hevc_ext_sps_st_rps *st_cache)
 {
+	if (rkvdec_hevc_validate_rps_ctrls(run)) {
+		pr_err_ratelimited("rkvdec: rejecting HEVC SPS/RPS controls with out-of-range counts\n");
+		return;
+	}
+
 	rkvdec_hevc_prepare_hw_st_rps(run, rps, st_cache);
 	rkvdec_hevc_assemble_hw_lt_rps(run, rps);
 }
Signature · 1 line
2.53.0
maintainer
#1 lore raw

Hi Michael,

Quoted 10 lines · Michael Bommarito

V4L2_CID_STATELESS_HEVC_EXT_SPS_ST_RPS and V4L2_CID_STATELESS_HEVC_EXT_SPS_LT_RPS are registered as dynamic-size controls with a per-control element cap of 65. The V4L2 control core enforces only the payload-element cap. It does not bound the spec-derived count fields that the rkvdec HEVC helper later uses to walk fixed hardware descriptor tables and temporary helper arrays:

- struct rkvdec_rps::refs[32]
- struct rkvdec_rps::short_term_ref_sets[64]
- struct calculated_rps_st_set::delta_poc_s0[16] / delta_poc_s1[16]

Still, did you try just changing the cap to 64 (.cfg.dims = { 64 },) ? You'd need a test that sets the control from userspace though.

It should refuse setting the control if there are more than 64 elements, therefore the hevc decoder will not run any function using the count values from the SPS (See  rkvdec-vdpu381-hevc.c:601)

Quoted 138 lines

A userspace V4L2 client that can open the Rockchip RKVDEC m2m decoder node may submit SPS/RPS controls whose counts exceed those capacities or whose prediction reference index underflows. rkvdec_hevc_assemble_hw_rps() then walks past the descriptor table or temporary-array bounds.

KASAN under a small KUnit harness wrapping the real helper reports slab-out-of-bounds in all of:

- num_short_term_ref_pic_sets = 65 (write past short_term_ref_sets[64])
- num_long_term_ref_pics_sps = 33 (write past refs[32], intra-struct)
- ext_sps_st_rps[i].num_negative_pics or num_positive_pics > 16 (write past delta_poc_s0[16] inside calculated_rps_st_set)
- INTER_REF_PIC_SET_PRED with delta_idx_minus1 + 1 > i
(u8 ref_rps_idx underflow then OOB read on calculated_rps_st_sets)

Validate the SPS/RPS counts before calling the assembly helpers. The cap values match both the HEVC spec ranges (num_short_term_ref_pic_sets <= 64, num_long_term_ref_pics_sps <= 32) and the fixed driver descriptor and helper-array capacities. Reject controls whose counts exceed those, and reject prediction entries whose reference index would underflow.

Fixes c9a59dc2acc7 ("media: rkvdec: Add HEVC support for the VDPU381 variant")
Assisted-by Claude:claude-opus-4-7

---

I don't have a RK3588 / RK3576 board to confirm this through a real /dev/videoN request path yet, but was convinced enough by:

1. Static reach: registration of EXT_SPS_ST_RPS / EXT_SPS_LT_RPS with .dims = { 65 } at drivers/media/platform/rockchip/rkvdec/rkvdec.c :239-287, the SPS-count-driven loops in rkvdec-hevc-common.c :213-225 and :228-251, and v4l2-ctrls-core.c :1213-1277, which validates only EXT RPS flags and not the spec-derived count fields.

2. A KUnit harness (separate, not in this patch) that allocates one struct rkvdec_rps + a single calculated_rps_st_sets element via kunit_kzalloc / kzalloc and calls the real rkvdec_hevc_assemble_hw_rps() helper. Under UML + KASAN_GENERIC with the kasan_multi_shot boot param, on a stock tree it produces these reports:

BUG: KASAN: slab-out-of-bounds in
rkvdec_hevc_assemble_hw_rps+0xb0c/0x1080 (num_short_term_ref_pic_sets = 65, write of size 36 0 bytes past the rps allocation)

BUG: KASAN: slab-out-of-bounds in
rkvdec_hevc_assemble_hw_rps (num_negative_pics = 64, write past the single-element kzalloc'd calculated_rps_st_sets buffer)

BUG: KASAN: slab-use-after-free / slab-out-of-bounds reads via
u8 ref_rps_idx underflow at calculated_rps_st_sets[255] (INTER_REF_PIC_SET_PRED with delta_idx_minus1 = 0, idx = 0)

The num_long_term_ref_pics_sps = 33 case is invisible to KASAN (the OOB write lands inside struct rkvdec_rps) but corrupts short_term_ref_sets[0]; the harness asserts that case explicitly.

3. Same harness on the patched tree: all five cases (four adversarial plus a legitimate-limit regression with ST=64, LT=32, num_neg=1) pass clean, no KASAN reports.

If hardware-side validation actually does reject these counts before rkvdec_hevc_assemble_hw_rps() runs and this patch is unnecessary, please say so and I will withdraw it. If it is reachable, I will follow up with a runtime hardware splat once the Orange Pi board I bought arrives.

Let me know if you want a patch set with the KUnit harnesses too.

checkpatch.pl: 0 errors / 0 warnings.

  .../rockchip/rkvdec/rkvdec-hevc-common.c      | 49 +++++++++++++++++++
  1 file changed, 49 insertions(+)
drivers/media/platform/rockchip/rkvdec/rkvdec-hevc-common.c +49 -0
index 3119f3bc9f98..895fb16bc572 100644
@@ -408,9 +408,58 @@ static void rkvdec_hevc_prepare_hw_st_rps(struct rkvdec_hevc_run *run, struct rk
  	memcpy(cache, run->ext_sps_st_rps, sizeof(struct v4l2_ctrl_hevc_ext_sps_st_rps));
  }
 
+/*
+ * V4L2 caps the EXT_SPS RPS payload length but not the SPS-derived counts
+ * that the helpers walk. Caps match the HEVC spec ranges.
+ */
+#define RKVDEC_HEVC_MAX_SHORT_TERM_REF_PIC_SETS	64
+#define RKVDEC_HEVC_MAX_LONG_TERM_REF_PICS_SPS	32
+#define RKVDEC_HEVC_MAX_RPS_NEG_POS_PICS	16
+
+static int rkvdec_hevc_validate_rps_ctrls(struct rkvdec_hevc_run *run)
+{
+	const struct v4l2_ctrl_hevc_sps *sps = run->sps;
+
+	if (run->ext_sps_lt_rps &&
+	    sps->num_long_term_ref_pics_sps >
+	    RKVDEC_HEVC_MAX_LONG_TERM_REF_PICS_SPS)
+		return -EINVAL;
+
+	if (run->ext_sps_st_rps) {
+		unsigned int i;
+
+		if (sps->num_short_term_ref_pic_sets >
+		    RKVDEC_HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
+			return -EINVAL;
+
+		for (i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
+			const struct v4l2_ctrl_hevc_ext_sps_st_rps *r =
+				&run->ext_sps_st_rps[i];
+
+			if (r->num_negative_pics >
+			    RKVDEC_HEVC_MAX_RPS_NEG_POS_PICS ||
+			    r->num_positive_pics >
+			    RKVDEC_HEVC_MAX_RPS_NEG_POS_PICS)
+				return -EINVAL;
+
+			if ((r->flags &
+			     V4L2_HEVC_EXT_SPS_ST_RPS_FLAG_INTER_REF_PIC_SET_PRED) &&
+			    (unsigned int)r->delta_idx_minus1 + 1 > i)
+				return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
  void rkvdec_hevc_assemble_hw_rps(struct rkvdec_hevc_run *run, struct rkvdec_rps *rps,
  				 struct v4l2_ctrl_hevc_ext_sps_st_rps *st_cache)
  {
+	if (rkvdec_hevc_validate_rps_ctrls(run)) {
+		pr_err_ratelimited("rkvdec: rejecting HEVC SPS/RPS controls with out-of-range counts\n");
+		return;
+	}
+
  	rkvdec_hevc_prepare_hw_st_rps(run, rps, st_cache);
  	rkvdec_hevc_assemble_hw_lt_rps(run, rps);
  }
#2 lore raw
Quoted 6 lines · Detlev Casanova

Still, did you try just changing the cap to 64 (.cfg.dims = { 64 },) ? You'd need a test that sets the control from userspace though.

It should refuse setting the control if there are more than 64 elements, therefore the hevc decoder will not run any function using the count values from the SPS (See rkvdec-vdpu381-hevc.c:601)

Sure, I can test that and send a v2 for ST. My understanding is that we have four spots we need to check across the flow though:

1. ST count > 64
2. LT count > 32
3. num_negative_pics / num_positive_pics > 16
4. delta_idx_minus1 + 1 > i

So would you also want the same .cfg approach for the LT cap?

Thanks,
Mike

maintainer
#3 lore raw

Hi Mike,

Quoted 5 lines · Michael Bommarito
Quoted 3 lines · Detlev Casanova

Still, did you try just changing the cap to 64 (.cfg.dims = { 64 },) ? You'd need a test that sets the control from userspace though.

Quoted 8 lines
Quoted 3 lines

It should refuse setting the control if there are more than 64 elements, therefore the hevc decoder will not run any function using the count values from the SPS (See rkvdec-vdpu381-hevc.c:601)

Sure, I can test that and send a v2 for ST. My understanding is that we have four spots we need to check across the flow though:

1. ST count > 64
2. LT count > 32

ST and LT can be handled through cfg.dims and when num_negative_pics / num_positive_pics are non-zero, the code check that ST and LT have actually been set. If not, they can be skipped.

Quoted 1 line

3. num_negative_pics / num_positive_pics > 16

This could be checked in the validation step of the control in v4l2-core.

Quoted 1 line

4. delta_idx_minus1 + 1 > i

This one would need to be checked in the construction code indeed.

Quoted 2 lines

So would you also want the same .cfg approach for the LT cap?

So yes for LT dimensions.

Detlev.

#4 lore raw

v2 cover media: rkvdec: hevc: bound EXT SPS RPS control counts

In reply to Michael Bommarito

A userspace V4L2 client that can open the Rockchip RKVDEC m2m decoder node can submit HEVC EXT SPS RPS controls whose spec-derived count fields exceed the driver's fixed hardware descriptor tables and temporary helper arrays. KASAN under a KUnit harness wrapping the real rkvdec_hevc_assemble_hw_rps() helper confirms slab-out-of-bounds writes on num_short_term_ref_pic_sets > 64, num_long_term_ref_pics_sps > 32, num_negative/positive_pics > 16, and an OOB read via u8 ref_rps_idx underflow when delta_idx_minus1 + 1 > idx.

v2 splits the single validation function from v1 into three layers at the appropriate level, per Detlev's review:

1/3 Tighten .cfg.dims on EXT_SPS_ST_RPS (65 -> 64) and
EXT_SPS_LT_RPS (65 -> 32) to match the HEVC spec limits and let the V4L2 control framework reject oversized payloads.

2/3 Add SPS ST/LT count validation plus
num_negative_pics / num_positive_pics validation in v4l2-ctrls-core.c so every consumer driver is protected.

3/3 Guard the delta_idx_minus1 underflow in
st_ref_pic_set_prediction() in the rkvdec construction code.

Changes in v2:
- Split the monolithic rkvdec-hevc-common.c validation function into dims, v4l2-core, and construction-code layers as Detlev suggested.
- Drop the rkvdec-local #defines and pr_err_ratelimited; the V4L2 framework now handles the bulk of the rejection, including the SPS count fields that drive the rkvdec loops.

Michael Bommarito (3):
media: rkvdec: hevc: tighten EXT SPS RPS control dimensions
media: v4l2-ctrls: validate HEVC EXT SPS RPS counts
media: rkvdec: hevc: guard INTER_REF_PIC_SET_PRED index underflow

 .../platform/rockchip/rkvdec/rkvdec-hevc-common.c |  3 +++
 drivers/media/platform/rockchip/rkvdec/rkvdec.c   |  4 ++--
 drivers/media/v4l2-core/v4l2-ctrls-core.c         | 15 +++++++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

base-commit: 7fd2df204f342fc17d1a0bfcd474b24232fb0f32 --
2.53.0

#5 lore raw

PATCH v2 1/3 media: rkvdec: hevc: tighten EXT SPS RPS control dimensions

The VDPU381 HEVC driver registers V4L2_CID_STATELESS_HEVC_EXT_SPS_ST_RPS and V4L2_CID_STATELESS_HEVC_EXT_SPS_LT_RPS with .cfg.dims = { 65 }, but the HEVC spec caps num_short_term_ref_pic_sets at 64 (ITU-T H.265 7.4.8) and num_long_term_ref_pics_sps at 32 (7.4.3.2.1). The hardware descriptor table (struct rkvdec_rps) sizes match those spec limits: refs[32] and short_term_ref_sets[64].

Reduce the dims to { 64 } and { 32 } respectively so the V4L2 control framework rejects oversized payloads before any driver code runs.

Fixes c9a59dc2acc7 ("media: rkvdec: Add HEVC support for the VDPU381 variant")
Assisted-by Claude:claude-opus-4-7
---
 drivers/media/platform/rockchip/rkvdec/rkvdec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/rockchip/rkvdec/rkvdec.c +2 -2
index 1d1e9bfef8e96..4fd06f4c04db0 100644
@@ -278,12 +278,12 @@ static const struct rkvdec_ctrl_desc vdpu38x_hevc_ctrl_descs[] = {
 	{
 		.cfg.id = V4L2_CID_STATELESS_HEVC_EXT_SPS_ST_RPS,
 		.cfg.ops = &rkvdec_ctrl_ops,
-		.cfg.dims = { 65 },
+		.cfg.dims = { 64 },
 	},
 	{
 		.cfg.id = V4L2_CID_STATELESS_HEVC_EXT_SPS_LT_RPS,
 		.cfg.ops = &rkvdec_ctrl_ops,
-		.cfg.dims = { 65 },
+		.cfg.dims = { 32 },
 	},
 };
 
Signature · 1 line
2.53.0
Reviewed-by on v2 1/3
#6 lore raw

Re: [PATCH v2 1/3] media: rkvdec: hevc: tighten EXT SPS RPS control dimensions

Le mercredi 27 mai 2026 à 15:47 -0400, Michael Bommarito a écrit :

Quoted 10 lines

The VDPU381 HEVC driver registers V4L2_CID_STATELESS_HEVC_EXT_SPS_ST_RPS and V4L2_CID_STATELESS_HEVC_EXT_SPS_LT_RPS with .cfg.dims = { 65 }, but the HEVC spec caps num_short_term_ref_pic_sets at 64 (ITU-T H.265 7.4.8) and num_long_term_ref_pics_sps at 32 (7.4.3.2.1). The hardware descriptor table (struct rkvdec_rps) sizes match those spec limits: refs[32] and short_term_ref_sets[64].

Reduce the dims to { 64 } and { 32 } respectively so the V4L2 control framework rejects oversized payloads before any driver code runs.

Quoted 6 lines
Fixes c9a59dc2acc7 ("media: rkvdec: Add HEVC support for the VDPU381

variant")

Assisted-by Claude:claude-opus-4-7
Quoted 25 lines

---
 drivers/media/platform/rockchip/rkvdec/rkvdec.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/rockchip/rkvdec/rkvdec.c +2 -2
index 1d1e9bfef8e96..4fd06f4c04db0 100644
@@ -278,12 +278,12 @@ static const struct rkvdec_ctrl_desc
 vdpu38x_hevc_ctrl_descs[] = {
  	{
  		.cfg.id = V4L2_CID_STATELESS_HEVC_EXT_SPS_ST_RPS,
  		.cfg.ops = &rkvdec_ctrl_ops,
-		.cfg.dims = { 65 },
+		.cfg.dims = { 64 },
  	},
  	{
  		.cfg.id = V4L2_CID_STATELESS_HEVC_EXT_SPS_LT_RPS,
  		.cfg.ops = &rkvdec_ctrl_ops,
-		.cfg.dims = { 65 },
+		.cfg.dims = { 32 },
  	},
  };

#7 lore raw

PATCH v2 2/3 media: v4l2-ctrls: validate HEVC EXT SPS RPS counts

In reply to Michael Bommarito

The HEVC SPS control carries the short-term and long-term RPS counts that decoder drivers use to walk the matching EXT SPS dynamic arrays. Reject SPS values that exceed the HEVC limits of 64 short-term sets and 32 long-term references so drivers cannot later index beyond those controls.

Also reject EXT SPS ST RPS entries whose negative or positive picture counts exceed the 16-entry arrays, or whose combined delta-POC count exceeds the HEVC DPB maximum.

Fixes c9a59dc2acc7 ("media: rkvdec: Add HEVC support for the VDPU381 variant")
Assisted-by Claude:claude-opus-4-7
---
 drivers/media/v4l2-core/v4l2-ctrls-core.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/media/v4l2-core/v4l2-ctrls-core.c +15 -0
index 6b375720e395c..a1d773e5de20c 100644
@@ -16,6 +16,9 @@
 
 static const union v4l2_ctrl_ptr ptr_null;
 
+#define V4L2_HEVC_MAX_SHORT_TERM_REF_PIC_SETS	64
+#define V4L2_HEVC_MAX_LONG_TERM_REF_PICS_SPS	32
+
 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl,
 		       u32 changes)
 {
@@ -1213,6 +1216,10 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 	case V4L2_CTRL_TYPE_HEVC_SPS:
 		p_hevc_sps = p;
 
+		if (p_hevc_sps->num_short_term_ref_pic_sets >
+		    V4L2_HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
+			return -EINVAL;
+
 		if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) {
 			p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0;
 			p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0;
@@ -1223,6 +1230,9 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 		if (!(p_hevc_sps->flags &
 		      V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT))
 			p_hevc_sps->num_long_term_ref_pics_sps = 0;
+		else if (p_hevc_sps->num_long_term_ref_pics_sps >
+			 V4L2_HEVC_MAX_LONG_TERM_REF_PICS_SPS)
+			return -EINVAL;
 		break;
 
 	case V4L2_CTRL_TYPE_HEVC_PPS:
@@ -1267,6 +1277,11 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 
 		if (p_hevc_st_rps->flags & ~V4L2_HEVC_EXT_SPS_ST_RPS_FLAG_INTER_REF_PIC_SET_PRED)
 			return -EINVAL;
+		if (p_hevc_st_rps->num_negative_pics > 16 ||
+		    p_hevc_st_rps->num_positive_pics > 16 ||
+		    p_hevc_st_rps->num_negative_pics +
+		    p_hevc_st_rps->num_positive_pics > 16)
+			return -EINVAL;
 		break;
 
 	case V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS:
Signature · 1 line
2.53.0
Reviewed-by on v2 2/3
#8 lore raw

Re: [PATCH v2 2/3] media: v4l2-ctrls: validate HEVC EXT SPS RPS counts

Le mercredi 27 mai 2026 à 15:47 -0400, Michael Bommarito a écrit :

Quoted 10 lines

The HEVC SPS control carries the short-term and long-term RPS counts that decoder drivers use to walk the matching EXT SPS dynamic arrays. Reject SPS values that exceed the HEVC limits of 64 short-term sets and 32 long-term references so drivers cannot later index beyond those controls.

Also reject EXT SPS ST RPS entries whose negative or positive picture counts exceed the 16-entry arrays, or whose combined delta-POC count exceeds the HEVC DPB maximum.

Quoted 5 lines
Fixes c9a59dc2acc7 ("media: rkvdec: Add HEVC support for the VDPU381 variant")
Assisted-by Claude:claude-opus-4-7
Quoted 51 lines

---
 drivers/media/v4l2-core/v4l2-ctrls-core.c | 15 +++++++++++++++

 1 file changed, 15 insertions(+)
drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
index 6b375720e395c..a1d773e5de20c 100644
@@ -16,6 +16,9 @@


 static const union v4l2_ctrl_ptr ptr_null;

drivers/media/v4l2-core/v4l2-ctrls-core.c +3 -0
+#define V4L2_HEVC_MAX_SHORT_TERM_REF_PIC_SETS	64
+#define V4L2_HEVC_MAX_LONG_TERM_REF_PICS_SPS	32
+

 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
 {

drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
@@ -1213,6 +1216,10 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,

  case V4L2_CTRL_TYPE_HEVC_SPS:
  p_hevc_sps = p;

drivers/media/v4l2-core/v4l2-ctrls-core.c +4 -0
+		if (p_hevc_sps->num_short_term_ref_pic_sets >
+		    V4L2_HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
+			return -EINVAL;
+

  if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) { p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0; p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0;

drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
@@ -1223,6 +1230,9 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,

  if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT)) p_hevc_sps->num_long_term_ref_pics_sps = 0;

drivers/media/v4l2-core/v4l2-ctrls-core.c +3 -0
+		else if (p_hevc_sps->num_long_term_ref_pics_sps >
+			 V4L2_HEVC_MAX_LONG_TERM_REF_PICS_SPS)
+			return -EINVAL;

  break;

  case V4L2_CTRL_TYPE_HEVC_PPS:

drivers/media/v4l2-core/v4l2-ctrls-core.c +0 -0
@@ -1267,6 +1277,11 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,


  if (p_hevc_st_rps->flags & ~V4L2_HEVC_EXT_SPS_ST_RPS_FLAG_INTER_REF_PIC_SET_PRED) return -EINVAL;

drivers/media/v4l2-core/v4l2-ctrls-core.c +5 -0
+		if (p_hevc_st_rps->num_negative_pics > 16 ||
+		    p_hevc_st_rps->num_positive_pics > 16 ||
+		    p_hevc_st_rps->num_negative_pics +
+		    p_hevc_st_rps->num_positive_pics > 16)
+			return -EINVAL;

  break;

  case V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS:

#9 lore raw

PATCH v2 3/3 media: rkvdec: hevc: guard INTER_REF_PIC_SET_PRED index underflow

In reply to Michael Bommarito

st_ref_pic_set_prediction() computes the reference RPS index as st_rps_idx - (delta_idx_minus1 + 1) per HEVC spec equation 7-59. Both operands are u8, so when delta_idx_minus1 + 1 exceeds the current index the subtraction wraps and the subsequent array access at calculated_rps_st_sets[ref_rps_idx] reads far out of bounds.

A userspace V4L2 client that can open the RKVDEC m2m decoder can submit an EXT_SPS_ST_RPS control with INTER_REF_PIC_SET_PRED set and delta_idx_minus1 crafted to trigger the underflow.

Reject the entry early when the reference index would underflow.

Fixes c9a59dc2acc7 ("media: rkvdec: Add HEVC support for the VDPU381 variant")
Assisted-by Claude:claude-opus-4-7
---
 drivers/media/platform/rockchip/rkvdec/rkvdec-hevc-common.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/platform/rockchip/rkvdec/rkvdec-hevc-common.c +3 -0
index 3119f3bc9f98b..898d1ce74f38a 100644
@@ -268,6 +268,9 @@ static void st_ref_pic_set_prediction(struct rkvdec_hevc_run *run, int idx,
 	int i, j;
 	int dPoc;
 
+	if ((unsigned int)rps_data->delta_idx_minus1 + 1 > idx)
+		return;
+
 	ref_rps_idx = st_rps_idx - (rps_data->delta_idx_minus1 + 1); /* 7-59 */
 	delta_rps = (1 - 2 * rps_data->delta_rps_sign) *
 		   (rps_data->abs_delta_rps_minus1 + 1); /* 7-60 */
Signature · 1 line
2.53.0
Reviewed-by on v2 3/3
#10 lore raw

Re: [PATCH v2 3/3] media: rkvdec: hevc: guard INTER_REF_PIC_SET_PRED index underflow

Le mercredi 27 mai 2026 à 15:47 -0400, Michael Bommarito a écrit :

Quoted 10 lines

st_ref_pic_set_prediction() computes the reference RPS index as st_rps_idx - (delta_idx_minus1 + 1) per HEVC spec equation 7-59. Both operands are u8, so when delta_idx_minus1 + 1 exceeds the current index the subtraction wraps and the subsequent array access at calculated_rps_st_sets[ref_rps_idx] reads far out of bounds.

A userspace V4L2 client that can open the RKVDEC m2m decoder can submit an EXT_SPS_ST_RPS control with INTER_REF_PIC_SET_PRED set and delta_idx_minus1 crafted to trigger the underflow.

Quoted 7 lines

Reject the entry early when the reference index would underflow.

Fixes c9a59dc2acc7 ("media: rkvdec: Add HEVC support for the VDPU381 variant")
Assisted-by Claude:claude-opus-4-7
Quoted 18 lines

---
 drivers/media/platform/rockchip/rkvdec/rkvdec-hevc-common.c | 3 +++

 1 file changed, 3 insertions(+)
drivers/media/platform/rockchip/rkvdec/rkvdec-hevc-common.c +0 -0
index 3119f3bc9f98b..898d1ce74f38a 100644
@@ -268,6 +268,9 @@ static void st_ref_pic_set_prediction(struct rkvdec_hevc_run *run, int idx,
  	int i, j;
  	int dPoc;

drivers/media/platform/rockchip/rkvdec/rkvdec-hevc-common.c +3 -0
+	if ((unsigned int)rps_data->delta_idx_minus1 + 1 > idx)
+		return;
+

  ref_rps_idx = st_rps_idx - (rps_data->delta_idx_minus1 + 1); /* 7-59 */ delta_rps = (1 - 2 * rps_data->delta_rps_sign) * (rps_data->abs_delta_rps_minus1 + 1); /* 7-60 */