Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp770333pxb; Wed, 27 Jan 2021 22:43:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJyeKw3YKO3bUzo98IFdQ1QwhRoSwA9NL+hmiVrWduxtYS8ibNbIgqDhfMu8A8mgca7+j+D+ X-Received: by 2002:a17:907:16a2:: with SMTP id hc34mr9927790ejc.9.1611816190855; Wed, 27 Jan 2021 22:43:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611816190; cv=none; d=google.com; s=arc-20160816; b=z88mRkT2TfUQrdDTuYu/aa3G5Nf+Y9bB8scI7tkSeKO//OQVhuOlMwCR0cIwNfVu+y 7H1VZkT4ONGtkCOu1WFUF6Ys9TAwXDV9PPoq/CFyPXjUncOBMggdoIghdrLtST9FStI0 SH5BZyvTWiVAXEv8UEPA4dinEXNE9bWLMNc0mlNQpu9WtoXUMmxB8KzMBN2kYZzKSjh2 zpmJ9zTZRwwaEb8MXFIE/N9xeqDFZVQW95UyZDxmeAOlsLyHwtYpK45mSmDc/ferVOew bp7B3wFxOLP0Uz+7leCnSQnp3RlepO09a8FAIbyEMgwi369UOaqaBcuLpviaFlOzvnRz G86Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=QtwsPaMPoT2nuohE1Nsiyx6iLrV6QyMIYSjUhf+SD8g=; b=O1FStYDMcOfNX9mKUCAQAqSMz+TWPSMB+a3nOumpzMokrStL2iQ7y3Y2GHOs1EbFkg xaVCBikVzi77GXdM3TIDZFq2O8uP13xwUqGQUaQ1Hj/YUeQ1ljbd/9yPnOsOBpEMJapJ VLYyLqVuXv2lqfEUxM30dGhFKaUFzDZcZNWaHKB9kToY7vYP9mYn4eh/NqXOZdF9gU9C YsildD2CfDoa8f0iSyZ0QcXsxP2iX169E6OclICbb1xZjxs5B4CFWRzH6K7zzfkuaEc4 SH3m/qDps4IIifWUtTsoe5BCfM2NHFqwUcaJf1NRqUx4NwUmwj7fPEdoZw5yXL3jRZ3H AcbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mgh+Uiw3; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gl3si1970468ejb.356.2021.01.27.22.42.26; Wed, 27 Jan 2021 22:43:10 -0800 (PST) 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=@chromium.org header.s=google header.b=mgh+Uiw3; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231126AbhA1Gjy (ORCPT + 99 others); Thu, 28 Jan 2021 01:39:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229709AbhA1Gjx (ORCPT ); Thu, 28 Jan 2021 01:39:53 -0500 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C775C061574 for ; Wed, 27 Jan 2021 22:39:13 -0800 (PST) Received: by mail-pf1-x42c.google.com with SMTP id m6so3376591pfk.1 for ; Wed, 27 Jan 2021 22:39:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=QtwsPaMPoT2nuohE1Nsiyx6iLrV6QyMIYSjUhf+SD8g=; b=mgh+Uiw3ZwBiSLZhQRyC3sAQBXsZdQTiz0KhID5U0riGG6r789JxUJbQB/ug5zLiX4 iMWz9DDy6gl/262dUeYxJIN39DlKSTB6vGLL8M21qFzmPSiAs5ZAg9vv6OK89v6jwFCw XYC3ZldAhWRTcNlv53HMzskZ3pdkNFTWJ9URo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=QtwsPaMPoT2nuohE1Nsiyx6iLrV6QyMIYSjUhf+SD8g=; b=IKclvnlj+FcU8d4QLKYkPPk9z7C4/vitY8/u8C1wAzoCOs24vJDxj7IzCRvmJFSyCW V+Me6v9Hr9HV4UXD2okbkapl7BWDLZZRpCA1g66vR+1xAglKjL8RBRJkdLOJiSe4HxdV dDFVT9c1nlW7n733AQ0J8jOo9uTkuZLsbi9WzeCkn+fOR8P3+jaDfLHrEOZrL2f+Wutx IO5fSRDiyarU2Sj1OBu/eO4dIHn9G+t5WERd1WAOr0GsZftYw/vzYQ6x+A/s5tqZld7V SwFTLaqjtP9Vdzpt/N8v2Zgl4c9mC2gFlY8fBRJUFYRVLiIZNzKjiUhkKO2dI+l3Glsq T7Fg== X-Gm-Message-State: AOAM5333QzYGC6TK2gu8ZPcW/n/I1+bQfIMp5uS4kiTI09hpHjpfGrZJ ouqR5PH5hMlEc9orgeQDYWQeJs22cyh7Pw== X-Received: by 2002:a63:48b:: with SMTP id 133mr14891490pge.139.1611815952103; Wed, 27 Jan 2021 22:39:12 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:3844:c07e:2372:ef64]) by smtp.gmail.com with ESMTPSA id v19sm3575145pjg.50.2021.01.27.22.39.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Jan 2021 22:39:11 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka Subject: [PATCH BlueZ v2] input/hog: Fix crashes of UAF of hog->attr Date: Wed, 27 Jan 2021 22:39:01 -0800 Message-Id: <20210128063901.1362-1-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org hog->attr does not own pointer, so it may be invalid when hog object gets re-attached at bt_hog_attach(). To solve this, this patch intentionally clears hog->attr at bt_hog_detach() to mark it as invalid so that it can be repopulated with the valid pointer at bt_hog_attach(). The same applies to all sub-instances. Sample stack trace: * thread #1, stop reason = signal SIGSEGV * frame #0: 0x05ad49f2 bluetoothd` at gatt-db.c:1428 frame #1: 0x05a91922 bluetoothd`bt_hog_attach at hog-lib.c:1694 frame #2: 0x05a9160e bluetoothd`hog_accept at hog.c:212 frame #3: 0x05ab4784 bluetoothd`service_accept at service.c:203 frame #4: 0x05aba1e6 bluetoothd`device_attach_att at device.c:4542 frame #5: 0x05a9c4a2 bluetoothd`connect_cb at gatt-database.c:656 frame #6: 0x05a98e8c bluetoothd`server_cb at btio.c:264 frame #7: 0xec8e6a1a libglib-2.0.so.0`g_main_context_dispatch at gmain.c:3325 frame #8: 0xec8e6c58 libglib-2.0.so.0`g_main_context_iterate at gmain.c:4119 frame #9: 0xec8e6e52 libglib-2.0.so.0`g_main_loop_run at gmain.c:4317 frame #10: 0x05ad582e bluetoothd`mainloop_run at mainloop-glib.c:79 frame #11: 0x05ad5a64 bluetoothd`mainloop_run_with_signal at mainloop-notify.c:201 frame #12: 0x05ac35ac bluetoothd`main at main.c:1103 frame #13: 0xec6ed0a2 libc.so.6`__libc_start_main at libc-start.c:308 --- profiles/input/hog-lib.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/profiles/input/hog-lib.c b/profiles/input/hog-lib.c index 1f132aa4c..711bda73c 100644 --- a/profiles/input/hog-lib.c +++ b/profiles/input/hog-lib.c @@ -1651,12 +1651,19 @@ static void primary_cb(uint8_t status, GSList *services, void *user_data) bool bt_hog_attach(struct bt_hog *hog, void *gatt) { GSList *l; + bt_uuid_t uuid; if (hog->attrib) return false; hog->attrib = g_attrib_ref(gatt); + if (!hog->attr && hog->gatt_db) { + bt_uuid16_create(&uuid, HOG_UUID16); + gatt_db_foreach_service(hog->gatt_db, &uuid, + foreach_hog_service, hog); + } + if (!hog->attr && !hog->primary) { discover_primary(hog, hog->attrib, NULL, primary_cb, hog); return true; @@ -1744,6 +1751,15 @@ void bt_hog_detach(struct bt_hog *hog) bt_hog_detach(instance); } + /* hog->attr doesn't own pointer, so it may be invalid when this hog + * object gets re-attached with bt_hog_attach(). So intentionally mark + * it as invalid and remove all instances so that the instances can be + * re-attached at bt_hog_attach(). + */ + hog->attr = NULL; + g_slist_free_full(hog->instances, hog_free); + hog->instances = NULL; + for (l = hog->reports; l; l = l->next) { struct report *r = l->data; -- 2.29.2