Skip to contentSkip to conversation

FAILED: patch "[PATCH] mm/kfence: randomize the freelist on initialization" failed to apply to 5.15-stable tree

PATCH Superseded [email protected] 2 participants 10 messages Feb 3 – Feb 5

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

The patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x 870ff19251bf3910dda7a7245da826924045fedd
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026020339-trickery-vegan-e9c3@gregkh' --subject-prefix 'PATCH 5.15.y' HEAD^..

Possible dependencies:

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From 870ff19251bf3910dda7a7245da826924045fedd Mon Sep 17 00:00:00 2001
From: Pimyn Girgis <[email protected]>
Date: Tue, 20 Jan 2026 17:15:10 +0100
Subject: [PATCH] mm/kfence: randomize the freelist on initialization

Randomize the KFENCE freelist during pool initialization to make allocation patterns less predictable. This is achieved by shuffling the order in which metadata objects are added to the freelist using get_random_u32_below().

Additionally, ensure the error path correctly calculates the address range to be reset if initialization fails, as the address increment logic has been moved to a separate loop.

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Ernesto Martnez Garca <[email protected]>
Cc Greg KH <[email protected]>
Cc Kees Cook <[email protected]>
mm/kfence/core.c +19 -4
index da0f5b6f5744..4f79ec720752 100644
@@ -596,7 +596,7 @@ static void rcu_guarded_free(struct rcu_head *h)
 static unsigned long kfence_init_pool(void)
 {
 	unsigned long addr, start_pfn;
-	int i;
+	int i, rand;
 
 	if (!arch_kfence_init_pool())
 		return (unsigned long)__kfence_pool;
@@ -647,13 +647,27 @@ static unsigned long kfence_init_pool(void)
 		INIT_LIST_HEAD(&meta->list);
 		raw_spin_lock_init(&meta->lock);
 		meta->state = KFENCE_OBJECT_UNUSED;
-		meta->addr = addr; /* Initialize for validation in metadata_to_pageaddr(). */
-		list_add_tail(&meta->list, &kfence_freelist);
+		/* Use addr to randomize the freelist. */
+		meta->addr = i;
 
 		/* Protect the right redzone. */
-		if (unlikely(!kfence_protect(addr + PAGE_SIZE)))
+		if (unlikely(!kfence_protect(addr + 2 * i * PAGE_SIZE + PAGE_SIZE)))
 			goto reset_slab;
+	}
 
+	for (i = CONFIG_KFENCE_NUM_OBJECTS; i > 0; i--) {
+		rand = get_random_u32_below(i);
+		swap(kfence_metadata_init[i - 1].addr, kfence_metadata_init[rand].addr);
+	}
+
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		struct kfence_metadata *meta_1 = &kfence_metadata_init[i];
+		struct kfence_metadata *meta_2 = &kfence_metadata_init[meta_1->addr];
+
+		list_add_tail(&meta_2->list, &kfence_freelist);
+	}
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		kfence_metadata_init[i].addr = addr;
 		addr += 2 * PAGE_SIZE;
 	}
 
@@ -666,6 +680,7 @@ static unsigned long kfence_init_pool(void)
 	return 0;
 
 reset_slab:
+	addr += 2 * i * PAGE_SIZE;
 	for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
 		struct page *page;
 
#1 lore raw

PATCH 1/1 mm/kfence: randomize the freelist on initialization

Randomize the KFENCE freelist during pool initialization to make allocation patterns less predictable. This is achieved by shuffling the order in which metadata objects are added to the freelist using get_random_u32_below().

Additionally, ensure the error path correctly calculates the address range to be reset if initialization fails, as the address increment logic has been moved to a separate loop.

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Ernesto Martnez Garca <[email protected]>
Cc Greg KH <[email protected]>
Cc Kees Cook <[email protected]>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)

# Conflicts:
# mm/kfence/core.c

---
 mm/kfence/core.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
mm/kfence/core.c +20 -4
index c49bc76b3a38..f94413a503f5 100644
@@ -520,7 +520,7 @@ static bool __init kfence_init_pool(void)
 {
 	unsigned long addr = (unsigned long)__kfence_pool;
 	struct page *pages;
-	int i;
+	int i, rand;
 	char *p;
 
 	if (!__kfence_pool)
@@ -576,13 +576,28 @@ static bool __init kfence_init_pool(void)
 		INIT_LIST_HEAD(&meta->list);
 		raw_spin_lock_init(&meta->lock);
 		meta->state = KFENCE_OBJECT_UNUSED;
-		meta->addr = addr; /* Initialize for validation in metadata_to_pageaddr(). */
-		list_add_tail(&meta->list, &kfence_freelist);
+		/* Use addr to randomize the freelist. */
+		meta->addr = i;
 
 		/* Protect the right redzone. */
-		if (unlikely(!kfence_protect(addr + PAGE_SIZE)))
+		if (unlikely(!kfence_protect(addr + 2 * i * PAGE_SIZE + PAGE_SIZE)))
 			goto err;
+	}
+
+	for (i = CONFIG_KFENCE_NUM_OBJECTS; i > 0; i--) {
+		rand = get_random_u32() % i;
+		swap(kfence_metadata[i - 1].addr, kfence_metadata[rand].addr);
+	}
 
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		struct kfence_metadata *meta_1 = &kfence_metadata[i];
+		struct kfence_metadata *meta_2 = &kfence_metadata[meta_1->addr];
+
+		list_add_tail(&meta_2->list, &kfence_freelist);
+	}
+
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		kfence_metadata[i].addr = addr;
 		addr += 2 * PAGE_SIZE;
 	}
 
@@ -597,6 +612,7 @@ static bool __init kfence_init_pool(void)
 	return true;
 
 err:
+	addr += 2 * i * PAGE_SIZE;
 	/*
 	 * Only release unprotected pages, and do not try to go back and change
 	 * page attributes due to risk of failing to do so as well. If changing
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
author maintainer
#2 lore raw

Re: [PATCH 5.15.y] mm/kfence: randomize the freelist on initialization

Quoted 23 lines · Pimyn Girgis

Randomize the KFENCE freelist during pool initialization to make allocation patterns less predictable. This is achieved by shuffling the order in which metadata objects are added to the freelist using get_random_u32_below().

Additionally, ensure the error path correctly calculates the address range to be reset if initialization fails, as the address increment logic has been moved to a separate loop.

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Ernesto Martnez Garca <[email protected]>
Cc Greg KH <[email protected]>
Cc Kees Cook <[email protected]>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
Quoted 3 lines

# Conflicts:
# mm/kfence/core.c
---

What are these # lines for? Please don't do that in the future as I have to manually edit them out :(

thanks,

greg k-h

#3 lore raw

PATCH v2 1/1 mm/kfence: randomize the freelist on initialization

In reply to [email protected]

Randomize the KFENCE freelist during pool initialization to make allocation patterns less predictable. This is achieved by shuffling the order in which metadata objects are added to the freelist using get_random_u32_below().

Additionally, ensure the error path correctly calculates the address range to be reset if initialization fails, as the address increment logic has been moved to a separate loop.

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Ernesto Martnez Garca <[email protected]>
Cc Greg KH <[email protected]>
Cc Kees Cook <[email protected]>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
---
 mm/kfence/core.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
mm/kfence/core.c +21 -4
index c49bc76b3a38..e1a555eeec45 100644
@@ -520,7 +520,7 @@ static bool __init kfence_init_pool(void)
 {
 	unsigned long addr = (unsigned long)__kfence_pool;
 	struct page *pages;
-	int i;
+	int i, rand;
 	char *p;
 
 	if (!__kfence_pool)
@@ -576,13 +576,30 @@ static bool __init kfence_init_pool(void)
 		INIT_LIST_HEAD(&meta->list);
 		raw_spin_lock_init(&meta->lock);
 		meta->state = KFENCE_OBJECT_UNUSED;
-		meta->addr = addr; /* Initialize for validation in metadata_to_pageaddr(). */
-		list_add_tail(&meta->list, &kfence_freelist);
+		/* Use addr to randomize the freelist. */
+		meta->addr = i;
 
 		/* Protect the right redzone. */
-		if (unlikely(!kfence_protect(addr + PAGE_SIZE)))
+		if (unlikely(!kfence_protect(addr + 2 * i * PAGE_SIZE + PAGE_SIZE))) {
+			addr += 2 * i * PAGE_SIZE;
 			goto err;
+		}
+	}
+
+	for (i = CONFIG_KFENCE_NUM_OBJECTS; i > 0; i--) {
+		rand = get_random_u32() % i;
+		swap(kfence_metadata[i - 1].addr, kfence_metadata[rand].addr);
+	}
 
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		struct kfence_metadata *meta_1 = &kfence_metadata[i];
+		struct kfence_metadata *meta_2 = &kfence_metadata[meta_1->addr];
+
+		list_add_tail(&meta_2->list, &kfence_freelist);
+	}
+
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		kfence_metadata[i].addr = addr;
 		addr += 2 * PAGE_SIZE;
 	}
 
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
author maintainer
#4 lore raw

Re: [PATCH 5.15.y v2] mm/kfence: randomize the freelist on initialization

Quoted 22 lines · Pimyn Girgis

Randomize the KFENCE freelist during pool initialization to make allocation patterns less predictable. This is achieved by shuffling the order in which metadata objects are added to the freelist using get_random_u32_below().

Additionally, ensure the error path correctly calculates the address range to be reset if initialization fails, as the address increment logic has been moved to a separate loop.

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Ernesto Martnez Garca <[email protected]>
Cc Greg KH <[email protected]>
Cc Kees Cook <[email protected]>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
Quoted 3 lines
---
 mm/kfence/core.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

What changed from v1? Always put that below the --- line, like any other kernel patch.

thanks,

greg k-h

#5 lore raw

Re: [PATCH 5.15.y v2] mm/kfence: randomize the freelist on initialization

Quoted 1 line

What changed from v1?

addr calculation in case of an error is handled in the appropriate loop in v2. This ensures that i will have the correct value. In v1, multiple goto err statements risked using an uninitialized or incorrect i.

Quoted 2 lines

Always put that below the --- line, like any
other kernel patch.

I'll keep that in mind for future patches :)

author maintainer
#6 lore raw

Re: [PATCH 5.15.y v2] mm/kfence: randomize the freelist on initialization

Quoted 11 lines · Pimyn Girgis
Quoted 1 line

What changed from v1?

addr calculation in case of an error is handled in the appropriate loop in v2. This ensures that i will have the correct value. In v1, multiple goto err statements risked using an uninitialized or incorrect i.

Quoted 2 lines

Always put that below the --- line, like any
other kernel patch.

I'll keep that in mind for future patches :)

Please do so for this one, a v3 perhaps?

thanks,

greg k-h

#7 lore raw

PATCH v3 1/1 mm/kfence: randomize the freelist on initialization

In reply to [email protected]

Randomize the KFENCE freelist during pool initialization to make allocation patterns less predictable. This is achieved by shuffling the order in which metadata objects are added to the freelist using get_random_u32_below().

Additionally, ensure the error path correctly calculates the address range to be reset if initialization fails, as the address increment logic has been moved to a separate loop.

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Ernesto Martnez Garca <[email protected]>
Cc Greg KH <[email protected]>
Cc Kees Cook <[email protected]>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)

---
v2: handle addr calculation for error path within appropriate loop

---
 mm/kfence/core.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
mm/kfence/core.c +21 -4
index c49bc76b3a38..e1a555eeec45 100644
@@ -520,7 +520,7 @@ static bool __init kfence_init_pool(void)
 {
 	unsigned long addr = (unsigned long)__kfence_pool;
 	struct page *pages;
-	int i;
+	int i, rand;
 	char *p;
 
 	if (!__kfence_pool)
@@ -576,13 +576,30 @@ static bool __init kfence_init_pool(void)
 		INIT_LIST_HEAD(&meta->list);
 		raw_spin_lock_init(&meta->lock);
 		meta->state = KFENCE_OBJECT_UNUSED;
-		meta->addr = addr; /* Initialize for validation in metadata_to_pageaddr(). */
-		list_add_tail(&meta->list, &kfence_freelist);
+		/* Use addr to randomize the freelist. */
+		meta->addr = i;
 
 		/* Protect the right redzone. */
-		if (unlikely(!kfence_protect(addr + PAGE_SIZE)))
+		if (unlikely(!kfence_protect(addr + 2 * i * PAGE_SIZE + PAGE_SIZE))) {
+			addr += 2 * i * PAGE_SIZE;
 			goto err;
+		}
+	}
+
+	for (i = CONFIG_KFENCE_NUM_OBJECTS; i > 0; i--) {
+		rand = get_random_u32() % i;
+		swap(kfence_metadata[i - 1].addr, kfence_metadata[rand].addr);
+	}
 
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		struct kfence_metadata *meta_1 = &kfence_metadata[i];
+		struct kfence_metadata *meta_2 = &kfence_metadata[meta_1->addr];
+
+		list_add_tail(&meta_2->list, &kfence_freelist);
+	}
+
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		kfence_metadata[i].addr = addr;
 		addr += 2 * PAGE_SIZE;
 	}
 
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
author maintainer
#8 lore raw

Re: [PATCH 5.15.y v3] mm/kfence: randomize the freelist on initialization

Quoted 22 lines · Pimyn Girgis

Randomize the KFENCE freelist during pool initialization to make allocation patterns less predictable. This is achieved by shuffling the order in which metadata objects are added to the freelist using get_random_u32_below().

Additionally, ensure the error path correctly calculates the address range to be reset if initialization fails, as the address increment logic has been moved to a separate loop.

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Ernesto Martnez Garca <[email protected]>
Cc Greg KH <[email protected]>
Cc Kees Cook <[email protected]>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
Quoted 2 lines

---
v2: handle addr calculation for error path within appropriate loop

What changed in v3?

Yes, it's a nit, but please, we have a process :)

thanks,

greg k-h

#9 lore raw

PATCH v4 1/1 mm/kfence: randomize the freelist on initialization

In reply to [email protected]

Randomize the KFENCE freelist during pool initialization to make allocation patterns less predictable. This is achieved by shuffling the order in which metadata objects are added to the freelist using get_random_u32_below().

Additionally, ensure the error path correctly calculates the address range to be reset if initialization fails, as the address increment logic has been moved to a separate loop.

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Ernesto Martnez Garca <[email protected]>
Cc Greg KH <[email protected]>
Cc Kees Cook <[email protected]>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)

---
v2: handle addr calculation for error path within appropriate loop
v3: clarify v2 changes in patch changelogs
v4: clarify v3 changes in patch changelogs

---
 mm/kfence/core.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
mm/kfence/core.c +21 -4
index c49bc76b3a38..e1a555eeec45 100644
@@ -520,7 +520,7 @@ static bool __init kfence_init_pool(void)
 {
 	unsigned long addr = (unsigned long)__kfence_pool;
 	struct page *pages;
-	int i;
+	int i, rand;
 	char *p;
 
 	if (!__kfence_pool)
@@ -576,13 +576,30 @@ static bool __init kfence_init_pool(void)
 		INIT_LIST_HEAD(&meta->list);
 		raw_spin_lock_init(&meta->lock);
 		meta->state = KFENCE_OBJECT_UNUSED;
-		meta->addr = addr; /* Initialize for validation in metadata_to_pageaddr(). */
-		list_add_tail(&meta->list, &kfence_freelist);
+		/* Use addr to randomize the freelist. */
+		meta->addr = i;
 
 		/* Protect the right redzone. */
-		if (unlikely(!kfence_protect(addr + PAGE_SIZE)))
+		if (unlikely(!kfence_protect(addr + 2 * i * PAGE_SIZE + PAGE_SIZE))) {
+			addr += 2 * i * PAGE_SIZE;
 			goto err;
+		}
+	}
+
+	for (i = CONFIG_KFENCE_NUM_OBJECTS; i > 0; i--) {
+		rand = get_random_u32() % i;
+		swap(kfence_metadata[i - 1].addr, kfence_metadata[rand].addr);
+	}
 
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		struct kfence_metadata *meta_1 = &kfence_metadata[i];
+		struct kfence_metadata *meta_2 = &kfence_metadata[meta_1->addr];
+
+		list_add_tail(&meta_2->list, &kfence_freelist);
+	}
+
+	for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+		kfence_metadata[i].addr = addr;
 		addr += 2 * PAGE_SIZE;
 	}
 
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog