linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86/its: Always return a writable page for dynamic thunks
@ 2025-06-04 10:47 Nikolay Borisov
  2025-06-04 10:54 ` Nikolay Borisov
  2025-06-04 12:18 ` Nikolay Borisov
  0 siblings, 2 replies; 3+ messages in thread
From: Nikolay Borisov @ 2025-06-04 10:47 UTC (permalink / raw)
  To: x86; +Cc: pawan.kumar.gupta, peterz, linux-kernel, dave.hansen,
	Nikolay Borisov

its_alloc unconditionally allocates and return a ROX page, but setting
it RW is predicated on its_mod being set. This can cause a #GP on the
memset in its_allocate_thunk() if its_mod is not set.

Since the function always returns a page, ensure it's always set
writable so it can be initialized properly.

Fixes: 872df34d7c51 ("x86/its: Use dynamic thunks for indirect branches")
Signed-off-by: Nikolay Borisov <nik.borisov@suse.com>
---
 arch/x86/kernel/alternative.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index ecfe7b497cad..191e909636a5 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -199,6 +199,8 @@ static void *its_alloc(void)
 	if (!page)
 		return NULL;
 
+	execmem_make_temp_rw(page, PAGE_SIZE);
+
 #ifdef CONFIG_MODULES
 	if (its_mod) {
 		void *tmp = krealloc(its_mod->its_page_array,
@@ -210,7 +212,6 @@ static void *its_alloc(void)
 		its_mod->its_page_array = tmp;
 		its_mod->its_page_array[its_mod->its_num_pages++] = page;
 
-		execmem_make_temp_rw(page, PAGE_SIZE);
 	}
 #endif /* CONFIG_MODULES */
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] x86/its: Always return a writable page for dynamic thunks
  2025-06-04 10:47 [PATCH] x86/its: Always return a writable page for dynamic thunks Nikolay Borisov
@ 2025-06-04 10:54 ` Nikolay Borisov
  2025-06-04 12:18 ` Nikolay Borisov
  1 sibling, 0 replies; 3+ messages in thread
From: Nikolay Borisov @ 2025-06-04 10:54 UTC (permalink / raw)
  To: x86; +Cc: pawan.kumar.gupta, peterz, linux-kernel, dave.hansen



On 6/4/25 13:47, Nikolay Borisov wrote:
> its_alloc unconditionally allocates and return a ROX page, but setting
> it RW is predicated on its_mod being set. This can cause a #GP on the
> memset in its_allocate_thunk() if its_mod is not set.
> 
> Since the function always returns a page, ensure it's always set
> writable so it can be initialized properly.
> 
> Fixes: 872df34d7c51 ("x86/its: Use dynamic thunks for indirect branches")
> Signed-off-by: Nikolay Borisov <nik.borisov@suse.com>
> ---
>   arch/x86/kernel/alternative.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index ecfe7b497cad..191e909636a5 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -199,6 +199,8 @@ static void *its_alloc(void)
>   	if (!page)
>   		return NULL;
>   
> +	execmem_make_temp_rw(page, PAGE_SIZE);

Actually this is not sufficient, in case we don't have its_mod set ergo 
its_fini_mod won't be executed I guess execmem_restore_rox should also 
be executed in its_allocate_thunk iff its_mod is not set?

> +
>   #ifdef CONFIG_MODULES
>   	if (its_mod) {
>   		void *tmp = krealloc(its_mod->its_page_array,
> @@ -210,7 +212,6 @@ static void *its_alloc(void)
>   		its_mod->its_page_array = tmp;
>   		its_mod->its_page_array[its_mod->its_num_pages++] = page;
>   
> -		execmem_make_temp_rw(page, PAGE_SIZE);
>   	}
>   #endif /* CONFIG_MODULES */
>   


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] x86/its: Always return a writable page for dynamic thunks
  2025-06-04 10:47 [PATCH] x86/its: Always return a writable page for dynamic thunks Nikolay Borisov
  2025-06-04 10:54 ` Nikolay Borisov
@ 2025-06-04 12:18 ` Nikolay Borisov
  1 sibling, 0 replies; 3+ messages in thread
From: Nikolay Borisov @ 2025-06-04 12:18 UTC (permalink / raw)
  To: Nikolay Borisov, x86; +Cc: pawan.kumar.gupta, peterz, linux-kernel, dave.hansen



On 6/4/25 13:47, Nikolay Borisov wrote:
> its_alloc unconditionally allocates and return a ROX page, but setting
> it RW is predicated on its_mod being set. This can cause a #GP on the
> memset in its_allocate_thunk() if its_mod is not set.
> 
> Since the function always returns a page, ensure it's always set
> writable so it can be initialized properly.
> 
> Fixes: 872df34d7c51 ("x86/its: Use dynamic thunks for indirect branches")
> Signed-off-by: Nikolay Borisov <nik.borisov@suse.com>
> ---
>   arch/x86/kernel/alternative.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index ecfe7b497cad..191e909636a5 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -199,6 +199,8 @@ static void *its_alloc(void)
>   	if (!page)
>   		return NULL;
>   
> +	execmem_make_temp_rw(page, PAGE_SIZE);
> +
>   #ifdef CONFIG_MODULES
>   	if (its_mod) {
>   		void *tmp = krealloc(its_mod->its_page_array,
> @@ -210,7 +212,6 @@ static void *its_alloc(void)
>   		its_mod->its_page_array = tmp;
>   		its_mod->its_page_array[its_mod->its_num_pages++] = page;
>   
> -		execmem_make_temp_rw(page, PAGE_SIZE);
>   	}
>   #endif /* CONFIG_MODULES */
>   

Ok, this actually works in the upstream kernel thanks to 
d6d1e3e6580ca35071ad474381f053cbf1fb6414 meaning at the time retpolines 
are processed the execmem cache is actually writable...

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-04 12:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-04 10:47 [PATCH] x86/its: Always return a writable page for dynamic thunks Nikolay Borisov
2025-06-04 10:54 ` Nikolay Borisov
2025-06-04 12:18 ` Nikolay Borisov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).