Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Input: ims-pcu - fix heap-buffer-overflow in ims_pcu_process_data()

The `ims_pcu_process_data()` processes incoming URB data byte by byte.
However, it fails to check if the `read_pos` index exceeds
IMS_PCU_BUF_SIZE.

If a malicious USB device sends a packet larger than IMS_PCU_BUF_SIZE,
`read_pos` will increment indefinitely. Moreover, since `read_pos` is
located immediately after `read_buf`, the attacker can overwrite
`read_pos` itself to arbitrarily control the index.

This manipulated `read_pos` is subsequently used in
`ims_pcu_handle_response()` to copy data into `cmd_buf`, leading to a
heap buffer overflow.

Specifically, an attacker can overwrite the `cmd_done.wait.head` located
at offset 136 relative to `cmd_buf` in the `ims_pcu_handle_response()`.
Consequently, when the driver calls `complete(&pcu->cmd_done)`, it
triggers a control flow hijack by using the manipulated pointer.

Fix this by adding a bounds check for `read_pos` before writing to
`read_buf`. If the packet is too long, discard it, log a warning,
and reset the parser state.

Fixes: 628329d524743 ("Input: add IMS Passenger Control Unit driver")
Co-developed-by: Sanghoon Choi <csh0052@gmail.com>
Signed-off-by: Sanghoon Choi <csh0052@gmail.com>
Signed-off-by: Seungjin Bae <eeodqql09@gmail.com>
Link: https://patch.msgid.link/20251221211442.841549-2-eeodqql09@gmail.com
[dtor: factor out resetting packet state, reset checksum as well]
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Seungjin Bae and committed by
Dmitry Torokhov
875115b8 f7a78e84

+26 -6
+26 -6
drivers/input/misc/ims-pcu.c
··· 438 438 } 439 439 } 440 440 441 + static void ims_pcu_reset_packet(struct ims_pcu *pcu) 442 + { 443 + pcu->have_stx = true; 444 + pcu->have_dle = false; 445 + pcu->read_pos = 0; 446 + pcu->check_sum = 0; 447 + } 448 + 441 449 static void ims_pcu_process_data(struct ims_pcu *pcu, struct urb *urb) 442 450 { 443 451 int i; ··· 458 450 continue; 459 451 460 452 if (pcu->have_dle) { 453 + if (pcu->read_pos >= IMS_PCU_BUF_SIZE) { 454 + dev_warn(pcu->dev, 455 + "Packet too long (%d bytes), discarding\n", 456 + pcu->read_pos); 457 + ims_pcu_reset_packet(pcu); 458 + continue; 459 + } 460 + 461 461 pcu->have_dle = false; 462 462 pcu->read_buf[pcu->read_pos++] = data; 463 463 pcu->check_sum += data; ··· 478 462 dev_warn(pcu->dev, 479 463 "Unexpected STX at byte %d, discarding old data\n", 480 464 pcu->read_pos); 465 + ims_pcu_reset_packet(pcu); 481 466 pcu->have_stx = true; 482 - pcu->have_dle = false; 483 - pcu->read_pos = 0; 484 - pcu->check_sum = 0; 485 467 break; 486 468 487 469 case IMS_PCU_PROTOCOL_DLE: ··· 499 485 ims_pcu_handle_response(pcu); 500 486 } 501 487 502 - pcu->have_stx = false; 503 - pcu->have_dle = false; 504 - pcu->read_pos = 0; 488 + ims_pcu_reset_packet(pcu); 505 489 break; 506 490 507 491 default: 492 + if (pcu->read_pos >= IMS_PCU_BUF_SIZE) { 493 + dev_warn(pcu->dev, 494 + "Packet too long (%d bytes), discarding\n", 495 + pcu->read_pos); 496 + ims_pcu_reset_packet(pcu); 497 + continue; 498 + } 499 + 508 500 pcu->read_buf[pcu->read_pos++] = data; 509 501 pcu->check_sum += data; 510 502 break;