linux-hams.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: duoming@zju.edu.cn
To: Jakub Kicinski <kuba@kernel.org>
Cc: Thomas Osterried <thomas@osterried.de>,
	"David S. Miller" <davem@davemloft.net>,
	Paolo Abeni <pabeni@redhat.com>,
	linux-hams@vger.kernel.org
Subject: Re: [PATCH net] ax25: Fix ax25 session cleanup problem in ax25_release
Date: Sat, 28 May 2022 21:29:20 +0800 (GMT+08:00)	[thread overview]
Message-ID: <20a99b3a.3a4c0.1810adb6a1a.Coremail.duoming@zju.edu.cn> (raw)
In-Reply-To: <20220527173312.71122dbe@kernel.org>

Hello,

On Fri, 27 May 2022 13:29:30 +0200 Thomas Osterried wrote:

> > > I Tested several cases: this patch works as expected.  
> > If you agree, that no concurrent process is able to re-use this ax25_cb,
> > and because all timers are stoppeed, the cleanup with  ax25_cb_del(s);
> > should be safe.
> > 
> > 
> > My successfull test was this:
> > 
> > diff --git a/net/ax25/af_ax25.c b/net/ax25/af_ax25.c
> > index 363d47f94532..de417b974c07 100644
> > --- a/net/ax25/af_ax25.c
> > +++ b/net/ax25/af_ax25.c
> > @@ -91,6 +92,7 @@ static void ax25_kill_by_device(struct net_device *dev)
> >                                 spin_unlock_bh(&ax25_list_lock);
> >                                 ax25_disconnect(s, ENETUNREACH);
> >                                 s->ax25_dev = NULL;
> > +                               ax25_cb_del(s);
> >                                 spin_lock_bh(&ax25_list_lock);
> >                                 goto again;
> >                         }

The above code also exists a race condition:

      (Thread 1)                      |      (Thread 2)
ax25_kill_by_device()                 | ax25_rcv()
  ...                                 |   ...
  if (!sk) {                          |     
    ax25_cb_del()                     |   ax25_destroy_socket()
      if (!hlist_unhashed(..))        |     ax25_cb_del()
        ...                           |       if (!hlist_unhashed(..))
        hlist_del_init()              |      
        ax25_cb_put(ax25)             |         ...
                                      |         ax25_cb_put(ax25)
                                      |         ...

If we use ax25_cb_del() to delete ax25 node from the hlist, the ax25_cb_put() should only be
called once. But the above picture shows that ax25_cb_put() is called twice and the refcount
of the same ax25_cb is decreased twice. As a result, it will cause "refcount_t: underflow".

The following is a solution that has been tested.

@@ -62,12 +62,12 @@ static void ax25_free_sock(struct sock *sk)
  */
 static void ax25_cb_del(ax25_cb *ax25)
 {
+       spin_lock_bh(&ax25_list_lock);
        if (!hlist_unhashed(&ax25->ax25_node)) {
-               spin_lock_bh(&ax25_list_lock);
                hlist_del_init(&ax25->ax25_node);
-               spin_unlock_bh(&ax25_list_lock);
                ax25_cb_put(ax25);
        }
+       spin_unlock_bh(&ax25_list_lock);
 }

Best regards,
Duoming Zhou

      reply	other threads:[~2022-05-28 13:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-25 11:28 [PATCH net] ax25: Fix ax25 session cleanup problem in ax25_release Duoming Zhou
2022-05-27  8:11 ` Thomas Osterried
2022-05-27 11:29   ` Thomas Osterried
2022-05-27 14:23     ` duoming
2022-05-28  0:33     ` Jakub Kicinski
2022-05-28 13:29       ` duoming [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=20a99b3a.3a4c0.1810adb6a1a.Coremail.duoming@zju.edu.cn \
    --to=duoming@zju.edu.cn \
    --cc=davem@davemloft.net \
    --cc=kuba@kernel.org \
    --cc=linux-hams@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=thomas@osterried.de \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).