Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4149816pxb; Tue, 26 Jan 2021 13:49:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUSjG/scvtZQ2ymeAtcMTSUsXuvabTcGPMQeiMC9l5QeL75KURk2VOjFM9KzQPROXL/2Nc X-Received: by 2002:a17:906:7798:: with SMTP id s24mr4782426ejm.19.1611697745717; Tue, 26 Jan 2021 13:49:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611697745; cv=none; d=google.com; s=arc-20160816; b=p8Kxo8RYDLehbY9t8iogp09xiE6EvxvFm5qZF+L2Az5xhOYw1JuSjjuNPxOu4y6pTz NuldYwHmiDAPMwRcuir0sGzObcBzmLl+eunO/xE2+zip7if7K9YA8eKnoZaMHwpeR7CX j0EoFZv0FF1QHIPp3FtR7SQA5rlqlp7Tg8Jmw7bzp+PtB3zw7e9cw0TFrJTAfEeuUiL/ N/7OojkNLypmEKza2rjEbQBxPH+89AvYV46O6HiVtQYkTBHTcf3LkaV6ETCVSoVjlgRy 3Xm5zeyjDAz16tsNMOdcLosQyjG4AZceGRLnabGRrOicRhwh0osLmO+9hBAeXiZbhe0V V0vw== 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=Rd61baDo1O8agTqfKzI1LVnjDA9xt1ECM38/G+bIWJ4=; b=qemU+ZzqF+j42QRbkyPsnNx+rLC/HSpDRiPUoNlUfRbe3CIdQrqzIzZKQd1/Ew8nzs xmqdMEVeaL4xNDmLHZ2lG9MQDGjTl4qFQ5oZ3fzIYoHbjTpFBZGdSZe4fZ5IcNnOLiEd h7MTkF+uzTF/Gi2wMhfrw/g0k38s/LDhlDttaXPYbHfH6LNS11TWS1880TItYW5uu2wo IbSPgUY47yGprSerGX7KJUPCNiv9VRuPO42fjD0I27rjDhXtU/RKWBBqccztgG/+jpcZ qgARZm3928rqHOmtzPOxlJ5lmNoQMAbsUILUQKebCEcb9hZ1MXzd4WaBF6nS/KH/E79a UO8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RzKcQ5oV; 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 k26si3651351eds.217.2021.01.26.13.48.24; Tue, 26 Jan 2021 13:49:05 -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=RzKcQ5oV; 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 S1731999AbhAZFco (ORCPT + 99 others); Tue, 26 Jan 2021 00:32:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728597AbhAZBbv (ORCPT ); Mon, 25 Jan 2021 20:31:51 -0500 Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0652CC061A86 for ; Mon, 25 Jan 2021 17:11:43 -0800 (PST) Received: by mail-pg1-x534.google.com with SMTP id o16so540517pgg.5 for ; Mon, 25 Jan 2021 17:11:43 -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=Rd61baDo1O8agTqfKzI1LVnjDA9xt1ECM38/G+bIWJ4=; b=RzKcQ5oVCP6kxQQptiP4bMvEGBoaa3Dg0+VkY9pws24AaEmhezUUF4Z2GlgxYYoHiW qFhWGsI+HAmtQjzCO//8uQOA1mMG1Vc8CakpiPRdhgNCQTFATBCT5HfoUsL52bAK0nba Xt/r2ptbey0nQ4w3NJLV/ieiiSWuYAKaAqWj4= 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=Rd61baDo1O8agTqfKzI1LVnjDA9xt1ECM38/G+bIWJ4=; b=YIoYLxpYNkBqO0ZioleiiQn2KOUJdua9P/VgvU+OVamqpLQXHGFzyiL8vvragEidWW bMVx504umLUaURcHMP7kLTrIuENp4v/oWGsN8oYqWQXsiYU8sqyrKMc3Fj3XxxNYG8dL IYBtwZ2/Lj42y4uwXvL8MkQqAxMlQ2iAB6bZi8Xle/Jly17c7ZPHSNw++kj2zkHD1Buo kJB2WRJb+wXhxqFiAvEYx7KmIhJzwAw2Q3amVjQuhomqHghcg1hBZRbB2INZk1poWlFi Yg6/EewF1IqvmNIadSy8D4NT1nF8a8oO5a8PFI8gRtlB2Oe1eMv9CxLNg4SN2BgMPMVD V9Pg== X-Gm-Message-State: AOAM533O6Acq3rEJRxMQcmKQTJF4b5cgEnrJmiWhlexESSiI5hUgdYX7 itr7YKaEhMHqo2HbF5RnUH9HS5srrmTM0A== X-Received: by 2002:aa7:909a:0:b029:1bc:22d3:9e58 with SMTP id i26-20020aa7909a0000b02901bc22d39e58mr2837675pfa.29.1611623502288; Mon, 25 Jan 2021 17:11:42 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:7c08:2ccf:8f7c:2eed]) by smtp.gmail.com with ESMTPSA id d76sm17776543pfd.193.2021.01.25.17.11.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jan 2021 17:11:41 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka Subject: [PATCH BlueZ] input/hog: Fix crashes of UAF of hog->attr Date: Mon, 25 Jan 2021 17:11:30 -0800 Message-Id: <20210126011130.5233-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 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/profiles/input/hog-lib.c b/profiles/input/hog-lib.c index 1f132aa4c..7d1ea53bd 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,14 @@ 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); + for (l = hog->reports; l; l = l->next) { struct report *r = l->data; -- 2.29.2