Skip to content

Commit 577ee9c

Browse files
committed
Fix heap buffer overflow in libopenapv encoder
Add bounds checking to BSW_FLUSH_4BYTE and BSW_FLUSH_8BYTE macros in oapv_vlc.c to prevent writing past the end of the bitstream buffer during VLC encoding. Add bounds checking in enc_frame in oapv.c to ensure the cumulative tile bitstream size does not exceed the target bitstream buffer end before copying tile bitstreams. These changes prevent heap buffer overflows in the encoder. Bug: 501452526 Test: Manual verification with PoC binaries on Cuttlefish Flag: EXEMPT CVE_FIX Change-Id: Ib40bc500096b6fda93e5802d97b306e4320ba6eb
1 parent dd4f9d5 commit 577ee9c

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

src/oapv.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ static int enc_frame(oapve_ctx_t *ctx, oapv_bs_t *bs)
11711171
/****************************************************/
11721172

11731173
for(int i = 0; i < ctx->num_tiles; i++) {
1174+
oapv_assert_gv(bs_tile_pos + ctx->tile[i].bs_size <= bs->end, ret, OAPV_ERR_OUT_OF_BS_BUF, ERR);
11741175
oapv_mcpy(bs_tile_pos, ctx->tile[i].bs_buf, ctx->tile[i].bs_size);
11751176
bs_tile_pos = bs_tile_pos + ctx->tile[i].bs_size;
11761177
ctx->fh.tile_size[i] = ctx->tile[i].bs_size - OAPV_TILE_SIZE_LEN;

src/oapv_vlc.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,31 @@
3737
#if ENABLE_ENCODER
3838
///////////////////////////////////////////////////////////////////////////////
3939
#define BSW_FLUSH_4BYTE(bs) { \
40-
*(bs)->cur++ = ((bs)->code >> 24) & 0xFF; \
41-
*(bs)->cur++ = ((bs)->code >> 16) & 0xFF; \
42-
*(bs)->cur++ = ((bs)->code >> 8) & 0xFF; \
43-
*(bs)->cur++ = ((bs)->code) & 0xFF; \
40+
if ((bs)->cur + 4 <= (bs)->end) { \
41+
*(bs)->cur++ = ((bs)->code >> 24) & 0xFF; \
42+
*(bs)->cur++ = ((bs)->code >> 16) & 0xFF; \
43+
*(bs)->cur++ = ((bs)->code >> 8) & 0xFF; \
44+
*(bs)->cur++ = ((bs)->code) & 0xFF; \
45+
} else { \
46+
(bs)->cur += 4; \
47+
} \
4448
(bs)->code = 0; \
4549
(bs)->leftbits = 32; \
4650
}
4751

4852
#define BSW_FLUSH_8BYTE(bs) { \
49-
*(bs)->cur++ = ((bs)->code >> 56) & 0xFF; \
50-
*(bs)->cur++ = ((bs)->code >> 48) & 0xFF; \
51-
*(bs)->cur++ = ((bs)->code >> 40) & 0xFF; \
52-
*(bs)->cur++ = ((bs)->code >> 32) & 0xFF; \
53-
*(bs)->cur++ = ((bs)->code >> 24) & 0xFF; \
54-
*(bs)->cur++ = ((bs)->code >> 16) & 0xFF; \
55-
*(bs)->cur++ = ((bs)->code >> 8) & 0xFF; \
56-
*(bs)->cur++ = ((bs)->code) & 0xFF; \
53+
if ((bs)->cur + 8 <= (bs)->end) { \
54+
*(bs)->cur++ = ((bs)->code >> 56) & 0xFF; \
55+
*(bs)->cur++ = ((bs)->code >> 48) & 0xFF; \
56+
*(bs)->cur++ = ((bs)->code >> 40) & 0xFF; \
57+
*(bs)->cur++ = ((bs)->code >> 32) & 0xFF; \
58+
*(bs)->cur++ = ((bs)->code >> 24) & 0xFF; \
59+
*(bs)->cur++ = ((bs)->code >> 16) & 0xFF; \
60+
*(bs)->cur++ = ((bs)->code >> 8) & 0xFF; \
61+
*(bs)->cur++ = ((bs)->code) & 0xFF; \
62+
} else { \
63+
(bs)->cur += 8; \
64+
} \
5765
(bs)->code = 0; \
5866
(bs)->leftbits = 64; \
5967
}

0 commit comments

Comments
 (0)