From nobody Sat Jul 25 03:20:56 2026 Received: from mail-pz2-f0.google.com (mail-pz2-f0.google.com [74.125.228.0]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 147A239447C for ; Mon, 20 Jul 2026 03:37:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.0 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784518635; cv=none; b=XBfGpfTlpl7MTPenPOPVhtcUqKKwoKb4MxVjljVUJZ8H1ni1e6SFY3MlyORE7uk9Bij+bJV0kUK7xmEt94H4Gvxee3L9WvKTlEZ0FeOnlgV4gMpBZYKhp4WL8qjl0a/zXyN3WDgvAiu8xAjYJdaUyUKAfnYdqSez6fnmgw9pm50= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784518635; c=relaxed/simple; bh=Ni9LVVOb8ioOCHwIJrRiUxc2nb1LDV4yWGK8w4zlFS4=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=oOKxWpr1ot/to0qjJhPz7BY+MjnjTM3XYojSJxusYBH79SSyoZhpvWtZhnlloK3Qtcojp6AT9HY9hUGdN8nT11XXdjBzRGV+SaDQwCjCLaGmgOXDUy3SnROUUz703/IL3z6+X5MJNEhEWbyuNdGLlFpjzxVuNDeVWF/727iFY84= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=k9LhtW2Q; arc=none smtp.client-ip=74.125.228.0 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="k9LhtW2Q" Received: by mail-pz2-f0.google.com with SMTP id 41be03b00d2f7-ca92f36c4acso1960493a12.1 for ; Sun, 19 Jul 2026 20:37:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784518633; x=1785123433; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=igVSDobxE8AuEHnuAAbopLMDGh11lnG9Mn6rU0MWnLA=; b=k9LhtW2Qm4cbyv5pk+eIkRBlIK7rCK1Atn634/miV+SsSH4/naocgx4Qz1U29tapUS 0MRCu+jVGI67KMk0btJzWWc4frV2QoDUfzE/2K7pt8u5rEB6Gr26nZ3m+ye6thEGRq1S kmJ7WgBGAG5lNAcEhewV9lZZE5de8MltO3ASJ8Ivi087wpZozI6TRLbODlOFVBPUstsq EQjrQK+bXFW2h8Kn6ABtwXi9HV9/1+jlYg9qCQWzB9EWG6RR74LO0zXHtY7YXGDaYrTr 1L/Q9DhWqGrRsUS7BHU0GmMwPQy9nJEQkzSWMWFZ6vMAjxftqjAQpSLtBBijmlKLyV18 6UbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784518633; x=1785123433; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=igVSDobxE8AuEHnuAAbopLMDGh11lnG9Mn6rU0MWnLA=; b=Nft6RU+sYDWcTiUXI5/dMrKhPVB7S52mc+uIDbVztyaL5hMSh40w7Vj/H9wBvDd9nA /V4lRpBpmnEYwENuyE/YfMzFXG5DpBMQUuh3tVigV+EJQKoeUZJS01CaGv8fNmHO7432 YIOcYYdQf5pKaEauD7NVYo8JWrr5w6QWgH4SiyGMalE7fcHsIZKfzopef0AbKYF9Vkcg s3odcl0pe6/U6p62wD/pvUa580A2fIeNwhBZyi7NNZ5PuDw8ON1+DMHWbyjmiXsxsI4j LXNP8RZBCcFoftSUfEStHiYQspBvVAdJXiF6nSBYO8rwp1TS/L3OGHNgpZkbHwPFMcJo 5INQ== X-Forwarded-Encrypted: i=1; AHgh+RrRWH6ChFp4uvQZMakWz2w/E0DzL88lGlq6YIkTzSH816tQAwXuq3V7UXqG/spbqhLxbHtZO9VFL3dXJD8=@vger.kernel.org X-Gm-Message-State: AOJu0YwUTsYKXc8moJR2qt4NY0DbvR3M5PqsG6zRCc+e8w/frDL5ZSJW WvGgFE1e+wjeDmgSY5vEDVD2S2nwA/5FHomeZUGzd0/ciIhhaijo5GCz X-Gm-Gg: AfdE7ck8dRryGXXuk34Xwn611QV6hJgA2BRsdK+b7t62cjphBQvGAtMh/kgcUz37L8+ NgT+OHTErqUWKSuNKWCrJGF6gZ2Hl+UMp6xIYLd3R5le8nzxo8ZNDX/nF/JKWPCvKeKA7Derqkr tIIjtWo170KX2+K3PCq+uA1MiXmTT1myTvVs099J1LOOt9637S1/klSy+7zVnsW8rQfDNJ1pD+t HxYjJZmPrSLBRTob7+u886DjuMcWmk7iJwSMy4o8EpQanTfcHUlU5lo/aCCipyih40q7B4ltFsD 7khfukuAI2NjAvjXZdrhYsLWczPYOh7quTV1ia5eQigU5GxNY6HKmY7hEs63LGcDvP0gi4lfyUj uG7Oy/IzbnhoYNvqNcT+4Yp6VudUdIM8YocXnXQhtEl4LlhF635zzS8iN110ZJGsZdDBD5AFSSy HL5Q9dX9LuTf2GrN9jzYT98EbjS00= X-Received: by 2002:a05:6a00:1391:b0:848:3e69:4ba6 with SMTP id d2e1a72fcca58-84c29532728mr12201765b3a.54.1784518633267; Sun, 19 Jul 2026 20:37:13 -0700 (PDT) Received: from DESKTOP-P5RIK7I.localdomain ([110.191.179.183]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84c2af6e3ebsm4811588b3a.46.2026.07.19.20.37.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jul 2026 20:37:12 -0700 (PDT) From: jianyungao89@gmail.com To: dm-devel@lists.linux.dev Cc: Jianyun Gao , Dongsheng Yang , Zheng Gu , linux-kernel@vger.kernel.org (open list) Subject: [RFC PATCH] dm-pcache: fix use-after-free and invalid seg operations in kset_replay() Date: Mon, 20 Jul 2026 11:36:32 +0800 Message-Id: <20260720033633.374-1-jianyungao89@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jianyun Gao 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. Signed-off-by: Jianyun Gao --- drivers/md/dm-pcache/cache_key.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/drivers/md/dm-pcache/cache_key.c b/drivers/md/dm-pcache/cache_= key.c index e068e878231b..84bad99d3fcd 100644 --- a/drivers/md/dm-pcache/cache_key.c +++ b/drivers/md/dm-pcache/cache_key.c @@ -728,18 +728,17 @@ static int kset_replay(struct pcache_cache *cache, st= ruct pcache_cache_kset_onme goto err; } =20 - __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 =3D 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; } =20 + __set_bit(key->cache_pos.cache_seg->cache_seg_id, cache->seg_map); + cache_subtree =3D 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); } =20 --=20 2.34.1