LZO write commands: `len - 6` underflows on short packets, and the decompressor's output limit comes from the packet

Open
#992 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c

Research direction

Read the three LZO write paths in comm/commands.c: COMM_WRITE_NEW_APP_DATA_LZO, COMM_WRITE_NEW_APP_DATA_ALL_CAN_LZO, and COMM_BM_WRITE_FLASH_LZO. Check the packet length before subtracting 6 and bound decompressed output to the available receive-buffer space; then verify that each path rejects short packets and oversized output. The issue does not name tests, so inspect the existing command tests or test entry points before choosing how to verify the changes.

Written by the indexing model from the issue text.

Description

Hi, I think the LZO variants of the firmware and flash write commands can write out of bounds when a packet is short or claims a large decompressed size.

The bug

In comm/commands.c (L354-L365):

case COMM_WRITE_NEW_APP_DATA_LZO:
case COMM_WRITE_NEW_APP_DATA: {
    if (packet_id == COMM_WRITE_NEW_APP_DATA_LZO) {
        uint8_t *send_buffer_global = mempools_get_packet_buffer();
        memcpy(send_buffer_global, data + 6, len - 6);
        int32_t ind = 4;
        lzo_uint decompressed_len = buffer_get_uint16(data, &ind);
        lzo1x_decompress_safe(send_buffer_global, len - 6, data + 4, &decompressed_len, NULL);
  • len is unsigned int, and nothing checks len >= 6 first: commands_process_packet only returns early for len == 0 (L199-L203). A payload of 1 to 5 bytes makes len - 6 wrap to about 4 GB, and the memcpy runs past the 512-byte packet buffer.
  • lzo1x_decompress_safe takes *out_len as the size of the output buffer (op_end = out + *out_len, util/lzo/minilzo.c L5953). Here it is a uint16 from the packet, while the output goes to data + 4, into the buffer that received the packet (PACKET_BUFFER_LEN, 520 bytes on USB/UART). A packet can declare up to 65535 bytes of output, and the decompressor will write that far.
  • The same code is in COMM_WRITE_NEW_APP_DATA_ALL_CAN_LZO (L333-L344) and in COMM_BM_WRITE_FLASH_LZO in the blocking thread (L2427-L2434, with send_buffer[512]).
What it costs

Anything that reaches commands_process_packet (USB, UART, BLE) can crash the controller with one short packet, or overwrite RAM after the receive buffer with a crafted one. VESC Tool sends well-formed packets, so this only matters for buggy or hostile senders. I found it by reading the code and have not reproduced it on hardware.

Where it came from

The lines are on master (0d4b313). git blame points to ff9679d (2022-09-18), which moved the buffer to mempools; the code is in 6.00.

Fix

Reject short packets, and bound the output by the space that is really there:

if (len < 6) {
    break;
}
/* … */
lzo_uint decompressed_len = buffer_get_uint16(data, &ind);
if (decompressed_len > PACKET_MAX_PL_LEN - 5) { // output starts at data + 4
    break;
}

The same two checks apply to the other two sites.

Dominant language
C
Stars
3.6k
Forks
2k
Avg merge
20h 29m
Merged PRs (30d)
12

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vedderb/bldc

All issues in vedderb/bldc

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.