Skip to contentSkip to conversation

mm: kfence: fix unexpected leak scan on kfence pool

PATCH Applied [email protected] 6 participants 12 messages Jun 28, 2022 – Aug 1, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover mm: kfence: fix unexpected leak scan on kfence pool

From: Yee Lee <[email protected]>

Since the latest patches of kmemleak separated its address management to two rb-trees, phys and virt, the kmemleak_free failed to free kfence_pool from the virt tree. It caused unexpected scan the blocks and triggered kfence faults.

Reference:
https://lore.kernel.org/linux-mm/[email protected]/

==================================================================

 BUG: KFENCE: invalid read in scan_block+0x13c/0x838

Invalid read at 0x000000003dc26873:

  scan_block+0x13c/0x838
  scan_gray_list+0x1f0/0x478
  kmemleak_scan+0x63c/0xd64
  kmemleak_write+0x618/0x8cc
  full_proxy_write+0x70/0x138
  vfs_write+0x108/0x314
  ksys_write+0x7c/0x14c
  __arm64_sys_write+0x20/0x30
  el0_svc_common+0xa8/0x144
  do_el0_svc+0x30/0xd4
  el0_svc+0x38/0x15c
  el0t_64_sync_handler+0x88/0xf8
  el0t_64_sync+0x1a0/0x1a4

 CPU: 0 PID: 128 Comm: sh Not tainted 5.18.0-mainline-40996-g7d83a175ff4a-dirty #1
 Hardware name: linux,dummy-virt (DT)
 ==================================================================

This patch applies kmemleak_ignore_phys() to replace the original kmemleak_free and adapts it to the late enabling case.

v1->v2:
- use kmemleak_ignore_phys() to bypass the scanning.
- move out the freeing opeartion from late enabling as it's trivial.

Yee Lee (1):
mm: kfence: apply kmemleak_ignore_phys on early allocated pool

 mm/kfence/core.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
Signature · 1 line
2.18.0
#1 lore raw

PATCH v2 1/1 mm: kfence: apply kmemleak_ignore_phys on early allocated pool

From: Yee Lee <[email protected]>

This patch solves two issues.

(1) The pool allocated by memblock needs to unregister from kmemleak scanning. Apply kmemleak_ignore_phys to replace the original kmemleak_free as its address now is stored in the phys tree.

(2) The pool late allocated by page-alloc doesn't need to unregister. Move out the freeing operation from its call path.

---
 mm/kfence/core.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
mm/kfence/core.c +9 -9
index 4e7cd4c8e687..32a4a75e820c 100644
@@ -600,14 +600,6 @@ static unsigned long kfence_init_pool(void)
 		addr += 2 * PAGE_SIZE;
 	}
 
-	/*
-	 * The pool is live and will never be deallocated from this point on.
-	 * Remove the pool object from the kmemleak object tree, as it would
-	 * otherwise overlap with allocations returned by kfence_alloc(), which
-	 * are registered with kmemleak through the slab post-alloc hook.
-	 */
-	kmemleak_free(__kfence_pool);
-
 	return 0;
 }
 
@@ -620,8 +612,16 @@ static bool __init kfence_init_pool_early(void)
 
 	addr = kfence_init_pool();
 
-	if (!addr)
+	if (!addr) {
+		/*
+		 * The pool is live and will never be deallocated from this point on.
+		 * Ignore the pool object from the kmemleak phys object tree, as it would
+		 * otherwise overlap with allocations returned by kfence_alloc(), which
+		 * are registered with kmemleak through the slab post-alloc hook.
+		 */
+		kmemleak_ignore_phys(__pa(__kfence_pool));
 		return true;
+	}
 
 	/*
 	 * Only release unprotected pages, and do not try to go back and change
Signature · 1 line
2.18.0
maintainer Reviewed-by on v2 1/1
#2 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

Quoted 9 lines · yee.lee via kasan-dev

From: Yee Lee <[email protected]>

This patch solves two issues.

(1) The pool allocated by memblock needs to unregister from kmemleak scanning. Apply kmemleak_ignore_phys to replace the original kmemleak_free as its address now is stored in the phys tree.

Quoted 6 lines

(2) The pool late allocated by page-alloc doesn't need to unregister. Move out the freeing operation from its call path.

Does this want a Fixes tag?

Quoted 43 lines
---
 mm/kfence/core.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
mm/kfence/core.c +9 -10
index 4e7cd4c8e687..32a4a75e820c 100644
@@ -600,14 +600,6 @@ static unsigned long kfence_init_pool(void)
                addr += 2 * PAGE_SIZE;
        }
 
-       /*
-        * The pool is live and will never be deallocated from this point on.
-        * Remove the pool object from the kmemleak object tree, as it would
-        * otherwise overlap with allocations returned by kfence_alloc(), which
-        * are registered with kmemleak through the slab post-alloc hook.
-        */
-       kmemleak_free(__kfence_pool);
-
        return 0;
 }
 
@@ -620,8 +612,16 @@ static bool __init kfence_init_pool_early(void)
 
        addr = kfence_init_pool();
 
-       if (!addr)
+       if (!addr) {
+               /*
+                * The pool is live and will never be deallocated from this point on.
+                * Ignore the pool object from the kmemleak phys object tree, as it would
+                * otherwise overlap with allocations returned by kfence_alloc(), which
+                * are registered with kmemleak through the slab post-alloc hook.
+                */
+               kmemleak_ignore_phys(__pa(__kfence_pool));
                return true;
+       }
 
        /*
         * Only release unprotected pages, and do not try to go back and change
--

2.18.0

Reviewed-by on v2 1/1
#3 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

In reply to [email protected]
Quoted 8 lines · On Tue, Jun 28, 2022 at 07:37:11PM +0800, [email protected] wrote:

From: Yee Lee <[email protected]>

This patch solves two issues.

(1) The pool allocated by memblock needs to unregister from kmemleak scanning. Apply kmemleak_ignore_phys to replace the original kmemleak_free as its address now is stored in the phys tree.

Quoted 6 lines

(2) The pool late allocated by page-alloc doesn't need to unregister. Move out the freeing operation from its call path.

Tested-by on v2 1/1
#4 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

In reply to [email protected]

Hi Yee,

Quoted 8 lines · On Tue, Jun 28, 2022 at 1:42 PM <[email protected]> wrote:

From: Yee Lee <[email protected]>

This patch solves two issues.

(1) The pool allocated by memblock needs to unregister from kmemleak scanning. Apply kmemleak_ignore_phys to replace the original kmemleak_free as its address now is stored in the phys tree.

Quoted 6 lines

(2) The pool late allocated by page-alloc doesn't need to unregister. Move out the freeing operation from its call path.

Thank you, this fixes the storm of

    BUG: KFENCE: invalid read in scan_block+0x78/0x130
    BUG: KFENCE: use-after-free read in scan_block+0x78/0x130
    BUG: KFENCE: out-of-bounds read in scan_block+0x78/0x130

messages I was seeing on arm64.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

maintainer
#5 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

Quoted 16 lines · On Fri, 15 Jul 2022 10:17:43 +0200 Geert Uytterhoeven <[email protected]>…
Quoted 8 lines · On Tue, Jun 28, 2022 at 1:42 PM <[email protected]> wrote:

From: Yee Lee <[email protected]>

This patch solves two issues.

(1) The pool allocated by memblock needs to unregister from kmemleak scanning. Apply kmemleak_ignore_phys to replace the original kmemleak_free as its address now is stored in the phys tree.

Quoted 6 lines

(2) The pool late allocated by page-alloc doesn't need to unregister. Move out the freeing operation from its call path.

Quoted 7 lines

Thank you, this fixes the storm of

    BUG: KFENCE: invalid read in scan_block+0x78/0x130
    BUG: KFENCE: use-after-free read in scan_block+0x78/0x130
    BUG: KFENCE: out-of-bounds read in scan_block+0x78/0x130

messages I was seeing on arm64.

Thanks, but...

- It would be great if we could identify a Fixes: for this.

- This patch has been accused of crashing the kernel:

	https://lkml.kernel.org/r/YsFeUHkrFTQ7T51Q@xsang-OptiPlex-9020

Do we think that report is bogus?

#6 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

Hi Andrew,

Quoted 23 lines · Andrew Morton
Quoted 22 lines · On Fri, 15 Jul 2022 10:17:43 +0200 Geert Uytterhoeven <[email protected]>…
Quoted 8 lines · On Tue, Jun 28, 2022 at 1:42 PM <[email protected]> wrote:

From: Yee Lee <[email protected]>

This patch solves two issues.

(1) The pool allocated by memblock needs to unregister from kmemleak scanning. Apply kmemleak_ignore_phys to replace the original kmemleak_free as its address now is stored in the phys tree.

Quoted 6 lines

(2) The pool late allocated by page-alloc doesn't need to unregister. Move out the freeing operation from its call path.

Thank you, this fixes the storm of

    BUG: KFENCE: invalid read in scan_block+0x78/0x130
    BUG: KFENCE: use-after-free read in scan_block+0x78/0x130
    BUG: KFENCE: out-of-bounds read in scan_block+0x78/0x130
Quoted 5 lines
Quoted 1 line

messages I was seeing on arm64.

Thanks, but...

- It would be great if we could identify a Fixes: for this.

IIRC, I started seeing the issue with "[PATCH v4 3/4] mm:
kmemleak: add rbtree and store physical address for objects allocated with PA" (i.e. commit 0c24e061196c21d5 ("mm: kmemleak:
add rbtree and store physical address for objects allocated with PA")) of series "[PATCH v4 0/4] mm: kmemleak: store objects allocated with physical address separately and check when scan"
(https://lore.kernel.org/all/[email protected]),
in an arm64 config that had enabled kfence.
So I think this patch is sort of a dependency for that series.

I had cherry-picked that series after bisecting a regression to commit 23c2d497de21f258 ("mm: kmemleak: take a full lowmem check in kmemleak_*_phys()") in v5.18-rc3, and having a look around.

Quoted 5 lines

- This patch has been accused of crashing the kernel:

        https://lkml.kernel.org/r/YsFeUHkrFTQ7T51Q@xsang-OptiPlex-9020

Do we think that report is bogus?

I think all of this is highly architecture-specific...

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

maintainer
#7 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

On Sat, 16 Jul 2022 at 20:43, Geert Uytterhoeven <[email protected]> wrote:
[...]

Quoted 7 lines
Quoted 5 lines

- This patch has been accused of crashing the kernel:

        https://lkml.kernel.org/r/YsFeUHkrFTQ7T51Q@xsang-OptiPlex-9020

Do we think that report is bogus?

I think all of this is highly architecture-specific...

The report can be reproduced on i386 with CONFIG_X86_PAE=y. But e.g. mm/memblock.c:memblock_free() is also guilty of using __pa() on previously memblock_alloc()'d addresses. Looking at the phys addr before memblock_alloc() does virt_to_phys(), the result of __pa() looks correct even on PAE, at least for the purpose of passing it on to kmemleak(). So I don't know what that BUG_ON(slow_virt_to_phys() != phys_addr) is supposed to tell us here.

Ideas?

maintainer
#8 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

Quoted 10 lines · On Mon, 18 Jul 2022 16:26:25 +0200 Marco Elver <[email protected]> wrote:

On Sat, 16 Jul 2022 at 20:43, Geert Uytterhoeven <[email protected]> wrote:
[...]

Quoted 7 lines
Quoted 5 lines

- This patch has been accused of crashing the kernel:

        https://lkml.kernel.org/r/YsFeUHkrFTQ7T51Q@xsang-OptiPlex-9020

Do we think that report is bogus?

I think all of this is highly architecture-specific...

Quoted 8 lines

The report can be reproduced on i386 with CONFIG_X86_PAE=y. But e.g. mm/memblock.c:memblock_free() is also guilty of using __pa() on previously memblock_alloc()'d addresses. Looking at the phys addr before memblock_alloc() does virt_to_phys(), the result of __pa() looks correct even on PAE, at least for the purpose of passing it on to kmemleak(). So I don't know what that BUG_ON(slow_virt_to_phys() != phys_addr) is supposed to tell us here.

It's only been nine years, so I'm sure Dave can remember why he added it ;)

		BUG_ON(slow_virt_to_phys((void *)x) != phys_addr);

in arch/x86/mm/physaddr.c:__phys_addr().

This kfence patch does seem to be desirable, but we can't proceed if it's resulting in kernel crashes.

#9 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

Quoted 18 lines · Andrew Morton
Quoted 7 lines · On Mon, 18 Jul 2022 16:26:25 +0200 Marco Elver <[email protected]> wrote:

On Sat, 16 Jul 2022 at 20:43, Geert Uytterhoeven <[email protected]> wrote:
[...]

Quoted 5 lines
Quoted 5 lines

- This patch has been accused of crashing the kernel:

        https://lkml.kernel.org/r/YsFeUHkrFTQ7T51Q@xsang-OptiPlex-9020

Do we think that report is bogus?

Quoted 9 lines
Quoted 1 line

I think all of this is highly architecture-specific...

The report can be reproduced on i386 with CONFIG_X86_PAE=y. But e.g. mm/memblock.c:memblock_free() is also guilty of using __pa() on previously memblock_alloc()'d addresses. Looking at the phys addr before memblock_alloc() does virt_to_phys(), the result of __pa() looks correct even on PAE, at least for the purpose of passing it on to kmemleak(). So I don't know what that BUG_ON(slow_virt_to_phys() != phys_addr) is supposed to tell us here.

Quoted 6 lines

It's only been nine years, so I'm sure Dave can remember why he added it ;)

		BUG_ON(slow_virt_to_phys((void *)x) != phys_addr);

in arch/x86/mm/physaddr.c:__phys_addr().

I think I intended it to double check that the linear map is *actually* a linear map for 'x'. Sure, we can use the "x - PAGE_OFFSET" shortcut, but did it turn out to be actually accurate for the address it was handed?

I'd be curious what the page tables actually say for the address that's causing problems.

maintainer
#10 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

[+x86 maintainers ...]

Quoted 24 lines · Dave Hansen
Quoted 23 lines · Andrew Morton
Quoted 7 lines · On Mon, 18 Jul 2022 16:26:25 +0200 Marco Elver <[email protected]> wrote:

On Sat, 16 Jul 2022 at 20:43, Geert Uytterhoeven <[email protected]> wrote:
[...]

Quoted 5 lines
Quoted 5 lines

- This patch has been accused of crashing the kernel:

        https://lkml.kernel.org/r/YsFeUHkrFTQ7T51Q@xsang-OptiPlex-9020

Do we think that report is bogus?

Quoted 9 lines
Quoted 1 line

I think all of this is highly architecture-specific...

The report can be reproduced on i386 with CONFIG_X86_PAE=y. But e.g. mm/memblock.c:memblock_free() is also guilty of using __pa() on previously memblock_alloc()'d addresses. Looking at the phys addr before memblock_alloc() does virt_to_phys(), the result of __pa() looks correct even on PAE, at least for the purpose of passing it on to kmemleak(). So I don't know what that BUG_ON(slow_virt_to_phys() != phys_addr) is supposed to tell us here.

It's only been nine years, so I'm sure Dave can remember why he added it ;)

              BUG_ON(slow_virt_to_phys((void *)x) != phys_addr);
Quoted 8 lines
Quoted 1 line

in arch/x86/mm/physaddr.c:__phys_addr().

I think I intended it to double check that the linear map is *actually* a linear map for 'x'. Sure, we can use the "x - PAGE_OFFSET" shortcut, but did it turn out to be actually accurate for the address it was handed?

I'd be curious what the page tables actually say for the address that's causing problems.

test robot just reminded us again:
https://lore.kernel.org/all/YufXncrWhJZH0ifB@xsang-OptiPlex-9020/T/#u

Few things I noticed:

* mm/memblock.c's memblock_free() also uses __pa() to convert back to physical address. Presumably that's also wrong. What should be used instead?

* kmemleak happily converts phys_addr_t to unsigned long everywhere, but with i386 PAE, this will narrow a 64-bit address to a 32-bit address. Is that correct? Does kmemleak need a "depends on 64BIT || !PHYS_ADDR_T_64BIT"?

#11 lore raw

Re: [PATCH v2 1/1] mm: kfence: apply kmemleak_ignore_phys on early allocated pool

In reply to Geert Uytterhoeven
Quoted 30 lines · Geert Uytterhoeven
Quoted 23 lines · Andrew Morton
Quoted 22 lines · On Fri, 15 Jul 2022 10:17:43 +0200 Geert Uytterhoeven <[email protected]>…
Quoted 8 lines · On Tue, Jun 28, 2022 at 1:42 PM <[email protected]> wrote:

From: Yee Lee <[email protected]>

This patch solves two issues.

(1) The pool allocated by memblock needs to unregister from kmemleak scanning. Apply kmemleak_ignore_phys to replace the original kmemleak_free as its address now is stored in the phys tree.

Quoted 6 lines

(2) The pool late allocated by page-alloc doesn't need to unregister. Move out the freeing operation from its call path.

Thank you, this fixes the storm of

    BUG: KFENCE: invalid read in scan_block+0x78/0x130
    BUG: KFENCE: use-after-free read in scan_block+0x78/0x130
    BUG: KFENCE: out-of-bounds read in scan_block+0x78/0x130
Quoted 5 lines
Quoted 1 line

messages I was seeing on arm64.

Thanks, but...

- It would be great if we could identify a Fixes: for this.

Quoted 8 lines

IIRC, I started seeing the issue with "[PATCH v4 3/4] mm:
kmemleak: add rbtree and store physical address for objects allocated with PA" (i.e. commit 0c24e061196c21d5 ("mm: kmemleak:
add rbtree and store physical address for objects allocated with PA")) of series "[PATCH v4 0/4] mm: kmemleak: store objects allocated with physical address separately and check when scan"
(https://lore.kernel.org/all/[email protected]),
in an arm64 config that had enabled kfence.

Yes, I think it fixes 0c24e061196c21d5 since after that commit, the kmemleak_free() no longer worked as expected on physically allocated objects.

Signature · 1 line
Catalin