Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/da14531/da14531_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "memory/memory_shared.h"
#include "screen.h"
#include "ui/screen_stack.h"
#include "usb/class/usb_size.h"
#include "usb/usb_frame.h"
#include "usb/usb_packet.h"
#include "utils_ringbuffer.h"
#include <ui/components/confirm.h>
Expand Down Expand Up @@ -267,7 +269,10 @@ static void _hww_handler(struct da14531_protocol_frame* frame, struct ringbuffer
{
// util_log(" in: %s", util_dbg_hex(frame->payload, 64));
(void)queue;
ASSERT(frame->payload_length == 64);
if (frame->payload_length != USB_REPORT_SIZE) {
util_log("da14531: invalid hww payload length %u, dropped frame", frame->payload_length);
return;
}
usb_packet_process((USB_FRAME*)&frame->payload[0]);
}

Expand Down
15 changes: 13 additions & 2 deletions src/da14531/da14531_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,18 @@ static struct da14531_protocol_frame* _serial_link_in_poll(
// util_log("frame len so far: %d", self->frame_len);
} break;
case SERIAL_LINK_STATE_CHECK: {
// Frame format: [type:1][len:2][payload:len][crc:2].
// Guard against `frame_len - 5` underflow and out-of-bounds reads below.
if (self->frame_len < 5) {
util_log(
"da14531: ERROR, short frame len %u, dropped frame", (unsigned)self->frame_len);
self->state = SERIAL_LINK_STATE_READING;
self->frame_len = 0;
return NULL;
}

// bytes with index 1-2 are the length
uint16_t len = *((uint16_t*)&self->frame[1]);
uint16_t len = (uint16_t)self->frame[1] | ((uint16_t)self->frame[2] << 8);

if (len > self->frame_len - 5) {
util_log("da14531: ERROR, invalid len %d, dropped frame", len);
Expand All @@ -292,7 +302,8 @@ static struct da14531_protocol_frame* _serial_link_in_poll(

// CRC in frame
// bytes with index n-2 and n-1 are the crc
uint16_t crc_frame = *(uint16_t*)&self->frame[3 + len];
uint16_t crc_frame =
(uint16_t)self->frame[3 + len] | ((uint16_t)self->frame[3 + len + 1] << 8);

// Recalculate CRC
crc_t crc = crc_init();
Expand Down