All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] net: can: j1939: Fix out-of-bounds memory access in j1939_tp_tx_dat_new
@ 2023-04-04  7:31 Oleksij Rempel
  2023-04-05  8:24 ` Marc Kleine-Budde
  0 siblings, 1 reply; 2+ messages in thread
From: Oleksij Rempel @ 2023-04-04  7:31 UTC (permalink / raw
  To: Robin van der Gracht, Oliver Hartkopp, Marc Kleine-Budde
  Cc: Oleksij Rempel, Shuangpeng Bai, kernel, linux-can, linux-kernel

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.

To address this issue, we have 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, a static_assert has been added 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: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Reported-by: Shuangpeng Bai <sjb7183@psu.edu>
Tested-by: Shuangpeng Bai <sjb7183@psu.edu>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 net/can/j1939/transport.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index fb92c3609e17..fe3df23a2595 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -604,7 +604,10 @@ sk_buff *j1939_tp_tx_dat_new(struct j1939_priv *priv,
 	/* reserve CAN header */
 	skb_reserve(skb, offsetof(struct can_frame, data));
 
-	memcpy(skb->cb, re_skcb, sizeof(skb->cb));
+	/* skb->cb must be large enough to hold a j1939_sk_buff_cb structure */
+	BUILD_BUG_ON(sizeof(skb->cb) < sizeof(*re_skcb));
+
+	memcpy(skb->cb, re_skcb, sizeof(*re_skcb));
 	skcb = j1939_skb_to_cb(skb);
 	if (swap_src_dst)
 		j1939_skbcb_swap(skcb);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v1] net: can: j1939: Fix out-of-bounds memory access in j1939_tp_tx_dat_new
  2023-04-04  7:31 [PATCH v1] net: can: j1939: Fix out-of-bounds memory access in j1939_tp_tx_dat_new Oleksij Rempel
@ 2023-04-05  8:24 ` Marc Kleine-Budde
  0 siblings, 0 replies; 2+ messages in thread
From: Marc Kleine-Budde @ 2023-04-05  8:24 UTC (permalink / raw
  To: Oleksij Rempel
  Cc: Robin van der Gracht, Oliver Hartkopp, Shuangpeng Bai, kernel,
	linux-can, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1439 bytes --]

On 04.04.2023 09:31:28, Oleksij Rempel wrote:
> 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.
> 
> To address this issue, we have 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, a static_assert has been added 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: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
> Reported-by: Shuangpeng Bai <sjb7183@psu.edu>
> Tested-by: Shuangpeng Bai <sjb7183@psu.edu>
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>

Applied.

regards,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-04-05  8:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-04  7:31 [PATCH v1] net: can: j1939: Fix out-of-bounds memory access in j1939_tp_tx_dat_new Oleksij Rempel
2023-04-05  8:24 ` Marc Kleine-Budde

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.