arm64: kprobes: check the return value of set_memory_rox()
PATCH 1/1 arm64: kprobes: check the return value of set_memory_rox()
Since commit a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full"), __change_memory_common has more chance to fail due to memory allocation fialure when splitting page table. So check the return value of set_memory_rox(), then bail out if it fails otherwise we may have RW memory mapping for kprobes insn page.
---
I actually epxected 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page") can be merged in 6.17-rcX, so I just restored it to before commit 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page"), however it turned out to be merged in 6.18-rc1 and it is after commit a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full"). So I made the fix tag point to it.
And I don't think we need to backport this patch to pre-6.18.
arch/arm64/kernel/probes/kprobes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/arm64/kernel/probes/kprobes.c +4 -1
@@ -49,7 +49,10 @@ void *alloc_insn_page(void) addr =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!addr)return NULL ; -set_memory_rox ((unsigned long )addr,1 ); +if (set_memory_rox ((unsigned long )addr,1 )) { +execmem_free (addr); +return NULL ; + }return addr; }
Signature · 1 line
2.47.0
Noticed a typo in the commit log while catching up with the rodata BBM optimizations.
Quoted 3 lines · Yang Shi
Since commit
a166563e7ec3("arm64: mm: support large block mapping when rodata=full"),__change_memory_commonhas more chance to fail due to memory allocation fialure when splitting page table. So check the return
Typo: failure
Quoted 32 lines
value of
set_memory_rox(), then bail out if it fails otherwise we may have RW memory mapping for kprobes insn page.Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")Signed-off-by Yang Shi <[email protected]>---
I actually epxected195a1b7d8388("arm64: kprobes: callset_memory_rox()for kprobe page") can be merged in 6.17-rcX, so I just restored it to before commit10d5e97c1bf8("arm64: usePAGE_KERNEL_ROXdirectly inalloc_insn_page"), however it turned out to be merged in 6.18-rc1 and it is after commita166563e7ec3("arm64: mm: support large block mapping when rodata=full"). So I made the fix tag point to it.
And I don't think we need to backport this patch to pre-6.18.arch/arm64/kernel/probes/kprobes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/arm64/kernel/probes/kprobes.c +4
-1@@ -49,7 +49,10 @@ void *alloc_insn_page(void) addr =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!addr)return NULL ; -set_memory_rox ((unsigned long )addr,1 ); +if (set_memory_rox ((unsigned long )addr,1 )) { +execmem_free (addr); +return NULL ; + }return addr; }
Quoted 9 lines · Punit Agrawal
Noticed a typo in the commit log while catching up with the rodata BBM optimizations.
Quoted 3 lines · Yang Shi
Since commit
a166563e7ec3("arm64: mm: support large block mapping when rodata=full"),__change_memory_commonhas more chance to fail due to memory allocation fialure when splitting page table. So check the returnTypo: failure
Thank you for catching this. Will fix in v2.
Yang
Quoted 34 lines
Quoted 34 lines
value of
set_memory_rox(), then bail out if it fails otherwise we may have RW memory mapping for kprobes insn page.Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")Signed-off-by Yang Shi <[email protected]>---
I actually epxected195a1b7d8388("arm64: kprobes: callset_memory_rox()for kprobe page") can be merged in 6.17-rcX, so I just restored it to before commit10d5e97c1bf8("arm64: usePAGE_KERNEL_ROXdirectly inalloc_insn_page"), however it turned out to be merged in 6.18-rc1 and it is after commita166563e7ec3("arm64: mm: support large block mapping when rodata=full"). So I made the fix tag point to it.
And I don't think we need to backport this patch to pre-6.18.arch/arm64/kernel/probes/kprobes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/arm64/kernel/probes/kprobes.c +4
-1@@ -49,7 +49,10 @@ void *alloc_insn_page(void) addr =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!addr)return NULL ; -set_memory_rox ((unsigned long )addr,1 ); +if (set_memory_rox ((unsigned long )addr,1 )) { +execmem_free (addr); +return NULL ; + }return addr; }
Quoted 8 lines · Yang Shi
Since commit
a166563e7ec3("arm64: mm: support large block mapping when rodata=full"),__change_memory_commonhas more chance to fail due to memory allocation fialure when splitting page table. So check the return value ofset_memory_rox(), then bail out if it fails otherwise we may have RW memory mapping for kprobes insn page.Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")Signed-off-by Yang Shi <[email protected]>
This patch looks correct so:
but, I think I see an separate issue below...
Quoted 17 lines
---
I actually epxected195a1b7d8388("arm64: kprobes: callset_memory_rox()for kprobe page") can be merged in 6.17-rcX, so I just restored it to before commit10d5e97c1bf8("arm64: usePAGE_KERNEL_ROXdirectly inalloc_insn_page"), however it turned out to be merged in 6.18-rc1 and it is after commita166563e7ec3("arm64: mm: support large block mapping when rodata=full"). So I made the fix tag point to it.
And I don't think we need to backport this patch to pre-6.18.arch/arm64/kernel/probes/kprobes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/arm64/kernel/probes/kprobes.c +0
-0
arch/arm64/kernel/probes/kprobes.c void *alloc_insn_page(void) · line 52
Quoted 5 lines
arch/arm64/kernel/probes/kprobes.c +1
-1@@ -49,7 +49,10 @@ void *alloc_insn_page(void) addr =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!addr)return NULL ; -set_memory_rox ((unsigned long )addr,1 ); +if (set_memory_rox ((unsigned long )addr,1 )) {
How does x get cleared when freeing this memory? arm64's set_memory_x() sets PTE_MAYBE_GP and clears PTE_PXN. The only function that will revert that is set_memory_nx(). But that only gets called from module_enable_data_nx() (which I don't think is applicable here) and execmem_force_rw() - but only if CONFIG_ARCH_HAS_EXECMEM_ROX is enabled, which I don't think it is for arm64?
So I think once we flip a page executable, it will be executable forever?
Do we need to modify set_direct_map_default_noflush() to make the memory nx? Then vm_reset_perms() will fix it up at vfree time?
Thanks,
Ryan
Quoted 6 lines
arch/arm64/kernel/probes/kprobes.c +3
-0+execmem_free (addr); +return NULL ; + }return addr; }
Quoted 45 lines · Ryan Roberts
Quoted 8 lines · Yang Shi
Since commit
a166563e7ec3("arm64: mm: support large block mapping when rodata=full"),__change_memory_commonhas more chance to fail due to memory allocation fialure when splitting page table. So check the return value ofset_memory_rox(), then bail out if it fails otherwise we may have RW memory mapping for kprobes insn page.Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")Signed-off-by Yang Shi <[email protected]>This patch looks correct so:
Reviewed-by Ryan Roberts <[email protected]>but, I think I see an separate issue below...
Quoted 17 lines
---
I actually epxected195a1b7d8388("arm64: kprobes: callset_memory_rox()for kprobe page") can be merged in 6.17-rcX, so I just restored it to before commit10d5e97c1bf8("arm64: usePAGE_KERNEL_ROXdirectly inalloc_insn_page"), however it turned out to be merged in 6.18-rc1 and it is after commita166563e7ec3("arm64: mm: support large block mapping when rodata=full"). So I made the fix tag point to it.
And I don't think we need to backport this patch to pre-6.18.arch/arm64/kernel/probes/kprobes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/arm64/kernel/probes/kprobes.c +0
-0arch/arm64/kernel/probes/kprobes.c void *alloc_insn_page(void) · line 52
Quoted 5 lines
arch/arm64/kernel/probes/kprobes.c +1
-1@@ -49,7 +49,10 @@ void *alloc_insn_page(void) addr =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!addr)return NULL ; -set_memory_rox ((unsigned long )addr,1 ); +if (set_memory_rox ((unsigned long )addr,1 )) {How does x get cleared when freeing this memory? arm64's
set_memory_x()setsPTE_MAYBE_GPand clearsPTE_PXN. The only function that will revert that isset_memory_nx(). But that only gets called frommodule_enable_data_nx()(which I don't think is applicable here) andexecmem_force_rw()- but only ifCONFIG_ARCH_HAS_EXECMEM_ROXis enabled, which I don't think it is for arm64?
Quoted 4 lines
So I think once we flip a page executable, it will be executable forever?
Do we need to modify
set_direct_map_default_noflush()to make the memory nx? Thenvm_reset_perms()will fix it up at vfree time?
Dev just pointed this out to me. Panic over!
static int change_memory_common(unsigned long addr, int numpages,
pgprot_t set_mask, pgprot_t clear_mask)
{
...
/*
* If we are manipulating read-only permissions, apply the same
* change to the linear mapping of the pages that back this VM area.
*/
if (rodata_full && (pgprot_val(set_mask) == PTE_RDONLY ||
pgprot_val(clear_mask) == PTE_RDONLY)) {
for (i = 0; i < area->nr_pages; i++) {
__change_memory_common(...);
}
}
...
}Quoted 11 lines
Thanks,
RyanQuoted 6 lines
arch/arm64/kernel/probes/kprobes.c +3
-0+execmem_free (addr); +return NULL ; + }return addr; }
Quoted 8 lines · Ryan Roberts
Quoted 7 lines · Ryan Roberts
Quoted 6 lines · Yang Shi
Since commit
a166563e7ec3("arm64: mm: support large block mapping when rodata=full"),__change_memory_commonhas more chance to fail due to memory allocation fialure when splitting page table. So check the return value ofset_memory_rox(), then bail out if it fails otherwise we may have RW memory mapping for kprobes insn page.
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")Signed-off-by Yang Shi <[email protected]>This patch looks correct so:
Reviewed-by Ryan Roberts <[email protected]>
Thank you.
Quoted 31 lines
Quoted 31 lines
but, I think I see an separate issue below...
Quoted 17 lines
---
I actually epxected195a1b7d8388("arm64: kprobes: callset_memory_rox()for kprobe page") can be merged in 6.17-rcX, so I just restored it to before commit10d5e97c1bf8("arm64: usePAGE_KERNEL_ROXdirectly inalloc_insn_page"), however it turned out to be merged in 6.18-rc1 and it is after commita166563e7ec3("arm64: mm: support large block mapping when rodata=full"). So I made the fix tag point to it.
And I don't think we need to backport this patch to pre-6.18.arch/arm64/kernel/probes/kprobes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/arm64/kernel/probes/kprobes.c +0
-0arch/arm64/kernel/probes/kprobes.c void *alloc_insn_page(void) · line 52
Quoted 5 lines
arch/arm64/kernel/probes/kprobes.c +1
-1@@ -49,7 +49,10 @@ void *alloc_insn_page(void) addr =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!addr)return NULL ; -set_memory_rox ((unsigned long )addr,1 ); +if (set_memory_rox ((unsigned long )addr,1 )) {How does x get cleared when freeing this memory? arm64's
set_memory_x()setsPTE_MAYBE_GPand clearsPTE_PXN. The only function that will revert that isset_memory_nx(). But that only gets called frommodule_enable_data_nx()(which I don't think is applicable here) andexecmem_force_rw()- but only ifCONFIG_ARCH_HAS_EXECMEM_ROXis enabled, which I don't think it is for arm64?
Quoted 6 lines
Quoted 4 lines
So I think once we flip a page executable, it will be executable forever?
Do we need to modify
set_direct_map_default_noflush()to make the memory nx? Thenvm_reset_perms()will fix it up at vfree time?Dev just pointed this out to me. Panic over!
Aha, yes, it doesn't clear PXN at all.
Thanks,
Yang
Quoted 30 lines
static int change_memory_common(unsigned long addr, int numpages,
pgprot_tset_mask,pgprot_tclear_mask){ ... /* * If we are manipulating read-only permissions, apply the same * change to the linear mapping of the pages that back this VM area. */ if (rodata_full && (pgprot_val(set_mask) == PTE_RDONLY || pgprot_val(clear_mask) == PTE_RDONLY)) { for (i = 0; i < area->nr_pages; i++) { __change_memory_common(...); } } ... }Quoted 9 lines
Thanks,
RyanQuoted 6 lines
arch/arm64/kernel/probes/kprobes.c +3
-0+execmem_free (addr); +return NULL ; + }return addr; }
Quoted 25 lines · Yang Shi
Since commit
a166563e7ec3("arm64: mm: support large block mapping when rodata=full"),__change_memory_commonhas more chance to fail due to memory allocation fialure when splitting page table. So check the return value ofset_memory_rox(), then bail out if it fails otherwise we may have RW memory mapping for kprobes insn page.Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")Signed-off-by Yang Shi <[email protected]>---
I actually epxected195a1b7d8388("arm64: kprobes: callset_memory_rox()for kprobe page") can be merged in 6.17-rcX, so I just restored it to before commit10d5e97c1bf8("arm64: usePAGE_KERNEL_ROXdirectly inalloc_insn_page"), however it turned out to be merged in 6.18-rc1 and it is after commita166563e7ec3("arm64: mm: support large block mapping when rodata=full"). So I made the fix tag point to it.
And I don't think we need to backport this patch to pre-6.18.arch/arm64/kernel/probes/kprobes.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/arm64/kernel/probes/kprobes.c +0
-0
arch/arm64/kernel/probes/kprobes.c void *alloc_insn_page(void) · line 57
Quoted 10 lines
arch/arm64/kernel/probes/kprobes.c +4
-1@@ -49,7 +49,10 @@ void *alloc_insn_page(void) addr =execmem_alloc (EXECMEM_KPROBES ,PAGE_SIZE );if (!addr)return NULL ; -set_memory_rox ((unsigned long )addr,1 ); +if (set_memory_rox ((unsigned long )addr,1 )) { +execmem_free (addr); +return NULL ; + }return addr; }
Looks obviously correct:
Although I got confused by why set_memory_rox() is being called; it is being used only to handle the linear map alias perm change, which is not nice :) but I don't see an obvious way to refactor the code to only perform the needed functionality here, and probably this is not a hot path that we care about.
Quoted 1 line
Quoted 40 lines · Dev Jain
Quoted 25 lines · Yang Shi
Since commit
a166563e7ec3("arm64: mm: support large block mapping when rodata=full"),__change_memory_commonhas more chance to fail due to memory allocation fialure when splitting page table. So check the return value ofset_memory_rox(), then bail out if it fails otherwise we may have RW memory mapping for kprobes insn page.Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")Signed-off-by Yang Shi <[email protected]>---
I actually epxected195a1b7d8388("arm64: kprobes: callset_memory_rox()for kprobe page") can be merged in 6.17-rcX, so I just restored it to before commit10d5e97c1bf8("arm64: usePAGE_KERNEL_ROXdirectly inalloc_insn_page"), however it turned out to be merged in 6.18-rc1 and it is after commita166563e7ec3("arm64: mm: support large block mapping when rodata=full"). So I made the fix tag point to it.
And I don't think we need to backport this patch to pre-6.18.
arch/arm64/kernel/probes/kprobes.c| 5 ++++-1 file changed, 4 insertions(+), 1 deletion(-)arch/arm64/kernel/probes/kprobes.c +0
-0arch/arm64/kernel/probes/kprobes.c void *alloc_insn_page(void) · line 57
Quoted 10 lines
arch/arm64/kernel/probes/kprobes.c +4
-1@@ -49,7 +49,10 @@ void *alloc_insn_page(void) addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE); if (!addr) return NULL; - set_memory_rox((unsigned long)addr, 1); + if (set_memory_rox((unsigned long)addr, 1)) { + execmem_free(addr); + return NULL; + } return addr; }Looks obviously correct:
Quoted 1 line
Reviewed-by Dev Jain <[email protected]>
Thank you.
Quoted 5 lines
Although I got confused by why
set_memory_rox()is being called; it is being used only to handle the linear map alias perm change, which is not nice :) but I don't see an obvious way to refactor the code to only perform the needed functionality here, and probably this is not a hot path that we care about.
Catalin actually raised the similar suggestion before. He suggested extract the linear mapping update code into a helper then just call it here if I remember correctly. However I have not gotten time to look it deeper yet. Anyway we agree it is not a critical path. It may be not worth all the effort.
Thanks,
Yang