Skip to content

Commit 90e8234

Browse files
cmzxoakpm00
authored andcommitted
mm/ksm: fix possible UAF of stable_node
The commit 2c653d0 ("ksm: introduce ksm_max_page_sharing per page deduplication limit") introduced a possible failure case in the stable_tree_insert(), where we may free the new allocated stable_node_dup if we fail to prepare the missing chain node. Then that kfolio return and unlock with a freed stable_node set... And any MM activities can come in to access kfolio->mapping, so UAF. Fix it by moving folio_set_stable_node() to the end after stable_node is inserted successfully. Link: https://lkml.kernel.org/r/20240513-b4-ksm-stable-node-uaf-v1-1-f687de76f452@linux.dev Fixes: 2c653d0 ("ksm: introduce ksm_max_page_sharing per page deduplication limit") Signed-off-by: Chengming Zhou <chengming.zhou@linux.dev> Acked-by: David Hildenbrand <david@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: Stefan Roesch <shr@devkernel.io> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 8cf360b commit 90e8234

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

mm/ksm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,6 @@ static struct ksm_stable_node *stable_tree_insert(struct folio *kfolio)
21532153

21542154
INIT_HLIST_HEAD(&stable_node_dup->hlist);
21552155
stable_node_dup->kpfn = kpfn;
2156-
folio_set_stable_node(kfolio, stable_node_dup);
21572156
stable_node_dup->rmap_hlist_len = 0;
21582157
DO_NUMA(stable_node_dup->nid = nid);
21592158
if (!need_chain) {
@@ -2172,6 +2171,8 @@ static struct ksm_stable_node *stable_tree_insert(struct folio *kfolio)
21722171
stable_node_chain_add_dup(stable_node_dup, stable_node);
21732172
}
21742173

2174+
folio_set_stable_node(kfolio, stable_node_dup);
2175+
21752176
return stable_node_dup;
21762177
}
21772178

0 commit comments

Comments
 (0)