Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3063318pxj; Mon, 7 Jun 2021 00:58:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2seu8OsRCwtau/dmjueYTJSI0UDZA8gs7LaRYoyXsjZrmtpfaVWAgGDCZq0UzEy8QUK0W X-Received: by 2002:a17:906:35db:: with SMTP id p27mr16613339ejb.391.1623052712583; Mon, 07 Jun 2021 00:58:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623052712; cv=none; d=google.com; s=arc-20160816; b=FxtN1IShrSUXoQpq9BKNU0efjdsCBMDyQJ5HScNAZHBtXip1layMAt0g/fe+lptuak kM4uPYPYqQ1F8fk0Aa7lPq8ZjzUyhD1x2Q1lE4eCChdlGfuk5yWfPuaiHQhm5vz1piCi DXoqPaBKTbkeiyt3s6SkVDOcCk40Ir1rvJi/bLJTR3tF+X3/XAZIXzhEGmHvQ2cu+hPH 6s7TxnzJ9KMxxDoUOG2WWP9XbxPR7cNYpfBndnGvEBx8wM0pt4MeSAaLdaFlJQ7pUcs3 ZG04tJGU+MTr0a9oJCvNJmfHVFTenh+348C/fGN7JDbj6EgHlqiSzhKshAYQk7OSKxtL 95vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=SSNkJywH2C4K5/oxNcKXoHF6boenP8vV2UEy818ji7g=; b=Gl8ilwPFMaQXIW0yz+TsrOLNCtsVKObkRkmsYFt6iFhL/TYT3n2GhQqVprjVYQtBLI dNzeei5UCZq87Ttv78Wg2MZytdLxp+1iTbER4wFu+rI3F3XTaj0fJmjHnuIGz6BuP1gk Nc7vWD0HaUj84PtPTSpqu7hVxoEGjvYpn+5prf2Cud+THL+4JSa1wXwfXpp0JFJYw7la 147Cmv/bTkc3SIeFufL8hVEPyoH23W5GVr+LKvy7Xg8UDc7rdhuX7U3Oty1sBajY70WN f6GDThhlREo7dV13rxIePLeqUpPG1E7qlv2IQjcrM5zjED6EhAAeX5JQp1mfK39whm0y DJmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=r0TrbnG7; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b22si4927337ejl.199.2021.06.07.00.57.55; Mon, 07 Jun 2021 00:58:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-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=@linuxfoundation.org header.s=korg header.b=r0TrbnG7; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229545AbhFGH5Z (ORCPT + 99 others); Mon, 7 Jun 2021 03:57:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:55566 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229436AbhFGH5Z (ORCPT ); Mon, 7 Jun 2021 03:57:25 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E8E2E60238; Mon, 7 Jun 2021 07:55:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623052534; bh=qlaXBBljfuNy9WKbCWLsVZBe2CUbxsFeGMx1HeZlRao=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=r0TrbnG7uOmzox7WYNlVPtv2R0PIW2dzfzFoCoabl0Z/vTQV1kgLsyHchCLxNLcQI 4baEnybEh4BAZGia/iNRJL6FTBI6PRj415vAikhogurOMkyqAWafpp0HcGQN3zFxYJ C7gcq62LUzHRziw7Qd7xiJPtbmaNoLi8xVGHOjPQ= Date: Mon, 7 Jun 2021 09:55:31 +0200 From: Greg KH To: Hillf Danton Cc: Leon Romanovsky , SyzScope , davem@davemloft.net, johan.hedberg@gmail.com, kuba@kernel.org, linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, Luiz Augusto von Dentz , marcel@holtmann.org, netdev@vger.kernel.org, syzkaller-bugs@googlegroups.com Subject: Re: KASAN: use-after-free Read in hci_chan_del Message-ID: References: <000000000000adea7f05abeb19cf@google.com> <0f489a64-f080-2f89-6e4a-d066aeaea519@gmail.com> <20210606085004.12212-1-hdanton@sina.com> <20210607074828.3259-1-hdanton@sina.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210607074828.3259-1-hdanton@sina.com> Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org On Mon, Jun 07, 2021 at 03:48:28PM +0800, Hillf Danton wrote: > On Sun, 6 Jun 2021 11:54:22 +0200 Greg KH wrote: > >On Sun, Jun 06, 2021 at 04:50:04PM +0800, Hillf Danton wrote: > >> > >> To fix the uaf reported, add reference count to hci channel to track users. > >> Then only channels with zero users will be released. > >> > >> It is now only for thoughts. > >> > >> +++ x/include/net/bluetooth/hci_core.h > >> @@ -704,6 +704,7 @@ struct hci_chan { > >> struct sk_buff_head data_q; > >> unsigned int sent; > >> __u8 state; > >> + atomic_t ref; > > > >Please no, never use "raw" atomic variables. Especially for something > >like this, use a kref. > > Fair, thanks for taking a look at it. > > Spin with care for the race the added ref fails to cut. I do not understand what you mean here. > To ease review the full syzreport is also attached. > > To fix uaf, add user track to hci channel and we will only release channel if > its user hits zero. And a dryrun mechanism is also added to take care of the > race user track fails to cut. > > CPU0 CPU1 > ---- ---- > hci_chan_del l2cap_conn_del > chan->user = 0; > > if (chan->user != 0) > return; > synchronize_rcu(); > kfree(chan); > > hci_chan_del(); > > It is now only for thoughts. > > +++ x/include/net/bluetooth/hci_core.h > @@ -704,6 +704,10 @@ struct hci_chan { > struct sk_buff_head data_q; > unsigned int sent; > __u8 state; > + __u8 user; No. > + __u8 release; No please no. > + > +#define HCHAN_RELEASE_DRYRUN 1 > }; > > struct hci_conn_params { > +++ x/net/bluetooth/l2cap_core.c > @@ -1903,6 +1903,12 @@ static void l2cap_conn_del(struct hci_co > > mutex_unlock(&conn->chan_lock); > > + /* see comment in hci_chan_del() */ > + conn->hchan->release = HCHAN_RELEASE_DRYRUN; > + smp_wmb(); > + conn->hchan->user--; And the reason you are open-coding a kref is why??? Please again no. > + hci_chan_del(conn->hchan); > + conn->hchan->release = 0; > hci_chan_del(conn->hchan); > > if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) > @@ -7716,6 +7722,8 @@ static struct l2cap_conn *l2cap_conn_add > kref_init(&conn->ref); > hcon->l2cap_data = conn; > conn->hcon = hci_conn_get(hcon); > + /* dec in l2cap_conn_del() */ > + hchan->user++; {sigh} No, there is a reason we wrote kref many _decades_ ago. Please use it, your original attempt with an atomic was just fine, just use the proper data structures the kernel provides you as this is obviously a reference counted object. thanks, greg k-h