linux-hams.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: duoming@zju.edu.cn
To: Jakub Kicinski <kuba@kernel.org>
Cc: linux-hams@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, ralf@linux-mips.org,
	davem@davemloft.net, edumazet@google.com, pabeni@redhat.com
Subject: Re: [PATCH v4] net: rose: fix null-ptr-deref caused by rose_kill_by_neigh
Date: Sun, 3 Jul 2022 08:43:10 +0800 (GMT+08:00)	[thread overview]
Message-ID: <194120ff.22ed2.181c182e706.Coremail.duoming@zju.edu.cn> (raw)
In-Reply-To: <20220702120108.32985427@kernel.org>

Hello,

On Sat, 2 Jul 2022 12:01:08 -0700 Jakub Kicinski wrote:

> On Sat, 2 Jul 2022 15:23:57 +0800 (GMT+08:00) duoming@zju.edu.cn wrote:
> > > On Wed, 29 Jun 2022 18:49:41 +0800 Duoming Zhou wrote:  
> > > > When the link layer connection is broken, the rose->neighbour is
> > > > set to null. But rose->neighbour could be used by rose_connection()
> > > > and rose_release() later, because there is no synchronization among
> > > > them. As a result, the null-ptr-deref bugs will happen.
> > > > 
> > > > One of the null-ptr-deref bugs is shown below:
> > > > 
> > > >     (thread 1)                  |        (thread 2)
> > > >                                 |  rose_connect
> > > > rose_kill_by_neigh              |    lock_sock(sk)
> > > >   spin_lock_bh(&rose_list_lock) |    if (!rose->neighbour)
> > > >   rose->neighbour = NULL;//(1)  |
> > > >                                 |    rose->neighbour->use++;//(2)  
> > >   
> > > >  		if (rose->neighbour == neigh) {  
> > > 
> > > Why is it okay to perform this comparison without the socket lock,
> > > if we need a socket lock to clear it? Looks like rose_kill_by_neigh()
> > > is not guaranteed to clear all the uses of a neighbor.  
> > 
> > I am sorry, the comparision should also be protected with socket lock.
> > The rose_kill_by_neigh() only clear the neighbor that is passed as
> > parameter of rose_kill_by_neigh(). 
> 
> Don't think that's possible, you'd have to drop the neigh lock every
> time.

The neighbour is cleared in two situations.

(1) When the rose device is down, the rose_link_device_down() traverses
the rose_neigh_list and uses the rose_kill_by_neigh() to clear the
neighbors of the device.

void rose_link_device_down(struct net_device *dev)
{
	struct rose_neigh *rose_neigh;

	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
		if (rose_neigh->dev == dev) {
			rose_del_route_by_neigh(rose_neigh);
			rose_kill_by_neigh(rose_neigh);
		}
	}
}

https://elixir.bootlin.com/linux/v5.19-rc4/source/net/rose/rose_route.c#L839

(2) When the level 2 link has timed out, the rose_link_failed() calls rose_kill_by_neigh()
to clear the rose_neigh.

https://elixir.bootlin.com/linux/v5.19-rc4/source/net/rose/rose_route.c#L813

> > > > +			sock_hold(s);
> > > > +			spin_unlock_bh(&rose_list_lock);
> > > > +			lock_sock(s);
> > > >  			rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
> > > >  			rose->neighbour->use--;  
> > > 
> > > What protects the use counter?  
> > 
> > The use counter is protected by socket lock.
> 
> Which one, the neigh object can be shared by multiple sockets, no?

The sk_for_each() traverses the rose_list and uses the lock of the socket that is extracted
from the rose_list to protect the use counter.

diff --git a/net/rose/af_rose.c b/net/rose/af_rose.c
index bf2d986a6bc..6d5088b030a 100644
--- a/net/rose/af_rose.c
+++ b/net/rose/af_rose.c
@@ -165,14 +165,26 @@ void rose_kill_by_neigh(struct rose_neigh *neigh)
        struct sock *s;
 
        spin_lock_bh(&rose_list_lock);
+again:
        sk_for_each(s, &rose_list) {
                struct rose_sock *rose = rose_sk(s);
 
+               sock_hold(s);
+               spin_unlock_bh(&rose_list_lock);
+               lock_sock(s);
                if (rose->neighbour == neigh) {
                        rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
                        rose->neighbour->use--;
                        rose->neighbour = NULL;
+                       release_sock(s);
+                       sock_put(s);
+                       spin_lock_bh(&rose_list_lock);
+                       goto again;
                }
+               release_sock(s);
+               sock_put(s);
+               spin_lock_bh(&rose_list_lock);
+               goto again;
        }
        spin_unlock_bh(&rose_list_lock);
 }

Best regards,
Duoming Zhou

      reply	other threads:[~2022-07-03  0:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-29 10:49 [PATCH v4] net: rose: fix null-ptr-deref caused by rose_kill_by_neigh Duoming Zhou
2022-07-02  2:41 ` Jakub Kicinski
2022-07-02  7:23   ` duoming
2022-07-02 19:01     ` Jakub Kicinski
2022-07-03  0:43       ` 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=194120ff.22ed2.181c182e706.Coremail.duoming@zju.edu.cn \
    --to=duoming@zju.edu.cn \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-hams@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=ralf@linux-mips.org \
    /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).