From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 1E14E4156F9; Fri, 4 Sep 2026 06:02:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788501736; cv=none; b=hzfM4zmVii47AL6w/Ww+DOfV1zurtMjzeqsSkOQE6yYfKNQcP7o5uIzFXtmyL4kw04ocjXEktO9A6KpIvFx+88l/yKoJ9VbcLmpFDwcMpqCkdcmgRwfuvcD/lRAZAz8QkOLTJsupHc3dOLQ/sugXA9IjyD/a0PqgsPwOpLSpucA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788501736; c=relaxed/simple; bh=Nth+trzEERg63GcnSReurzKfGjFNOVM5/BLOGZKBtw8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s6kfSRIcf6oxPZHo5Et7PAiHs4Q8ztFkOX1Ws+wtZPliz7XkO/vu705h6U0zb5DsfAky5SlwZ5LSzAmLib+lgkm9dmu8bzXvjF+K3JjKSsfNL0E60m45GFdAxvM883p7in4chDK2nROgUNiDsVST4wQhgSSbFRB4BMghISpsIIw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=lGhrZtDv; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="lGhrZtDv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 753EB1F00A3D; Fri, 4 Sep 2026 06:02:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1788501735; bh=3/ktuSQ+3sCjTuHb68TVyF2WQwNfPLKJjjLc/AwiFcE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lGhrZtDvNWgPFkWduByzvP3cCr4z1UJdqJ5LWigC7IlsWDU4WUWaYbHZ/SF1i92ok XjZH+w2uTn64p6z5XWduSRLIyeqJhJhHIprFeGplme6HIFPdMGb0UkgHD7K12pTy0D o3qmFwjqBwrR74sMd2Ij67xbUCo+uUIt6wwWxdZM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Jianyun Gao , Mikulas Patocka Subject: [PATCH 6.18 514/552] dm-pcache: fix use-after-free and invalid seg operations in kset_replay() Date: Fri, 4 Sep 2026 07:01:11 +0200 Message-ID: <20260904045802.286579347@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260904045747.813364717@linuxfoundation.org> References: <20260904045747.813364717@linuxfoundation.org> User-Agent: quilt/0.69 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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jianyun Gao commit c2e894eac398b258f12fdec73ed6ba081047f7b3 upstream. In kset_replay, when key->seg_gen is stale (key->seg_gen < key->cache_pos.cache_seg->gen), cache_key_put(key) is called but then key->cache_pos.cache_seg is accessed as the argument to cache_seg_get(). This is a use-after-free on the freed key memory. Although mempool recycled memory is not immediately reclaimed or overwritten in practice, this is still a potential UAF bug. Additionally, for expired invalid keys, setting the cache->seg_map bit and calling cache_seg_get() is unreasonable since the corresponding segment data is no longer valid. Fix both issues by moving cache_seg_get() and __set_bit() after the gen check, so they only execute for valid keys, and using continue to skip invalid keys. Cc: stable@vger.kernel.org Fixes: 1d57628ff95b ("dm-pcache: add persistent cache target in device-mapper") Signed-off-by: Jianyun Gao Signed-off-by: Mikulas Patocka Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-pcache/cache_key.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) --- a/drivers/md/dm-pcache/cache_key.c +++ b/drivers/md/dm-pcache/cache_key.c @@ -736,18 +736,17 @@ static int kset_replay(struct pcache_cac goto err; } - __set_bit(key->cache_pos.cache_seg->cache_seg_id, cache->seg_map); - /* Check if the segment generation is valid for insertion. */ if (key->seg_gen < key->cache_pos.cache_seg->gen) { cache_key_put(key); - } else { - cache_subtree = get_subtree(&cache->req_key_tree, key->off); - spin_lock(&cache_subtree->tree_lock); - cache_key_insert(&cache->req_key_tree, key, true); - spin_unlock(&cache_subtree->tree_lock); + continue; } + __set_bit(key->cache_pos.cache_seg->cache_seg_id, cache->seg_map); + cache_subtree = get_subtree(&cache->req_key_tree, key->off); + spin_lock(&cache_subtree->tree_lock); + cache_key_insert(&cache->req_key_tree, key, true); + spin_unlock(&cache_subtree->tree_lock); cache_seg_get(key->cache_pos.cache_seg); }