Skip to contentSkip to conversation

media: coda: fix out-of-bounds read when parsing H.264 SPS profile

PATCH Applied hyunjungg 2 participants 2 messages Nov 6 – Nov 30

Subsystems (2)
lore
Share & tools
lore

hyunjungg

author
#0 lore raw

PATCH v2 1/1 media: coda: fix out-of-bounds read when parsing H.264 SPS profile

From: Hyunjung Ko <[email protected]>

coda_sps_parse_profile() scans the compressed OUTPUT-plane buffer supplied by userspace via VIDIOC_QBUF for an SPS NAL unit and then reads the profile_idc (buf[0]) and level_idc (buf[2]) that follow the NAL unit header byte. Neither the NAL scanning helper nor the final dereferences are bounded against the end of the payload:

- coda_find_nal_header() dereferences *buf++ before checking buf against end, so it reads one byte past the payload when it is entered with buf == end (e.g. a zero-length or start-code-terminated buffer).

- after the SPS NAL unit header byte is consumed, buf may point at or near the end of the payload, yet buf[0] and buf[2] are read with no check that three bytes are actually available, reading up to three bytes past the end of the payload (and past the allocation when bytesused == plane size).

A local user with access to /dev/videoN can trigger this by queueing a crafted H.264 bitstream buffer ending right after an SPS start code and NAL header byte, resulting in an out-of-bounds read (KASAN splat / possible crash).

Rewrite coda_find_nal_header() to bound-check before each read and never return a pointer at or past end, so the caller can always read the NAL unit header byte it returns. After consuming that byte, verify that profile_idc and level_idc are inside the payload before dereferencing them.

---
v2:
- Move the end check into coda_find_nal_header() so it never returns a pointer >= end, and drop the redundant "buf >= end" check from the caller's SPS-search loop (per Philipp Zabel).
- No functional change to the profile_idc/level_idc bounds check.

 drivers/media/platform/chips-media/coda/coda-h264.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/media/platform/chips-media/coda/coda-h264.c +12 -5
@@ -19,13 +19,13 @@
 {
 	u32 val = 0xffffffff;
 
-	do {
+	while (buf < end) {
 		val = val << 8 | *buf++;
-		if (buf >= end)
-			return NULL;
-	} while (val != 0x00000001);
+		if (val == 0x00000001)
+			return buf < end ? buf : NULL;
+	}
 
-	return buf;
+	return NULL;
 }
 
 int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb)
@@ -40,6 +40,13 @@
 			return -EINVAL;
 	} while ((*buf++ & 0x1f) != 0x7);
 
+	/*
+	 * profile_idc (buf[0]) and level_idc (buf[2]) must lie within the
+	 * payload before they can be dereferenced.
+	 */
+	if (end - buf < 3)
+		return -EINVAL;
+
 	ctx->params.h264_profile_idc = buf[0];
 	ctx->params.h264_level_idc = buf[2];
 
#1 lore raw
Quoted 6 lines · hyunjungg

coda_sps_parse_profile() scans the compressed OUTPUT-plane buffer supplied by userspace via VIDIOC_QBUF for an SPS NAL unit and then reads the profile_idc (buf[0]) and level_idc (buf[2]) that follow the NAL unit header byte. Neither the NAL scanning helper nor the final dereferences are bounded against the end of the payload:

Quoted 6 lines

- coda_find_nal_header() dereferences *buf++ before checking buf against end, so it reads one byte past the payload when it is entered with buf == end (e.g. a zero-length or start-code-terminated buffer).

[...]

Applied, thanks!

[1/1] media: coda: fix out-of-bounds read when parsing H.264 SPS profile

      commit: ec7f6a3a88b57e7c97b0d6ae06c0fbdffc8e30a0

Best regards,

Signature · 1 line
Nicolas Dufresne <[email protected]>