linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Desmond Cheong Zhi Xi <desmondcheongzx@gmail.com>
To: Hillf Danton <hdanton@sina.com>,
	syzbot <syzbot+2bef95d3ab4daa10155b@syzkaller.appspotmail.com>
Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
	luiz.von.dentz@intel.com, syzkaller-bugs@googlegroups.com
Subject: Re: [syzbot] KASAN: use-after-free Write in sco_sock_timeout
Date: Mon, 30 Aug 2021 02:34:11 +0800	[thread overview]
Message-ID: <9b1afb1f-45d5-8664-eeb6-e8373d95cad3@gmail.com> (raw)
In-Reply-To: <11a30bc1-574f-aa1a-21d6-b886de9fc981@gmail.com>

On 29/8/21 10:53 pm, Desmond Cheong Zhi Xi wrote:
> On 29/8/21 4:29 pm, Hillf Danton wrote:
>> On Fri, 27 Aug 2021 15:58:34 +0800 Desmond Cheong Zhi Xi wrote:
>>> On 27/8/21 9:19 am, Hillf Danton wrote:
>>>> On Thu, 26 Aug 2021 09:29:24 -0700
>>>>> syzbot found the following issue on:
>>>>>
>>>>> HEAD commit:    e3f30ab28ac8 Merge branch 'pktgen-samples-next'
>>>>> git tree:       net-next
>>>>> console output: 
>>>>> https://syzkaller.appspot.com/x/log.txt?x=13249c96300000
>>>>> kernel config:  
>>>>> https://syzkaller.appspot.com/x/.config?x=ef482942966bf763
>>>>> dashboard link: 
>>>>> https://syzkaller.appspot.com/bug?extid=2bef95d3ab4daa10155b
>>>>> compiler:       gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU 
>>>>> Binutils for Debian) 2.35.1
>>>>> syz repro:      
>>>>> https://syzkaller.appspot.com/x/repro.syz?x=16a29ea9300000
>>>>>
>>>>> The issue was bisected to:
>>>>>
>>>>> commit e1dee2c1de2b4dd00eb44004a4bda6326ed07b59
>>>>> Author: Desmond Cheong Zhi Xi <desmondcheongzx@gmail.com>
>>>>> Date:   Tue Aug 10 04:14:10 2021 +0000
>>>>>
>>>>>       Bluetooth: fix repeated calls to sco_sock_kill
>>
>> To fix the uaf, grab another hold to sock to make the timeout work safe.
>>
>> #syz test: 
>> git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git 
>> e3f30ab28ac8
>>
>> --- a/net/bluetooth/sco.c
>> +++ b/net/bluetooth/sco.c
>> @@ -190,15 +190,14 @@ static void sco_conn_del(struct hci_conn
>>       sco_conn_unlock(conn);
>>       if (sk) {
>> -        sock_hold(sk);
>>           lock_sock(sk);
>>           sco_sock_clear_timer(sk);
>>           sco_chan_del(sk, err);
>>           release_sock(sk);
>> -        sock_put(sk);
>>           /* Ensure no more work items will run before freeing conn. */
>>           cancel_delayed_work_sync(&conn->timeout_work);
>> +        sock_put(sk);
> 
> Hi Hillf,
> 
> Saw that this passed the reproducer. But on closer inspection, I think 
> what's happening is that sco_conn_del is never run.
> 
> So the extra sock_hold prevents a UAF, but that's because now the 
> reference count never goes to 0. In my opinion, something closer to your 
> previous proposal (+ also addressing other calls to __sco_sock_close) 
> where we call cancel_delayed_work_sync after the channel is deleted 
> would address the root cause better.
> 
> Just my two cents.
> 

Ok I went back to make a more thorough audit. Even without calling
cancel_delayed_work_sync, sco_sock_timeout should not cause a UAF.

I believe the real issue is that we can allocate a connection twice in
sco_connect. This means that the first connection gets lost and we're
unable to clean it up properly.

Thoughts on this?

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git e3f30ab28ac8

--- a/net/bluetooth/sco.c
+++ b/net/bluetooth/sco.c
@@ -578,9 +578,6 @@ static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen
  	    addr->sa_family != AF_BLUETOOTH)
  		return -EINVAL;
  
-	if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
-		return -EBADFD;
-
  	if (sk->sk_type != SOCK_SEQPACKET)
  		return -EINVAL;
  
@@ -591,6 +588,13 @@ static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen
  
  	lock_sock(sk);
  
+	if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
+		hci_dev_unlock(hdev);
+		hci_dev_put(hdev);
+		err = -EBADFD;
+		goto done;
+	}
+
  	/* Set destination address and psm */
  	bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
  

  reply	other threads:[~2021-08-29 18:34 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-26 16:29 [syzbot] KASAN: use-after-free Write in sco_sock_timeout syzbot
     [not found] ` <20210827011931.2323-1-hdanton@sina.com>
2021-08-27  7:58   ` Desmond Cheong Zhi Xi
     [not found]   ` <20210829082929.2897-1-hdanton@sina.com>
2021-08-29  8:51     ` syzbot
2021-08-29 14:53     ` Desmond Cheong Zhi Xi
2021-08-29 18:34       ` Desmond Cheong Zhi Xi [this message]
2021-08-30  3:50         ` syzbot
     [not found]       ` <20210830001538.1980-1-hdanton@sina.com>
2021-08-30  4:41         ` Desmond Cheong Zhi Xi
2022-02-23 16:15 ` syzbot
     [not found] <20220227025605.2681-1-hdanton@sina.com>
2022-02-27  3:07 ` syzbot
     [not found] <20220227032747.2752-1-hdanton@sina.com>
2022-02-27  3:39 ` syzbot
     [not found] <20220227060827.2844-1-hdanton@sina.com>
2022-02-27  6:20 ` syzbot
     [not found] <20220227074046.2963-1-hdanton@sina.com>
2022-02-27  7:53 ` syzbot

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=9b1afb1f-45d5-8664-eeb6-e8373d95cad3@gmail.com \
    --to=desmondcheongzx@gmail.com \
    --cc=hdanton@sina.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luiz.von.dentz@intel.com \
    --cc=syzbot+2bef95d3ab4daa10155b@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /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).