From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 754731EBA1F; Tue, 15 Oct 2024 12:13:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728994389; cv=none; b=N/UjePKrM3EAZ2tyiZhxfj3V432XWyNWuX7u8LQqYV4CXOnXytk67sSOW0llpYlVnH+1n0JH6Ofzj9Yyn/Vh6CLVu723nTm+BFUR8iQpHt+KPoN6IL8Gz3hzjJksikVtD3lXsqDNLxqgnmSG/EdFYdrtz4eWq9XzwXRTvrdAwBQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728994389; c=relaxed/simple; bh=BXCTtBxPokqXzPQTA18tMcWduXJmEkbOcnNPOGwcjMU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hDLG3fCGiLvhB0vAPNC9vBhBp1UO3+3gNVvmYOFAx3gycKh6w0jjUJQPv72IfOoq1qV0qtKUjxmxuUaWKEaXRXB/p3wrYI3F5EV1HUuNejJmpjtnyyaK6H3ym9jjSfYEVXFAwPoLdiLfup+ggt1BlmmQOfGa2q4kBmCsIeFrcyY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=gf7TjvPt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="gf7TjvPt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D46DFC4CEC6; Tue, 15 Oct 2024 12:13:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1728994389; bh=BXCTtBxPokqXzPQTA18tMcWduXJmEkbOcnNPOGwcjMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gf7TjvPto+8x1WbuneVLxXzqlYYKOTsyJcQ6jWjG4PQP6A1p6lTkezD+96zzsfyXO L1rB3lhOCo6KgZkF8ZgoBrT9wq6saFWzDgs7FxR1zvMOnKWxT0dfe8EDtC0Uif+/nq OVkN/oxoewHsRWdjcBBmSwRbPTSDsNBTrIpRN0qM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Yu Kuai , Jens Axboe Subject: [PATCH 5.15 679/691] block, bfq: fix uaf for accessing waker_bfqq after splitting Date: Tue, 15 Oct 2024 13:30:27 +0200 Message-ID: <20241015112507.273014478@linuxfoundation.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241015112440.309539031@linuxfoundation.org> References: <20241015112440.309539031@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yu Kuai commit 1ba0403ac6447f2d63914fb760c44a3b19c44eaf upstream. After commit 42c306ed7233 ("block, bfq: don't break merge chain in bfq_split_bfqq()"), if the current procress is the last holder of bfqq, the bfqq can be freed after bfq_split_bfqq(). Hence recored the bfqq and then access bfqq->waker_bfqq may trigger UAF. What's more, the waker_bfqq may in the merge chain of bfqq, hence just recored waker_bfqq is still not safe. Fix the problem by adding a helper bfq_waker_bfqq() to check if bfqq->waker_bfqq is in the merge chain, and current procress is the only holder. Fixes: 42c306ed7233 ("block, bfq: don't break merge chain in bfq_split_bfqq()") Signed-off-by: Yu Kuai Link: https://lore.kernel.org/r/20240909134154.954924-2-yukuai1@huaweicloud.com Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- block/bfq-iosched.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -6568,6 +6568,31 @@ static void bfq_prepare_request(struct r rq->elv.priv[0] = rq->elv.priv[1] = NULL; } +static struct bfq_queue *bfq_waker_bfqq(struct bfq_queue *bfqq) +{ + struct bfq_queue *new_bfqq = bfqq->new_bfqq; + struct bfq_queue *waker_bfqq = bfqq->waker_bfqq; + + if (!waker_bfqq) + return NULL; + + while (new_bfqq) { + if (new_bfqq == waker_bfqq) { + /* + * If waker_bfqq is in the merge chain, and current + * is the only procress. + */ + if (bfqq_process_refs(waker_bfqq) == 1) + return NULL; + break; + } + + new_bfqq = new_bfqq->new_bfqq; + } + + return waker_bfqq; +} + /* * If needed, init rq, allocate bfq data structures associated with * rq, and increment reference counters in the destination bfq_queue @@ -6628,7 +6653,7 @@ static struct bfq_queue *bfq_init_rq(str /* If the queue was seeky for too long, break it apart. */ if (bfq_bfqq_coop(bfqq) && bfq_bfqq_split_coop(bfqq) && !bic->stably_merged) { - struct bfq_queue *old_bfqq = bfqq; + struct bfq_queue *waker_bfqq = bfq_waker_bfqq(bfqq); /* Update bic before losing reference to bfqq */ if (bfq_bfqq_in_large_burst(bfqq)) @@ -6647,7 +6672,7 @@ static struct bfq_queue *bfq_init_rq(str bfqq_already_existing = true; if (!bfqq_already_existing) { - bfqq->waker_bfqq = old_bfqq->waker_bfqq; + bfqq->waker_bfqq = waker_bfqq; bfqq->tentative_waker_bfqq = NULL; /* @@ -6657,7 +6682,7 @@ static struct bfq_queue *bfq_init_rq(str * woken_list of the waker. See * bfq_check_waker for details. */ - if (bfqq->waker_bfqq) + if (waker_bfqq) hlist_add_head(&bfqq->woken_list_node, &bfqq->waker_bfqq->woken_list); }