mshv_vtl: Check per-CPU register page before mmap
PATCH 1/1 mshv_vtl: Check per-CPU register page before mmap
Register-page setup is optional for each CPU. If allocation or registration fails, mshv_vtl_configure_reg_page() warns and leaves that CPU's reg_page NULL, but initial context setup continues successfully. Meanwhile, successful setup on another CPU sets the global mshv_has_reg_page flag.
mshv_vtl_fault() checks this global flag before selecting the requested CPU's register page. With mixed setup results across online CPUs, the check passes even for a CPU with no register page, reaching get_page(NULL) when userspace faults in that mapping.
Check the selected per-CPU register page before taking its reference and return VM_FAULT_SIGBUS if it is absent.
--- drivers/hv/mshv_vtl_main.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/hv/mshv_vtl_main.c +3 -0
@@ -912,6 +912,9 @@ static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ; } +if (!page) +return VM_FAULT_SIGBUS ; +get_page (page); vmf->page = page;
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] Returning VM_FAULT_NOPAGE for invalid memory offsets without installing a PTE causes an unkillable infinite page fault loop.
- [High] Per-CPU memory allocated for run and reg_page is leaked on driver module unload and upon CPU hotplug setup failures.
- [High] Premature character device registration allows userspace to trigger page faults and ioctls before per-CPU data is initialized, bypassing the newly added if (!page) check and causing kernel panics or UAF. --
commit 33a60782bd60657bd70b537be47acdb4ffe78f38
Author: Hardik Garg <[email protected]>
mshv_vtl: Check per-CPU register page before mmap
Register-page setup is optional for each CPU. If allocation or registration fails, mshv_vtl_configure_reg_page() warns and leaves that CPU's reg_page NULL, but initial context setup continues successfully. Meanwhile, successful setup on another CPU sets the global mshv_has_reg_page flag.
mshv_vtl_fault() checks this global flag before selecting the requested CPU's register page. With mixed setup results across online CPUs, the check passes even for a CPU with no register page, reaching get_page(NULL) when userspace faults in that mapping.
Check the selected per-CPU register page before taking its reference and return VM_FAULT_SIGBUS if it is absent.
drivers/hv/mshv_vtl_main.c static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf) · line 820
Quoted 7 lines
drivers/hv/mshv_vtl_main.c +0
-0@@ -819,6 +819,9 @@ static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ; }
[Severity: High]
This isn't a bug introduced by this patch, but returning VM_FAULT_NOPAGE for an invalid memory offset without installing a PTE causes an unkillable infinite page fault loop.
If a userspace process maps the /dev/mshv device and accesses an invalid page offset (neither MSHV_RUN_PAGE_OFFSET nor MSHV_REG_PAGE_OFFSET), this else branch is taken. Returning VM_FAULT_NOPAGE signals to the core MM subsystem that the fault handler successfully installed the page table entry, so the kernel returns to userspace without doing anything. Since no PTE was actually installed, the CPU will immediately re-fault on the same instruction, creating a 100% CPU hung task.
Could we return VM_FAULT_SIGBUS here instead?
drivers/hv/mshv_vtl_main.c static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf) · line 916
Quoted 3 lines
drivers/hv/mshv_vtl_main.c +2
-0+if (!page) +return VM_FAULT_SIGBUS ;
[Severity: High]
This is a pre-existing issue, but premature character device registration allows userspace to bypass this newly added check.
In mshv_vtl_init(), the character device is exposed to userspace early:
mshv_vtl_init() {
...
ret = misc_register(&mshv_dev);
...
}If a userspace process races the module initialization by opening /dev/mshv and mapping it before hv_vtl_setup_synic() allocates the per-CPU run page, mshv_vtl_cpu_run(cpu) will return NULL.
When this happens, virt_to_page(NULL) is called:
mshv_vtl_fault() {
...
if (real_off == MSHV_RUN_PAGE_OFFSET) {
page = virt_to_page(mshv_vtl_cpu_run(cpu));
...
}This produces a garbage non-NULL pointer, causing the new if (!page) check to pass, and the subsequent get_page() will dereference the garbage pointer and panic.
If initialization subsequently fails, the module load aborts and .text is freed while the racing userspace process retains an open file descriptor, leading to a use-after-free.
Could the device registration be deferred until after the per-CPU data is initialized?
drivers/hv/mshv_vtl_main.c static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf) · line 920
Quoted 4 lines
drivers/hv/mshv_vtl_main.c +1
-0+get_page (page); vmf->page = page;
[Severity: High]
This is a pre-existing issue, but does this code leak the per-CPU pages allocated for run and reg_page on driver module unload and upon CPU hotplug setup failures?
When setting up the hotplug state, the teardown callback is set to NULL:
hv_vtl_setup_synic() {
...
ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vtl:online",
mshv_vtl_alloc_context, NULL);
...
}When the module unloads, cpuhp_remove_state() is called, but without a teardown callback, the memory permanently allocated via __get_free_page() in mshv_vtl_alloc_context() is leaked:
mshv_vtl_alloc_context() {
...
per_cpu->run = (struct mshv_vtl_run *)
__get_free_page(GFP_KERNEL | __GFP_ZERO);
...
}Additionally, if the hotplug state setup fails midway, the successfully processed CPUs will not be cleaned up.
Could a teardown callback be provided to free these pages?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 7 lines · On 9/11/2026 11:05 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] ReturningVM_FAULT_NOPAGEfor invalid memory offsets without installing a PTE causes an unkillable infinite page fault loop.
- [High] Per-CPU memory allocated forrunandreg_pageis leaked on driver module unload and upon CPU hotplug setup failures.
- [High] Premature character device registration allows userspace to trigger page faults and ioctls before per-CPU data is initialized, bypassing the newly addedif (!page)check and causing kernel panics or UAF. --
All the reported issues are pre-existing and not related to this patch. I will review the issues and fix them in a separate patch.
Quoted 117 lines
commit
33a60782bd60657bd70b537be47acdb4ffe78f38
Author: Hardik Garg <[email protected]>
mshv_vtl: Check per-CPU register page before mmapRegister-page setup is optional for each CPU. If allocation or registration fails,
mshv_vtl_configure_reg_page()warns and leaves that CPU'sreg_pageNULL, but initial context setup continues successfully. Meanwhile, successful setup on another CPU sets the globalmshv_has_reg_pageflag.
mshv_vtl_fault()checks this global flag before selecting the requested CPU's register page. With mixed setup results across online CPUs, the check passes even for a CPU with no register page, reachingget_page(NULL)when userspace faults in that mapping.Check the selected per-CPU register page before taking its reference and return
VM_FAULT_SIGBUSif it is absent.Fixes 7bfe3b8ea6e3 ("Drivers: hv: Introduce mshv_vtl driver")drivers/hv/mshv_vtl_main.c static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf) · line 820
Quoted 7 lines
drivers/hv/mshv_vtl_main.c +0
-0@@ -819,6 +819,9 @@ static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ; }[Severity: High]
This isn't a bug introduced by this patch, but returningVM_FAULT_NOPAGEfor an invalid memory offset without installing a PTE causes an unkillable infinite page fault loop.If a userspace process maps the
/dev/mshvdevice and accesses an invalid page offset (neitherMSHV_RUN_PAGE_OFFSETnorMSHV_REG_PAGE_OFFSET), this else branch is taken. ReturningVM_FAULT_NOPAGEsignals to the core MM subsystem that the fault handler successfully installed the page table entry, so the kernel returns to userspace without doing anything. Since no PTE was actually installed, the CPU will immediately re-fault on the same instruction, creating a 100% CPU hung task.Could we return
VM_FAULT_SIGBUShere instead?drivers/hv/mshv_vtl_main.c static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf) · line 916
Quoted 3 lines
drivers/hv/mshv_vtl_main.c +2
-0+if (!page) +return VM_FAULT_SIGBUS ;[Severity: High]
This is a pre-existing issue, but premature character device registration allows userspace to bypass this newly added check.In
mshv_vtl_init(), the character device is exposed to userspace early:mshv_vtl_init() { ... ret = misc_register(&mshv_dev); ... }If a userspace process races the module initialization by opening
/dev/mshvand mapping it beforehv_vtl_setup_synic()allocates the per-CPU run page,mshv_vtl_cpu_run(cpu)will return NULL.When this happens,
virt_to_page(NULL)is called:mshv_vtl_fault() { ... if (real_off == MSHV_RUN_PAGE_OFFSET) { page = virt_to_page(mshv_vtl_cpu_run(cpu)); ... }This produces a garbage non-NULL pointer, causing the new if (!page) check to pass, and the subsequent
get_page()will dereference the garbage pointer and panic.If initialization subsequently fails, the module load aborts and .text is freed while the racing userspace process retains an open file descriptor, leading to a use-after-free.
Could the device registration be deferred until after the per-CPU data is initialized?
drivers/hv/mshv_vtl_main.c static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf) · line 920
Quoted 4 lines
drivers/hv/mshv_vtl_main.c +1
-0+get_page (page); vmf->page = page;[Severity: High]
This is a pre-existing issue, but does this code leak the per-CPU pages allocated for run andreg_pageon driver module unload and upon CPU hotplug setup failures?When setting up the hotplug state, the teardown callback is set to NULL:
hv_vtl_setup_synic() { ... ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vtl:online", mshv_vtl_alloc_context, NULL); ... }When the module unloads,
cpuhp_remove_state()is called, but without a teardown callback, the memory permanently allocated via__get_free_page()inmshv_vtl_alloc_context()is leaked:mshv_vtl_alloc_context() { ... per_cpu->run = (struct mshv_vtl_run *) __get_free_page(GFP_KERNEL | __GFP_ZERO); ... }Additionally, if the hotplug state setup fails midway, the successfully processed CPUs will not be cleaned up.
Could a teardown callback be provided to free these pages?
Quoted 26 lines · Hardik Garg
Register-page setup is optional for each CPU. If allocation or registration fails,
mshv_vtl_configure_reg_page()warns and leaves that CPU'sreg_pageNULL, but initial context setup continues successfully. Meanwhile, successful setup on another CPU sets the globalmshv_has_reg_pageflag.
mshv_vtl_fault()checks this global flag before selecting the requested CPU's register page. With mixed setup results across online CPUs, the check passes even for a CPU with no register page, reachingget_page(NULL)when userspace faults in that mapping.Check the selected per-CPU register page before taking its reference and return
VM_FAULT_SIGBUSif it is absent.Fixes 7bfe3b8ea6e3 ("Drivers: hv: Introduce mshv_vtl driver")Signed-off-by Hardik Garg <[email protected]>--- drivers/hv/mshv_vtl_main.c | 3 +++ 1 file changed, 3 insertions(+)drivers/hv/mshv_vtl_main.c +0
-0
drivers/hv/mshv_vtl_main.c static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf) · line 920
Quoted 9 lines
drivers/hv/mshv_vtl_main.c +3
-0@@ -912,6 +912,9 @@ static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ; } +if (!page) +return VM_FAULT_SIGBUS ; +get_page (page); vmf->page = page;
LGTM. Thanks.
Regards,
Naman
Quoted 40 lines · Naman Jain
Quoted 26 lines · Hardik Garg
Register-page setup is optional for each CPU. If allocation or registration fails,
mshv_vtl_configure_reg_page()warns and leaves that CPU'sreg_pageNULL, but initial context setup continues successfully. Meanwhile, successful setup on another CPU sets the globalmshv_has_reg_pageflag.
mshv_vtl_fault()checks this global flag before selecting the requested CPU's register page. With mixed setup results across online CPUs, the check passes even for a CPU with no register page, reachingget_page(NULL)when userspace faults in that mapping.Check the selected per-CPU register page before taking its reference and return
VM_FAULT_SIGBUSif it is absent.Fixes 7bfe3b8ea6e3 ("Drivers: hv: Introduce mshv_vtl driver")Signed-off-by Hardik Garg <[email protected]>--- drivers/hv/mshv_vtl_main.c | 3 +++ 1 file changed, 3 insertions(+)drivers/hv/mshv_vtl_main.c +0
-0drivers/hv/mshv_vtl_main.c static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf) · line 918
Quoted 7 lines
drivers/hv/mshv_vtl_main.c +3
-0@@ -912,6 +912,9 @@ static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ; } +if (!page) +return VM_FAULT_SIGBUS ; +get_page (page); vmf->page = page;LGTM. Thanks.
Quoted 1 line
Reviewed-by Naman Jain <[email protected]>
Applied. Thanks.
From: Hardik Garg <[email protected]>
Quoted 11 lines
Register-page setup is optional for each CPU. If allocation or registration fails,
mshv_vtl_configure_reg_page()warns and leaves that CPU'sreg_pageNULL, but initial context setup continues successfully. Meanwhile, successful setup on another CPU sets the globalmshv_has_reg_pageflag.
mshv_vtl_fault()checks this global flag before selecting the requested CPU's register page. With mixed setup results across online CPUs, the check passes even for a CPU with no register page, reachingget_page(NULL)when userspace faults in that mapping.
I'm a bit late in reviewing this because I was travelling all last week.
This patch seems like it is just papering over the real problem, which is that a global variable like mshv_has_reg_page can't represent the status of an operation that may succeed or fail on a per-cpu basis. I pointed this out (as did Sashiko) in review comments back in April [1].
The only other place mshv_has_reg_page is used is in mshv_ioctl_check_extensions() where its value is returned from an ioctl() system call made by user space. That's a questionable practice since ioctl() usually returns 0 on success, though the man page for ioctl() does admit that some ioctls use the return value as an output parameter and return a non-negative value on success. But even then, a single value can't accurately reflect the status of an operation that may succeed or fail on a per-cpu basis. User space would presumably have a similar problem to what is being fixed by this patch.
Is there any reason that the real problem couldn't be fixed instead of doing this fix on top of something that is fundamentally broken?
Michael
Quoted 25 lines
Check the selected per-CPU register page before taking its reference and return
VM_FAULT_SIGBUSif it is absent.Fixes 7bfe3b8ea6e3 ("Drivers: hv: Introduce mshv_vtl driver")Signed-off-by Hardik Garg <[email protected]>--- drivers/hv/mshv_vtl_main.c | 3 +++ 1 file changed, 3 insertions(+)drivers/hv/mshv_vtl_main.c +3
-0@@ -912,6 +912,9 @@ static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ; } +if (!page) +return VM_FAULT_SIGBUS ; +get_page (page); vmf->page = page;
Quoted 30 lines · Michael Kelley
From: Hardik Garg <[email protected]>
Quoted 10 lines
Register-page setup is optional for each CPU. If allocation or registration fails,
mshv_vtl_configure_reg_page()warns and leaves that CPU'sreg_pageNULL, but initial context setup continues successfully. Meanwhile, successful setup on another CPU sets the globalmshv_has_reg_pageflag.
mshv_vtl_fault()checks this global flag before selecting the requested CPU's register page. With mixed setup results across online CPUs, the check passes even for a CPU with no register page, reachingget_page(NULL)when userspace faults in that mapping.I'm a bit late in reviewing this because I was travelling all last week.
This patch seems like it is just papering over the real problem, which is that a global variable like
mshv_has_reg_pagecan't represent the status of an operation that may succeed or fail on a per-cpu basis. I pointed this out (as did Sashiko) in review comments back in April [1].The only other place
mshv_has_reg_pageis used is inmshv_ioctl_check_extensions()where its value is returned from anioctl()system call made by user space. That's a questionable practice sinceioctl()usually returns 0 on success, though the man page forioctl()does admit that some ioctls use the return value as an output parameter and return a non-negative value on success. But even then, a single value can't accurately reflect the status of an operation that may succeed or fail on a per-cpu basis. User space would presumably have a similar problem to what is being fixed by this patch.
Quoted 7 lines
Is there any reason that the real problem couldn't be fixed instead of doing this fix on top of something that is fundamentally broken?
Michael
Hi Michael,
Thanks for the feedback and for pointing me to your April review. After going through it, I understand that the NULL check only addresses the immediate fault, while the global capability still misrepresents per-CPU availability.
I'll work on addressing the underlying issue and send a proposed fix soon.
Thanks,
Hardik
Quoted 24 lines
Quoted 24 lines
Check the selected per-CPU register page before taking its reference and return
VM_FAULT_SIGBUSif it is absent.Fixes 7bfe3b8ea6e3 ("Drivers: hv: Introduce mshv_vtl driver")Signed-off-by Hardik Garg <[email protected]>--- drivers/hv/mshv_vtl_main.c | 3 +++ 1 file changed, 3 insertions(+)drivers/hv/mshv_vtl_main.c +3
-0@@ -912,6 +912,9 @@ static vm_fault_t mshv_vtl_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ; } +if (!page) +return VM_FAULT_SIGBUS ; +get_page (page); vmf->page = page;
Wei Liu
maintainerQuoted 40 lines · Hardik Garg
Quoted 30 lines · Michael Kelley
From: Hardik Garg <[email protected]>
Quoted 10 lines
Register-page setup is optional for each CPU. If allocation or registration fails,
mshv_vtl_configure_reg_page()warns and leaves that CPU'sreg_pageNULL, but initial context setup continues successfully. Meanwhile, successful setup on another CPU sets the globalmshv_has_reg_pageflag.
mshv_vtl_fault()checks this global flag before selecting the requested CPU's register page. With mixed setup results across online CPUs, the check passes even for a CPU with no register page, reachingget_page(NULL)when userspace faults in that mapping.I'm a bit late in reviewing this because I was travelling all last week.
This patch seems like it is just papering over the real problem, which is that a global variable like
mshv_has_reg_pagecan't represent the status of an operation that may succeed or fail on a per-cpu basis. I pointed this out (as did Sashiko) in review comments back in April [1].The only other place
mshv_has_reg_pageis used is inmshv_ioctl_check_extensions()where its value is returned from anioctl()system call made by user space. That's a questionable practice sinceioctl()usually returns 0 on success, though the man page forioctl()does admit that some ioctls use the return value as an output parameter and return a non-negative value on success. But even then, a single value can't accurately reflect the status of an operation that may succeed or fail on a per-cpu basis. User space would presumably have a similar problem to what is being fixed by this patch.Quoted 7 lines
Is there any reason that the real problem couldn't be fixed instead of doing this fix on top of something that is fundamentally broken?
Michael
Quoted 8 lines
Hi Michael,
Thanks for the feedback and for pointing me to your April review. After going through it, I understand that the NULL check only addresses the immediate fault, while the global capability still misrepresents per-CPU availability.I'll work on addressing the underlying issue and send a proposed fix soon.
I have not seen this new fix yet. I will keep this patch for now.
Wei