Skip to contentSkip to conversation

x86/kprobes: Simplify alloc_insn_page() with __vmalloc_node_range

PATCH In review Jisheng Zhang 3 participants 9 messages Apr 13, 2021 – Apr 16, 2021 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/kprobes: Simplify alloc_insn_page() with __vmalloc_node_range

Use the __vmalloc_node_range() to simplify x86's alloc_insn_page() implementation.

---
 arch/x86/kernel/kprobes/core.c | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
arch/x86/kernel/kprobes/core.c +4 -20
index df776cdca327..75081f3dbe44 100644
@@ -383,26 +383,10 @@ static int prepare_boost(kprobe_opcode_t *buf, struct kprobe *p,
 /* Make page to RO mode when allocate it */
 void *alloc_insn_page(void)
 {
-	void *page;
-
-	page = module_alloc(PAGE_SIZE);
-	if (!page)
-		return NULL;
-
-	set_vm_flush_reset_perms(page);
-	/*
-	 * First make the page read-only, and only then make it executable to
-	 * prevent it from being W+X in between.
-	 */
-	set_memory_ro((unsigned long)page, 1);
-
-	/*
-	 * TODO: Once additional kernel code protection mechanisms are set, ensure
-	 * that the page was not maliciously altered and it is still zeroed.
-	 */
-	set_memory_x((unsigned long)page, 1);
-
-	return page;
+	return __vmalloc_node_range(PAGE_SIZE, PAGE_SIZE, VMALLOC_START,
+			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
+			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
+			__builtin_return_address(0));
 }
 
 /* Recover page to RW mode before releasing it */
Signature · 1 line
2.31.0
#1 lore raw

Hi,

Quoted 2 lines · On Tue, 13 Apr 2021 18:03:24 +0800 Jisheng Zhang <[email protected]>…

Use the __vmalloc_node_range() to simplify x86's alloc_insn_page() implementation.

Have you checked this is equivarent to the original code on all architecture? IIRC, some arch has a special module_alloc(), thus I NACKed similar patch previously.

Thank you,

Quoted 44 lines
---
 arch/x86/kernel/kprobes/core.c | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
arch/x86/kernel/kprobes/core.c +4 -20
index df776cdca327..75081f3dbe44 100644
@@ -383,26 +383,10 @@ static int prepare_boost(kprobe_opcode_t *buf, struct kprobe *p,
 /* Make page to RO mode when allocate it */
 void *alloc_insn_page(void)
 {
-	void *page;
-
-	page = module_alloc(PAGE_SIZE);
-	if (!page)
-		return NULL;
-
-	set_vm_flush_reset_perms(page);
-	/*
-	 * First make the page read-only, and only then make it executable to
-	 * prevent it from being W+X in between.
-	 */
-	set_memory_ro((unsigned long)page, 1);
-
-	/*
-	 * TODO: Once additional kernel code protection mechanisms are set, ensure
-	 * that the page was not maliciously altered and it is still zeroed.
-	 */
-	set_memory_x((unsigned long)page, 1);
-
-	return page;
+	return __vmalloc_node_range(PAGE_SIZE, PAGE_SIZE, VMALLOC_START,
+			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
+			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
+			__builtin_return_address(0));
 }
 
 /* Recover page to RW mode before releasing it */
Signature · 1 line
2.31.0
Signature · 1 line
Masami Hiramatsu <[email protected]>
#2 lore raw
Quoted 3 lines · On Tue, 13 Apr 2021 22:00:30 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi,

Hi

Quoted 9 lines
Quoted 2 lines · On Tue, 13 Apr 2021 18:03:24 +0800 Jisheng Zhang <[email protected]>…

Use the __vmalloc_node_range() to simplify x86's alloc_insn_page() implementation.

Have you checked this is equivarent to the original code on all architecture? IIRC, some arch has a special module_alloc(),

Indeed, this isn't equivarent to the original code. FWICT, the differences on x86 are:

1) module_alloc() allocates a special vmalloc range
2) module_alloc() randomizes the return address via. module_load_offset()
3) module_alloc() also supports kasan instrumentation by kasan_module_alloc()

But I'm not sure whether the above differences are useful for kprobes ss insn slot page or not. Take 1) for example, special range in module_alloc is due to relative jump limitation, modules need to call kernel .text. does kprobes ss ins slot needs this limitation too?

Thanks

Quoted 52 lines

thus I NACKed similar patch previously.

Thank you,

Quoted 42 lines
---
 arch/x86/kernel/kprobes/core.c | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
arch/x86/kernel/kprobes/core.c +4 -21
index df776cdca327..75081f3dbe44 100644
@@ -383,26 +383,10 @@ static int prepare_boost(kprobe_opcode_t *buf, struct kprobe *p,
 /* Make page to RO mode when allocate it */
 void *alloc_insn_page(void)
 {
-     void *page;
-
-     page = module_alloc(PAGE_SIZE);
-     if (!page)
-             return NULL;
-
-     set_vm_flush_reset_perms(page);
-     /*
-      * First make the page read-only, and only then make it executable to
-      * prevent it from being W+X in between.
-      */
-     set_memory_ro((unsigned long)page, 1);
-
-     /*
-      * TODO: Once additional kernel code protection mechanisms are set, ensure
-      * that the page was not maliciously altered and it is still zeroed.
-      */
-     set_memory_x((unsigned long)page, 1);
-
-     return page;
+     return __vmalloc_node_range(PAGE_SIZE, PAGE_SIZE, VMALLOC_START,
+                     VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
+                     VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
+                     __builtin_return_address(0));
 }
 
 /* Recover page to RW mode before releasing it */
--
Quoted 2 lines

2.31.0

--
Masami Hiramatsu <[email protected]>

#3 lore raw
In reply to Masami Hiramatsu
Quoted 3 lines · Jisheng Zhang

Hi,

Hi

Quoted 12 lines
Quoted 2 lines · On Tue, 13 Apr 2021 18:03:24 +0800 Jisheng Zhang <[email protected]>…

Use the __vmalloc_node_range() to simplify x86's alloc_insn_page() implementation.

Have you checked this is equivarent to the original code on all architecture? IIRC, some arch has a special module_alloc(),

Indeed, this isn't equivarent to the original code. FWICT, the differences on x86 are:

Quoted 8 lines

1) module_alloc() allocates a special vmalloc range
2) module_alloc() randomizes the return address via. module_load_offset()
3) module_alloc() also supports kasan instrumentation by kasan_module_alloc()

But I'm not sure whether the above differences are useful for kprobes ss insn slot page or not. Take 1) for example, special range in module_alloc is due to relative jump limitation, modules need to call kernel .text. does kprobes ss ins slot needs this limitation too?

Oops, I found this wonderful thread:
https://www.lkml.org/lkml/2020/7/28/1413

So kprobes ss ins slot page "must be in the range of relative branching only for x86 and arm"

And Jarkko's "arch/x86: kprobes: Remove MODULES dependency" series look much better. The last version is v5, I'm not sure whether Jarkko will send new version to mainline the series.

thanks

#4 lore raw

Hi Jisheng,

On Wed, 14 Apr 2021 15:27:28 +0800
Jisheng Zhang <[email protected]> wrote:

\

Quoted 21 lines
Quoted 12 lines
Quoted 2 lines · On Tue, 13 Apr 2021 18:03:24 +0800 Jisheng Zhang <[email protected]>…

Use the __vmalloc_node_range() to simplify x86's alloc_insn_page() implementation.

Have you checked this is equivarent to the original code on all architecture? IIRC, some arch has a special module_alloc(),

Indeed, this isn't equivarent to the original code. FWICT, the differences on x86 are:

Quoted 8 lines

1) module_alloc() allocates a special vmalloc range
2) module_alloc() randomizes the return address via. module_load_offset()
3) module_alloc() also supports kasan instrumentation by kasan_module_alloc()

But I'm not sure whether the above differences are useful for kprobes ss insn slot page or not. Take 1) for example, special range in module_alloc is due to relative jump limitation, modules need to call kernel .text. does kprobes ss ins slot needs this limitation too?

Quoted 5 lines

Oops, I found this wonderful thread:
https://www.lkml.org/lkml/2020/7/28/1413

So kprobes ss ins slot page "must be in the range of relative branching only for x86 and arm"

Yes, at this moment. (Not sure we can introduce similar feature on other arch too)

Quoted 4 lines

And Jarkko's "arch/x86: kprobes: Remove MODULES dependency" series look much better. The last version is v5, I'm not sure whether Jarkko will send new version to mainline the series.

I hope so. If module_alloc() itself is implemented on the generic text_alloc(), I can replace the module_alloc() with text_alloc().

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#5 lore raw
Quoted 36 lines · Masami Hiramatsu

Hi Jisheng,

On Wed, 14 Apr 2021 15:27:28 +0800
Jisheng Zhang <[email protected]> wrote:

\

Quoted 21 lines
Quoted 12 lines
Quoted 2 lines · On Tue, 13 Apr 2021 18:03:24 +0800 Jisheng Zhang <[email protected]>…

Use the __vmalloc_node_range() to simplify x86's alloc_insn_page() implementation.

Have you checked this is equivarent to the original code on all architecture? IIRC, some arch has a special module_alloc(),

Indeed, this isn't equivarent to the original code. FWICT, the differences on x86 are:

Quoted 8 lines

1) module_alloc() allocates a special vmalloc range
2) module_alloc() randomizes the return address via. module_load_offset()
3) module_alloc() also supports kasan instrumentation by kasan_module_alloc()

But I'm not sure whether the above differences are useful for kprobes ss insn slot page or not. Take 1) for example, special range in module_alloc is due to relative jump limitation, modules need to call kernel .text. does kprobes ss ins slot needs this limitation too?

Quoted 5 lines

Oops, I found this wonderful thread:
https://www.lkml.org/lkml/2020/7/28/1413

So kprobes ss ins slot page "must be in the range of relative branching only for x86 and arm"

Yes, at this moment. (Not sure we can introduce similar feature on other arch too)

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

And Jarkko's "arch/x86: kprobes: Remove MODULES dependency" series look much better. The last version is v5, I'm not sure whether Jarkko will send new version to mainline the series.

I hope so. If module_alloc() itself is implemented on the generic text_alloc(), I can replace the module_alloc() with text_alloc().

I can of course look into this too. Right now in two vacation coming back end of this month.

/Jarkko

#6 lore raw
In reply to Jisheng Zhang
Quoted 32 lines · Jisheng Zhang
Quoted 3 lines · Jisheng Zhang

Hi,

Hi

Quoted 12 lines
Quoted 2 lines · On Tue, 13 Apr 2021 18:03:24 +0800 Jisheng Zhang <[email protected]>…

Use the __vmalloc_node_range() to simplify x86's alloc_insn_page() implementation.

Have you checked this is equivarent to the original code on all architecture? IIRC, some arch has a special module_alloc(),

Indeed, this isn't equivarent to the original code. FWICT, the differences on x86 are:

Quoted 8 lines

1) module_alloc() allocates a special vmalloc range
2) module_alloc() randomizes the return address via. module_load_offset()
3) module_alloc() also supports kasan instrumentation by kasan_module_alloc()

But I'm not sure whether the above differences are useful for kprobes ss insn slot page or not. Take 1) for example, special range in module_alloc is due to relative jump limitation, modules need to call kernel .text. does kprobes ss ins slot needs this limitation too?

Oops, I found this wonderful thread:
https://www.lkml.org/lkml/2020/7/28/1413

Quoted 6 lines

So kprobes ss ins slot page "must be in the range of relative branching only for x86 and arm"

And Jarkko's "arch/x86: kprobes: Remove MODULES dependency" series look much better. The last version is v5, I'm not sure whether Jarkko will send new version to mainline the series.

Ya, I got really busy with upstreaming SGX. That's why this was left out (but luckily SGX got finally into upstream).

Thanks for reminding. Any motivation to pick it up and continue where I left of?

/Jarkko

#7 lore raw
Quoted 7 lines · On Wed, 14 Apr 2021 16:12:23 +0300 Jarkko Sakkinen <[email protected]> wrote:
Quoted 6 lines

So kprobes ss ins slot page "must be in the range of relative branching only for x86 and arm"

And Jarkko's "arch/x86: kprobes: Remove MODULES dependency" series look much better. The last version is v5, I'm not sure whether Jarkko will send new version to mainline the series.

Quoted 6 lines

Ya, I got really busy with upstreaming SGX. That's why this was left out (but luckily SGX got finally into upstream).

Thanks for reminding. Any motivation to pick it up and continue where I left of?

I can try, I will try to send patches once next rc1 is released.

thanks

#8 lore raw
Quoted 13 lines · Jisheng Zhang
Quoted 10 lines · On Wed, 14 Apr 2021 16:12:23 +0300 Jarkko Sakkinen <[email protected]> wrote:
Quoted 6 lines

So kprobes ss ins slot page "must be in the range of relative branching only for x86 and arm"

And Jarkko's "arch/x86: kprobes: Remove MODULES dependency" series look much better. The last version is v5, I'm not sure whether Jarkko will send new version to mainline the series.

Ya, I got really busy with upstreaming SGX. That's why this was left out (but luckily SGX got finally into upstream).

Quoted 7 lines
Quoted 3 lines

Thanks for reminding. Any motivation to pick it up and continue where I left of?

I can try, I will try to send patches once next rc1 is released.

thanks

Alright, thanks. Be sure to CC me, I'm happy to test them in my environment.

/Jarkko