public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] can: gw: fix OOB heap access in cgw_csum_crc8_rel()
@ 2026-03-18 16:51 Oliver Hartkopp
  2026-03-18 16:51 ` [PATCH 2/2] can: isotp: fix tx.buf use-after-free in isotp_sendmsg() Oliver Hartkopp
  0 siblings, 1 reply; 16+ messages in thread
From: Oliver Hartkopp @ 2026-03-18 16:51 UTC (permalink / raw)
  To: linux-can; +Cc: Ali Norouzi, stable, Oliver Hartkopp

From: Ali Norouzi <ali.norouzi@keysight.com>

cgw_csum_crc8_rel() correctly computes bounds-safe indices via calc_idx():

    int from = calc_idx(crc8->from_idx, cf->len);
    int to   = calc_idx(crc8->to_idx,   cf->len);
    int res  = calc_idx(crc8->result_idx, cf->len);

    if (from < 0 || to < 0 || res < 0)
        return;

However, the loop and the result write then use the raw s8 fields directly
instead of the computed variables:

    for (i = crc8->from_idx; ...)        /* BUG: raw negative index */
    cf->data[crc8->result_idx] = ...;    /* BUG: raw negative index */

With from_idx = to_idx = result_idx = -64 on a 64-byte CAN FD frame,
calc_idx(-64, 64) = 0 so the guard passes, but the loop iterates with
i = -64, reading cf->data[-64], and the write goes to cf->data[-64].
This write might end up to 56 (7.0-rc) or 40 (<= 6.19) bytes before the
start of the canfd_frame on the heap.

The companion function cgw_csum_xor_rel() uses `from`/`to`/`res`
correctly throughout; fix cgw_csum_crc8_rel() to match.

Confirmed with KASAN on linux-7.0-rc2:
  BUG: KASAN: slab-out-of-bounds in cgw_csum_crc8_rel+0x515/0x5b0
  Read of size 1 at addr ffff8880076619c8 by task poc_cgw_oob/62

To configure the can-gw crc8 checksums CAP_NET_ADMIN is needed.

Fixes: 456a8a646b25 ("can: gw: add support for CAN FD frames")
Cc: stable@vger.kernel.org
Reported-by: Ali Norouzi <ali.norouzi@keysight.com>
Reviewed-by: Oliver Hartkopp <socketcan@hartkopp.net>
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Ali Norouzi <ali.norouzi@keysight.com>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 net/can/gw.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/net/can/gw.c b/net/can/gw.c
index 8ee4d67a07d3..0ec99f68aa45 100644
--- a/net/can/gw.c
+++ b/net/can/gw.c
@@ -373,14 +373,14 @@ static void cgw_csum_crc8_rel(struct canfd_frame *cf,
 
 	if (from < 0 || to < 0 || res < 0)
 		return;
 
 	if (from <= to) {
-		for (i = crc8->from_idx; i <= crc8->to_idx; i++)
+		for (i = from; i <= to; i++)
 			crc = crc8->crctab[crc ^ cf->data[i]];
 	} else {
-		for (i = crc8->from_idx; i >= crc8->to_idx; i--)
+		for (i = from; i >= to; i--)
 			crc = crc8->crctab[crc ^ cf->data[i]];
 	}
 
 	switch (crc8->profile) {
 	case CGW_CRC8PRF_1U8:
@@ -395,11 +395,11 @@ static void cgw_csum_crc8_rel(struct canfd_frame *cf,
 		crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
 				   (cf->can_id >> 8 & 0xFF)];
 		break;
 	}
 
-	cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
+	cf->data[res] = crc ^ crc8->final_xor_val;
 }
 
 static void cgw_csum_crc8_pos(struct canfd_frame *cf,
 			      struct cgw_csum_crc8 *crc8)
 {
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread
[parent not found: <20260318161914.15140-1-socketcan@hartkopp.net>]

end of thread, other threads:[~2026-03-23  9:36 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-18 16:51 [PATCH 1/2] can: gw: fix OOB heap access in cgw_csum_crc8_rel() Oliver Hartkopp
2026-03-18 16:51 ` [PATCH 2/2] can: isotp: fix tx.buf use-after-free in isotp_sendmsg() Oliver Hartkopp
2026-03-19 13:13   ` Oliver Hartkopp
2026-03-19 13:33     ` Marc Kleine-Budde
2026-03-19 13:34       ` Oliver Hartkopp
2026-03-19 13:23   ` Marc Kleine-Budde
2026-03-19 14:58   ` Marc Kleine-Budde
2026-03-19 15:10     ` Ali Norouzi
2026-03-19 15:16       ` Marc Kleine-Budde
2026-03-19 15:27       ` Oliver Hartkopp
2026-03-19 15:50         ` Marc Kleine-Budde
2026-03-23  8:47           ` Oliver Hartkopp
2026-03-23  9:35             ` Marc Kleine-Budde
2026-03-19 15:12     ` Oliver Hartkopp
2026-03-19 15:23       ` Marc Kleine-Budde
     [not found] <20260318161914.15140-1-socketcan@hartkopp.net>
2026-03-18 16:19 ` Oliver Hartkopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox