LZO write commands: `len - 6` underflows on short packets, and the decompressor's output limit comes from the packet
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
- Domain
- embedded-iot, security
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);
lenisunsigned int, and nothing checkslen >= 6first:commands_process_packetonly returns early forlen == 0(L199-L203). A payload of 1 to 5 bytes makeslen - 6wrap to about 4 GB, and thememcpyruns past the 512-byte packet buffer.lzo1x_decompress_safetakes*out_lenas 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 todata + 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 inCOMM_BM_WRITE_FLASH_LZOin the blocking thread (L2427-L2434, withsend_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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from vedderb/bldc
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
CAN_PACKET_FILL_RX_BUFFER(_LONG): a frame shorter than its header makes `len` negative before memcpyOpen
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
foc_sensor_mode from COMM_SET_MCCONF isn't range-checked, and the FOC ISR switch has no defaultOpen
Difficulty 3/5 1-2 days Newbie friendliness 58/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
deprecation:active impact:breaking-change
Difficulty 1/5 1-3 hours Newbie friendliness 80/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Cisco-Talos/clamav#1957 ·
Maintainers usually reply within 1 day
-
Read-only client can close a dead pane (remain-on-exit key)Possibly taken @lingyicute claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
bug mod: quantum info
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Qiskit/qiskit#17699 · 2 comments ·
Maintainers usually reply within 1 day