* [PATCH 5.15.y] mm/kfence: randomize the freelist on initialization
2026-02-03 12:48 FAILED: patch "[PATCH] mm/kfence: randomize the freelist on initialization" failed to apply to 5.15-stable tree gregkh
@ 2026-02-04 12:56 ` Pimyn Girgis
2026-02-04 13:12 ` Greg KH
2026-02-05 9:53 ` [PATCH 5.15.y v2] " Pimyn Girgis
` (2 subsequent siblings)
3 siblings, 1 reply; 10+ messages in thread
From: Pimyn Girgis @ 2026-02-04 12:56 UTC (permalink / raw)
To: stable
Cc: Pimyn Girgis, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Greg KH, Kees Cook, Andrew Morton
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.
Link: https://lkml.kernel.org/r/20260120161510.3289089-1-pimyn@google.com
Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Signed-off-by: Pimyn Girgis <pimyn@google.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Ernesto Martnez Garca <ernesto.martinezgarcia@tugraz.at>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Kees Cook <kees@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
Signed-off-by: Pimyn Girgis <pimyn@google.com>
# Conflicts:
# mm/kfence/core.c
---
mm/kfence/core.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index c49bc76b3a38..f94413a503f5 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -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
--
2.53.0.rc2.204.g2597b5adb4-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH 5.15.y] mm/kfence: randomize the freelist on initialization
2026-02-04 12:56 ` [PATCH 5.15.y] mm/kfence: randomize the freelist on initialization Pimyn Girgis
@ 2026-02-04 13:12 ` Greg KH
0 siblings, 0 replies; 10+ messages in thread
From: Greg KH @ 2026-02-04 13:12 UTC (permalink / raw)
To: Pimyn Girgis
Cc: stable, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Kees Cook, Andrew Morton
On Wed, Feb 04, 2026 at 01:56:53PM +0100, Pimyn Girgis wrote:
> 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.
>
> Link: https://lkml.kernel.org/r/20260120161510.3289089-1-pimyn@google.com
> Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
> Signed-off-by: Pimyn Girgis <pimyn@google.com>
> Reviewed-by: Alexander Potapenko <glider@google.com>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Cc: Marco Elver <elver@google.com>
> Cc: Ernesto Martnez Garca <ernesto.martinezgarcia@tugraz.at>
> Cc: Greg KH <gregkh@linuxfoundation.org>
> Cc: Kees Cook <kees@kernel.org>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> (cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
> Signed-off-by: Pimyn Girgis <pimyn@google.com>
>
> # 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
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 5.15.y v2] mm/kfence: randomize the freelist on initialization
2026-02-03 12:48 FAILED: patch "[PATCH] mm/kfence: randomize the freelist on initialization" failed to apply to 5.15-stable tree gregkh
2026-02-04 12:56 ` [PATCH 5.15.y] mm/kfence: randomize the freelist on initialization Pimyn Girgis
@ 2026-02-05 9:53 ` Pimyn Girgis
2026-02-05 14:11 ` Greg KH
2026-02-05 14:50 ` [PATCH 5.15.y v3] " Pimyn Girgis
2026-02-05 16:33 ` [PATCH 5.15.y v4] " Pimyn Girgis
3 siblings, 1 reply; 10+ messages in thread
From: Pimyn Girgis @ 2026-02-05 9:53 UTC (permalink / raw)
To: stable
Cc: Pimyn Girgis, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Greg KH, Kees Cook, Andrew Morton
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.
Link: https://lkml.kernel.org/r/20260120161510.3289089-1-pimyn@google.com
Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Signed-off-by: Pimyn Girgis <pimyn@google.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Ernesto Martnez Garca <ernesto.martinezgarcia@tugraz.at>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Kees Cook <kees@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
Signed-off-by: Pimyn Girgis <pimyn@google.com>
---
mm/kfence/core.c | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index c49bc76b3a38..e1a555eeec45 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -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;
}
--
2.53.0.rc2.204.g2597b5adb4-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH 5.15.y v2] mm/kfence: randomize the freelist on initialization
2026-02-05 9:53 ` [PATCH 5.15.y v2] " Pimyn Girgis
@ 2026-02-05 14:11 ` Greg KH
2026-02-05 14:27 ` Pimyn Girgis
0 siblings, 1 reply; 10+ messages in thread
From: Greg KH @ 2026-02-05 14:11 UTC (permalink / raw)
To: Pimyn Girgis
Cc: stable, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Kees Cook, Andrew Morton
On Thu, Feb 05, 2026 at 10:53:23AM +0100, Pimyn Girgis wrote:
> 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.
>
> Link: https://lkml.kernel.org/r/20260120161510.3289089-1-pimyn@google.com
> Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
> Signed-off-by: Pimyn Girgis <pimyn@google.com>
> Reviewed-by: Alexander Potapenko <glider@google.com>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Cc: Marco Elver <elver@google.com>
> Cc: Ernesto Martnez Garca <ernesto.martinezgarcia@tugraz.at>
> Cc: Greg KH <gregkh@linuxfoundation.org>
> Cc: Kees Cook <kees@kernel.org>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> (cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
> Signed-off-by: Pimyn Girgis <pimyn@google.com>
> ---
> 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
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 5.15.y v2] mm/kfence: randomize the freelist on initialization
2026-02-05 14:11 ` Greg KH
@ 2026-02-05 14:27 ` Pimyn Girgis
2026-02-05 14:30 ` Greg KH
0 siblings, 1 reply; 10+ messages in thread
From: Pimyn Girgis @ 2026-02-05 14:27 UTC (permalink / raw)
To: Greg KH
Cc: stable, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Kees Cook, Andrew Morton
> 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`.
> Always put that below the --- line, like any
> other kernel patch.
I'll keep that in mind for future patches :)
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 5.15.y v2] mm/kfence: randomize the freelist on initialization
2026-02-05 14:27 ` Pimyn Girgis
@ 2026-02-05 14:30 ` Greg KH
0 siblings, 0 replies; 10+ messages in thread
From: Greg KH @ 2026-02-05 14:30 UTC (permalink / raw)
To: Pimyn Girgis
Cc: stable, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Kees Cook, Andrew Morton
On Thu, Feb 05, 2026 at 03:27:13PM +0100, Pimyn Girgis wrote:
> > 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`.
>
> > 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
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 5.15.y v3] mm/kfence: randomize the freelist on initialization
2026-02-03 12:48 FAILED: patch "[PATCH] mm/kfence: randomize the freelist on initialization" failed to apply to 5.15-stable tree gregkh
2026-02-04 12:56 ` [PATCH 5.15.y] mm/kfence: randomize the freelist on initialization Pimyn Girgis
2026-02-05 9:53 ` [PATCH 5.15.y v2] " Pimyn Girgis
@ 2026-02-05 14:50 ` Pimyn Girgis
2026-02-05 15:32 ` Greg KH
2026-02-05 16:33 ` [PATCH 5.15.y v4] " Pimyn Girgis
3 siblings, 1 reply; 10+ messages in thread
From: Pimyn Girgis @ 2026-02-05 14:50 UTC (permalink / raw)
To: stable
Cc: Pimyn Girgis, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Greg KH, Kees Cook, Andrew Morton
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.
Link: https://lkml.kernel.org/r/20260120161510.3289089-1-pimyn@google.com
Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Signed-off-by: Pimyn Girgis <pimyn@google.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Ernesto Martnez Garca <ernesto.martinezgarcia@tugraz.at>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Kees Cook <kees@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
Signed-off-by: Pimyn Girgis <pimyn@google.com>
---
v2: handle addr calculation for error path within appropriate loop
---
mm/kfence/core.c | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index c49bc76b3a38..e1a555eeec45 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -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;
}
--
2.53.0.rc2.204.g2597b5adb4-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH 5.15.y v3] mm/kfence: randomize the freelist on initialization
2026-02-05 14:50 ` [PATCH 5.15.y v3] " Pimyn Girgis
@ 2026-02-05 15:32 ` Greg KH
0 siblings, 0 replies; 10+ messages in thread
From: Greg KH @ 2026-02-05 15:32 UTC (permalink / raw)
To: Pimyn Girgis
Cc: stable, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Kees Cook, Andrew Morton
On Thu, Feb 05, 2026 at 03:50:55PM +0100, Pimyn Girgis wrote:
> 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.
>
> Link: https://lkml.kernel.org/r/20260120161510.3289089-1-pimyn@google.com
> Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
> Signed-off-by: Pimyn Girgis <pimyn@google.com>
> Reviewed-by: Alexander Potapenko <glider@google.com>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Cc: Marco Elver <elver@google.com>
> Cc: Ernesto Martnez Garca <ernesto.martinezgarcia@tugraz.at>
> Cc: Greg KH <gregkh@linuxfoundation.org>
> Cc: Kees Cook <kees@kernel.org>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> (cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
> Signed-off-by: Pimyn Girgis <pimyn@google.com>
> ---
> 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
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 5.15.y v4] mm/kfence: randomize the freelist on initialization
2026-02-03 12:48 FAILED: patch "[PATCH] mm/kfence: randomize the freelist on initialization" failed to apply to 5.15-stable tree gregkh
` (2 preceding siblings ...)
2026-02-05 14:50 ` [PATCH 5.15.y v3] " Pimyn Girgis
@ 2026-02-05 16:33 ` Pimyn Girgis
3 siblings, 0 replies; 10+ messages in thread
From: Pimyn Girgis @ 2026-02-05 16:33 UTC (permalink / raw)
To: stable
Cc: Pimyn Girgis, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
Ernesto Martnez Garca, Greg KH, Kees Cook, Andrew Morton
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.
Link: https://lkml.kernel.org/r/20260120161510.3289089-1-pimyn@google.com
Fixes: 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Signed-off-by: Pimyn Girgis <pimyn@google.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Ernesto Martnez Garca <ernesto.martinezgarcia@tugraz.at>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Kees Cook <kees@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 870ff19251bf3910dda7a7245da826924045fedd)
Signed-off-by: Pimyn Girgis <pimyn@google.com>
---
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(-)
diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index c49bc76b3a38..e1a555eeec45 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -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;
}
--
2.53.0.rc2.204.g2597b5adb4-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread