x86/its: Always return a writable page for dynamic thunks
PATCH 1/1 x86/its: Always return a writable page for dynamic thunks
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.
--- arch/x86/kernel/alternative.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/alternative.c +2 -1
@@ -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 */
Signature · 1 line
2.43.0
Quoted 18 lines · Nikolay Borisov
its_allocunconditionally allocates and return a ROX page, but setting it RW is predicated onits_modbeing set. This can cause a #GP on the memset inits_allocate_thunk()ifits_modis 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 <[email protected]>--- arch/x86/kernel/alternative.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kernel/alternative.c +0
-0
arch/x86/kernel/alternative.c static void *its_alloc(void) · line 202
Quoted 4 lines
arch/x86/kernel/alternative.c +1
-0@@ -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?
Quoted 12 lines
arch/x86/kernel/alternative.c +1
-1+#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 */
Quoted 27 lines · Nikolay Borisov
its_allocunconditionally allocates and return a ROX page, but setting it RW is predicated onits_modbeing set. This can cause a #GP on the memset inits_allocate_thunk()ifits_modis 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 <[email protected]>--- arch/x86/kernel/alternative.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kernel/alternative.c +2
-0@@ -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,
arch/x86/kernel/alternative.c static void *its_alloc(void) · line 217
Quoted 7 lines
arch/x86/kernel/alternative.c +0
-1@@ -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...