From: Mike Rapoport <rppt@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: "Jürgen Groß" <jgross@suse.com>,
linux-kernel@vger.kernel.org, x86@kernel.org, xin@zytor.com,
"Thomas Gleixner" <tglx@linutronix.de>,
"Ingo Molnar" <mingo@redhat.com>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>,
stable@vger.kernel.org
Subject: Re: [PATCH 3/3] x86/alternative: make kernel ITS thunks read-only
Date: Wed, 28 May 2025 20:24:35 +0300 [thread overview]
Message-ID: <aDdG09zhIddI6Wty@kernel.org> (raw)
In-Reply-To: <20250528155821.GD39944@noisy.programming.kicks-ass.net>
On Wed, May 28, 2025 at 05:58:21PM +0200, Peter Zijlstra wrote:
> On Wed, May 28, 2025 at 03:30:33PM +0200, Jürgen Groß wrote:
>
> > Have a look at its_fini_mod().
>
> Oh, that's what you mean. But this still isn't very nice, you now have
> restore_rox() without make_temp_rw(), which was the intended usage
> pattern.
>
> Bah, I hate how execmem works different for !PSE, Mike, you see a sane
> way to fix this?
Not really :(
But just resetting permissions in the end like you did makes perfect sense
to me. It's like STRICT_MODULE_RWX, somebody has to set the pages to ROX at
some point and running execmem_restore_rox() on something that was already
ROX won't cost much, set_memory will bail out early.
> Anyway, if we have to do something like this, then I would prefer it
> shaped something like so:
>
> ---
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index ecfe7b497cad..33d4d139cb50 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -111,9 +111,8 @@ static bool cfi_paranoid __ro_after_init;
>
> #ifdef CONFIG_MITIGATION_ITS
>
> -#ifdef CONFIG_MODULES
> static struct module *its_mod;
> -#endif
> +static struct its_array its_pages;
> static void *its_page;
> static unsigned int its_offset;
>
> @@ -151,68 +150,78 @@ static void *its_init_thunk(void *thunk, int reg)
> return thunk + offset;
> }
>
> -#ifdef CONFIG_MODULES
> void its_init_mod(struct module *mod)
> {
> if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
> return;
>
> - mutex_lock(&text_mutex);
> - its_mod = mod;
> - its_page = NULL;
> + if (mod) {
> + mutex_lock(&text_mutex);
> + its_mod = mod;
> + its_page = NULL;
> + }
> }
>
> void its_fini_mod(struct module *mod)
> {
> + struct its_array *pages = &its_pages;
> +
> if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
> return;
>
> WARN_ON_ONCE(its_mod != mod);
>
> - its_mod = NULL;
> - its_page = NULL;
> - mutex_unlock(&text_mutex);
> + if (mod) {
> + pages = &mod->arch.its_pages;
> + its_mod = NULL;
> + its_page = NULL;
> + mutex_unlock(&text_mutex);
> + }
>
> - for (int i = 0; i < mod->its_num_pages; i++) {
> - void *page = mod->its_page_array[i];
> + for (int i = 0; i < pages->num; i++) {
> + void *page = pages->pages[i];
> execmem_restore_rox(page, PAGE_SIZE);
> }
> +
> + if (!mod)
> + kfree(pages->pages);
> }
>
> void its_free_mod(struct module *mod)
> {
> + struct its_array *pages = &its_pages;
> +
> if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
> return;
>
> - for (int i = 0; i < mod->its_num_pages; i++) {
> - void *page = mod->its_page_array[i];
> + if (mod)
> + pages = &mod->arch.its_pages;
> +
> + for (int i = 0; i < pages->num; i++) {
> + void *page = pages->pages[i];
> execmem_free(page);
> }
> - kfree(mod->its_page_array);
> + kfree(pages->pages);
> }
> -#endif /* CONFIG_MODULES */
>
> static void *its_alloc(void)
> {
> - void *page __free(execmem) = execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SIZE);
> + struct its_array *pages = &its_pages;
> + void *tmp;
>
> + void *page __free(execmem) = execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SIZE);
> if (!page)
> return NULL;
>
> -#ifdef CONFIG_MODULES
> - if (its_mod) {
> - void *tmp = krealloc(its_mod->its_page_array,
> - (its_mod->its_num_pages+1) * sizeof(void *),
> - GFP_KERNEL);
> - if (!tmp)
> - return NULL;
> + tmp = krealloc(pages->pages, (pages->num + 1) * sizeof(void *), GFP_KERNEL);
> + if (!tmp)
> + return NULL;
>
> - its_mod->its_page_array = tmp;
> - its_mod->its_page_array[its_mod->its_num_pages++] = page;
> + pages->pages = tmp;
> + pages->pages[pages->num++] = page;
>
> + if (its_mod)
> execmem_make_temp_rw(page, PAGE_SIZE);
> - }
> -#endif /* CONFIG_MODULES */
>
> return no_free_ptr(page);
> }
> @@ -2338,6 +2347,8 @@ void __init alternative_instructions(void)
> apply_retpolines(__retpoline_sites, __retpoline_sites_end);
> apply_returns(__return_sites, __return_sites_end);
>
> + its_fini_mod(NULL);
> +
> /*
> * Adjust all CALL instructions to point to func()-10, including
> * those in .altinstr_replacement.
--
Sincerely yours,
Mike.
next prev parent reply other threads:[~2025-05-28 17:24 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-28 12:35 [PATCH 0/3] x86: Fix some bugs related to ITS mitigation Juergen Gross
2025-05-28 12:35 ` [PATCH 1/3] x86/execmem: don't use PAGE_KERNEL protection for code pages Juergen Gross
2025-05-28 17:27 ` Mike Rapoport
2025-05-28 18:22 ` Jürgen Groß
2025-05-30 7:44 ` Peter Zijlstra
2025-05-28 12:35 ` [PATCH 2/3] x86/mm/pat: don't collapse pages without PSE set Juergen Gross
2025-06-11 9:30 ` [tip: x86/urgent] " tip-bot2 for Juergen Gross
2025-05-28 12:35 ` [PATCH 3/3] x86/alternative: make kernel ITS thunks read-only Juergen Gross
2025-05-28 13:10 ` Peter Zijlstra
2025-05-28 13:19 ` Jürgen Groß
2025-05-28 13:22 ` Peter Zijlstra
2025-05-28 13:30 ` Jürgen Groß
2025-05-28 15:58 ` Peter Zijlstra
2025-05-28 16:17 ` Peter Zijlstra
2025-05-28 17:24 ` Mike Rapoport [this message]
2025-05-28 17:31 ` Mike Rapoport
2025-06-03 11:17 ` Mike Rapoport
2025-05-29 4:09 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aDdG09zhIddI6Wty@kernel.org \
--to=rppt@kernel.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=jgross@suse.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=stable@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
--cc=xin@zytor.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).