From: Oleksij Rempel <o.rempel@pengutronix.de>
To: Fedor Pchelkin <pchelkin@ispras.ru>
Cc: Oleksij Rempel <linux@rempel-privat.de>,
Marc Kleine-Budde <mkl@pengutronix.de>,
kernel@pengutronix.de, Robin van der Gracht <robin@protonic.nl>,
Oliver Hartkopp <socketcan@hartkopp.net>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Kurt Van Dijck <dev.kurt@vandijck-laurijssen.be>,
linux-can@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org,
Alexey Khoroshilov <khoroshilov@ispras.ru>,
lvc-project@linuxtesting.org
Subject: Re: [PATCH 2/2] can: j1939: avoid possible use-after-free when j1939_can_rx_register fails
Date: Sat, 27 May 2023 07:57:04 +0200 [thread overview]
Message-ID: <20230527055704.GA17237@pengutronix.de> (raw)
In-Reply-To: <20230526185026.33pcjvoyq5jzlnxk@fpc>
Hi Fedor,
On Fri, May 26, 2023 at 09:50:26PM +0300, Fedor Pchelkin wrote:
> Hi Oleksij,
>
> thanks for the reply!
>
> On Fri, May 26, 2023 at 08:15:00PM +0200, Oleksij Rempel wrote:
> > Hi Fedor,
> >
> > On Fri, May 26, 2023 at 08:19:10PM +0300, Fedor Pchelkin wrote:
> >
> >
> > Thank you for your investigation. How about this change?
> > --- a/net/can/j1939/main.c
> > +++ b/net/can/j1939/main.c
> > @@ -285,8 +285,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
> > */
> > kref_get(&priv_new->rx_kref);
> > spin_unlock(&j1939_netdev_lock);
> > - dev_put(ndev);
> > - kfree(priv);
> > + j1939_priv_put(priv);
>
> I don't think that's good because the priv which is directly freed here is
> still local to the thread, and parallel threads don't have any access to
> it. j1939_priv_create() has allocated a fresh priv and called dev_hold()
> so dev_put() and kfree() here are okay.
>
> > return priv_new;
> > }
> > j1939_priv_set(ndev, priv);
> > @@ -300,8 +299,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
> >
> > out_priv_put:
> > j1939_priv_set(ndev, NULL);
> > - dev_put(ndev);
> > - kfree(priv);
> > + j1939_priv_put(priv);
> >
> > return ERR_PTR(ret);
> > }
> >
> > If I see it correctly, the problem is kfree() which is called without respecting
> > the ref counting. If CPU1 has priv_new, refcounting is increased. The priv will
> > not be freed on this place.
>
> With your suggestion, I think it doesn't work correctly if
> j1939_can_rx_register() fails and we go to out_priv_put. The priv is kept
> but the parallel thread which may have already grabbed it thinks that
> j1939_can_rx_register() has succeeded when actually it hasn't succeed.
> Moreover, j1939_priv_set() makes it NULL on error path so that priv cannot
> be accessed from ndev.
>
> I also considered the alternatives where we don't have to serialize access
> to j1939_can_rx_register() and subsequently introduce mutex. But with
> current j1939_netdev_start() implementation I can't see how to fix the
> racy bug without it.
Ok, it make sense.
I'll try to do some testing next week. If i'll forget it, please feel
free to ping me.
Regards,
Oleksij
--
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 |
next prev parent reply other threads:[~2023-05-27 5:57 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-26 17:19 [PATCH 0/2] can: j1939: avoid possible use-after-free when j1939_can_rx_register fails Fedor Pchelkin
2023-05-26 17:19 ` [PATCH 1/2] can: j1939: change j1939_netdev_lock type to mutex Fedor Pchelkin
2023-06-02 12:33 ` Oleksij Rempel
2023-05-26 17:19 ` [PATCH 2/2] can: j1939: avoid possible use-after-free when j1939_can_rx_register fails Fedor Pchelkin
2023-05-26 18:15 ` Oleksij Rempel
2023-05-26 18:50 ` Fedor Pchelkin
2023-05-27 5:57 ` Oleksij Rempel [this message]
2023-05-27 10:05 ` Fedor Pchelkin
2023-06-02 12:35 ` Oleksij Rempel
2023-06-02 16:06 ` Fedor Pchelkin
2023-06-05 6:37 ` [PATCH 0/2] " Marc Kleine-Budde
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=20230527055704.GA17237@pengutronix.de \
--to=o.rempel@pengutronix.de \
--cc=davem@davemloft.net \
--cc=dev.kurt@vandijck-laurijssen.be \
--cc=edumazet@google.com \
--cc=kernel@pengutronix.de \
--cc=khoroshilov@ispras.ru \
--cc=kuba@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rempel-privat.de \
--cc=lvc-project@linuxtesting.org \
--cc=mkl@pengutronix.de \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pchelkin@ispras.ru \
--cc=robin@protonic.nl \
--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;
as well as URLs for NNTP newsgroup(s).