Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1340339rwr; Thu, 27 Apr 2023 16:30:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5bKWzm6fNH0MhoLIdNiHSXkT9McA6SkELbRyTQxebTCKEdFoH6W272rKLQnlX6tBvKeK0R X-Received: by 2002:a17:90b:1b4d:b0:247:2dc4:91d5 with SMTP id nv13-20020a17090b1b4d00b002472dc491d5mr3587623pjb.0.1682638258352; Thu, 27 Apr 2023 16:30:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682638258; cv=none; d=google.com; s=arc-20160816; b=OjvfGlm0/qJFS1S2K2eWBu7n0/7LX56QAccTRkhEfxWwLEwhQddF0VQbjww2n4J3L3 l2jhjT35SykPlZSbMmvTe2qGkalMlWTX/oLe23G3cIy04OIBqZNnp8NpmiR2YzcNCAji T3PBy4pJ09V7Dnn2shUwILH7DMpL3XgikpkBJbfnHm7Z9SKgObFLr6sFEbokwcc0jOwb PmUrbLGIpti4Cg/iPsBfrbZUGt7ToSKRw155bRC7WnGHyx9xFhCY/ITWVjZZnVqIiQ4u eYuj5fbdj8yEZMKIw3kemN4sZHcAOd7vW3qUUtmL4YpPcxdh3gud2p473N1O5xbAVBUG +kmw== 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=2TxEgwLhiQ41y0of/BgNY+ugH8sfqwBu6KV8WR5vgIE=; b=yQH2nNXH3BWB+TJMNVuVezWb0xqD2CCH3hkU9LRRXUDLfbWUyWH/thf/hfJOmJkvEw 260bsUW1MUXIDQJy0r3QB1cV00SDAhh3JfYYGMRPKtcuvOgVfMYah6KReLelGqYDaTsZ F52tiRIbkXd8SXoreSmw9pJ2X0NzTdf95oDqx3/xVQGB/3I1BsCuFgfauANAJfAQch/N UEai1iu+0AiKCOC9xnaHk14BeeoQbLTPLSuRcXoex38vGuhCbmkp9g3dpwMLj/5H8IeS +UDH21vpxkBPrqxQJiaN/xsMvi3aRH3t9RnYCLBgZmsX6Q8WjnJzccVA5EuyQMRXhmpQ IFEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=AUsdcvOU; 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 gz18-20020a17090b0ed200b002473c471551si808353pjb.23.2023.04.27.16.30.43; Thu, 27 Apr 2023 16:30:58 -0700 (PDT) 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=20221208 header.b=AUsdcvOU; 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 S1344394AbjD0XN7 (ORCPT + 99 others); Thu, 27 Apr 2023 19:13:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344392AbjD0XN6 (ORCPT ); Thu, 27 Apr 2023 19:13:58 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B442272E for ; Thu, 27 Apr 2023 16:13:57 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1a6c5acf6ccso68627305ad.3 for ; Thu, 27 Apr 2023 16:13:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682637236; x=1685229236; 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=2TxEgwLhiQ41y0of/BgNY+ugH8sfqwBu6KV8WR5vgIE=; b=AUsdcvOUV2SYGTtqt85wN9HDnKzS1ZZjKAW3gyYUK2WoRPGdoRpFJ5HmY+dZwYw6pV +Nk02P/zZy4F/aZIlwlOEWy8EyJ9ZyrZnN4AuPlKmx5H1vuDeAZc+xU1PGgLjMG/zlO5 7wOyI3LwWa0fc3gP5KJ0NAtuvPshW1smTzzHRxfudeNHIML83t35FUuSCOI5xWoPxeAm ZaH3a7y017k1f4XS0MQxk+R8NcmgCVYj1oekNe2lZx9nx2M17NL59i0PlmUYiWkf22Lp juLNndzj9BSz/tGjdA/GfsO0nLrV07G31TQS9dSR91jqEz8TwU4yXmc0+OY7nn1KTIpG sr8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682637236; x=1685229236; 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=2TxEgwLhiQ41y0of/BgNY+ugH8sfqwBu6KV8WR5vgIE=; b=GnQAoZavMef6eGs2jvX3g4Qae7/ynGBVx/iq32hbkX4h4Ddyu95V837LLaBJ7BSlAb I/1uO571oRRN89GRpkJEh6wz9sJoxMsy/U6mBJQaS96QkaahbrMXQlKyA0xxDCLV+TFq dCaFvcoPLrPMohNqz1KvPo+9XEoIVSS79iUH2ZyB0rFcecQv0V1yXXms9FXkYFrF9b3f D2K7iRRwP6O6DsMFYREiXpQdUNcE9b+TDxeUsxwuE7CDS7vpikGMuJskq4+Y1NsQ8/Xp N2w0yOLJXPYq8x7Kh94aPxGeyCTQTUx6bDv+ttembcQ4blg/imbbzhD3uHGVYRk9emlV sXWg== X-Gm-Message-State: AC+VfDzztT26gTw2Mmua8SGhvL63nx2OSlrNrAyCuNIo++Pavyj+vVYl 8Z8oY3YQm1d5SCibBdKL4fSfo77IWms= X-Received: by 2002:a17:902:8c91:b0:1a9:2b7f:a594 with SMTP id t17-20020a1709028c9100b001a92b7fa594mr3100044plo.29.1682637236184; Thu, 27 Apr 2023 16:13:56 -0700 (PDT) Received: from lvondent-mobl4.. (c-71-59-129-171.hsd1.or.comcast.net. [71.59.129.171]) by smtp.gmail.com with ESMTPSA id c5-20020a170902aa4500b001a24cded097sm12112369plr.236.2023.04.27.16.13.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 16:13:55 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [BlueZ PATCH v3 2/9] shared/bap: Fix crash detaching streams Date: Thu, 27 Apr 2023 16:13:45 -0700 Message-Id: <20230427231352.576337-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230427231352.576337-1-luiz.dentz@gmail.com> References: <20230427231352.576337-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,T_SCC_BODY_TEXT_LINE 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 a stream is being detached but bt_bap reference is already 0 don't attempt to detach the stream as they would be freed anyway: Invalid read of size 8 at 0x19A360: bap_free (bap.c:2576) by 0x19A360: bt_bap_unref (bap.c:2735) by 0x19A360: bt_bap_unref (bap.c:2727) by 0x160E9A: test_teardown (test-bap.c:412) by 0x1A8BCA: teardown_callback (tester.c:434) Address 0x55e05e0 is 16 bytes inside a block of size 160 free'd at 0x48480E4: free (vg_replace_malloc.c:872) by 0x1AD5F6: queue_foreach (queue.c:207) by 0x19A1C5: bt_bap_detach (bap.c:3879) by 0x19A1C5: bt_bap_detach (bap.c:3855) by 0x19A33F: bap_free (bap.c:2574) --- src/shared/bap.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/shared/bap.c b/src/shared/bap.c index 5a12a64d292b..bc6177a5ba90 100644 --- a/src/shared/bap.c +++ b/src/shared/bap.c @@ -1168,6 +1168,14 @@ static void bap_stream_set_io(void *data, void *user_data) } } +static struct bt_bap *bt_bap_ref_safe(struct bt_bap *bap) +{ + if (!bap || !bap->ref_count) + return NULL; + + return bt_bap_ref(bap); +} + static void bap_stream_state_changed(struct bt_bap_stream *stream) { struct bt_bap *bap = stream->bap; @@ -1178,7 +1186,14 @@ static void bap_stream_state_changed(struct bt_bap_stream *stream) bt_bap_stream_statestr(stream->ep->old_state), bt_bap_stream_statestr(stream->ep->state)); - bt_bap_ref(bap); + /* Check if ref_count is already 0 which means detaching is in + * progress. + */ + bap = bt_bap_ref_safe(bap); + if (!bap) { + bap_stream_detach(stream); + return; + } /* Pre notification updates */ switch (stream->ep->state) { @@ -2730,14 +2745,6 @@ 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) -- 2.40.0