Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6225178rwb; Mon, 14 Nov 2022 16:41:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf5QJ541hVfZFEKnnuE/Eku8dneeKzqKaI0Gmnxf4tKH3tNiZqqZ+99CadPd4rD001W2udJF X-Received: by 2002:a17:907:d091:b0:78e:11ea:852e with SMTP id vc17-20020a170907d09100b0078e11ea852emr12590310ejc.183.1668472870997; Mon, 14 Nov 2022 16:41:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668472870; cv=none; d=google.com; s=arc-20160816; b=sSTveYr4H4xmrbyq3izmb46EwWv587otRilF50eswT9+EevwaFHGSfJFSoUAudCycn 0f7R+a8aYZpld6Y0kqsURffIk8oZNIL1O7JyyXO04uejnDPnkbfZsgKpUSJh/sZyEWLO R6nK66RD34ofjdk858YWNWdgsPUL8GLRk86EqvprOxaeeLvBWyTgm6e/upXpTRejws+r 5TLz2mYIHN4n3SYtM1WGbKC+jC1bAn/fqYwDxmCqap+eEasDcknAmOHWBVRcBtYcxPe+ pHdm8J8bJ2p3bhlt9nGuEs9Ag80fmHVyyvC3xmuGsbWk3jnYjeNmOiALv66o48oCSAPO CqJQ== 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 :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=UiyFT+n+qVI/WESmarfkamwzmvVYckxgQ1AedNoQGr8=; b=ek2jFBDJpgS87WlLPBlEHtzvin6PVQblxuLFUvR2S6mOfRhlAow/ntSr+mT5piMa+f NXFlGLj57b8YRGUNVrp1NpVmEm1By6sKiH/J8s7fFP/NhZmg6QKfopyYcVlJb/bovjwW oszqGlJ2yLbTxaOlXtfcuLnpuzU2XQtYOEdqKdbG7J3H4VnxzXnvzKWTVFu8R6OZkmIC MBwdv8lmGa/76KW48/9rz8/i4INDMop7qEsm4WckLSNLbE9PY1MLBwEqekagB2KPgDR2 cc0/H9eBmyrxDztrbmtC5o796QT9wcyOamWEF4UbaVb3PL8aucsie+64bsaw0ugGIBGg c9FA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GGtmP5Um; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nc7-20020a1709071c0700b007ae165c79c8si11586374ejc.940.2022.11.14.16.40.45; Mon, 14 Nov 2022 16:41:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GGtmP5Um; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-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 S229774AbiKOAax (ORCPT + 99 others); Mon, 14 Nov 2022 19:30:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235917AbiKOAaw (ORCPT ); Mon, 14 Nov 2022 19:30:52 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2612A1758B for ; Mon, 14 Nov 2022 16:30:51 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id io19so11650889plb.8 for ; Mon, 14 Nov 2022 16:30:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=UiyFT+n+qVI/WESmarfkamwzmvVYckxgQ1AedNoQGr8=; b=GGtmP5Umreh6wnFVD1F9g7836/E2eBRk9rCv5stHJtA5kHx/0NBBOAUhM1BxVsIZ/j 3vPz9pgWJhQKd4cFCszNk0/thkLRe0T6PEAYZZ3zS1Hh7kT0JfXP1A2awfBN3hKwiNal seEKd6Tu2jV5ymGsv47BygifnAWDKQ+9od3zVxkDGYTNW+FkC9oHmCBJs7Blgz7JpydC 3QVoCjRMRfc2KCubrS+9t9cEtyQqNdnvOltXPDj/6I+fue9A57s/fwr4/hmBZJ8W/xZ2 1kbV+S2EQl+EQ4hoEs6uhfF+Pp82loel4X/M1EHE0McOobGiaK+Fa66VBL6syi5XsdMv ld2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UiyFT+n+qVI/WESmarfkamwzmvVYckxgQ1AedNoQGr8=; b=irPBlRtOXPP/pxKAJvkkyTknKjocJKsNdrxdtfwmFVfyk3NCHf2y/3k4hWJCVbS2T1 sx55USN4/ftmfw3TYOUvUWzvLKxq3FeJ1u5eztS07sjVEwXAYhRBTKehtMPWDJ+Uq5hr L/UIyCdLwjP1mZ9Ee8sMdTiUoNNOMrnaUzH4JZBKYKrVRaA2gC9kGX7WqWtr8pXoZhB6 kMnuP/z7IOAADdIV4Wpnb/wLDDzdPCgjaoQdywD4P57gD/mZa1QIALuLV/lE3UQfI4qj 94hrb2D2D6fplbfALzEkAdCAcQWndmsryLvv7NO8AMAFVDYazUUXi/yfRLWmjYB8fj+o ltlw== X-Gm-Message-State: ANoB5pm9bs1JxF5M8Lg1DewKECs0UYUBizQwIumDmXbEh/76iM2Ekgtm rOUo78O0S82pxhZP4i8GpxHdLS8eAcw= X-Received: by 2002:a17:90b:d95:b0:20d:23ee:c041 with SMTP id bg21-20020a17090b0d9500b0020d23eec041mr15662592pjb.140.1668472250150; Mon, 14 Nov 2022 16:30:50 -0800 (PST) Received: from lvondent-mobl4.. (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id h3-20020a17090a3d0300b0020a28156e11sm10307888pjc.26.2022.11.14.16.30.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Nov 2022 16:30:49 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ 5/6] shared/bap: Fix crash when canceling requests Date: Mon, 14 Nov 2022 16:30:37 -0800 Message-Id: <20221115003038.2134340-5-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221115003038.2134340-1-luiz.dentz@gmail.com> References: <20221115003038.2134340-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz If bt_bap_unref/bap_free is called while there is an ongoing pending request it may endup calling into bap_notify_ready which will try to notify ready callbacks while holding a reference, but in case the reference is already 0 that means it would switch to 1 and back 0 causing a double free. To prevent that bap_notify_ready now checks that the reference is not 0 with use of bt_bap_ref_safe. --- src/shared/bap.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/shared/bap.c b/src/shared/bap.c index 25369e619051..21aa8aa6c5ca 100644 --- a/src/shared/bap.c +++ b/src/shared/bap.c @@ -2638,6 +2638,14 @@ struct bt_bap *bt_bap_ref(struct bt_bap *bap) return bap; } +static struct bt_bap *bt_bap_ref_safe(struct bt_bap *bap) +{ + if (!bap || !bap->ref_count) + return NULL; + + return bt_bap_ref(bap); +} + void bt_bap_unref(struct bt_bap *bap) { if (!bap) @@ -2656,7 +2664,8 @@ static void bap_notify_ready(struct bt_bap *bap) if (!queue_isempty(bap->pending)) return; - bt_bap_ref(bap); + if (!bt_bap_ref_safe(bap)) + return; for (entry = queue_get_entries(bap->ready_cbs); entry; entry = entry->next) { -- 2.37.3