Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tetsuo Handa" <penguin-kernel@I-love.SAKURA.ne.jp>,
	linux-can@vger.kernel.org
Cc: "Marc Kleine-Budde" <mkl@pengutronix.de>,
	"Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Vincent Mailhol" <mailhol@kernel.org>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>
Subject: Re: [PATCH v17] can: j1939: fix "struct j1939_ecu" refcount leak
Date: Sun, 06 Dec 2026 02:45:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-252079@kernel.org> (raw)
In-Reply-To: <9118bc1c-d410-4db1-a19c-c522c18c2b03@I-love.SAKURA.ne.jp>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] can: j1939: missing rcu_barrier() in j1939_module_exit() for
  call_rcu()
- [Medium] can: j1939: missing rcu_barrier() for __j1939_ecu_release()
  call_rcu()
--

commit 72f10982ed821fa0b2b5d890bf51c72e33be31e3
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>

can: j1939: fix "struct j1939_ecu" refcount leak

This commit makes j1939_ecu_map_locked() a no-op once priv->rx_kref
reaches zero and defers the body of __j1939_ecu_release() to an RCU
callback via call_rcu().

Link: https://lore.kernel.org/all/ffdcdd9a-408b-40a0-bbb6-652900766b26@I-love.SAKURA.ne.jp/2-repro.c [1]
Link: https://lore.kernel.org/all/aa93c245-399c-499b-a89e-dddefbf42fcb@I-love.SAKURA.ne.jp/4-repro.c [2]

> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index eb9a3fd847f42..7f4f854e30296 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -23,17 +23,24 @@
>   */
>  #define J1939_ECU_TIMER_RETRY_WARN 5
>  
> -static void __j1939_ecu_release(struct kref *kref)
> +static void __j1939_ecu_release_rcu(struct rcu_head *rcu)
>  {
> -	struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
> +	struct j1939_ecu *ecu = container_of(rcu, struct j1939_ecu, rcu);
>  	struct j1939_priv *priv = ecu->priv;
>  
> -	list_del(&ecu->list);
>  	netdev_put(priv->ndev, &ecu->priv_dev_tracker);
>  	kfree(ecu);
>  	j1939_priv_put(priv);
>  }
>  
> +static void __j1939_ecu_release(struct kref *kref)
> +{
> +	struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
> +
> +	list_del(&ecu->list);
> +	call_rcu(&ecu->rcu, __j1939_ecu_release_rcu);
> +}

[Severity: Medium]
Can unloading can-j1939 crash with a use-after-free while
__j1939_ecu_release_rcu() is still pending?

j1939_module_exit() only calls can_proto_unregister() and
unregister_netdevice_notifier(), neither of which waits for queued
call_rcu() callbacks to execute.  synchronize_rcu() only waits for RCU
read-side critical sections to finish, so the module text can be unmapped
before __j1939_ecu_release_rcu() runs on another CPU:

CPU0                                 CPU1
j1939_sk_release()
  j1939_local_ecu_put()
    call_rcu(..., __j1939_ecu_release_rcu)
  j1939_netdev_stop() /* sleeps */
                                     sock_put(sk)
                                       __sk_destruct()
                                         module_put(THIS_MODULE)
                                     rmmod can-j1939
                                       j1939_module_exit()
                                         can_proto_unregister()
                                           synchronize_rcu()
                                         unregister_netdevice_notifier()
                                       /* module text unmapped */
__j1939_ecu_release_rcu()  <-- use-after-free

Adding rcu_barrier() to j1939_module_exit() (as in bcm_module_exit() and
cgw_module_exit()) would drain pending callbacks before module unload.

Also, the commit message under Problem 3 states that synchronize_rcu() in
j1939_module_exit() can wait for __j1939_ecu_release_rcu() completion;
that requires rcu_barrier() rather than synchronize_rcu().

[ ... ]

> diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
> index 5ed4f4b592e36..9e1380becab78 100644
> --- a/net/can/j1939/j1939-priv.h
> +++ b/net/can/j1939/j1939-priv.h
> @@ -43,6 +43,8 @@ struct j1939_ecu {
>  
>  	/* count users, to help transport protocol decide for interaction */
>  	int nusers;
> +
> +	struct rcu_head rcu;
>  };

[Severity: Medium]
Does fixing Problem 2 need call_rcu() deferral and struct rcu_head rcu in
struct j1939_ecu at all?

j1939_ecu_timer_handler() is the only j1939_ecu_put() caller that holds
priv->lock without an independent priv reference.  Taking a reference with
j1939_priv_get(priv) before j1939_ecu_put(ecu) and releasing it after
write_unlock(&priv->lock) fixes Problem 2 synchronously.

Keeping call_rcu() widens the module-unload use-after-free window to
seconds on CONFIG_RCU_LAZY=y kernels unless j1939_module_exit() calls
rcu_barrier().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/9118bc1c-d410-4db1-a19c-c522c18c2b03@I-love.SAKURA.ne.jp?part=1

      reply	other threads:[~2026-12-06  2:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  2:27 [PATCH v17] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-12-06  2:45 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-252079@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=penguin-kernel@I-love.SAKURA.ne.jp \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=socketcan@hartkopp.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox