Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp3047338pxb; Sun, 29 Aug 2021 11:35:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzc/Ydbkk2VN7+BNrbDcEUAmh8QVmH8TLmhJ4ZPC0oiqMFYuCRO+s32floIGjBE7ix7uvSb X-Received: by 2002:a92:d12:: with SMTP id 18mr13076173iln.287.1630262157935; Sun, 29 Aug 2021 11:35:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630262157; cv=none; d=google.com; s=arc-20160816; b=nmVKIQIZwpboNCnijD6xsQD5CrHmlj98oY7Ft4vuTeU2lxb78ea+im6YwB2q9J8NI/ ifd2MSrolDqaplm5dtrSwK2Ur/SrdyqXgMyKfpZgLOj3fE4xAUMF4p8SXmEie3tvidPF xKwJas/Ffwa3ag2mbzGetzE0BUEcDZLB5SZyvoG7BvNIuzcEILCP0uaInzez2SQ3Posq 9oYrXNP1djfHgyJIGs/G/ho7OCXkvPDyDIfkL3tN910G1JZywpNLakRJm4cIXMJflQW1 hGXKPfmkmtlhnmDSKC0c1E0AgtpHElcgqmeOb68H4wN8OJkDVNlVyRMnWCEvVH46yyem 47MA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:from:subject:dkim-signature; bh=qQjfu8WxFIS1DldFoOXKxC6q+BF0Msctmm3ppp/j+9E=; b=O8jy5eQQfVjYKr5ztF/E+ReXGdB1WEOjEVBZKDjMswgNhfYqwE7rfTI69I7BpX3iRn rLE/XDTNRghXR9TIw9BB2cNqZauwzychsERC4Gikh9O58el+lYztaxZmKWrFSOpjd95V KjOAe9a8PDG3BaZntMGFNUM6lXb2nQkXQY8nYrsZGFp4JlY49cnZxOlGJkAsEOMr9YS3 SHAysQYsDZhb8fGU3xhAsH79u3efwFvwrOnX3FOH2WQtH5N9okNVu1ZXHSBkfyqy2XLt KbESXGOpv57tdA944yx8qaeLdW9D//HdD2KwLJIiLpe/Q0JwQgMGV/d7lT03cexpuMXz e9fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PdstMbCe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r20si12707622jad.61.2021.08.29.11.35.46; Sun, 29 Aug 2021 11:35:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PdstMbCe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233327AbhH2SfI (ORCPT + 99 others); Sun, 29 Aug 2021 14:35:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230010AbhH2SfI (ORCPT ); Sun, 29 Aug 2021 14:35:08 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB2DDC061575; Sun, 29 Aug 2021 11:34:15 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id v123so10373913pfb.11; Sun, 29 Aug 2021 11:34:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=qQjfu8WxFIS1DldFoOXKxC6q+BF0Msctmm3ppp/j+9E=; b=PdstMbCe4wjuo5XTSUGYKYi1BVmCnR99/SE8rnzloUFya9CUn0Q0UWnnUwkeTAtGlK KdVMdAW3K6iI30gAgQLiqxG0J4Lhrkk/+9e0GrYuANkTEyn9Ci0j6ARC9wv5zt+pglKD LmV3Yic5HXawqwvi0Se/b26JQTvqoYkdSAg4h/0HjN59st2FjO/rfp8SWEVganyz3bER Fztc3dXgYup9dN3VHhcLMnxK5HqciUpaqiKXh068wdyQu0rwSKZMjBGRco7iH4KMEFAj lJQSprDZAiMUKISUAAjfs087noBXsxCiJ6EMEbHEPRFORrjcDdPVJ3e1YPwP7sICu6+q 1eKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=qQjfu8WxFIS1DldFoOXKxC6q+BF0Msctmm3ppp/j+9E=; b=cx+a+Pfe3IsThiY7nyFIPzvt/6aCTji/lYSqYJ9U5qjYelqdQ55CeF/tu0+CJVswmN bBAy/tW+3AFIWflAIvn9d17kdBJRamzL+hXErAJfS+ev6dkwsoEEXadx4H7YMJ8mvM1w +fSiReiTvNtMOtt2CseOIV29D0/6VOAvtLg3qv8r9YA4Ea8iwKt8dH6NnS2RElXaWPsW +JbwYZx8UtN2SLQmtknweAXFxM0+F8YccbWI1IpVncLKtTh6qDzTYL7VlEx9qUIFgPwF Bbg0IM0fVPxDdmMom5YV0EtruoXaGo0Qnc8I3qIWLA1bTyvB96dFsJ8STBwRgS8bXHNJ +YAQ== X-Gm-Message-State: AOAM531msAPjGeuTyWM62xCkxFavan0ORQpmli80CQ8S1VKa8AKqsxZd 6wHInZ+xCb2tAgqI8O/D27CppTf385lsJt1v X-Received: by 2002:aa7:978d:0:b0:3f2:526b:f247 with SMTP id o13-20020aa7978d000000b003f2526bf247mr17938617pfp.68.1630262055191; Sun, 29 Aug 2021 11:34:15 -0700 (PDT) Received: from [192.168.1.237] ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id k12sm18478793pjg.6.2021.08.29.11.34.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Aug 2021 11:34:14 -0700 (PDT) Subject: Re: [syzbot] KASAN: use-after-free Write in sco_sock_timeout From: Desmond Cheong Zhi Xi To: Hillf Danton , syzbot Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, luiz.von.dentz@intel.com, syzkaller-bugs@googlegroups.com References: <000000000000b2725705ca78de29@google.com> <20210827011931.2323-1-hdanton@sina.com> <20210829082929.2897-1-hdanton@sina.com> <11a30bc1-574f-aa1a-21d6-b886de9fc981@gmail.com> Message-ID: <9b1afb1f-45d5-8664-eeb6-e8373d95cad3@gmail.com> Date: Mon, 30 Aug 2021 02:34:11 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <11a30bc1-574f-aa1a-21d6-b886de9fc981@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 >>>>> 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);