Skip to content

Commit b45193c

Browse files
oleremmarckleinebudde
authored andcommitted
can: j1939: j1939_tp_tx_dat_new(): fix out-of-bounds memory access
In the j1939_tp_tx_dat_new() function, an out-of-bounds memory access could occur during the memcpy() operation if the size of skb->cb is larger than the size of struct j1939_sk_buff_cb. This is because the memcpy() operation uses the size of skb->cb, leading to a read beyond the struct j1939_sk_buff_cb. Updated the memcpy() operation to use the size of struct j1939_sk_buff_cb instead of the size of skb->cb. This ensures that the memcpy() operation only reads the memory within the bounds of struct j1939_sk_buff_cb, preventing out-of-bounds memory access. Additionally, add a BUILD_BUG_ON() to check that the size of skb->cb is greater than or equal to the size of struct j1939_sk_buff_cb. This ensures that the skb->cb buffer is large enough to hold the j1939_sk_buff_cb structure. Fixes: 9d71dd0 ("can: add support of SAE J1939 protocol") Reported-by: Shuangpeng Bai <[email protected]> Tested-by: Shuangpeng Bai <[email protected]> Signed-off-by: Oleksij Rempel <[email protected]> Link: https://groups.google.com/g/syzkaller/c/G_LL-C3plRs/m/-8xCi6dCAgAJ Link: https://lore.kernel.org/all/[email protected] Cc: [email protected] [mkl: rephrase commit message] Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent 3ce9345 commit b45193c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

net/can/j1939/transport.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,10 @@ sk_buff *j1939_tp_tx_dat_new(struct j1939_priv *priv,
604604
/* reserve CAN header */
605605
skb_reserve(skb, offsetof(struct can_frame, data));
606606

607-
memcpy(skb->cb, re_skcb, sizeof(skb->cb));
607+
/* skb->cb must be large enough to hold a j1939_sk_buff_cb structure */
608+
BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*re_skcb));
609+
610+
memcpy(skb->cb, re_skcb, sizeof(*re_skcb));
608611
skcb = j1939_skb_to_cb(skb);
609612
if (swap_src_dst)
610613
j1939_skbcb_swap(skcb);

0 commit comments

Comments
 (0)