Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp3017089rdb; Mon, 4 Dec 2023 14:16:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IEvSyTIAf7UO8/bqf2D1/mJV/lm+hRlQiJHVVUhR1xIX+gu92No49fFI0yQeA58I9V59832 X-Received: by 2002:a05:6830:25d1:b0:6d8:5421:1c9f with SMTP id d17-20020a05683025d100b006d854211c9fmr7358189otu.35.1701728160635; Mon, 04 Dec 2023 14:16:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701728160; cv=none; d=google.com; s=arc-20160816; b=OjYTSiV/lVY7jcLxJC0tQdTi5hfuB9cKva7bP24JqdPl0T01AXeuqQ2gW+SvPaiYPm X8Nd7oo9lWA7zApa54u8laaubXqmQXn7PtbMniVty2DZe0zxrlL+ipI3GONrQSw/1O7C 9lTG7wC6IuOiRoRdq0BzxwW6RPmmCiHNo5kP2FWrqG+LR3SKh1axImqRemfJm9QDkyTl cS9Q8o/Hru2UfhsU+ItsetY8YvS4r++AKIqRjRbsSd8HM/RVgnMRVvF5xvj5uuKmA/Hh NR5qQHj8MHOGEGfKiufOv0T5bFI/bqJdqVgJYu5jWLHrzCq3BX2iWLwvRZVqCjY2aHVJ ilcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:to:from:dkim-signature; bh=3ksYSdxZhMuwVUzXhKZUFZKDWNfR+dei+bwpsAXb7yI=; fh=4lGQI6056MBa4/oovMyIYaKLn+Wz/24RGvUivMmQP8E=; b=HTwmQEhR9vh2eEQO/ZzMXcAu852Wt/wf7g6AqCdrkm6bP+uOypkoMwfuKlOCTz1QOW +JL1j2I8m1sRa+hTEnnja5EwMgv3oqrZj21XDiKshvPFTf2IbA+1rPRo73NHqOI1ZDBD +cZyw/srLys3/IC3I6l1FIZH8LiA5cXsKnAYQT67GbthMbNqg9jU1FSRBeid9o5E4sXq 6fGXr8L4/S7ARp4kp0NkBQQ7/d+Bitgq7vVGD2o8rd9HMBaZ9j+v/oeIoKdOvxihiFDt l1GyjzzjZ8cQRwg88hOCJxbLhyfiZnZ1hOmzDWdbaHOoP6Gds0yZ/oft3jG//Z3VduD0 rgTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=buXqQ5Yw; spf=pass (google.com: domain of linux-bluetooth+bounces-381-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-381-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 k5-20020ac86045000000b0041cb3f5c40esi9856316qtm.756.2023.12.04.14.16.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 14:16:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-381-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=buXqQ5Yw; spf=pass (google.com: domain of linux-bluetooth+bounces-381-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-381-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 55F0C1C20BAA for ; Mon, 4 Dec 2023 22:16:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5C7413B7A8; Mon, 4 Dec 2023 22:15:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="buXqQ5Yw" X-Original-To: linux-bluetooth@vger.kernel.org Received: from mail-ua1-x92b.google.com (mail-ua1-x92b.google.com [IPv6:2607:f8b0:4864:20::92b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32BB518C for ; Mon, 4 Dec 2023 14:15:41 -0800 (PST) Received: by mail-ua1-x92b.google.com with SMTP id a1e0cc1a2514c-7c57aa2cce8so743804241.2 for ; Mon, 04 Dec 2023 14:15:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701728139; x=1702332939; darn=vger.kernel.org; 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=3ksYSdxZhMuwVUzXhKZUFZKDWNfR+dei+bwpsAXb7yI=; b=buXqQ5Yw1LiITCujUGOtYJWfY6c5jPqcd5vyRNrWb13Gyg7Z+2DqZCSG9kF3bZkLcZ SKwETgtDMhJ7ObymE2mhM0bcbCRhUNNFKc4KTn5ai2iRwTvtD2ws+G++q82M2hC0LHsD XaDVgvo6lwe2Tqjc/8QlbTGh9/rtTl+2ce8PhEAQa2AkC9UIVnOh2EhcKAW3/K0oRWcy QfQFWprKSgF2eapEmhuDGMl7Pzw3hUdK8l700UtVReRyduKZ7BFxkHgpIBwFm5U3PBK5 jrweK/nw4GGUJGEVv1In9eDCTcIFrcCfTF9uFNm4SpeTGLToG3/ewfzEKNenMDyC/SMw tBng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701728139; x=1702332939; 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=3ksYSdxZhMuwVUzXhKZUFZKDWNfR+dei+bwpsAXb7yI=; b=d3wX4DHrNLKWokscfJTVeZkjPVXgoMg4+kQoas9dSB7TO9/uAFy+AgjpXK0CY8xU3S 58aKnfiGo5hOLSzZpxSAjTBKoZyR/zX4J1YD/s1Obvjpi7h8SLBPYdXeJy6XGzQxxF0L EsCQ1BMG/+bXpna+vCVpnXlI/wEBixu5Ruj9XIOtC/neKjwUlv0+ueinpN1w5NA57XRS GYZ+KUbEFV/Zdupf7NXZoPBU2GN0BVZVLBQ+fseJ3PYRasrTImR7XMTBzJI+DeU7+PqP 7Ltwp7pbPqlQ5A9ZxlJKx4o1OLgptTCqP6hHn1D6LVrn9PGtkf6Et8XpIvvTw4+ZEF16 r20A== X-Gm-Message-State: AOJu0Ywy4KWSAk3oBRc9LMgbUHsRS+55ETBB3QVLu8WSWAZPrFsbHRQH HgxzQ/9mGGRYlMGekO+w8ZA3+BmPBRM= X-Received: by 2002:a05:6102:292b:b0:464:4c9d:6f1d with SMTP id cz43-20020a056102292b00b004644c9d6f1dmr2017833vsb.23.1701728139616; Mon, 04 Dec 2023 14:15:39 -0800 (PST) Received: from lvondent-mobl4.. (071-047-239-151.res.spectrum.com. [71.47.239.151]) by smtp.gmail.com with ESMTPSA id bl13-20020a056102508d00b0044d4e63aa03sm1136612vsb.25.2023.12.04.14.15.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 14:15:37 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v3 6/8] shared/bap: Fix possible crash when freeing requests Date: Mon, 4 Dec 2023 17:15:25 -0500 Message-ID: <20231204221527.2990674-6-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231204221527.2990674-1-luiz.dentz@gmail.com> References: <20231204221527.2990674-1-luiz.dentz@gmail.com> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Luiz Augusto von Dentz Requests maybe queued using a stream so when detaching it needs to be canceled otherwise they can lead crashes like bellow: Invalid read of size 8 at 0x1C3247: stream_stop_complete (bap.c:1230) by 0x1BB2A3: bap_req_complete (bap.c:3863) by 0x1C7BB3: bap_req_detach (bap.c:4219) by 0x1C7BB3: bt_bap_detach (bap.c:4231) by 0x1C7BB3: bt_bap_detach (bap.c:4222) by 0x1C7E67: bap_free (bap.c:2937) by 0x1C7E67: bt_bap_unref (bap.c:3090) by 0x1C7E67: bt_bap_unref (bap.c:3082) by 0x18CCC2: test_teardown (test-bap.c:513) by 0x1D826A: teardown_callback (tester.c:434) by 0x491E4FC: g_idle_dispatch (gmain.c:6163) by 0x49224FB: UnknownInlinedFun (gmain.c:3460) by 0x49224FB: g_main_context_dispatch (gmain.c:4200) by 0x49806B7: g_main_context_iterate.isra.0 (gmain.c:4276) by 0x4921AFE: g_main_loop_run (gmain.c:4479) by 0x1E8EF4: mainloop_run (mainloop-glib.c:66) by 0x1E93F7: mainloop_run_with_signal (mainloop-notify.c:188) Address 0x57b9ad0 is 0 bytes inside a block of size 120 free'd at 0x4845B2C: free (vg_replace_malloc.c:985) by 0x1CBAB7: bap_stream_state_changed (bap.c:1290) by 0x1D3104: bap_ep_set_status (bap.c:3673) by 0x1DC746: queue_foreach (queue.c:207) by 0x1A5320: notify_cb (gatt-client.c:2271) by 0x19A1EF: handle_notify (att.c:1012) by 0x19A1EF: can_read_data (att.c:1096) by 0x1D68CF: watch_callback (io-glib.c:157) by 0x49224FB: UnknownInlinedFun (gmain.c:3460) by 0x49224FB: g_main_context_dispatch (gmain.c:4200) by 0x49806B7: g_main_context_iterate.isra.0 (gmain.c:4276) by 0x4921AFE: g_main_loop_run (gmain.c:4479) by 0x1E8EF4: mainloop_run (mainloop-glib.c:66) by 0x1E93F7: mainloop_run_with_signal (mainloop-notify.c:188) --- src/shared/bap.c | 91 ++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/src/shared/bap.c b/src/shared/bap.c index d8a3adde60ca..a1495ca84bcc 100644 --- a/src/shared/bap.c +++ b/src/shared/bap.c @@ -1232,6 +1232,47 @@ static void stream_stop_complete(struct bt_bap_stream *stream, uint8_t code, bap_stream_io_detach(stream); } +static void bap_req_free(void *data) +{ + struct bt_bap_req *req = data; + size_t i; + + queue_destroy(req->group, bap_req_free); + + for (i = 0; i < req->len; i++) + free(req->iov[i].iov_base); + + free(req->iov); + free(req); +} + +static void bap_req_complete(struct bt_bap_req *req, + const struct bt_ascs_ase_rsp *rsp) +{ + struct queue *group; + + if (!req->func) + goto done; + + if (rsp) + req->func(req->stream, rsp->code, rsp->reason, req->user_data); + else + req->func(req->stream, BT_ASCS_RSP_UNSPECIFIED, 0x00, + req->user_data); + +done: + /* Detach from request so it can be freed separately */ + group = req->group; + req->group = NULL; + + queue_foreach(group, (queue_foreach_func_t)bap_req_complete, + (void *)rsp); + + queue_destroy(group, NULL); + + bap_req_free(req); +} + static void bap_stream_state_changed(struct bt_bap_stream *stream) { struct bt_bap *bap = stream->bap; @@ -1286,6 +1327,10 @@ static void bap_stream_state_changed(struct bt_bap_stream *stream) /* Post notification updates */ switch (stream->ep->state) { case BT_ASCS_ASE_STATE_IDLE: + if (bap->req && bap->req->stream == stream) { + bap_req_complete(bap->req, NULL); + bap->req = NULL; + } bap_stream_detach(stream); break; case BT_ASCS_ASE_STATE_QOS: @@ -2905,20 +2950,6 @@ static void bap_state_free(void *data) free(state); } -static void bap_req_free(void *data) -{ - struct bt_bap_req *req = data; - size_t i; - - queue_destroy(req->group, bap_req_free); - - for (i = 0; i < req->len; i++) - free(req->iov[i].iov_base); - - free(req->iov); - free(req); -} - static void bap_detached(void *data, void *user_data) { struct bt_bap_cb *cb = data; @@ -3809,6 +3840,11 @@ static bool bap_send(struct bt_bap *bap, struct bt_bap_req *req) DBG(bap, "req %p len %u", req, iov.iov_len); + if (req->stream && !queue_find(bap->streams, NULL, req->stream)) { + DBG(bap, "stream %p detached, aborting op 0x%02x", req->op); + return false; + } + if (!gatt_db_attribute_get_char_data(ascs->ase_cp, NULL, &handle, NULL, NULL, NULL)) { DBG(bap, "Unable to find Control Point"); @@ -3843,33 +3879,6 @@ static bool bap_send(struct bt_bap *bap, struct bt_bap_req *req) return true; } -static void bap_req_complete(struct bt_bap_req *req, - const struct bt_ascs_ase_rsp *rsp) -{ - struct queue *group; - - if (!req->func) - goto done; - - if (rsp) - req->func(req->stream, rsp->code, rsp->reason, req->user_data); - else - req->func(req->stream, BT_ASCS_RSP_UNSPECIFIED, 0x00, - req->user_data); - -done: - /* Detach from request so it can be freed separately */ - group = req->group; - req->group = NULL; - - queue_foreach(group, (queue_foreach_func_t)bap_req_complete, - (void *)rsp); - - queue_destroy(group, NULL); - - bap_req_free(req); -} - static bool bap_process_queue(void *data) { struct bt_bap *bap = data; -- 2.43.0