Skip to contentSkip to conversation

hv_hypercall_pg page permissios

DISCUSSION Christoph Hellwig 6 participants 27 messages Apr 7, 2020 – Jun 16, 2020

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

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv: Turn off write permission on the hypercall page") it was even mapped writable..

#1 lore raw
Quoted 8 lines · Christoph Hellwig

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv: Turn off write permission on the hypercall page") it was even mapped writable..

[There is nothing secret in the hypercall page, by reading it you can figure out if you're running on Intel or AMD (VMCALL/VMMCALL) but it's likely not the only possible way :-)]

I see no reason for hv_hypercall_pg to remain readable. I just smoke-tested

arch/x86/hyperv/hv_init.c +1 -1
index 7581cab74acb..17845db67fe2 100644
@@ -382,7 +382,7 @@ void __init hyperv_init(void)
        guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 
-       hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
+       hv_hypercall_pg  = vmalloc_exec(PAGE_SIZE);
        if (hv_hypercall_pg == NULL) {
                wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
                goto remove_cpuhp_state;

on a Hyper-V 2016 guest and nothing broke, feel free to go ahead and kill PAGE_KERNEL_RX.

Signature · 1 line
Vitaly
#2 lore raw
Quoted 11 lines · Vitaly Kuznetsov
Quoted 8 lines · Christoph Hellwig

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv: Turn off write permission on the hypercall page") it was even mapped writable..

Quoted 6 lines

[There is nothing secret in the hypercall page, by reading it you can figure out if you're running on Intel or AMD (VMCALL/VMMCALL) but it's likely not the only possible way :-)]

I see no reason for hv_hypercall_pg to remain readable. I just smoke-tested

Thanks, I have the same in my WIP tree, but just wanted to confirm this makes sense.

maintainer
#25 lore raw
In reply to Vitaly Kuznetsov
Quoted 27 lines

From: [email protected] <[email protected]> On Behalf Of Vitaly Kuznetsov
Sent: Tuesday, April 7, 2020 12:28 AM

Quoted 7 lines · Christoph Hellwig

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv: Turn

off

Quoted 1 line

write permission on the hypercall page") it was even mapped writable..

[There is nothing secret in the hypercall page, by reading it you can figure out if you're running on Intel or AMD (VMCALL/VMMCALL) but it's likely not the only possible way :-)]

I see no reason for hv_hypercall_pg to remain readable. I just smoke-tested

arch/x86/hyperv/hv_init.c +0 -0
index 7581cab74acb..17845db67fe2 100644

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 386

Quoted 6 lines
arch/x86/hyperv/hv_init.c +1 -1
@@ -382,7 +382,7 @@ void __init hyperv_init(void)
        guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 
-       hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL,
 PAGE_KERNEL_RX);
+       hv_hypercall_pg  = vmalloc_exec(PAGE_SIZE);

If we try to write into the page, Hyper-V will kill the guest immediately by a virtual double-fault (or triple fault?), IIRC.

Quoted 4 lines

on a Hyper-V 2016 guest and nothing broke, feel free to go ahead and kill PAGE_KERNEL_RX.
--
Vitaly

This should be OK. Just remember never try to write into the page, unless you're trying to use this as a means of forcing a guest reboot. :-)

Thanks,
-- Dexuan

maintainer
#26 lore raw
Quoted 30 lines · Dexuan Cui
Quoted 30 lines

From: [email protected] <[email protected]> On Behalf Of Vitaly Kuznetsov
Sent: Tuesday, April 7, 2020 12:28 AM

Quoted 7 lines · Christoph Hellwig

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv: Turn

off

Quoted 1 line

write permission on the hypercall page") it was even mapped writable..

[There is nothing secret in the hypercall page, by reading it you can figure out if you're running on Intel or AMD (VMCALL/VMMCALL) but it's likely not the only possible way :-)]

I see no reason for hv_hypercall_pg to remain readable. I just smoke-tested

arch/x86/hyperv/hv_init.c +0 -0
index 7581cab74acb..17845db67fe2 100644
@@ -382,7 +382,7 @@ void __init hyperv_init(void)
        guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
        wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 
Quoted 7 lines

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 386

Quoted 3 lines
arch/x86/hyperv/hv_init.c +1 -1
-       hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL,
 PAGE_KERNEL_RX);
+       hv_hypercall_pg  = vmalloc_exec(PAGE_SIZE);

If we try to write into the page, Hyper-V will kill the guest immediately by a virtual double-fault (or triple fault?), IIRC.

The guest would get injected a #GP fault in that case FWIW. Perhaps that leads to further double-fault or triple-fault.

Wei.

#3 lore raw
In reply to Christoph Hellwig
Quoted 19 lines · Christoph Hellwig
Quoted 15 lines · Vitaly Kuznetsov
Quoted 8 lines · Christoph Hellwig

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv: Turn off write permission on the hypercall page") it was even mapped writable..

[There is nothing secret in the hypercall page, by reading it you can figure out if you're running on Intel or AMD (VMCALL/VMMCALL) but it's likely not the only possible way :-)]

Quoted 5 lines
Quoted 2 lines

I see no reason for hv_hypercall_pg to remain readable. I just smoke-tested

Thanks, I have the same in my WIP tree, but just wanted to confirm this makes sense.

Just to make sure we’re all on the same page: x86 doesn’t normally have an execute-only mode. Executable memory in the kernel is readable unless you are using fancy hypervisor-based XO support.

maintainer
#4 lore raw
Quoted 33 lines

From: [email protected] <[email protected]> On Behalf Of Andy Lutomirski
Sent: Tuesday, April 7, 2020 2:01 PM
To: Christoph Hellwig <[email protected]>
Cc: vkuznets <[email protected]>; [email protected]; [email protected]; [email protected]; KY Srinivasan <[email protected]>; Stephen Hemminger <[email protected]>; Andy Lutomirski <[email protected]>; Peter Zijlstra <[email protected]>
Subject: Re: hv_hypercall_pg page permissios

Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Vitaly Kuznetsov

Christoph Hellwig <[email protected]> writes:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv: Turn

off

Quoted 9 lines
Quoted 8 lines
Quoted 1 line

write permission on the hypercall page") it was even mapped writable..

[There is nothing secret in the hypercall page, by reading it you can figure out if you're running on Intel or AMD (VMCALL/VMMCALL) but it's likely not the only possible way :-)]

I see no reason for hv_hypercall_pg to remain readable. I just smoke-tested

Quoted 6 lines
Quoted 2 lines

Thanks, I have the same in my WIP tree, but just wanted to confirm this makes sense.

Just to make sure we’re all on the same page: x86 doesn’t normally have an execute-only mode. Executable memory in the kernel is readable unless you are using fancy hypervisor-based XO support.

Hi hch,
The patch is merged into the mainine recently, but unluckily we noticed a warning with CONFIG_DEBUG_WX=y (it looks typically this config is defined by default in Linux distros, at least in Ubuntu 18.04's /boot/config-4.18.0-11-generic).

Should we revert this patch, or figure out a way to ask the DEBUG_WX code to ignore this page?

[   19.387536] debug: unmapping init [mem 0xffffffff82713000-0xffffffff82886fff]
[   19.431766] Write protecting the kernel read-only data: 18432k
[   19.438662] debug: unmapping init [mem 0xffffffff81c02000-0xffffffff81dfffff]
[   19.446830] debug: unmapping init [mem 0xffffffff821d6000-0xffffffff821fffff]
[   19.522368] ------------[ cut here ]------------
[   19.527495] x86/mm: Found insecure W+X mapping at address 0xffffc90000012000
[   19.535066] WARNING: CPU: 26 PID: 1 at arch/x86/mm/dump_pagetables.c:248 note_page+0x639/0x690
[   19.539038] Modules linked in:
[   19.539038] CPU: 26 PID: 1 Comm: swapper/0 Not tainted 5.7.0+ #1
[   19.539038] Hardware name: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS 090008  12/07/2018
[   19.539038] RIP: 0010:note_page+0x639/0x690
[   19.539038] Code: fe ff ff 31 c0 e9 a0 fe ff ff 80 3d 39 d1 31 01 00 0f 85 76 fa ff ff 48 c7 c7 98 55 0a 82 c6 05 25 d1 31 01 01 e8 f7 c9 00 00 <0f> 0b e9 5c fa ff ff 48 83 c0 18 48 c7 45 68 00 00 00 00 48 89 45
[   19.539038] RSP: 0000:ffffc90003137cb0 EFLAGS: 00010282
[   19.539038] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000007
[   19.539038] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffff810fa9c4
[   19.539038] RBP: ffffc90003137ea0 R08: 0000000000000000 R09: 0000000000000000
[   19.539038] R10: 0000000000000001 R11: 0000000000000000 R12: ffffc90000013000
[   19.539038] R13: 0000000000000000 R14: ffffc900001ff000 R15: 0000000000000000
[   19.539038] FS:  0000000000000000(0000) GS:ffff8884dad00000(0000) knlGS:0000000000000000
[   19.539038] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   19.539038] CR2: 0000000000000000 CR3: 0000000002210001 CR4: 00000000003606e0
[   19.539038] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[   19.539038] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[   19.539038] Call Trace:
[   19.539038]  ptdump_pte_entry+0x39/0x40
[   19.539038]  __walk_page_range+0x5b7/0x960
[   19.539038]  walk_page_range_novma+0x7e/0xd0
[   19.539038]  ptdump_walk_pgd+0x53/0x90
[   19.539038]  ptdump_walk_pgd_level_core+0xdf/0x110
[   19.539038]  ? ptdump_walk_pgd_level_debugfs+0x40/0x40
[   19.539038]  ? hugetlb_get_unmapped_area+0x2f0/0x2f0
[   19.703692]  ? rest_init+0x24d/0x24d
[   19.703692]  ? rest_init+0x24d/0x24d
[   19.703692]  kernel_init+0x2c/0x113
[   19.703692]  ret_from_fork+0x24/0x30
[   19.703692] irq event stamp: 2840666
[   19.703692] hardirqs last  enabled at (2840665): [<ffffffff810fa9c4>] console_unlock+0x444/0x5b0
[   19.703692] hardirqs last disabled at (2840666): [<ffffffff81001ec9>] trace_hardirqs_off_thunk+0x1a/0x1c
[   19.703692] softirqs last  enabled at (2840662): [<ffffffff81c00366>] __do_softirq+0x366/0x490
[   19.703692] softirqs last disabled at (2840655): [<ffffffff8107dba8>] irq_exit+0xe8/0x100
[   19.703692] ---[ end trace 99ca90806a8e657c ]---
[   19.786235] x86/mm: Checked W+X mappings: FAILED, 1 W+X pages found.
[   19.793298] rodata_test: all tests were successful
[   19.798508] x86/mm: Checking user space page tables
[   19.818007] x86/mm: Checked W+X mappings: passed, no W+X pages found.

Thanks,
-- Dexuan

#5 lore raw
Quoted 40 lines · Dexuan Cui
Quoted 33 lines

From: [email protected] <[email protected]> On Behalf Of Andy Lutomirski
Sent: Tuesday, April 7, 2020 2:01 PM
To: Christoph Hellwig <[email protected]>
Cc: vkuznets <[email protected]>; [email protected]; [email protected]; [email protected]; KY Srinivasan <[email protected]>; Stephen Hemminger <[email protected]>; Andy Lutomirski <[email protected]>; Peter Zijlstra <[email protected]>
Subject: Re: hv_hypercall_pg page permissios

Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Vitaly Kuznetsov

Christoph Hellwig <[email protected]> writes:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv: Turn

off

Quoted 9 lines
Quoted 8 lines
Quoted 1 line

write permission on the hypercall page") it was even mapped writable..

[There is nothing secret in the hypercall page, by reading it you can figure out if you're running on Intel or AMD (VMCALL/VMMCALL) but it's likely not the only possible way :-)]

I see no reason for hv_hypercall_pg to remain readable. I just smoke-tested

Quoted 6 lines
Quoted 2 lines

Thanks, I have the same in my WIP tree, but just wanted to confirm this makes sense.

Just to make sure we’re all on the same page: x86 doesn’t normally have an execute-only mode. Executable memory in the kernel is readable unless you are using fancy hypervisor-based XO support.

Quoted 9 lines

Hi hch,
The patch is merged into the mainine recently, but unluckily we noticed a warning with CONFIG_DEBUG_WX=y (it looks typically this config is defined by default in Linux distros, at least in Ubuntu 18.04's /boot/config-4.18.0-11-generic).

Should we revert this patch, or figure out a way to ask the DEBUG_WX code to ignore this page?

Are you sure it is hv_hypercall_pg? AFAIU it shouldn't be W+X as we are allocating it with vmalloc_exec(). In other words, if you revert 78bb17f76edc, does the issue go away?

Quoted 48 lines
[   19.387536] debug: unmapping init [mem 0xffffffff82713000-0xffffffff82886fff]
[   19.431766] Write protecting the kernel read-only data: 18432k
[   19.438662] debug: unmapping init [mem 0xffffffff81c02000-0xffffffff81dfffff]
[   19.446830] debug: unmapping init [mem 0xffffffff821d6000-0xffffffff821fffff]
[   19.522368] ------------[ cut here ]------------
[   19.527495] x86/mm: Found insecure W+X mapping at address 0xffffc90000012000
[   19.535066] WARNING: CPU: 26 PID: 1 at arch/x86/mm/dump_pagetables.c:248 note_page+0x639/0x690
[   19.539038] Modules linked in:
[   19.539038] CPU: 26 PID: 1 Comm: swapper/0 Not tainted 5.7.0+ #1
[   19.539038] Hardware name: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS 090008  12/07/2018
[   19.539038] RIP: 0010:note_page+0x639/0x690
[   19.539038] Code: fe ff ff 31 c0 e9 a0 fe ff ff 80 3d 39 d1 31 01 00 0f 85 76 fa ff ff 48 c7 c7 98 55 0a 82 c6 05 25 d1 31 01 01 e8 f7 c9 00 00 <0f> 0b e9 5c fa ff ff 48 83 c0 18 48 c7 45 68 00 00 00 00 48 89 45
[   19.539038] RSP: 0000:ffffc90003137cb0 EFLAGS: 00010282
[   19.539038] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000007
[   19.539038] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffffffff810fa9c4
[   19.539038] RBP: ffffc90003137ea0 R08: 0000000000000000 R09: 0000000000000000
[   19.539038] R10: 0000000000000001 R11: 0000000000000000 R12: ffffc90000013000
[   19.539038] R13: 0000000000000000 R14: ffffc900001ff000 R15: 0000000000000000
[   19.539038] FS:  0000000000000000(0000) GS:ffff8884dad00000(0000) knlGS:0000000000000000
[   19.539038] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   19.539038] CR2: 0000000000000000 CR3: 0000000002210001 CR4: 00000000003606e0
[   19.539038] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[   19.539038] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[   19.539038] Call Trace:
[   19.539038]  ptdump_pte_entry+0x39/0x40
[   19.539038]  __walk_page_range+0x5b7/0x960
[   19.539038]  walk_page_range_novma+0x7e/0xd0
[   19.539038]  ptdump_walk_pgd+0x53/0x90
[   19.539038]  ptdump_walk_pgd_level_core+0xdf/0x110
[   19.539038]  ? ptdump_walk_pgd_level_debugfs+0x40/0x40
[   19.539038]  ? hugetlb_get_unmapped_area+0x2f0/0x2f0
[   19.703692]  ? rest_init+0x24d/0x24d
[   19.703692]  ? rest_init+0x24d/0x24d
[   19.703692]  kernel_init+0x2c/0x113
[   19.703692]  ret_from_fork+0x24/0x30
[   19.703692] irq event stamp: 2840666
[   19.703692] hardirqs last  enabled at (2840665): [<ffffffff810fa9c4>] console_unlock+0x444/0x5b0
[   19.703692] hardirqs last disabled at (2840666): [<ffffffff81001ec9>] trace_hardirqs_off_thunk+0x1a/0x1c
[   19.703692] softirqs last  enabled at (2840662): [<ffffffff81c00366>] __do_softirq+0x366/0x490
[   19.703692] softirqs last disabled at (2840655): [<ffffffff8107dba8>] irq_exit+0xe8/0x100
[   19.703692] ---[ end trace 99ca90806a8e657c ]---
[   19.786235] x86/mm: Checked W+X mappings: FAILED, 1 W+X pages found.
[   19.793298] rodata_test: all tests were successful
[   19.798508] x86/mm: Checking user space page tables
[   19.818007] x86/mm: Checked W+X mappings: passed, no W+X pages found.

Thanks,
-- Dexuan

Signature · 1 line
Vitaly
maintainer
#6 lore raw
Quoted 44 lines

From: Vitaly Kuznetsov <[email protected]>
Sent: Monday, June 15, 2020 1:35 AM

Quoted 8 lines · Dexuan Cui
Quoted 8 lines

From: [email protected] <[email protected]> On Behalf Of Andy Lutomirski
Sent: Tuesday, April 7, 2020 2:01 PM

Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Vitaly Kuznetsov

Christoph Hellwig <[email protected]> writes:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Hi all,

The x86 Hyper-V hypercall page (hv_hypercall_pg) is the only allocation in the kernel using __vmalloc with exectutable persmissions, and the only user of PAGE_KERNEL_RX. Is there any good reason it needs to be readable? Otherwise we could use vmalloc_exec and kill off PAGE_KERNEL_RX. Note that before 372b1e91343e6 ("drivers: hv:

Turn

Quoted 7 lines
Quoted 7 lines

off

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

write permission on the hypercall page") it was even mapped writable..

[There is nothing secret in the hypercall page, by reading it you can figure out if you're running on Intel or AMD (VMCALL/VMMCALL) but it's likely not the only possible way :-)]

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I see no reason for hv_hypercall_pg to remain readable. I just smoke-tested

Thanks, I have the same in my WIP tree, but just wanted to confirm this makes sense.

Just to make sure we’re all on the same page: x86 doesn’t normally have

an

Quoted 9 lines
Quoted 2 lines

execute-only mode. Executable memory in the kernel is readable unless you are using fancy hypervisor-based XO support.

Hi hch,
The patch is merged into the mainine recently, but unluckily we noticed a warning with CONFIG_DEBUG_WX=y (it looks typically this config is defined by default in Linux distros, at least in Ubuntu 18.04's /boot/config-4.18.0-11-generic).

Quoted 6 lines
Quoted 1 line

Should we revert this patch, or figure out a way to ask the DEBUG_WX code

to

Quoted 2 lines

ignore this page?

Are you sure it is hv_hypercall_pg?

Yes, 100% sure. I printed the value of hv_hypercall_pg and and it matched the address in the warning line " x86/mm: Found insecure W+X mapping at address".

Quoted 5 lines

AFAIU it shouldn't be W+X as we
are allocating it with vmalloc_exec(). In other words, if you revert 78bb17f76edc, does the issue go away?

Vitaly

Yes, the warning goes away if I revert
78bb17f76edc ("x86/hyperv: use vmalloc_exec for the hypercall page") 88dca4ca5a93 ("mm: remove the pgprot argument to __vmalloc") (I have to revert the second as well with some manual adjustments, since __vmalloc() has 2 parameters now.)

Thanks,
Dexuan

maintainer
#7 lore raw
Quoted 8 lines

From: [email protected] <[email protected]> On Behalf Of Dexuan Cui
Sent: Monday, June 15, 2020 10:42 AM

Quoted 5 lines
Quoted 5 lines

Hi hch,
The patch is merged into the mainine recently, but unluckily we noticed a warning with CONFIG_DEBUG_WX=y

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Should we revert this patch, or figure out a way to ask the DEBUG_WX code to ignore this page?

Are you sure it is hv_hypercall_pg?

Yes, 100% sure. I printed the value of hv_hypercall_pg and and it matched the address in the warning line " x86/mm: Found insecure W+X mapping at address".

I did this experiment:
1. export vmalloc_exec and ptdump_walk_pgd_level_checkwx.
2. write a test module that calls them.
3. It turns out that every call of vmalloc_exec() triggers such a warning.

vmalloc_exec() uses PAGE_KERNEL_EXEC, which is defined as
(__PP|__RW| 0|___A| 0|___D| 0|___G)

It looks the logic in note_page() is: for_each_RW_page, if the NX bit is unset, then report the page as an insecure W+X mapping. IMO this explains the warning?

Thanks,
-- Dexuan

#8 lore raw
Quoted 11 lines · Dexuan Cui

I did this experiment:
1. export vmalloc_exec and ptdump_walk_pgd_level_checkwx.
2. write a test module that calls them.
3. It turns out that every call of vmalloc_exec() triggers such a warning.

vmalloc_exec() uses PAGE_KERNEL_EXEC, which is defined as
(__PP|__RW| 0|___A| 0|___D| 0|___G)

It looks the logic in note_page() is: for_each_RW_page, if the NX bit is unset, then report the page as an insecure W+X mapping. IMO this explains the warning?

It does. But it also means every other user of PAGE_KERNEL_EXEC should trigger this, of which there are a few (kexec, tboot, hibernate, early xen pv mapping, early SEV identity mapping)

We really shouldn't create mappings like this by default. Either we need to flip PAGE_KERNEL_EXEC itself based on the needs of the above users, or add another define to overload vmalloc_exec as there is no other user of that for x86.

#19 lore raw
In reply to Dexuan Cui
Quoted 21 lines · Dexuan Cui
Quoted 8 lines

From: [email protected] <[email protected]> On Behalf Of Dexuan Cui
Sent: Monday, June 15, 2020 10:42 AM

Quoted 5 lines
Quoted 5 lines

Hi hch,
The patch is merged into the mainine recently, but unluckily we noticed a warning with CONFIG_DEBUG_WX=y

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Should we revert this patch, or figure out a way to ask the DEBUG_WX code to ignore this page?

Are you sure it is hv_hypercall_pg?

Yes, 100% sure. I printed the value of hv_hypercall_pg and and it matched the address in the warning line " x86/mm: Found insecure W+X mapping at address".

I did this experiment:
1. export vmalloc_exec and ptdump_walk_pgd_level_checkwx.
2. write a test module that calls them.
3. It turns out that every call of vmalloc_exec() triggers such a warning.

Quoted 6 lines

vmalloc_exec() uses PAGE_KERNEL_EXEC, which is defined as
(__PP|__RW| 0|___A| 0|___D| 0|___G)

It looks the logic in note_page() is: for_each_RW_page, if the NX bit is unset, then report the page as an insecure W+X mapping. IMO this explains the warning?

Yea, bummer.

it seems we need something like PAGE_KERNEL_READONLY_EXEC but we don't seem to have one on x86. Hypercall page is special in a way that the guest doesn't need to write there at all. vmalloc_exec() seems to have only one other user on x86: module_alloc() and it has other needs. On ARM, alloc_insn_page() does the following:

arch/arm64/kernel/probes/kprobes.c: page = vmalloc_exec(PAGE_SIZE);
arch/arm64/kernel/probes/kprobes.c- if (page) {
arch/arm64/kernel/probes/kprobes.c- set_memory_ro((unsigned long)page, 1);
arch/arm64/kernel/probes/kprobes.c- set_vm_flush_reset_perms(page);
arch/arm64/kernel/probes/kprobes.c- }

What if we do the same? (almost untested):

arch/x86/hyperv/hv_init.c +3 -0
index e2137070386a..31aadfea589b 100644
@@ -23,6 +23,7 @@
 #include <linux/kernel.h>
 #include <linux/cpuhotplug.h>
 #include <linux/syscore_ops.h>
+#include <linux/set_memory.h>
 #include <clocksource/hyperv_timer.h>
 
 void *hv_hypercall_pg;
@@ -383,6 +384,8 @@ void __init hyperv_init(void)
                wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
                goto remove_cpuhp_state;
        }
+       set_memory_ro((unsigned long)hv_hypercall_pg, 1);
+       set_vm_flush_reset_perms(hv_hypercall_pg);
 
        rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
        hypercall_msr.enable = 1;
Signature · 1 line
Vitaly
#20 lore raw
Quoted 4 lines · Vitaly Kuznetsov

it seems we need something like PAGE_KERNEL_READONLY_EXEC but we don't seem to have one on x86. Hypercall page is special in a way that the guest doesn't need to write there at all. vmalloc_exec() seems to have only one other user on x86: module_alloc() and it has other needs.

module_alloc actually is a weak function and overriden on x86 (and many other architectures) , so it isn't used either (did I mention that I hate weak functions?)

Quoted 27 lines

On
ARM, alloc_insn_page() does the following:

arch/arm64/kernel/probes/kprobes.c: page = vmalloc_exec(PAGE_SIZE);
arch/arm64/kernel/probes/kprobes.c- if (page) {
arch/arm64/kernel/probes/kprobes.c- set_memory_ro((unsigned long)page, 1);
arch/arm64/kernel/probes/kprobes.c- set_vm_flush_reset_perms(page);
arch/arm64/kernel/probes/kprobes.c- }

What if we do the same? (almost untested):

arch/x86/hyperv/hv_init.c +1 -0
index e2137070386a..31aadfea589b 100644
@@ -23,6 +23,7 @@
 #include <linux/kernel.h>
 #include <linux/cpuhotplug.h>
 #include <linux/syscore_ops.h>
+#include <linux/set_memory.h>
 #include <clocksource/hyperv_timer.h>
 
 void *hv_hypercall_pg;

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 388

Quoted 5 lines
arch/x86/hyperv/hv_init.c +2 -0
@@ -383,6 +384,8 @@ void __init hyperv_init(void)
                wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
                goto remove_cpuhp_state;
        }
+       set_memory_ro((unsigned long)hv_hypercall_pg, 1);
+       set_vm_flush_reset_perms(hv_hypercall_pg);

This should work and might be the best for 5.8, but I think we need to sort this whole mess out for real.

#21 lore raw

Actually, what do you think of this one:

---
From a5ee278f4244c6bfc0ce2d2b2fd4f99358dbde4d Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Tue, 16 Jun 2020 11:50:59 +0200
Subject: x86/hyperv: allocate the hypercall page with only read and execute
bits

Avoid a W^X violation cause by the fact that PAGE_KERNEL_EXEC includes the writable bit.

Fixes 78bb17f76edc ("x86/hyperv: use vmalloc_exec for the hypercall page")
---
 arch/x86/hyperv/hv_init.c            | 4 +++-
 arch/x86/include/asm/pgtable_types.h | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/hyperv/hv_init.c +3 -1
index a54c6a401581dd..f4875bf05c17ff 100644
@@ -375,7 +375,9 @@ void __init hyperv_init(void)
 	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 
-	hv_hypercall_pg = vmalloc_exec(PAGE_SIZE);
+	hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
+			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_RX,
+			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, __func__);
 	if (hv_hypercall_pg == NULL) {
 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 		goto remove_cpuhp_state;
arch/x86/include/asm/pgtable_types.h +2 -0
index 2da1f95b88d761..591b5c92a66249 100644
@@ -194,6 +194,7 @@ enum page_cache_mode {
 #define _PAGE_TABLE_NOENC	 (__PP|__RW|_USR|___A|   0|___D|   0|   0)
 #define _PAGE_TABLE		 (__PP|__RW|_USR|___A|   0|___D|   0|   0| _ENC)
 #define __PAGE_KERNEL_RO	 (__PP|   0|   0|___A|__NX|___D|   0|___G)
+#define __PAGE_KERNEL_RX	 (__PP|   0|   0|___A|   0|___D|   0|___G)
 #define __PAGE_KERNEL_NOCACHE	 (__PP|__RW|   0|___A|__NX|___D|   0|___G| __NC)
 #define __PAGE_KERNEL_VVAR	 (__PP|   0|_USR|___A|__NX|___D|   0|___G)
 #define __PAGE_KERNEL_LARGE	 (__PP|__RW|   0|___A|__NX|___D|_PSE|___G)
@@ -219,6 +220,7 @@ enum page_cache_mode {
 #define PAGE_KERNEL_RO		__pgprot_mask(__PAGE_KERNEL_RO         | _ENC)
 #define PAGE_KERNEL_EXEC	__pgprot_mask(__PAGE_KERNEL_EXEC       | _ENC)
 #define PAGE_KERNEL_EXEC_NOENC	__pgprot_mask(__PAGE_KERNEL_EXEC       |    0)
+#define PAGE_KERNEL_RX		__pgprot_mask(__PAGE_KERNEL_RX         | _ENC)
 #define PAGE_KERNEL_NOCACHE	__pgprot_mask(__PAGE_KERNEL_NOCACHE    | _ENC)
 #define PAGE_KERNEL_LARGE	__pgprot_mask(__PAGE_KERNEL_LARGE      | _ENC)
 #define PAGE_KERNEL_LARGE_EXEC	__pgprot_mask(__PAGE_KERNEL_LARGE_EXEC | _ENC)
Signature · 1 line
2.26.2
#9 lore raw
In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 8 lines · Dexuan Cui

I did this experiment:
1. export vmalloc_exec and ptdump_walk_pgd_level_checkwx.
2. write a test module that calls them.
3. It turns out that every call of vmalloc_exec() triggers such a warning.

vmalloc_exec() uses PAGE_KERNEL_EXEC, which is defined as
(__PP|__RW| 0|___A| 0|___D| 0|___G)

Quoted 7 lines
Quoted 3 lines

It looks the logic in note_page() is: for_each_RW_page, if the NX bit is unset, then report the page as an insecure W+X mapping. IMO this explains the warning?

It does. But it also means every other user of PAGE_KERNEL_EXEC should trigger this, of which there are a few (kexec, tboot, hibernate, early xen pv mapping, early SEV identity mapping)

There are only 3 users in the entire tree afaict:

arch/arm64/kernel/probes/kprobes.c: page = vmalloc_exec(PAGE_SIZE);

arch/x86/hyperv/hv_init.c:      hv_hypercall_pg = vmalloc_exec(PAGE_SIZE);
kernel/module.c:        return vmalloc_exec(size);

And that last one is a weak function that any arch that has STRICT_RWX ought to override.

Quoted 4 lines

We really shouldn't create mappings like this by default. Either we need to flip PAGE_KERNEL_EXEC itself based on the needs of the above users, or add another define to overload vmalloc_exec as there is no other user of that for x86.

We really should get rid of the two !module users of this though; both x86 and arm64 have STRICT_RWX and sufficient primitives to DTRT.

What is HV even trying to do with that page? AFAICT it never actually writes to it, it seens to give the physica address to an MSR (which I suspect then writes crud into the page for us from host context).

Suggesting the page really only needs to be RX.

On top of that, vmalloc_exec() gets us a page from the entire vmalloc range, which can be outside of the 2G executable range, which seems to suggest vmalloc_exec() is wrong too and all this works by accident.

How about something like this:

arch/x86/hyperv/hv_init.c +4 -1
index a54c6a401581..82a3a4a9481f 100644
@@ -375,12 +375,15 @@ void __init hyperv_init(void)
 	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 
-	hv_hypercall_pg = vmalloc_exec(PAGE_SIZE);
+	hv_hypercall_pg = module_alloc(PAGE_SIZE);
 	if (hv_hypercall_pg == NULL) {
 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 		goto remove_cpuhp_state;
 	}
 
+	set_memory_ro((unsigned long)hv_hypercall_pg, 1);
+	set_memory_x((unsigned long)hv_hypercall_pg, 1);
+
 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 	hypercall_msr.enable = 1;
 	hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
#24 lore raw
In reply to Christoph Hellwig

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 380

Quoted 3 lines · Christoph Hellwig
arch/x86/hyperv/hv_init.c +3 -0
+	hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
+			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_RX,
+			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, __func__);

I think that's wrong, they seem to want to CALL into that page, and that doesn't unconditionally work for any page in the vmalloc range. This really ought to use module_alloc() to guarantees it gets a page in the correct range.

#10 lore raw
In reply to Peter Zijlstra
Quoted 41 lines · Peter Zijlstra
Quoted 3 lines

It does. But it also means every other user of PAGE_KERNEL_EXEC should trigger this, of which there are a few (kexec, tboot, hibernate, early xen pv mapping, early SEV identity mapping)

There are only 3 users in the entire tree afaict:

arch/arm64/kernel/probes/kprobes.c: page = vmalloc_exec(PAGE_SIZE);

arch/x86/hyperv/hv_init.c:      hv_hypercall_pg = vmalloc_exec(PAGE_SIZE);
kernel/module.c:        return vmalloc_exec(size);

And that last one is a weak function that any arch that has STRICT_RWX ought to override.

Quoted 4 lines

We really shouldn't create mappings like this by default. Either we need to flip PAGE_KERNEL_EXEC itself based on the needs of the above users, or add another define to overload vmalloc_exec as there is no other user of that for x86.

We really should get rid of the two !module users of this though; both x86 and arm64 have STRICT_RWX and sufficient primitives to DTRT.

What is HV even trying to do with that page? AFAICT it never actually writes to it, it seens to give the physica address to an MSR (which I suspect then writes crud into the page for us from host context).

Suggesting the page really only needs to be RX.

On top of that, vmalloc_exec() gets us a page from the entire vmalloc range, which can be outside of the 2G executable range, which seems to suggest vmalloc_exec() is wrong too and all this works by accident.

How about something like this:

arch/x86/hyperv/hv_init.c +0 -0
index a54c6a401581..82a3a4a9481f 100644
@@ -375,12 +375,15 @@ void __init hyperv_init(void)
 	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 385

Quoted 10 lines
arch/x86/hyperv/hv_init.c +3 -1
 
-	hv_hypercall_pg = vmalloc_exec(PAGE_SIZE);
+	hv_hypercall_pg = module_alloc(PAGE_SIZE);
 	if (hv_hypercall_pg == NULL) {
 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 		goto remove_cpuhp_state;
 	}
 
+	set_memory_ro((unsigned long)hv_hypercall_pg, 1);
+	set_memory_x((unsigned long)hv_hypercall_pg, 1);

The changing of the permissions sucks. I thought about adding a module_alloc_prot with an explicit pgprot_t argument. On x86 alone at least ftrace would also benefit from that.

#11 lore raw
Quoted 50 lines · Christoph Hellwig
Quoted 49 lines · Peter Zijlstra
Quoted 3 lines

It does. But it also means every other user of PAGE_KERNEL_EXEC should trigger this, of which there are a few (kexec, tboot, hibernate, early xen pv mapping, early SEV identity mapping)

There are only 3 users in the entire tree afaict:

arch/arm64/kernel/probes/kprobes.c: page = vmalloc_exec(PAGE_SIZE);

arch/x86/hyperv/hv_init.c:      hv_hypercall_pg = vmalloc_exec(PAGE_SIZE);
kernel/module.c:        return vmalloc_exec(size);

And that last one is a weak function that any arch that has STRICT_RWX ought to override.

Quoted 4 lines

We really shouldn't create mappings like this by default. Either we need to flip PAGE_KERNEL_EXEC itself based on the needs of the above users, or add another define to overload vmalloc_exec as there is no other user of that for x86.

We really should get rid of the two !module users of this though; both x86 and arm64 have STRICT_RWX and sufficient primitives to DTRT.

What is HV even trying to do with that page? AFAICT it never actually writes to it, it seens to give the physica address to an MSR (which I suspect then writes crud into the page for us from host context).

Suggesting the page really only needs to be RX.

On top of that, vmalloc_exec() gets us a page from the entire vmalloc range, which can be outside of the 2G executable range, which seems to suggest vmalloc_exec() is wrong too and all this works by accident.

How about something like this:

arch/x86/hyperv/hv_init.c +1 -1
index a54c6a401581..82a3a4a9481f 100644
@@ -375,12 +375,15 @@ void __init hyperv_init(void)
 	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 
-	hv_hypercall_pg = vmalloc_exec(PAGE_SIZE);
+	hv_hypercall_pg = module_alloc(PAGE_SIZE);
 	if (hv_hypercall_pg == NULL) {
 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 		goto remove_cpuhp_state;
 	}
 
Quoted 6 lines

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 385

Quoted 2 lines
arch/x86/hyperv/hv_init.c +2 -0
+	set_memory_ro((unsigned long)hv_hypercall_pg, 1);
+	set_memory_x((unsigned long)hv_hypercall_pg, 1);

The changing of the permissions sucks. I thought about adding a module_alloc_prot with an explicit pgprot_t argument. On x86 alone at least ftrace would also benefit from that.

The above is also missing a set_vm_flush_reset_perms.

#12 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 6 lines
arch/x86/hyperv/hv_init.c +1 -0
+	hv_hypercall_pg = module_alloc(PAGE_SIZE);
 	if (hv_hypercall_pg == NULL) {
 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 		goto remove_cpuhp_state;
 	}
Quoted 6 lines
Quoted 6 lines

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 385

Quoted 2 lines
arch/x86/hyperv/hv_init.c +2 -0
+	set_memory_ro((unsigned long)hv_hypercall_pg, 1);
+	set_memory_x((unsigned long)hv_hypercall_pg, 1);

The changing of the permissions sucks. I thought about adding a module_alloc_prot with an explicit pgprot_t argument. On x86 alone at least ftrace would also benefit from that.

How would ftrace benefit from a RX permission? We need to actually write to the page first. This HV thing is special in that it lets the host write.

Quoted 1 line

The above is also missing a set_vm_flush_reset_perms.

Ah, indeed. Otherwise we don't clean up properly.

#13 lore raw
Quoted 16 lines · Peter Zijlstra
Quoted 8 lines · Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 6 lines
arch/x86/hyperv/hv_init.c +1 -0
+	hv_hypercall_pg = module_alloc(PAGE_SIZE);
 	if (hv_hypercall_pg == NULL) {
 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 		goto remove_cpuhp_state;
 	}
Quoted 6 lines
Quoted 6 lines

arch/x86/hyperv/hv_init.c void __init hyperv_init(void) · line 385

Quoted 2 lines
arch/x86/hyperv/hv_init.c +2 -0
+	set_memory_ro((unsigned long)hv_hypercall_pg, 1);
+	set_memory_x((unsigned long)hv_hypercall_pg, 1);

The changing of the permissions sucks. I thought about adding a module_alloc_prot with an explicit pgprot_t argument. On x86 alone at least ftrace would also benefit from that.

Quoted 3 lines

How would ftrace benefit from a RX permission? We need to actually write to the page first. This HV thing is special in that it lets the host write.

sorry, s/ftrace/kprobes/. See my updated branch here:

http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/module_alloc-cleanup

#15 lore raw
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Christoph Hellwig

sorry, s/ftrace/kprobes/. See my updated branch here:

http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/module_alloc-cleanup

Ah the insn slot page, yes. Didn't you just loose VM_FLUSH_RESET_PERMS there?

Yes, we did. vmalloc_exec had it, but given that module_alloc didn't allocate executable on x86 it didn't. I guess we should make sure it is set by the low-level vmalloc code if exec permissions are set to sort this mess out.

#23 lore raw
In reply to Christoph Hellwig
Quoted 3 lines · Christoph Hellwig
Quoted 1 line · Christoph Hellwig

Actually, what do you think of this one:

I see Peter has some concerns but if vmalloc_exec() used to work then open-coding it with the right flags should do. I've also tested your patch with Hyper-V, it works, so:

Quoted 4 lines

Plus this whole series to kill of vmalloc_exec entirely:

http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/vmalloc_exec-fixes

FWIW, the vmalloc_exec() doing W+X allocation is misleading indeed, thus killing it makes perfect sense.

Signature · 1 line
Vitaly
#16 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Christoph Hellwig

sorry, s/ftrace/kprobes/. See my updated branch here:

http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/module_alloc-cleanup

Quoted 7 lines
Quoted 2 lines

Ah the insn slot page, yes. Didn't you just loose VM_FLUSH_RESET_PERMS there?

Yes, we did. vmalloc_exec had it, but given that module_alloc didn't allocate executable on x86 it didn't. I guess we should make sure it is set by the low-level vmalloc code if exec permissions are set to sort this mess out.

I think something like this should solve the issue:

--

arch/x86/include/asm/module.h +2 -0
index e988bac0a4a1c3..716e4de44a8e78 100644
@@ -13,4 +13,6 @@ struct mod_arch_specific {
 #endif
 };
 
+void *module_alloc_prot(unsigned long size, pgprot_t prot);
+
 #endif /* _ASM_X86_MODULE_H */
arch/x86/kernel/module.c +9 -2
index 34b153cbd4acb4..4db6e655120960 100644
@@ -65,8 +65,10 @@ static unsigned long int get_module_load_offset(void)
 }
 #endif
 
-void *module_alloc(unsigned long size)
+void *module_alloc_prot(unsigned long size, pgprot_t prot)
 {
+	unsigned int flags = (pgprot_val(prot) & _PAGE_NX) ?
+			0 : VM_FLUSH_RESET_PERMS;
 	void *p;
 
 	if (PAGE_ALIGN(size) > MODULES_LEN)
@@ -75,7 +77,7 @@ void *module_alloc(unsigned long size)
 	p = __vmalloc_node_range(size, MODULE_ALIGN,
 				    MODULES_VADDR + get_module_load_offset(),
 				    MODULES_END, GFP_KERNEL,
-				    PAGE_KERNEL, 0, NUMA_NO_NODE,
+				    prot, flags, NUMA_NO_NODE,
 				    __builtin_return_address(0));
 	if (p && (kasan_module_alloc(p, size) < 0)) {
 		vfree(p);
@@ -85,6 +87,11 @@ void *module_alloc(unsigned long size)
 	return p;
 }
 
+void *module_alloc(unsigned long size)
+{
+	return module_alloc_prot(size, PAGE_KERNEL);
+}
+
 #ifdef CONFIG_X86_32
 int apply_relocate(Elf32_Shdr *sechdrs,
 		   const char *strtab,
#17 lore raw
Quoted 31 lines · Christoph Hellwig

I think something like this should solve the issue:

--

arch/x86/include/asm/module.h +2 -0
index e988bac0a4a1c3..716e4de44a8e78 100644
@@ -13,4 +13,6 @@ struct mod_arch_specific {
 #endif
 };
 
+void *module_alloc_prot(unsigned long size, pgprot_t prot);
+
 #endif /* _ASM_X86_MODULE_H */
arch/x86/kernel/module.c +3 -1
index 34b153cbd4acb4..4db6e655120960 100644
@@ -65,8 +65,10 @@ static unsigned long int get_module_load_offset(void)
 }
 #endif
 
-void *module_alloc(unsigned long size)
+void *module_alloc_prot(unsigned long size, pgprot_t prot)
 {
+	unsigned int flags = (pgprot_val(prot) & _PAGE_NX) ?
+			0 : VM_FLUSH_RESET_PERMS;
 	void *p;
 
 	if (PAGE_ALIGN(size) > MODULES_LEN)

arch/x86/kernel/module.c void *module_alloc(unsigned long size) · line 83

Quoted 8 lines
arch/x86/kernel/module.c +1 -1
@@ -75,7 +77,7 @@ void *module_alloc(unsigned long size)
 	p = __vmalloc_node_range(size, MODULE_ALIGN,
 				    MODULES_VADDR + get_module_load_offset(),
 				    MODULES_END, GFP_KERNEL,
-				    PAGE_KERNEL, 0, NUMA_NO_NODE,
+				    prot, flags, NUMA_NO_NODE,
 				    __builtin_return_address(0));
 	if (p && (kasan_module_alloc(p, size) < 0)) {
 		vfree(p);

Hurmm.. Yes it would. It just doesn't feel right though. Can't we unconditionally set the flag? At worst it makes free a little bit more expensive.

The thing is, I don't think _NX is the only prot that needs restoring. Any prot other than the default (RW IIRC) needs restoring.

#18 lore raw
Quoted 21 lines · Peter Zijlstra
Quoted 8 lines
arch/x86/kernel/module.c +3 -0
+void *module_alloc_prot(unsigned long size, pgprot_t prot)
 {
+	unsigned int flags = (pgprot_val(prot) & _PAGE_NX) ?
+			0 : VM_FLUSH_RESET_PERMS;
 	void *p;
 
 	if (PAGE_ALIGN(size) > MODULES_LEN)

arch/x86/kernel/module.c void *module_alloc(unsigned long size) · line 83

Quoted 8 lines
arch/x86/kernel/module.c +1 -1
@@ -75,7 +77,7 @@ void *module_alloc(unsigned long size)
 	p = __vmalloc_node_range(size, MODULE_ALIGN,
 				    MODULES_VADDR + get_module_load_offset(),
 				    MODULES_END, GFP_KERNEL,
-				    PAGE_KERNEL, 0, NUMA_NO_NODE,
+				    prot, flags, NUMA_NO_NODE,
 				    __builtin_return_address(0));
 	if (p && (kasan_module_alloc(p, size) < 0)) {
 		vfree(p);

Hurmm.. Yes it would. It just doesn't feel right though. Can't we unconditionally set the flag? At worst it makes free a little bit more expensive.

Quoted 2 lines

The thing is, I don't think _NX is the only prot that needs restoring. Any prot other than the default (RW IIRC) needs restoring.

If that actually is the case I think we should just check for a non-default permission in __vmalloc_node_range itself and handle it there, which seems like a nice solution.