Skip to contentSkip to conversation

arm64: kprobes: check the return value of set_memory_rox()

PATCH Superseded Yang Shi 4 participants 8 messages Nov 3, 2025 – Nov 4, 2025 arm64

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yang Shi

author
#0 lore raw

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.

Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe 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
index 8ab6104a4883..43a0361a8bf0 100644
@@ -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
#1 lore raw

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_common has 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")

---
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
index 8ab6104a4883..43a0361a8bf0 100644
@@ -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;
 }
author
#2 lore raw
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_common has more chance to fail due to memory allocation fialure when splitting page table. So check the return

Typo: 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")

---
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
index 8ab6104a4883..43a0361a8bf0 100644
@@ -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;
  }
Reviewed-by on v1 1/1
#3 lore raw
In reply to Yang Shi
Quoted 8 lines · Yang Shi

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.

Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")

This patch looks correct so:

but, I think I see an separate issue below...

Quoted 17 lines

---
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 +0 -0
index 8ab6104a4883..43a0361a8bf0 100644

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;
 }
 
#4 lore raw
Quoted 45 lines · Ryan Roberts
Quoted 8 lines · Yang Shi

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.

Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")

This patch looks correct so:

but, I think I see an separate issue below...

Quoted 17 lines

---
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 +0 -0
index 8ab6104a4883..43a0361a8bf0 100644

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?

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? Then vm_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,
Ryan

Quoted 6 lines
arch/arm64/kernel/probes/kprobes.c +3 -0
+		execmem_free(addr);
+		return NULL;
+	}
 	return addr;
 }
 
author
#5 lore raw
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_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.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe page")

This patch looks correct so:

Thank you.

Quoted 31 lines
Quoted 31 lines

but, I think I see an separate issue below...

Quoted 17 lines

---
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 +0 -0
index 8ab6104a4883..43a0361a8bf0 100644

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?

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? Then vm_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_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 9 lines

Thanks,
Ryan

Quoted 6 lines
arch/arm64/kernel/probes/kprobes.c +3 -0
+		execmem_free(addr);
+		return NULL;
+	}
  	return addr;
  }
 
Reviewed-by on v1 1/1
#6 lore raw
In reply to Yang Shi
Quoted 25 lines · Yang Shi

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.

Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe 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 +0 -0
index 8ab6104a4883..43a0361a8bf0 100644

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
author
#7 lore raw
Quoted 40 lines · Dev Jain
Quoted 25 lines · Yang Shi

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.

Fixes 195a1b7d8388 ("arm64: kprobes: call set_memory_rox() for kprobe 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 +0 -0
index 8ab6104a4883..43a0361a8bf0 100644

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:

Quoted 1 line

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

Quoted 1 line