2023-03-28 10:05:43

by Muchun Song

[permalink] [raw]
Subject: [PATCH 0/6] Simplify kfence code

This series aims to simplify kfence code, please review each patch separately.

Thanks.

Muchun Song (6):
mm: kfence: simplify kfence pool initialization
mm: kfence: check kfence pool size at building time
mm: kfence: make kfence_protect_page() void
mm: kfence: remove useless check for CONFIG_KFENCE_NUM_OBJECTS
mm: kfence: change kfence pool page layout
mm: kfence: replace ALIGN_DOWN(x, PAGE_SIZE) with PAGE_ALIGN_DOWN(x)

arch/arm/include/asm/kfence.h | 4 +-
arch/arm64/include/asm/kfence.h | 4 +-
arch/parisc/include/asm/kfence.h | 7 +-
arch/powerpc/include/asm/kfence.h | 8 +-
arch/riscv/include/asm/kfence.h | 4 +-
arch/s390/include/asm/kfence.h | 3 +-
arch/x86/include/asm/kfence.h | 9 +-
include/linux/kfence.h | 8 +-
mm/kfence/core.c | 229 +++++++++++++-------------------------
mm/kfence/kfence.h | 2 +-
mm/kfence/kfence_test.c | 14 ---
11 files changed, 89 insertions(+), 203 deletions(-)

--
2.11.0


2023-03-28 10:06:04

by Muchun Song

[permalink] [raw]
Subject: [PATCH 1/6] mm: kfence: simplify kfence pool initialization

There are three similar loops to initialize kfence pool, we could merge
all of them into one loop to simplify the code and make code more
efficient.

Signed-off-by: Muchun Song <[email protected]>
---
mm/kfence/core.c | 47 ++++++-----------------------------------------
1 file changed, 6 insertions(+), 41 deletions(-)

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index 7d01a2c76e80..de62a84d4830 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -539,35 +539,10 @@ static void rcu_guarded_free(struct rcu_head *h)
static unsigned long kfence_init_pool(void)
{
unsigned long addr = (unsigned long)__kfence_pool;
- struct page *pages;
int i;

if (!arch_kfence_init_pool())
return addr;
-
- pages = virt_to_page(__kfence_pool);
-
- /*
- * Set up object pages: they must have PG_slab set, to avoid freeing
- * these as real pages.
- *
- * We also want to avoid inserting kfence_free() in the kfree()
- * fast-path in SLUB, and therefore need to ensure kfree() correctly
- * enters __slab_free() slow-path.
- */
- for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
- struct slab *slab = page_slab(nth_page(pages, i));
-
- if (!i || (i % 2))
- continue;
-
- __folio_set_slab(slab_folio(slab));
-#ifdef CONFIG_MEMCG
- slab->memcg_data = (unsigned long)&kfence_metadata[i / 2 - 1].objcg |
- MEMCG_DATA_OBJCGS;
-#endif
- }
-
/*
* Protect the first 2 pages. The first page is mostly unnecessary, and
* merely serves as an extended guard page. However, adding one
@@ -581,8 +556,9 @@ static unsigned long kfence_init_pool(void)
addr += PAGE_SIZE;
}

- for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
+ for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++, addr += 2 * PAGE_SIZE) {
struct kfence_metadata *meta = &kfence_metadata[i];
+ struct slab *slab = page_slab(virt_to_page(addr));

/* Initialize metadata. */
INIT_LIST_HEAD(&meta->list);
@@ -593,26 +569,15 @@ static unsigned long kfence_init_pool(void)

/* Protect the right redzone. */
if (unlikely(!kfence_protect(addr + PAGE_SIZE)))
- goto reset_slab;
-
- addr += 2 * PAGE_SIZE;
- }
-
- return 0;
-
-reset_slab:
- for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
- struct slab *slab = page_slab(nth_page(pages, i));
+ return addr;

- if (!i || (i % 2))
- continue;
+ __folio_set_slab(slab_folio(slab));
#ifdef CONFIG_MEMCG
- slab->memcg_data = 0;
+ slab->memcg_data = (unsigned long)&meta->objcg | MEMCG_DATA_OBJCGS;
#endif
- __folio_clear_slab(slab_folio(slab));
}

- return addr;
+ return 0;
}

static bool __init kfence_init_pool_early(void)
--
2.11.0

2023-03-28 10:06:12

by Muchun Song

[permalink] [raw]
Subject: [PATCH 2/6] mm: kfence: check kfence pool size at building time

Check kfence pool size at building time to expose problem ASAP.

Signed-off-by: Muchun Song <[email protected]>
---
mm/kfence/core.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index de62a84d4830..6781af1dfa66 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -841,10 +841,9 @@ static int kfence_init_late(void)
return -ENOMEM;
__kfence_pool = page_to_virt(pages);
#else
- if (nr_pages > MAX_ORDER_NR_PAGES) {
- pr_warn("KFENCE_NUM_OBJECTS too large for buddy allocator\n");
- return -EINVAL;
- }
+ BUILD_BUG_ON_MSG(get_order(KFENCE_POOL_SIZE) > MAX_ORDER,
+ "CONFIG_KFENCE_NUM_OBJECTS is too large for buddy allocator");
+
__kfence_pool = alloc_pages_exact(KFENCE_POOL_SIZE, GFP_KERNEL);
if (!__kfence_pool)
return -ENOMEM;
--
2.11.0

2023-03-28 10:06:13

by Muchun Song

[permalink] [raw]
Subject: [PATCH 4/6] mm: kfence: remove useless check for CONFIG_KFENCE_NUM_OBJECTS

The CONFIG_KFENCE_NUM_OBJECTS is limited by kconfig and vary from 1 to
65535, so CONFIG_KFENCE_NUM_OBJECTS cannot be equabl to or smaller than
0. Removing it to simplify code.

Signed-off-by: Muchun Song <[email protected]>
---
mm/kfence/core.c | 1 -
1 file changed, 1 deletion(-)

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index 5726bf2ae13c..41befcb3b069 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -115,7 +115,6 @@ EXPORT_SYMBOL(__kfence_pool); /* Export for test modules. */
* Per-object metadata, with one-to-one mapping of object metadata to
* backing pages (in __kfence_pool).
*/
-static_assert(CONFIG_KFENCE_NUM_OBJECTS > 0);
struct kfence_metadata kfence_metadata[CONFIG_KFENCE_NUM_OBJECTS];

/* Freelist with available objects. */
--
2.11.0

2023-03-28 10:06:14

by Muchun Song

[permalink] [raw]
Subject: [PATCH 6/6] mm: kfence: replace ALIGN_DOWN(x, PAGE_SIZE) with PAGE_ALIGN_DOWN(x)

Replace ALIGN_DOWN(x, PAGE_SIZE) with PAGE_ALIGN_DOWN(x) to simplify
the code a bit.

Signed-off-by: Muchun Song <[email protected]>
---
mm/kfence/core.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index f205b860f460..dbfb79a4d624 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -230,17 +230,17 @@ static bool alloc_covered_contains(u32 alloc_stack_hash)

static inline void kfence_protect(unsigned long addr)
{
- kfence_protect_page(ALIGN_DOWN(addr, PAGE_SIZE), true);
+ kfence_protect_page(PAGE_ALIGN_DOWN(addr), true);
}

static inline void kfence_unprotect(unsigned long addr)
{
- kfence_protect_page(ALIGN_DOWN(addr, PAGE_SIZE), false);
+ kfence_protect_page(PAGE_ALIGN_DOWN(addr), false);
}

static inline unsigned long metadata_to_pageaddr(const struct kfence_metadata *meta)
{
- return ALIGN_DOWN(meta->addr, PAGE_SIZE);
+ return PAGE_ALIGN_DOWN(meta->addr);
}

/*
@@ -308,7 +308,7 @@ static inline bool check_canary_byte(u8 *addr)
/* __always_inline this to ensure we won't do an indirect call to fn. */
static __always_inline void for_each_canary(const struct kfence_metadata *meta, bool (*fn)(u8 *))
{
- const unsigned long pageaddr = ALIGN_DOWN(meta->addr, PAGE_SIZE);
+ const unsigned long pageaddr = PAGE_ALIGN_DOWN(meta->addr);
unsigned long addr;

/*
@@ -455,7 +455,7 @@ static void kfence_guarded_free(void *addr, struct kfence_metadata *meta, bool z
}

/* Detect racy use-after-free, or incorrect reallocation of this page by KFENCE. */
- kcsan_begin_scoped_access((void *)ALIGN_DOWN((unsigned long)addr, PAGE_SIZE), PAGE_SIZE,
+ kcsan_begin_scoped_access((void *)PAGE_ALIGN_DOWN((unsigned long)addr), PAGE_SIZE,
KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT,
&assert_page_exclusive);

@@ -464,7 +464,7 @@ static void kfence_guarded_free(void *addr, struct kfence_metadata *meta, bool z

/* Restore page protection if there was an OOB access. */
if (meta->unprotected_page) {
- memzero_explicit((void *)ALIGN_DOWN(meta->unprotected_page, PAGE_SIZE), PAGE_SIZE);
+ memzero_explicit((void *)PAGE_ALIGN_DOWN(meta->unprotected_page), PAGE_SIZE);
kfence_protect(meta->unprotected_page);
meta->unprotected_page = 0;
}
--
2.11.0

2023-03-28 10:06:49

by Muchun Song

[permalink] [raw]
Subject: [PATCH 5/6] mm: kfence: change kfence pool page layout

The original kfence pool layout (Given a layout with 2 objects):

+------------+------------+------------+------------+------------+------------+
| guard page | guard page | object | guard page | object | guard page |
+------------+------------+------------+------------+------------+------------+
| | |
+----kfence_metadata[0]---+----kfence_metadata[1]---+

The comment says "the additional page in the beginning gives us an even
number of pages, which simplifies the mapping of address to metadata index".

However, removing the additional page does not complicate any mapping
calculations. So changing it to the new layout to save a page. And remmove
the KFENCE_ERROR_INVALID test since we cannot test this case easily.

The new kfence pool layout (Given a layout with 2 objects):

+------------+------------+------------+------------+------------+
| guard page | object | guard page | object | guard page |
+------------+------------+------------+------------+------------+
| | |
+----kfence_metadata[0]---+----kfence_metadata[1]---+

Signed-off-by: Muchun Song <[email protected]>
---
include/linux/kfence.h | 8 ++------
mm/kfence/core.c | 40 ++++++++--------------------------------
mm/kfence/kfence.h | 2 +-
mm/kfence/kfence_test.c | 14 --------------
4 files changed, 11 insertions(+), 53 deletions(-)

diff --git a/include/linux/kfence.h b/include/linux/kfence.h
index 726857a4b680..25b13a892717 100644
--- a/include/linux/kfence.h
+++ b/include/linux/kfence.h
@@ -19,12 +19,8 @@

extern unsigned long kfence_sample_interval;

-/*
- * We allocate an even number of pages, as it simplifies calculations to map
- * address to metadata indices; effectively, the very first page serves as an
- * extended guard page, but otherwise has no special purpose.
- */
-#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 * PAGE_SIZE)
+/* The last page serves as an extended guard page. */
+#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS * 2 + 1) * PAGE_SIZE)
extern char *__kfence_pool;

DECLARE_STATIC_KEY_FALSE(kfence_allocation_key);
diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index 41befcb3b069..f205b860f460 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -240,24 +240,7 @@ static inline void kfence_unprotect(unsigned long addr)

static inline unsigned long metadata_to_pageaddr(const struct kfence_metadata *meta)
{
- unsigned long offset = (meta - kfence_metadata + 1) * PAGE_SIZE * 2;
- unsigned long pageaddr = (unsigned long)&__kfence_pool[offset];
-
- /* The checks do not affect performance; only called from slow-paths. */
-
- /* Only call with a pointer into kfence_metadata. */
- if (KFENCE_WARN_ON(meta < kfence_metadata ||
- meta >= kfence_metadata + CONFIG_KFENCE_NUM_OBJECTS))
- return 0;
-
- /*
- * This metadata object only ever maps to 1 page; verify that the stored
- * address is in the expected range.
- */
- if (KFENCE_WARN_ON(ALIGN_DOWN(meta->addr, PAGE_SIZE) != pageaddr))
- return 0;
-
- return pageaddr;
+ return ALIGN_DOWN(meta->addr, PAGE_SIZE);
}

/*
@@ -535,34 +518,27 @@ static void kfence_init_pool(void)
unsigned long addr = (unsigned long)__kfence_pool;
int i;

- /*
- * Protect the first 2 pages. The first page is mostly unnecessary, and
- * merely serves as an extended guard page. However, adding one
- * additional page in the beginning gives us an even number of pages,
- * which simplifies the mapping of address to metadata index.
- */
- for (i = 0; i < 2; i++, addr += PAGE_SIZE)
- kfence_protect(addr);
-
for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++, addr += 2 * PAGE_SIZE) {
struct kfence_metadata *meta = &kfence_metadata[i];
- struct slab *slab = page_slab(virt_to_page(addr));
+ struct slab *slab = page_slab(virt_to_page(addr + PAGE_SIZE));

/* Initialize metadata. */
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(). */
+ meta->addr = addr + PAGE_SIZE;
list_add_tail(&meta->list, &kfence_freelist);

- /* Protect the right redzone. */
- kfence_protect(addr + PAGE_SIZE);
+ /* Protect the left redzone. */
+ kfence_protect(addr);

__folio_set_slab(slab_folio(slab));
#ifdef CONFIG_MEMCG
slab->memcg_data = (unsigned long)&meta->objcg | MEMCG_DATA_OBJCGS;
#endif
}
+
+ kfence_protect(addr);
}

static bool __init kfence_init_pool_early(void)
@@ -1043,7 +1019,7 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs

atomic_long_inc(&counters[KFENCE_COUNTER_BUGS]);

- if (page_index % 2) {
+ if (page_index % 2 == 0) {
/* This is a redzone, report a buffer overflow. */
struct kfence_metadata *meta;
int distance = 0;
diff --git a/mm/kfence/kfence.h b/mm/kfence/kfence.h
index 600f2e2431d6..249d420100a7 100644
--- a/mm/kfence/kfence.h
+++ b/mm/kfence/kfence.h
@@ -110,7 +110,7 @@ static inline struct kfence_metadata *addr_to_metadata(unsigned long addr)
* __kfence_pool, in which case we would report an "invalid access"
* error.
*/
- index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2) - 1;
+ index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2);
if (index < 0 || index >= CONFIG_KFENCE_NUM_OBJECTS)
return NULL;

diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c
index b5d66a69200d..d479f9c8afb1 100644
--- a/mm/kfence/kfence_test.c
+++ b/mm/kfence/kfence_test.c
@@ -637,19 +637,6 @@ static void test_gfpzero(struct kunit *test)
KUNIT_EXPECT_FALSE(test, report_available());
}

-static void test_invalid_access(struct kunit *test)
-{
- const struct expect_report expect = {
- .type = KFENCE_ERROR_INVALID,
- .fn = test_invalid_access,
- .addr = &__kfence_pool[10],
- .is_write = false,
- };
-
- READ_ONCE(__kfence_pool[10]);
- KUNIT_EXPECT_TRUE(test, report_matches(&expect));
-}
-
/* Test SLAB_TYPESAFE_BY_RCU works. */
static void test_memcache_typesafe_by_rcu(struct kunit *test)
{
@@ -787,7 +774,6 @@ static struct kunit_case kfence_test_cases[] = {
KUNIT_CASE(test_kmalloc_aligned_oob_write),
KUNIT_CASE(test_shrink_memcache),
KUNIT_CASE(test_memcache_ctor),
- KUNIT_CASE(test_invalid_access),
KUNIT_CASE(test_gfpzero),
KUNIT_CASE(test_memcache_typesafe_by_rcu),
KUNIT_CASE(test_krealloc),
--
2.11.0

2023-03-28 10:21:22

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH 2/6] mm: kfence: check kfence pool size at building time

On Tue, 28 Mar 2023 at 11:58, 'Muchun Song' via kasan-dev
<[email protected]> wrote:
>
> Check kfence pool size at building time to expose problem ASAP.
>
> Signed-off-by: Muchun Song <[email protected]>
> ---
> mm/kfence/core.c | 7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> index de62a84d4830..6781af1dfa66 100644
> --- a/mm/kfence/core.c
> +++ b/mm/kfence/core.c
> @@ -841,10 +841,9 @@ static int kfence_init_late(void)
> return -ENOMEM;
> __kfence_pool = page_to_virt(pages);
> #else
> - if (nr_pages > MAX_ORDER_NR_PAGES) {
> - pr_warn("KFENCE_NUM_OBJECTS too large for buddy allocator\n");
> - return -EINVAL;
> - }
> + BUILD_BUG_ON_MSG(get_order(KFENCE_POOL_SIZE) > MAX_ORDER,
> + "CONFIG_KFENCE_NUM_OBJECTS is too large for buddy allocator");
> +

It's perfectly valid to want to use KFENCE with a very large pool that
is initialized on boot, and simply sacrifice the ability to initialize
late.

Nack.

2023-03-28 11:57:51

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH 1/6] mm: kfence: simplify kfence pool initialization

On Tue, 28 Mar 2023 at 11:58, Muchun Song <[email protected]> wrote:
>
> There are three similar loops to initialize kfence pool, we could merge
> all of them into one loop to simplify the code and make code more
> efficient.
>
> Signed-off-by: Muchun Song <[email protected]>

Reviewed-by: Marco Elver <[email protected]>

> ---
> mm/kfence/core.c | 47 ++++++-----------------------------------------
> 1 file changed, 6 insertions(+), 41 deletions(-)
>
> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> index 7d01a2c76e80..de62a84d4830 100644
> --- a/mm/kfence/core.c
> +++ b/mm/kfence/core.c
> @@ -539,35 +539,10 @@ static void rcu_guarded_free(struct rcu_head *h)
> static unsigned long kfence_init_pool(void)
> {
> unsigned long addr = (unsigned long)__kfence_pool;
> - struct page *pages;
> int i;
>
> if (!arch_kfence_init_pool())
> return addr;
> -
> - pages = virt_to_page(__kfence_pool);
> -
> - /*
> - * Set up object pages: they must have PG_slab set, to avoid freeing
> - * these as real pages.
> - *
> - * We also want to avoid inserting kfence_free() in the kfree()
> - * fast-path in SLUB, and therefore need to ensure kfree() correctly
> - * enters __slab_free() slow-path.
> - */
> - for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
> - struct slab *slab = page_slab(nth_page(pages, i));
> -
> - if (!i || (i % 2))
> - continue;
> -
> - __folio_set_slab(slab_folio(slab));
> -#ifdef CONFIG_MEMCG
> - slab->memcg_data = (unsigned long)&kfence_metadata[i / 2 - 1].objcg |
> - MEMCG_DATA_OBJCGS;
> -#endif
> - }
> -
> /*
> * Protect the first 2 pages. The first page is mostly unnecessary, and
> * merely serves as an extended guard page. However, adding one
> @@ -581,8 +556,9 @@ static unsigned long kfence_init_pool(void)
> addr += PAGE_SIZE;
> }
>
> - for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
> + for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++, addr += 2 * PAGE_SIZE) {
> struct kfence_metadata *meta = &kfence_metadata[i];
> + struct slab *slab = page_slab(virt_to_page(addr));
>
> /* Initialize metadata. */
> INIT_LIST_HEAD(&meta->list);
> @@ -593,26 +569,15 @@ static unsigned long kfence_init_pool(void)
>
> /* Protect the right redzone. */
> if (unlikely(!kfence_protect(addr + PAGE_SIZE)))
> - goto reset_slab;
> -
> - addr += 2 * PAGE_SIZE;
> - }
> -
> - return 0;
> -
> -reset_slab:
> - for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
> - struct slab *slab = page_slab(nth_page(pages, i));
> + return addr;
>
> - if (!i || (i % 2))
> - continue;
> + __folio_set_slab(slab_folio(slab));
> #ifdef CONFIG_MEMCG
> - slab->memcg_data = 0;
> + slab->memcg_data = (unsigned long)&meta->objcg | MEMCG_DATA_OBJCGS;
> #endif
> - __folio_clear_slab(slab_folio(slab));
> }
>
> - return addr;
> + return 0;
> }
>
> static bool __init kfence_init_pool_early(void)
> --
> 2.11.0
>

2023-03-28 11:58:03

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH 6/6] mm: kfence: replace ALIGN_DOWN(x, PAGE_SIZE) with PAGE_ALIGN_DOWN(x)

On Tue, 28 Mar 2023 at 11:59, 'Muchun Song' via kasan-dev
<[email protected]> wrote:
>
> Replace ALIGN_DOWN(x, PAGE_SIZE) with PAGE_ALIGN_DOWN(x) to simplify
> the code a bit.
>
> Signed-off-by: Muchun Song <[email protected]>

Reviewed-by: Marco Elver <[email protected]>

> ---
> mm/kfence/core.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> index f205b860f460..dbfb79a4d624 100644
> --- a/mm/kfence/core.c
> +++ b/mm/kfence/core.c
> @@ -230,17 +230,17 @@ static bool alloc_covered_contains(u32 alloc_stack_hash)
>
> static inline void kfence_protect(unsigned long addr)
> {
> - kfence_protect_page(ALIGN_DOWN(addr, PAGE_SIZE), true);
> + kfence_protect_page(PAGE_ALIGN_DOWN(addr), true);
> }
>
> static inline void kfence_unprotect(unsigned long addr)
> {
> - kfence_protect_page(ALIGN_DOWN(addr, PAGE_SIZE), false);
> + kfence_protect_page(PAGE_ALIGN_DOWN(addr), false);
> }
>
> static inline unsigned long metadata_to_pageaddr(const struct kfence_metadata *meta)
> {
> - return ALIGN_DOWN(meta->addr, PAGE_SIZE);
> + return PAGE_ALIGN_DOWN(meta->addr);
> }
>
> /*
> @@ -308,7 +308,7 @@ static inline bool check_canary_byte(u8 *addr)
> /* __always_inline this to ensure we won't do an indirect call to fn. */
> static __always_inline void for_each_canary(const struct kfence_metadata *meta, bool (*fn)(u8 *))
> {
> - const unsigned long pageaddr = ALIGN_DOWN(meta->addr, PAGE_SIZE);
> + const unsigned long pageaddr = PAGE_ALIGN_DOWN(meta->addr);
> unsigned long addr;
>
> /*
> @@ -455,7 +455,7 @@ static void kfence_guarded_free(void *addr, struct kfence_metadata *meta, bool z
> }
>
> /* Detect racy use-after-free, or incorrect reallocation of this page by KFENCE. */
> - kcsan_begin_scoped_access((void *)ALIGN_DOWN((unsigned long)addr, PAGE_SIZE), PAGE_SIZE,
> + kcsan_begin_scoped_access((void *)PAGE_ALIGN_DOWN((unsigned long)addr), PAGE_SIZE,
> KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT,
> &assert_page_exclusive);
>
> @@ -464,7 +464,7 @@ static void kfence_guarded_free(void *addr, struct kfence_metadata *meta, bool z
>
> /* Restore page protection if there was an OOB access. */
> if (meta->unprotected_page) {
> - memzero_explicit((void *)ALIGN_DOWN(meta->unprotected_page, PAGE_SIZE), PAGE_SIZE);
> + memzero_explicit((void *)PAGE_ALIGN_DOWN(meta->unprotected_page), PAGE_SIZE);
> kfence_protect(meta->unprotected_page);
> meta->unprotected_page = 0;
> }
> --
> 2.11.0
>
> --
> You received this message because you are subscribed to the Google Groups "kasan-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20230328095807.7014-7-songmuchun%40bytedance.com.

2023-03-28 12:08:55

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH 1/6] mm: kfence: simplify kfence pool initialization

On Tue, 28 Mar 2023 at 13:55, Marco Elver <[email protected]> wrote:
>
> On Tue, 28 Mar 2023 at 11:58, Muchun Song <[email protected]> wrote:
> >
> > There are three similar loops to initialize kfence pool, we could merge
> > all of them into one loop to simplify the code and make code more
> > efficient.
> >
> > Signed-off-by: Muchun Song <[email protected]>
>
> Reviewed-by: Marco Elver <[email protected]>
>
> > ---
> > mm/kfence/core.c | 47 ++++++-----------------------------------------
> > 1 file changed, 6 insertions(+), 41 deletions(-)
> >
> > diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> > index 7d01a2c76e80..de62a84d4830 100644
> > --- a/mm/kfence/core.c
> > +++ b/mm/kfence/core.c
> > @@ -539,35 +539,10 @@ static void rcu_guarded_free(struct rcu_head *h)
> > static unsigned long kfence_init_pool(void)
> > {
> > unsigned long addr = (unsigned long)__kfence_pool;
> > - struct page *pages;
> > int i;
> >
> > if (!arch_kfence_init_pool())
> > return addr;
> > -
> > - pages = virt_to_page(__kfence_pool);
> > -
> > - /*
> > - * Set up object pages: they must have PG_slab set, to avoid freeing
> > - * these as real pages.
> > - *
> > - * We also want to avoid inserting kfence_free() in the kfree()
> > - * fast-path in SLUB, and therefore need to ensure kfree() correctly
> > - * enters __slab_free() slow-path.
> > - */

Actually: can you retain this comment somewhere?

> > - for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
> > - struct slab *slab = page_slab(nth_page(pages, i));
> > -
> > - if (!i || (i % 2))
> > - continue;
> > -
> > - __folio_set_slab(slab_folio(slab));
> > -#ifdef CONFIG_MEMCG
> > - slab->memcg_data = (unsigned long)&kfence_metadata[i / 2 - 1].objcg |
> > - MEMCG_DATA_OBJCGS;
> > -#endif
> > - }
> > -
> > /*
> > * Protect the first 2 pages. The first page is mostly unnecessary, and
> > * merely serves as an extended guard page. However, adding one
> > @@ -581,8 +556,9 @@ static unsigned long kfence_init_pool(void)
> > addr += PAGE_SIZE;
> > }
> >
> > - for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++) {
> > + for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++, addr += 2 * PAGE_SIZE) {
> > struct kfence_metadata *meta = &kfence_metadata[i];
> > + struct slab *slab = page_slab(virt_to_page(addr));
> >
> > /* Initialize metadata. */
> > INIT_LIST_HEAD(&meta->list);
> > @@ -593,26 +569,15 @@ static unsigned long kfence_init_pool(void)
> >
> > /* Protect the right redzone. */
> > if (unlikely(!kfence_protect(addr + PAGE_SIZE)))
> > - goto reset_slab;
> > -
> > - addr += 2 * PAGE_SIZE;
> > - }
> > -
> > - return 0;
> > -
> > -reset_slab:
> > - for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
> > - struct slab *slab = page_slab(nth_page(pages, i));
> > + return addr;
> >
> > - if (!i || (i % 2))
> > - continue;
> > + __folio_set_slab(slab_folio(slab));
> > #ifdef CONFIG_MEMCG
> > - slab->memcg_data = 0;
> > + slab->memcg_data = (unsigned long)&meta->objcg | MEMCG_DATA_OBJCGS;
> > #endif
> > - __folio_clear_slab(slab_folio(slab));
> > }
> >
> > - return addr;
> > + return 0;
> > }
> >
> > static bool __init kfence_init_pool_early(void)
> > --
> > 2.11.0
> >

2023-03-28 13:03:57

by Muchun Song

[permalink] [raw]
Subject: Re: [PATCH 1/6] mm: kfence: simplify kfence pool initialization



> On Mar 28, 2023, at 20:05, Marco Elver <[email protected]> wrote:
>
> On Tue, 28 Mar 2023 at 13:55, Marco Elver <[email protected]> wrote:
>>
>> On Tue, 28 Mar 2023 at 11:58, Muchun Song <[email protected]> wrote:
>>>
>>> There are three similar loops to initialize kfence pool, we could merge
>>> all of them into one loop to simplify the code and make code more
>>> efficient.
>>>
>>> Signed-off-by: Muchun Song <[email protected]>
>>
>> Reviewed-by: Marco Elver <[email protected]>
>>
>>> ---
>>> mm/kfence/core.c | 47 ++++++-----------------------------------------
>>> 1 file changed, 6 insertions(+), 41 deletions(-)
>>>
>>> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
>>> index 7d01a2c76e80..de62a84d4830 100644
>>> --- a/mm/kfence/core.c
>>> +++ b/mm/kfence/core.c
>>> @@ -539,35 +539,10 @@ static void rcu_guarded_free(struct rcu_head *h)
>>> static unsigned long kfence_init_pool(void)
>>> {
>>> unsigned long addr = (unsigned long)__kfence_pool;
>>> - struct page *pages;
>>> int i;
>>>
>>> if (!arch_kfence_init_pool())
>>> return addr;
>>> -
>>> - pages = virt_to_page(__kfence_pool);
>>> -
>>> - /*
>>> - * Set up object pages: they must have PG_slab set, to avoid freeing
>>> - * these as real pages.
>>> - *
>>> - * We also want to avoid inserting kfence_free() in the kfree()
>>> - * fast-path in SLUB, and therefore need to ensure kfree() correctly
>>> - * enters __slab_free() slow-path.
>>> - */
>
> Actually: can you retain this comment somewhere?

Sure, I'll move this to right place.

Thanks.

2023-03-28 13:08:34

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH 5/6] mm: kfence: change kfence pool page layout

On Tue, 28 Mar 2023 at 11:58, 'Muchun Song' via kasan-dev
<[email protected]> wrote:
>
> The original kfence pool layout (Given a layout with 2 objects):
>
> +------------+------------+------------+------------+------------+------------+
> | guard page | guard page | object | guard page | object | guard page |
> +------------+------------+------------+------------+------------+------------+
> | | |
> +----kfence_metadata[0]---+----kfence_metadata[1]---+
>
> The comment says "the additional page in the beginning gives us an even
> number of pages, which simplifies the mapping of address to metadata index".
>
> However, removing the additional page does not complicate any mapping
> calculations. So changing it to the new layout to save a page. And remmove
> the KFENCE_ERROR_INVALID test since we cannot test this case easily.
>
> The new kfence pool layout (Given a layout with 2 objects):
>
> +------------+------------+------------+------------+------------+
> | guard page | object | guard page | object | guard page |
> +------------+------------+------------+------------+------------+
> | | |
> +----kfence_metadata[0]---+----kfence_metadata[1]---+
>
> Signed-off-by: Muchun Song <[email protected]>
> ---
> include/linux/kfence.h | 8 ++------
> mm/kfence/core.c | 40 ++++++++--------------------------------
> mm/kfence/kfence.h | 2 +-
> mm/kfence/kfence_test.c | 14 --------------
> 4 files changed, 11 insertions(+), 53 deletions(-)
>
> diff --git a/include/linux/kfence.h b/include/linux/kfence.h
> index 726857a4b680..25b13a892717 100644
> --- a/include/linux/kfence.h
> +++ b/include/linux/kfence.h
> @@ -19,12 +19,8 @@
>
> extern unsigned long kfence_sample_interval;
>
> -/*
> - * We allocate an even number of pages, as it simplifies calculations to map
> - * address to metadata indices; effectively, the very first page serves as an
> - * extended guard page, but otherwise has no special purpose.
> - */
> -#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 * PAGE_SIZE)
> +/* The last page serves as an extended guard page. */

The last page is just a normal guard page? I.e. the last 2 pages are:
<object page> | <guard page>

Or did I misunderstand?

> +#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS * 2 + 1) * PAGE_SIZE)
> extern char *__kfence_pool;
>
> DECLARE_STATIC_KEY_FALSE(kfence_allocation_key);
> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> index 41befcb3b069..f205b860f460 100644
> --- a/mm/kfence/core.c
> +++ b/mm/kfence/core.c
> @@ -240,24 +240,7 @@ static inline void kfence_unprotect(unsigned long addr)
>
> static inline unsigned long metadata_to_pageaddr(const struct kfence_metadata *meta)
> {
> - unsigned long offset = (meta - kfence_metadata + 1) * PAGE_SIZE * 2;
> - unsigned long pageaddr = (unsigned long)&__kfence_pool[offset];
> -
> - /* The checks do not affect performance; only called from slow-paths. */
> -
> - /* Only call with a pointer into kfence_metadata. */
> - if (KFENCE_WARN_ON(meta < kfence_metadata ||
> - meta >= kfence_metadata + CONFIG_KFENCE_NUM_OBJECTS))
> - return 0;

Could we retain this WARN_ON? Or just get rid of
metadata_to_pageaddr() altogether, because there's only 1 use left and
the function would now just be a simple ALIGN_DOWN() anyway.

> - /*
> - * This metadata object only ever maps to 1 page; verify that the stored
> - * address is in the expected range.
> - */
> - if (KFENCE_WARN_ON(ALIGN_DOWN(meta->addr, PAGE_SIZE) != pageaddr))
> - return 0;
> -
> - return pageaddr;
> + return ALIGN_DOWN(meta->addr, PAGE_SIZE);
> }
>
> /*
> @@ -535,34 +518,27 @@ static void kfence_init_pool(void)
> unsigned long addr = (unsigned long)__kfence_pool;
> int i;
>
> - /*
> - * Protect the first 2 pages. The first page is mostly unnecessary, and
> - * merely serves as an extended guard page. However, adding one
> - * additional page in the beginning gives us an even number of pages,
> - * which simplifies the mapping of address to metadata index.
> - */
> - for (i = 0; i < 2; i++, addr += PAGE_SIZE)
> - kfence_protect(addr);
> -
> for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++, addr += 2 * PAGE_SIZE) {
> struct kfence_metadata *meta = &kfence_metadata[i];
> - struct slab *slab = page_slab(virt_to_page(addr));
> + struct slab *slab = page_slab(virt_to_page(addr + PAGE_SIZE));
>
> /* Initialize metadata. */
> 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(). */
> + meta->addr = addr + PAGE_SIZE;
> list_add_tail(&meta->list, &kfence_freelist);
>
> - /* Protect the right redzone. */
> - kfence_protect(addr + PAGE_SIZE);
> + /* Protect the left redzone. */
> + kfence_protect(addr);
>
> __folio_set_slab(slab_folio(slab));
> #ifdef CONFIG_MEMCG
> slab->memcg_data = (unsigned long)&meta->objcg | MEMCG_DATA_OBJCGS;
> #endif
> }
> +
> + kfence_protect(addr);
> }
>
> static bool __init kfence_init_pool_early(void)
> @@ -1043,7 +1019,7 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs
>
> atomic_long_inc(&counters[KFENCE_COUNTER_BUGS]);
>
> - if (page_index % 2) {
> + if (page_index % 2 == 0) {
> /* This is a redzone, report a buffer overflow. */
> struct kfence_metadata *meta;
> int distance = 0;
> diff --git a/mm/kfence/kfence.h b/mm/kfence/kfence.h
> index 600f2e2431d6..249d420100a7 100644
> --- a/mm/kfence/kfence.h
> +++ b/mm/kfence/kfence.h
> @@ -110,7 +110,7 @@ static inline struct kfence_metadata *addr_to_metadata(unsigned long addr)
> * __kfence_pool, in which case we would report an "invalid access"
> * error.
> */
> - index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2) - 1;
> + index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2);
> if (index < 0 || index >= CONFIG_KFENCE_NUM_OBJECTS)
> return NULL;

Assume there is a right OOB that hit the last guard page. In this case

addr >= __kfence_pool + (NUM_OBJECTS * 2 * PAGE_SIZE) && addr <
__kfence_pool + POOL_SIZE

therefore

index >= (NUM_OBJECTS * 2 * PAGE_SIZE) / (PAGE_SIZE * 2) && index <
POOL_SIZE / (PAGE_SIZE * 2)
index == NUM_OBJECTS

And according to the above comparison, this will return NULL and
report KFENCE_ERROR_INVALID, which is wrong.

> diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c
> index b5d66a69200d..d479f9c8afb1 100644
> --- a/mm/kfence/kfence_test.c
> +++ b/mm/kfence/kfence_test.c
> @@ -637,19 +637,6 @@ static void test_gfpzero(struct kunit *test)
> KUNIT_EXPECT_FALSE(test, report_available());
> }
>
> -static void test_invalid_access(struct kunit *test)
> -{
> - const struct expect_report expect = {
> - .type = KFENCE_ERROR_INVALID,
> - .fn = test_invalid_access,
> - .addr = &__kfence_pool[10],
> - .is_write = false,
> - };
> -
> - READ_ONCE(__kfence_pool[10]);
> - KUNIT_EXPECT_TRUE(test, report_matches(&expect));
> -}
> -
> /* Test SLAB_TYPESAFE_BY_RCU works. */
> static void test_memcache_typesafe_by_rcu(struct kunit *test)
> {
> @@ -787,7 +774,6 @@ static struct kunit_case kfence_test_cases[] = {
> KUNIT_CASE(test_kmalloc_aligned_oob_write),
> KUNIT_CASE(test_shrink_memcache),
> KUNIT_CASE(test_memcache_ctor),
> - KUNIT_CASE(test_invalid_access),

The test can be retained by doing an access to a guard page in between
2 unallocated objects. But it's probably not that easy to reliably set
that up (could try to allocate 2 objects and see if they're next to
each other, then free them).

2023-03-28 13:09:57

by Muchun Song

[permalink] [raw]
Subject: Re: [PATCH 2/6] mm: kfence: check kfence pool size at building time



> On Mar 28, 2023, at 18:14, Marco Elver <[email protected]> wrote:
>
> On Tue, 28 Mar 2023 at 11:58, 'Muchun Song' via kasan-dev
> <[email protected]> wrote:
>>
>> Check kfence pool size at building time to expose problem ASAP.
>>
>> Signed-off-by: Muchun Song <[email protected]>
>> ---
>> mm/kfence/core.c | 7 +++----
>> 1 file changed, 3 insertions(+), 4 deletions(-)
>>
>> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
>> index de62a84d4830..6781af1dfa66 100644
>> --- a/mm/kfence/core.c
>> +++ b/mm/kfence/core.c
>> @@ -841,10 +841,9 @@ static int kfence_init_late(void)
>> return -ENOMEM;
>> __kfence_pool = page_to_virt(pages);
>> #else
>> - if (nr_pages > MAX_ORDER_NR_PAGES) {
>> - pr_warn("KFENCE_NUM_OBJECTS too large for buddy allocator\n");
>> - return -EINVAL;
>> - }
>> + BUILD_BUG_ON_MSG(get_order(KFENCE_POOL_SIZE) > MAX_ORDER,
>> + "CONFIG_KFENCE_NUM_OBJECTS is too large for buddy allocator");
>> +
>
> It's perfectly valid to want to use KFENCE with a very large pool that
> is initialized on boot, and simply sacrifice the ability to initialize
> late.

You are right. I didn’t realize this.

Thanks

>
> Nack.


2023-03-28 13:35:48

by Muchun Song

[permalink] [raw]
Subject: Re: [PATCH 5/6] mm: kfence: change kfence pool page layout



> On Mar 28, 2023, at 20:59, Marco Elver <[email protected]> wrote:
>
> On Tue, 28 Mar 2023 at 11:58, 'Muchun Song' via kasan-dev
> <[email protected]> wrote:
>>
>> The original kfence pool layout (Given a layout with 2 objects):
>>
>> +------------+------------+------------+------------+------------+------------+
>> | guard page | guard page | object | guard page | object | guard page |
>> +------------+------------+------------+------------+------------+------------+
>> | | |
>> +----kfence_metadata[0]---+----kfence_metadata[1]---+
>>
>> The comment says "the additional page in the beginning gives us an even
>> number of pages, which simplifies the mapping of address to metadata index".
>>
>> However, removing the additional page does not complicate any mapping
>> calculations. So changing it to the new layout to save a page. And remmove
>> the KFENCE_ERROR_INVALID test since we cannot test this case easily.
>>
>> The new kfence pool layout (Given a layout with 2 objects):
>>
>> +------------+------------+------------+------------+------------+
>> | guard page | object | guard page | object | guard page |
>> +------------+------------+------------+------------+------------+
>> | | |
>> +----kfence_metadata[0]---+----kfence_metadata[1]---+
>>
>> Signed-off-by: Muchun Song <[email protected]>
>> ---
>> include/linux/kfence.h | 8 ++------
>> mm/kfence/core.c | 40 ++++++++--------------------------------
>> mm/kfence/kfence.h | 2 +-
>> mm/kfence/kfence_test.c | 14 --------------
>> 4 files changed, 11 insertions(+), 53 deletions(-)
>>
>> diff --git a/include/linux/kfence.h b/include/linux/kfence.h
>> index 726857a4b680..25b13a892717 100644
>> --- a/include/linux/kfence.h
>> +++ b/include/linux/kfence.h
>> @@ -19,12 +19,8 @@
>>
>> extern unsigned long kfence_sample_interval;
>>
>> -/*
>> - * We allocate an even number of pages, as it simplifies calculations to map
>> - * address to metadata indices; effectively, the very first page serves as an
>> - * extended guard page, but otherwise has no special purpose.
>> - */
>> -#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 * PAGE_SIZE)
>> +/* The last page serves as an extended guard page. */
>
> The last page is just a normal guard page? I.e. the last 2 pages are:
> <object page> | <guard page>

Right.

The new kfence pool layout (Given a layout with 2 objects):

+------------+------------+------------+------------+------------+
| guard page | object | guard page | object | guard page |
+------------+------------+------------+------------+------------+
| | | ^
+----kfence_metadata[0]---+----kfence_metadata[1]---+ |
|
|
the last page

>
> Or did I misunderstand?
>
>> +#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS * 2 + 1) * PAGE_SIZE)
>> extern char *__kfence_pool;
>>
>> DECLARE_STATIC_KEY_FALSE(kfence_allocation_key);
>> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
>> index 41befcb3b069..f205b860f460 100644
>> --- a/mm/kfence/core.c
>> +++ b/mm/kfence/core.c
>> @@ -240,24 +240,7 @@ static inline void kfence_unprotect(unsigned long addr)
>>
>> static inline unsigned long metadata_to_pageaddr(const struct kfence_metadata *meta)
>> {
>> - unsigned long offset = (meta - kfence_metadata + 1) * PAGE_SIZE * 2;
>> - unsigned long pageaddr = (unsigned long)&__kfence_pool[offset];
>> -
>> - /* The checks do not affect performance; only called from slow-paths. */
>> -
>> - /* Only call with a pointer into kfence_metadata. */
>> - if (KFENCE_WARN_ON(meta < kfence_metadata ||
>> - meta >= kfence_metadata + CONFIG_KFENCE_NUM_OBJECTS))
>> - return 0;
>
> Could we retain this WARN_ON? Or just get rid of
> metadata_to_pageaddr() altogether, because there's only 1 use left and
> the function would now just be a simple ALIGN_DOWN() anyway.

I'll inline this function to its caller since the warning is unlikely.

>
>> - /*
>> - * This metadata object only ever maps to 1 page; verify that the stored
>> - * address is in the expected range.
>> - */
>> - if (KFENCE_WARN_ON(ALIGN_DOWN(meta->addr, PAGE_SIZE) != pageaddr))
>> - return 0;
>> -
>> - return pageaddr;
>> + return ALIGN_DOWN(meta->addr, PAGE_SIZE);
>> }
>>
>> /*
>> @@ -535,34 +518,27 @@ static void kfence_init_pool(void)
>> unsigned long addr = (unsigned long)__kfence_pool;
>> int i;
>>
>> - /*
>> - * Protect the first 2 pages. The first page is mostly unnecessary, and
>> - * merely serves as an extended guard page. However, adding one
>> - * additional page in the beginning gives us an even number of pages,
>> - * which simplifies the mapping of address to metadata index.
>> - */
>> - for (i = 0; i < 2; i++, addr += PAGE_SIZE)
>> - kfence_protect(addr);
>> -
>> for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++, addr += 2 * PAGE_SIZE) {
>> struct kfence_metadata *meta = &kfence_metadata[i];
>> - struct slab *slab = page_slab(virt_to_page(addr));
>> + struct slab *slab = page_slab(virt_to_page(addr + PAGE_SIZE));
>>
>> /* Initialize metadata. */
>> 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(). */
>> + meta->addr = addr + PAGE_SIZE;
>> list_add_tail(&meta->list, &kfence_freelist);
>>
>> - /* Protect the right redzone. */
>> - kfence_protect(addr + PAGE_SIZE);
>> + /* Protect the left redzone. */
>> + kfence_protect(addr);
>>
>> __folio_set_slab(slab_folio(slab));
>> #ifdef CONFIG_MEMCG
>> slab->memcg_data = (unsigned long)&meta->objcg | MEMCG_DATA_OBJCGS;
>> #endif
>> }
>> +
>> + kfence_protect(addr);
>> }
>>
>> static bool __init kfence_init_pool_early(void)
>> @@ -1043,7 +1019,7 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs
>>
>> atomic_long_inc(&counters[KFENCE_COUNTER_BUGS]);
>>
>> - if (page_index % 2) {
>> + if (page_index % 2 == 0) {
>> /* This is a redzone, report a buffer overflow. */
>> struct kfence_metadata *meta;
>> int distance = 0;
>> diff --git a/mm/kfence/kfence.h b/mm/kfence/kfence.h
>> index 600f2e2431d6..249d420100a7 100644
>> --- a/mm/kfence/kfence.h
>> +++ b/mm/kfence/kfence.h
>> @@ -110,7 +110,7 @@ static inline struct kfence_metadata *addr_to_metadata(unsigned long addr)
>> * __kfence_pool, in which case we would report an "invalid access"
>> * error.
>> */
>> - index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2) - 1;
>> + index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2);
>> if (index < 0 || index >= CONFIG_KFENCE_NUM_OBJECTS)
>> return NULL;
>
> Assume there is a right OOB that hit the last guard page. In this case
>
> addr >= __kfence_pool + (NUM_OBJECTS * 2 * PAGE_SIZE) && addr <
> __kfence_pool + POOL_SIZE
>
> therefore
>
> index >= (NUM_OBJECTS * 2 * PAGE_SIZE) / (PAGE_SIZE * 2) && index <
> POOL_SIZE / (PAGE_SIZE * 2)
> index == NUM_OBJECTS
>
> And according to the above comparison, this will return NULL and
> report KFENCE_ERROR_INVALID, which is wrong.

Look at kfence_handle_page_fault(), which first look up "addr - PAGE_SIZE" (passed
to addr_to_metadata()) and then look up "addr + PAGE_SIZE", the former will not
return NULL, the latter will return NULL. So kfence will report KFENCE_ERROR_OOB
in this case, right? Or what I missed here?

>
>> diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c
>> index b5d66a69200d..d479f9c8afb1 100644
>> --- a/mm/kfence/kfence_test.c
>> +++ b/mm/kfence/kfence_test.c
>> @@ -637,19 +637,6 @@ static void test_gfpzero(struct kunit *test)
>> KUNIT_EXPECT_FALSE(test, report_available());
>> }
>>
>> -static void test_invalid_access(struct kunit *test)
>> -{
>> - const struct expect_report expect = {
>> - .type = KFENCE_ERROR_INVALID,
>> - .fn = test_invalid_access,
>> - .addr = &__kfence_pool[10],
>> - .is_write = false,
>> - };
>> -
>> - READ_ONCE(__kfence_pool[10]);
>> - KUNIT_EXPECT_TRUE(test, report_matches(&expect));
>> -}
>> -
>> /* Test SLAB_TYPESAFE_BY_RCU works. */
>> static void test_memcache_typesafe_by_rcu(struct kunit *test)
>> {
>> @@ -787,7 +774,6 @@ static struct kunit_case kfence_test_cases[] = {
>> KUNIT_CASE(test_kmalloc_aligned_oob_write),
>> KUNIT_CASE(test_shrink_memcache),
>> KUNIT_CASE(test_memcache_ctor),
>> - KUNIT_CASE(test_invalid_access),
>
> The test can be retained by doing an access to a guard page in between
> 2 unallocated objects. But it's probably not that easy to reliably set
> that up (could try to allocate 2 objects and see if they're next to
> each other, then free them).

Yes, it's not easy to trigger it 100%. So I removed the test.


2023-03-28 14:18:41

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH 5/6] mm: kfence: change kfence pool page layout

On Tue, 28 Mar 2023 at 15:33, Muchun Song <[email protected]> wrote:
>
>
>
> > On Mar 28, 2023, at 20:59, Marco Elver <[email protected]> wrote:
> >
> > On Tue, 28 Mar 2023 at 11:58, 'Muchun Song' via kasan-dev
> > <[email protected]> wrote:
> >>
> >> The original kfence pool layout (Given a layout with 2 objects):
> >>
> >> +------------+------------+------------+------------+------------+------------+
> >> | guard page | guard page | object | guard page | object | guard page |
> >> +------------+------------+------------+------------+------------+------------+
> >> | | |
> >> +----kfence_metadata[0]---+----kfence_metadata[1]---+
> >>
> >> The comment says "the additional page in the beginning gives us an even
> >> number of pages, which simplifies the mapping of address to metadata index".
> >>
> >> However, removing the additional page does not complicate any mapping
> >> calculations. So changing it to the new layout to save a page. And remmove
> >> the KFENCE_ERROR_INVALID test since we cannot test this case easily.
> >>
> >> The new kfence pool layout (Given a layout with 2 objects):
> >>
> >> +------------+------------+------------+------------+------------+
> >> | guard page | object | guard page | object | guard page |
> >> +------------+------------+------------+------------+------------+
> >> | | |
> >> +----kfence_metadata[0]---+----kfence_metadata[1]---+
> >>
> >> Signed-off-by: Muchun Song <[email protected]>
> >> ---
> >> include/linux/kfence.h | 8 ++------
> >> mm/kfence/core.c | 40 ++++++++--------------------------------
> >> mm/kfence/kfence.h | 2 +-
> >> mm/kfence/kfence_test.c | 14 --------------
> >> 4 files changed, 11 insertions(+), 53 deletions(-)
> >>
> >> diff --git a/include/linux/kfence.h b/include/linux/kfence.h
> >> index 726857a4b680..25b13a892717 100644
> >> --- a/include/linux/kfence.h
> >> +++ b/include/linux/kfence.h
> >> @@ -19,12 +19,8 @@
> >>
> >> extern unsigned long kfence_sample_interval;
> >>
> >> -/*
> >> - * We allocate an even number of pages, as it simplifies calculations to map
> >> - * address to metadata indices; effectively, the very first page serves as an
> >> - * extended guard page, but otherwise has no special purpose.
> >> - */
> >> -#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 * PAGE_SIZE)
> >> +/* The last page serves as an extended guard page. */
> >
> > The last page is just a normal guard page? I.e. the last 2 pages are:
> > <object page> | <guard page>
>
> Right.
>
> The new kfence pool layout (Given a layout with 2 objects):
>
> +------------+------------+------------+------------+------------+
> | guard page | object | guard page | object | guard page |
> +------------+------------+------------+------------+------------+
> | | | ^
> +----kfence_metadata[0]---+----kfence_metadata[1]---+ |
> |
> |
> the last page
>
> >
> > Or did I misunderstand?
> >
> >> +#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS * 2 + 1) * PAGE_SIZE)
> >> extern char *__kfence_pool;
> >>
> >> DECLARE_STATIC_KEY_FALSE(kfence_allocation_key);
> >> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> >> index 41befcb3b069..f205b860f460 100644
> >> --- a/mm/kfence/core.c
> >> +++ b/mm/kfence/core.c
> >> @@ -240,24 +240,7 @@ static inline void kfence_unprotect(unsigned long addr)
> >>
> >> static inline unsigned long metadata_to_pageaddr(const struct kfence_metadata *meta)
> >> {
> >> - unsigned long offset = (meta - kfence_metadata + 1) * PAGE_SIZE * 2;
> >> - unsigned long pageaddr = (unsigned long)&__kfence_pool[offset];
> >> -
> >> - /* The checks do not affect performance; only called from slow-paths. */
> >> -
> >> - /* Only call with a pointer into kfence_metadata. */
> >> - if (KFENCE_WARN_ON(meta < kfence_metadata ||
> >> - meta >= kfence_metadata + CONFIG_KFENCE_NUM_OBJECTS))
> >> - return 0;
> >
> > Could we retain this WARN_ON? Or just get rid of
> > metadata_to_pageaddr() altogether, because there's only 1 use left and
> > the function would now just be a simple ALIGN_DOWN() anyway.
>
> I'll inline this function to its caller since the warning is unlikely.
>
> >
> >> - /*
> >> - * This metadata object only ever maps to 1 page; verify that the stored
> >> - * address is in the expected range.
> >> - */
> >> - if (KFENCE_WARN_ON(ALIGN_DOWN(meta->addr, PAGE_SIZE) != pageaddr))
> >> - return 0;
> >> -
> >> - return pageaddr;
> >> + return ALIGN_DOWN(meta->addr, PAGE_SIZE);
> >> }
> >>
> >> /*
> >> @@ -535,34 +518,27 @@ static void kfence_init_pool(void)
> >> unsigned long addr = (unsigned long)__kfence_pool;
> >> int i;
> >>
> >> - /*
> >> - * Protect the first 2 pages. The first page is mostly unnecessary, and
> >> - * merely serves as an extended guard page. However, adding one
> >> - * additional page in the beginning gives us an even number of pages,
> >> - * which simplifies the mapping of address to metadata index.
> >> - */
> >> - for (i = 0; i < 2; i++, addr += PAGE_SIZE)
> >> - kfence_protect(addr);
> >> -
> >> for (i = 0; i < CONFIG_KFENCE_NUM_OBJECTS; i++, addr += 2 * PAGE_SIZE) {
> >> struct kfence_metadata *meta = &kfence_metadata[i];
> >> - struct slab *slab = page_slab(virt_to_page(addr));
> >> + struct slab *slab = page_slab(virt_to_page(addr + PAGE_SIZE));
> >>
> >> /* Initialize metadata. */
> >> 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(). */
> >> + meta->addr = addr + PAGE_SIZE;
> >> list_add_tail(&meta->list, &kfence_freelist);
> >>
> >> - /* Protect the right redzone. */
> >> - kfence_protect(addr + PAGE_SIZE);
> >> + /* Protect the left redzone. */
> >> + kfence_protect(addr);
> >>
> >> __folio_set_slab(slab_folio(slab));
> >> #ifdef CONFIG_MEMCG
> >> slab->memcg_data = (unsigned long)&meta->objcg | MEMCG_DATA_OBJCGS;
> >> #endif
> >> }
> >> +
> >> + kfence_protect(addr);
> >> }
> >>
> >> static bool __init kfence_init_pool_early(void)
> >> @@ -1043,7 +1019,7 @@ bool kfence_handle_page_fault(unsigned long addr, bool is_write, struct pt_regs
> >>
> >> atomic_long_inc(&counters[KFENCE_COUNTER_BUGS]);
> >>
> >> - if (page_index % 2) {
> >> + if (page_index % 2 == 0) {
> >> /* This is a redzone, report a buffer overflow. */
> >> struct kfence_metadata *meta;
> >> int distance = 0;
> >> diff --git a/mm/kfence/kfence.h b/mm/kfence/kfence.h
> >> index 600f2e2431d6..249d420100a7 100644
> >> --- a/mm/kfence/kfence.h
> >> +++ b/mm/kfence/kfence.h
> >> @@ -110,7 +110,7 @@ static inline struct kfence_metadata *addr_to_metadata(unsigned long addr)
> >> * __kfence_pool, in which case we would report an "invalid access"
> >> * error.
> >> */
> >> - index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2) - 1;
> >> + index = (addr - (unsigned long)__kfence_pool) / (PAGE_SIZE * 2);
> >> if (index < 0 || index >= CONFIG_KFENCE_NUM_OBJECTS)
> >> return NULL;
> >
> > Assume there is a right OOB that hit the last guard page. In this case
> >
> > addr >= __kfence_pool + (NUM_OBJECTS * 2 * PAGE_SIZE) && addr <
> > __kfence_pool + POOL_SIZE
> >
> > therefore
> >
> > index >= (NUM_OBJECTS * 2 * PAGE_SIZE) / (PAGE_SIZE * 2) && index <
> > POOL_SIZE / (PAGE_SIZE * 2)
> > index == NUM_OBJECTS
> >
> > And according to the above comparison, this will return NULL and
> > report KFENCE_ERROR_INVALID, which is wrong.
>
> Look at kfence_handle_page_fault(), which first look up "addr - PAGE_SIZE" (passed
> to addr_to_metadata()) and then look up "addr + PAGE_SIZE", the former will not
> return NULL, the latter will return NULL. So kfence will report KFENCE_ERROR_OOB
> in this case, right? Or what I missed here?

Yes, you're right.

Thanks,
-- Marco

2023-04-03 09:02:43

by Alexander Potapenko

[permalink] [raw]
Subject: Re: [PATCH 4/6] mm: kfence: remove useless check for CONFIG_KFENCE_NUM_OBJECTS

On Tue, Mar 28, 2023 at 11:58 AM Muchun Song <[email protected]> wrote:
>
> The CONFIG_KFENCE_NUM_OBJECTS is limited by kconfig and vary from 1 to
> 65535, so CONFIG_KFENCE_NUM_OBJECTS cannot be equabl to or smaller than

Nit: "equal"

> 0. Removing it to simplify code.
>
> Signed-off-by: Muchun Song <[email protected]>
Reviewed-by: Alexander Potapenko <[email protected]>