Re: [PATCH net v3 1/1] can: can_skb_set_owner(): fix ref counting if socket was closed before setting skb ownership

From: Marc Kleine-Budde
Date: Wed Feb 24 2021 - 16:15:33 EST


On 24.02.2021 21:32:29, Oliver Hartkopp wrote:
> > > To fix this problem, only set skb ownership to sockets which have still
> > > a ref count > 0.
> > >
> > > Cc: Oliver Hartkopp <socketcan@xxxxxxxxxxxx>
> > > Cc: Andre Naujoks <nautsch2@xxxxxxxxx>
> > > Suggested-by: Eric Dumazet <edumazet@xxxxxxxxxx>
> > > Fixes: 0ae89beb283a ("can: add destructor for self generated skbs")
> > > Signed-off-by: Oleksij Rempel <o.rempel@xxxxxxxxxxxxxx>
> >
> > SGTM
> >
> > Reviewed-by: Eric Dumazet <edumazet@xxxxxxxxxx>
> >
> > > ---
> > > include/linux/can/skb.h | 3 +--
> > > 1 file changed, 1 insertion(+), 2 deletions(-)
> > >
> > > diff --git a/include/linux/can/skb.h b/include/linux/can/skb.h
> > > index 685f34cfba20..655f33aa99e3 100644
> > > --- a/include/linux/can/skb.h
> > > +++ b/include/linux/can/skb.h
> > > @@ -65,8 +65,7 @@ static inline void can_skb_reserve(struct sk_buff *skb)
> > >
> > > static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
> > > {
> > > - if (sk) {
> > > - sock_hold(sk);
>
> Although the commit message gives a comprehensive reason for this patch: Can
> you please add some comment here as I do not think the use of
> refcount_inc_not_zero() makes clear what is checked here.

Good point. What about:

If the socket has already been closed by user space, the refcount may
already be 0 (and the socket will be freed after the last TX skb has
been freed). So only increase socket refcount if the refcount is > 0.

regards
Marc

P.S.: Have you had time to look at my ISOTOP RFC patch?

--
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 |

Attachment: signature.asc
Description: PGP signature