2022-08-05 15:21:57

by Fedor Pchelkin

[permalink] [raw]
Subject: [PATCH] can: j1939: fix memory leak of skbs

We need to drop skb references taken in j1939_session_skb_queue() when
destroying a session in j1939_session_destroy(). Otherwise those skbs
would be lost.

Link to Syzkaller info and repro: https://forge.ispras.ru/issues/11743.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Suggested-by: Oleksij Rempel <[email protected]>
Signed-off-by: Fedor Pchelkin <[email protected]>
Signed-off-by: Alexey Khoroshilov <[email protected]>
---
net/can/j1939/transport.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index 307ee1174a6e..d7d86c944d76 100644
--- a/net/can/j1939/transport.c
+++ b/net/can/j1939/transport.c
@@ -260,6 +260,8 @@ static void __j1939_session_drop(struct j1939_session *session)

static void j1939_session_destroy(struct j1939_session *session)
{
+ struct sk_buff *skb;
+
if (session->transmission) {
if (session->err)
j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ABORT);
@@ -274,7 +276,11 @@ static void j1939_session_destroy(struct j1939_session *session)
WARN_ON_ONCE(!list_empty(&session->sk_session_queue_entry));
WARN_ON_ONCE(!list_empty(&session->active_session_list_entry));

- skb_queue_purge(&session->skb_queue);
+ while ((skb = skb_dequeue(&session->skb_queue)) != NULL) {
+ /* drop ref taken in j1939_session_skb_queue() */
+ skb_unref(skb);
+ kfree_skb(skb);
+ }
__j1939_session_drop(session);
j1939_priv_put(session->priv);
kfree(session);
--
2.25.1



2022-08-05 16:30:03

by Oleksij Rempel

[permalink] [raw]
Subject: Re: [PATCH] can: j1939: fix memory leak of skbs

On Fri, Aug 05, 2022 at 06:02:16PM +0300, Fedor Pchelkin wrote:
> We need to drop skb references taken in j1939_session_skb_queue() when
> destroying a session in j1939_session_destroy(). Otherwise those skbs
> would be lost.
>
> Link to Syzkaller info and repro: https://forge.ispras.ru/issues/11743.
>
> Found by Linux Verification Center (linuxtesting.org) with Syzkaller.
>
> Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
> Suggested-by: Oleksij Rempel <[email protected]>
> Signed-off-by: Fedor Pchelkin <[email protected]>
> Signed-off-by: Alexey Khoroshilov <[email protected]>

Acked-by: Oleksij Rempel <[email protected]>

> ---
> net/can/j1939/transport.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index 307ee1174a6e..d7d86c944d76 100644
> --- a/net/can/j1939/transport.c
> +++ b/net/can/j1939/transport.c
> @@ -260,6 +260,8 @@ static void __j1939_session_drop(struct j1939_session *session)
>
> static void j1939_session_destroy(struct j1939_session *session)
> {
> + struct sk_buff *skb;
> +
> if (session->transmission) {
> if (session->err)
> j1939_sk_errqueue(session, J1939_ERRQUEUE_TX_ABORT);
> @@ -274,7 +276,11 @@ static void j1939_session_destroy(struct j1939_session *session)
> WARN_ON_ONCE(!list_empty(&session->sk_session_queue_entry));
> WARN_ON_ONCE(!list_empty(&session->active_session_list_entry));
>
> - skb_queue_purge(&session->skb_queue);
> + while ((skb = skb_dequeue(&session->skb_queue)) != NULL) {
> + /* drop ref taken in j1939_session_skb_queue() */
> + skb_unref(skb);
> + kfree_skb(skb);
> + }
> __j1939_session_drop(session);
> j1939_priv_put(session->priv);
> kfree(session);
> --
> 2.25.1
>
>
>

--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |

2022-08-08 08:07:55

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: [PATCH] can: j1939: fix memory leak of skbs

On 05.08.2022 18:02:16, Fedor Pchelkin wrote:
> We need to drop skb references taken in j1939_session_skb_queue() when
> destroying a session in j1939_session_destroy(). Otherwise those skbs
> would be lost.
>
> Link to Syzkaller info and repro: https://forge.ispras.ru/issues/11743.
>
> Found by Linux Verification Center (linuxtesting.org) with Syzkaller.
>
> Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
> Suggested-by: Oleksij Rempel <[email protected]>
> Signed-off-by: Fedor Pchelkin <[email protected]>
> Signed-off-by: Alexey Khoroshilov <[email protected]>

Added to can/master

Thanks,
Marc

--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung West/Dortmund | Phone: +49-231-2826-924 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |


Attachments:
(No filename) (922.00 B)
signature.asc (499.00 B)
Download all attachments