Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp153301lqo; Thu, 16 May 2024 02:07:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX7vNi0ss8934QRy+mcCCWbBz8BS8av7Zh6J6oXoYv500CpoGXLW/cjY33JzN2r65yfclinN6+wvXDxcpUUXpu7qdJ1qw9SZIGS7c+oYg== X-Google-Smtp-Source: AGHT+IEk2xdNb9/kmmPVJ9sZq0MHuKa2kezLc9Q8VEwX3lDqUi8eNa+24ul91vopt6sWncsQIGff X-Received: by 2002:a05:6a20:9f99:b0:1ac:de57:b1e3 with SMTP id adf61e73a8af0-1afddeea5c9mr28341795637.0.1715850460144; Thu, 16 May 2024 02:07:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715850460; cv=pass; d=google.com; s=arc-20160816; b=KMxDjkweyBwWNpDSt3OqUtJaMjUjwUzZ0cury3NdSIp1i4s+GrPi/xkz6Hgyk4zLh4 kOYBT+8GR/yQzLIZ16l54u6xMj+HJiip+M3Zep3QVcwtF1U3vudm7RhrgPW6OcRYvlza cAqkIoNorwTZ4xytwgr2A/Ks1fPw8H1KIbwvRjbbV3/wQ3gzdAhHiYPp65k56tLtydhU prMf1rjpabnjT1Fiu+dibSAoCb8OAGSzaGQ1ci//wzatFuBxrv5saB2u0OtaVo79ygUP M+TGah/2KCCs0E/UzHo4SQkIjnwatFW5WqukHmuOAp/nzO8WHejUh6EIxaZvMXYYagQ0 FD/Q== ARC-Message-Signature: i=2; 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:cc:to:from; bh=sfYTmqCg6eMuYsOerZX+uJFb5P0i7/Nn7Wp+sb/onDM=; fh=uo5u2TQK/HQlhydMwGLjBAnuDwaLxndJ/cgWTjIagS8=; b=OpBMOzmbNbXjUF8mp49eMkMsE0Ubej1SmmM+cPyHeS5GBoe7mDo2Vo5RrifuS75HRb iuvr78P1K7+u8cpTpJsAMEjOyUnLoLnTuF1bSqaabM3eiC6pvQwx/1D+c+Prcns9yFFv O6V6tnivb7GausQ0eXqIwxbduxRaHDqCVtbtJbqrqGWONDvZtgkH/5HpC7WtEivFIvf+ eNR7fOugTQXBhD2HvGeoR4GtL83uWuYRoMQpyrnqIyy6nnouoSeecYmC6UF9C0eLmZGe rhlUWvivjM8MN1Ex/HoiQpLBrhtux3rV3B9VQxRZITuhO1kvy9U2MhxQb1KJUJvFqIFY Spdw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=hadess.net); spf=pass (google.com: domain of linux-bluetooth+bounces-4689-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-4689-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-65a8f805364si778098a12.707.2024.05.16.02.07.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 02:07:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-4689-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=hadess.net); spf=pass (google.com: domain of linux-bluetooth+bounces-4689-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-4689-linux.lists.archive=gmail.com@vger.kernel.org" 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 9F57DB235FA for ; Thu, 16 May 2024 09:04:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BD151142E94; Thu, 16 May 2024 09:03:50 +0000 (UTC) X-Original-To: linux-bluetooth@vger.kernel.org Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3281F142E75 for ; Thu, 16 May 2024 09:03:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715850230; cv=none; b=WqQBotSvja3wgLf7aS5n1E6afyNYFcnIb+2ApZNM4EjONyQ/pIAhLdp6IAU+2Fnd6MUFTlwLK7yEjTtsnOBaOq3WbP2Se3mm3fAjarh4ub5qO2toZvgjxcT2dvabC7Ty3r6vOd2cAV1Y8HC7s7Q2T/FNb98CoSGX9Ok3s5FntQY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715850230; c=relaxed/simple; bh=PqHPX83R97G7eIq/784Jd02Ywg3ig3f/+g+FHUYlLxA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nhH1oBodjXN1orTrxPTclqz7fFb0TLVN6xLSDuQDqBXf/kxPT9B7TaWE3WmviAYsYS7xD/8z+yMXNFN7tTcUVbjI1iMWuseXUh0Y/N9WqHcxmStmuT3QTE2nluHvzmk0NMQHEDHBVOS4AQA6moo0JKwlxHrO0/0Y39CWe7LrL0o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hadess.net; spf=pass smtp.mailfrom=hadess.net; arc=none smtp.client-ip=217.70.183.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hadess.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hadess.net Received: by mail.gandi.net (Postfix) with ESMTPSA id 56B001C000A; Thu, 16 May 2024 09:03:43 +0000 (UTC) From: Bastien Nocera To: linux-bluetooth@vger.kernel.org Cc: Bastien Nocera Subject: [BlueZ 08/15] shared/bap: Fix possible use-after-free Date: Thu, 16 May 2024 11:03:12 +0200 Message-ID: <20240516090340.61417-9-hadess@hadess.net> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240516090340.61417-1-hadess@hadess.net> References: <20240516090340.61417-1-hadess@hadess.net> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-GND-Sasl: hadess@hadess.net stream_set_state() might call bap_stream_detach() if the stream is in the process of being detached, causing a use-after-free. Return false from stream_set_state() if the stream is unsafe to manipulate (ie. was in the process of being detached and freed). Error: USE_AFTER_FREE (CWE-416): [#def37] [important] bluez-5.75/src/shared/bap.c:2490:2: freed_arg: "stream_set_state" frees "stream". bluez-5.75/src/shared/bap.c:2493:2: deref_after_free: Dereferencing freed pointer "stream". 2491| 2492| /* Sink can autonomously for to Streaming state if io already exits */ 2493|-> if (stream->io && stream->ep->dir == BT_BAP_SINK) 2494| stream_set_state(stream, BT_BAP_STREAM_STATE_STREAMING); 2495| --- src/shared/bap.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/shared/bap.c b/src/shared/bap.c index 1316d7c73d02..0026bc8dc989 100644 --- a/src/shared/bap.c +++ b/src/shared/bap.c @@ -1298,7 +1298,8 @@ static void bap_stream_state_changed(struct bt_bap_stream *stream) } } -static void stream_set_state(struct bt_bap_stream *stream, uint8_t state) +/* Return false if the stream is being detached */ +static bool stream_set_state(struct bt_bap_stream *stream, uint8_t state) { struct bt_bap *bap = stream->bap; @@ -1308,13 +1309,14 @@ static void stream_set_state(struct bt_bap_stream *stream, uint8_t state) bap = bt_bap_ref_safe(bap); if (!bap) { bap_stream_detach(stream); - return; + return false; } if (stream->ops && stream->ops->set_state) stream->ops->set_state(stream, state); bt_bap_unref(bap); + return true; } static void ep_config_cb(struct bt_bap_stream *stream, int err) @@ -2487,7 +2489,8 @@ static uint8_t stream_enable(struct bt_bap_stream *stream, struct iovec *meta, util_iov_free(stream->meta, 1); stream->meta = util_iov_dup(meta, 1); - stream_set_state(stream, BT_BAP_STREAM_STATE_ENABLING); + if (!stream_set_state(stream, BT_BAP_STREAM_STATE_ENABLING)) + return 1; /* Sink can autonomously for to Streaming state if io already exits */ if (stream->io && stream->ep->dir == BT_BAP_SINK) -- 2.44.0