Skip to content

fix(cam_hal): guard cam_verify_jpeg_eoi() against buffer-underflow #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions driver/cam_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static cam_obj_t *cam_obj = NULL;
static const uint8_t JPEG_SOI_MARKER[] = {0xFF, 0xD8, 0xFF}; /* SOI = FF D8 FF */
#define JPEG_SOI_MARKER_LEN (3)
static const uint16_t JPEG_EOI_MARKER = 0xD9FF; /* EOI = FF D9 */
#define JPEG_EOI_MARKER_LEN (2)

static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)
{
Expand All @@ -66,10 +67,14 @@ static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)

static int cam_verify_jpeg_eoi(const uint8_t *inbuf, uint32_t length)
{
if (length < JPEG_EOI_MARKER_LEN) {
return -1;
}

int offset = -1;
uint8_t *dptr = (uint8_t *)inbuf + length - 2;
uint8_t *dptr = (uint8_t *)inbuf + length - JPEG_EOI_MARKER_LEN;
while (dptr > inbuf) {
if (memcmp(dptr, &JPEG_EOI_MARKER, 2) == 0) {
if (memcmp(dptr, &JPEG_EOI_MARKER, JPEG_EOI_MARKER_LEN) == 0) {
offset = dptr - inbuf;
//ESP_LOGW(TAG, "EOI: %d", length - (offset + 2));
return offset;
Expand Down