Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp217442rdb; Fri, 5 Jan 2024 07:40:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IF4Csv425vZr/pdMttiQd/eh3oIXoCYVWfAlSKvJrYYhG0CLJ3yVWBe6DWNIV60DzDeNl0J X-Received: by 2002:a05:620a:1d9b:b0:781:7073:2047 with SMTP id pj27-20020a05620a1d9b00b0078170732047mr2234232qkn.72.1704469241842; Fri, 05 Jan 2024 07:40:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704469241; cv=none; d=google.com; s=arc-20160816; b=MUzTKdvPshfpx6nSszGkzh5K4yvNQJXfgE6uLo0ZVWtthzRjBnz/ndenU2F8OGQ5mv eeWCYjYyjMUv7aNKavicnlSBd2f7GBDUkxF/mOk+Pb+P+ZyGZ4OgH0g8ghPUclevvus5 MGwKXOHVhVFhBtqoohKuVFsrvgOR/81+uNEHP3qxeWThtnU0TtOsoOjszSc/e4+h+9C5 +IIWlWgzIumeRrVFqpsl2vhc+xb/ccrBD7o4Zw+k8nsnQoGyFRJ1/5/2Uz3tRz/7tRqP p+gZJ8N8BR8Av1DUp4L06u1MMpbBt2T9piYywzLgJKyPyXlQVlD2hhAoleuZ7fHVRfVJ VyhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=ydIbLEmZowGG9B8TAW5+HEhwyDa/x9zAWSy2CKRslyE=; fh=i+NhMAH/JI0dqVM3qNZ5q7A4njsrSeYwUdGWwz+saP0=; b=ki5/axPT6IN1BBnn7YmGygAHfXpCpYcMoV+idLgA1n/aRRy9VyVzyZI2YVf5Jv9SaJ +y0U9P/zMDVKTfRvzZDOfhxGaRjyPwpBv7imclrT6YDIdrqo4tkqvZaW6BgvupIz6Sn6 6l8IyIf0QUKdXQK0AV/4R8UYDCd2K0RgBj1kqowucZi9zjRde/WOet7+Cn4cfe3I12ps GKEKZNdto/K4Ybk24OaZSi89/9M26vZHUMhb/eHcIKHb+hRSfrCqNSoymqi2LELCSG5Y e5717fMA3z6FQ2gFGzX8Pd8ts+792uOgaVY421BLiJUbh4eIaNLP2ZifFoRBJzTJqua5 BDmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Rjv8vLSp; spf=pass (google.com: domain of linux-kernel+bounces-18004-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18004-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p13-20020a05620a22ad00b00781bac0c0b3si1812973qkh.396.2024.01.05.07.40.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 07:40:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18004-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Rjv8vLSp; spf=pass (google.com: domain of linux-kernel+bounces-18004-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18004-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 890D11C22DF9 for ; Fri, 5 Jan 2024 15:40:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5A6C72E65A; Fri, 5 Jan 2024 15:40:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Rjv8vLSp" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8CA22E634; Fri, 5 Jan 2024 15:40:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2cce70ad1a3so21547821fa.1; Fri, 05 Jan 2024 07:40:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704469229; x=1705074029; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ydIbLEmZowGG9B8TAW5+HEhwyDa/x9zAWSy2CKRslyE=; b=Rjv8vLSp7rEz6iOA8yWjFOjKSWUQ0XX6jtC9Du7GrOGZOK3Pgk2YrwxL9RzW2U9jdn 9kC2tDWZW4Lw7WAcOm7c6lsmcC2Gb0Nu6Gkr/klFzPaFAFbB2GKkbl7/oLn8kz1gYwgr Ms0cx42sKNwqM/y/S19QxzXsoJduGL7pQu33IVVYB82PXdAKCLVLPYK2bqlbPGArsdwy wgnIDcNqf5z0kzA6iCa3tDrY28S15fw41p6RLeXflMizI53EYZGPMyrlCSUQw0ltVEog IMJZewRjtjx1GF626FF8ObaAzLfu8YdoHhOCzuYwIOhSGvlbArbgH1tf17DU+eMMrwyE TUpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704469229; x=1705074029; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ydIbLEmZowGG9B8TAW5+HEhwyDa/x9zAWSy2CKRslyE=; b=U5F8769P20fH2NEpj5ESBLx8pyzwWFZGdhEXnnnd12LL4YIjzydhyen1JHtqbJk1OJ qWwoadkm/nM7zMep/A8qw0xFrJJhcXWOQb0wvYeK1mPpBcdy5Q3z7ptWf3Kb3OZulX6C ZvGRDwhXukAbEXV9W/ocPNoP6kIEIlAnAXy16rKEaC86troO7kwYoIS+T25rry51NBV0 vI4m1yjNPZ08Q1AXMnUtNBOg1nP6AAgF00mDJ89h6cTViL4flp5TEbsn0UHIocAW5WDy JuizaYIdtV+twv63ySQsU10dwO666CQyp+PVD/NVJUIpX+wsBBn44ptms8x8rhVoRmhW DUQA== X-Gm-Message-State: AOJu0Yw75iZtYBrGos8IFWDLSVxcFnZxZnMMH6qV6icFKufCoY0tEWih sniRYeeh9kX5t03XuODORf9J8gxCLJxFT0vx0EA= X-Received: by 2002:a2e:9f53:0:b0:2cc:df53:5322 with SMTP id v19-20020a2e9f53000000b002ccdf535322mr1100079ljk.61.1704469228656; Fri, 05 Jan 2024 07:40:28 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240104115453.1.Iaa08c695d3dcf819910ea723c3eb502935638172@changeid> <63d2b52d-8f0b-4456-896c-ecdaf835c65a@wanadoo.fr> In-Reply-To: From: Luiz Augusto von Dentz Date: Fri, 5 Jan 2024 10:40:15 -0500 Message-ID: Subject: Re: [PATCH] Bluetooth: Avoid potential use-after-free in hci_error_reset To: Christophe JAILLET Cc: yinghsu@chromium.org, chromeos-bluetooth-upstreaming@chromium.org, linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, On Fri, Jan 5, 2024 at 10:36=E2=80=AFAM Luiz Augusto von Dentz wrote: > > Hi Christophe, > > On Fri, Jan 5, 2024 at 2:15=E2=80=AFAM Christophe JAILLET > wrote: > > > > Le 04/01/2024 =C3=A0 12:56, Ying Hsu a =C3=A9crit : > > > While handling the HCI_EV_HARDWARE_ERROR event, if the underlying > > > BT controller is not responding, the GPIO reset mechanism would > > > free the hci_dev and lead to a use-after-free in hci_error_reset. > > > > > > Here's the call trace observed on a ChromeOS device with Intel AX201: > > > queue_work_on+0x3e/0x6c > > > __hci_cmd_sync_sk+0x2ee/0x4c0 [bluetooth ] > > > ? init_wait_entry+0x31/0x31 > > > __hci_cmd_sync+0x16/0x20 [bluetooth ] > > > hci_error_reset+0x4f/0xa4 [bluetooth ] > > > process_one_work+0x1d8/0x33f > > > worker_thread+0x21b/0x373 > > > kthread+0x13a/0x152 > > > ? pr_cont_work+0x54/0x54 > > > ? kthread_blkcg+0x31/0x31 > > > ret_from_fork+0x1f/0x30 > > > > > > This patch holds the reference count on the hci_dev while processing > > > a HCI_EV_HARDWARE_ERROR event to avoid potential crash. > > > > > > Signed-off-by: Ying Hsu > > > --- > > > Tested this commit on a chromebook with Intel BT controller. > > > > > > net/bluetooth/hci_core.c | 2 ++ > > > 1 file changed, 2 insertions(+) > > > > > > diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c > > > index 65601aa52e0d..a42417926028 100644 > > > --- a/net/bluetooth/hci_core.c > > > +++ b/net/bluetooth/hci_core.c > > > @@ -1049,6 +1049,7 @@ static void hci_error_reset(struct work_struct = *work) > > > { > > > struct hci_dev *hdev =3D container_of(work, struct hci_dev, err= or_reset); > > > > > > + hci_dev_hold(hdev); > > > BT_DBG("%s", hdev->name); > > > > > > if (hdev->hw_error) > > > @@ -1060,6 +1061,7 @@ static void hci_error_reset(struct work_struct = *work) > > > return; > > > > ^^^^^ > > Should we also call hci_dev_put() if we hit this return? > > Yep, I will fix that since Ive already pushed to bluetooth-next. Here is the proposed change: - if (hci_dev_do_close(hdev)) - return; + if (!hci_dev_do_close(hdev)) + hci_dev_do_open(hdev); - hci_dev_do_open(hdev); + hci_dev_put(hdev); > > > > CJ > > > > > > > > hci_dev_do_open(hdev); > > > + hci_dev_put(hdev); > > > } > > > > > > void hci_uuids_clear(struct hci_dev *hdev) > > > > > -- > Luiz Augusto von Dentz --=20 Luiz Augusto von Dentz