From nobody Sat Oct 11 04:17:27 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C82254C79; Wed, 11 Jun 2025 09:30:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749634233; cv=none; b=Pusa0ANN/qA5Cbs6ywF54njs3YsLyBHXPyXxeRMOd9xMGhfrSik3+UGeHdAjfdAVQ96N2uxJtzpvx9yeoF2PSCSJga93ADU22nt6/WfhnReU2a3BlvNdFGmaQ6w9cW4MmBQ4LA4OhoaRml9IywIzDwBR0vNc0MN8fbnqCe07IN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749634233; c=relaxed/simple; bh=Sgb/DgIaSs3qVbyMpEegddQSYa9SUTmUPj0bH1Up/8M=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=RxaFgc0lGZEA9K52EAfSBYpUAnsdgQ2hyoM1LxMuVSsBZOS3QWl0dvg1hFamzCL4+slzQHbL7MBZKj0AV7j2BGKtXl+UVGty8oisDufnA+5TWq0ayjqKPkizWYLg5Zop7NsUjTpGQ+M4WxlWLP3eOEC+csMCnhRHQsPXIB67Sxk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=N5tUY51I; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=DYo6GNxn; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="N5tUY51I"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="DYo6GNxn" Date: Wed, 11 Jun 2025 09:30:29 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1749634230; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wpX3t/47z7P0zY5EBLfYH7y73VToEnsrPps/AcXptXs=; b=N5tUY51ICNg65plG3Zh6M4vpUOJ+IdDyR458NvIg9p3CGZ93uGxRAS7AaxrB1q1828RXvJ 3dZ3xUFJG84E8i2GOy0nShkIlnH6cAnyZaBEKqmJvWhTBjl5vk2haBIx5qcy3TNgNr17Fy QHqwe3rxIVtT/iPWgyHXikJr6RpwB3JYP835lQqh3aaxcfw6qYz2Tc+vr3QSig1dvQNNwN gmONp+KBTYzIwCeHiDHfEGnf1sZvSZH9mvaMsLQKsIxMH+nPiw7pKjvvixk1vPe4viicR9 e6vWo9Aok8F+tBveYTerBzvlCxyAfVnewrEqPDsm1srUuHMGadt2V4eos713fQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1749634230; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wpX3t/47z7P0zY5EBLfYH7y73VToEnsrPps/AcXptXs=; b=DYo6GNxn3+oO2vU14uz6Oq6SXlbHAsSx3S8lw3AwuILeVMWvkgiOZ1NRUyXAx5cKAepxHk Bk9qhsL2Mb0D2FBQ== From: "tip-bot2 for Peter Zijlstra (Intel)" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] x86/its: explicitly manage permissions for ITS pages Cc: "Peter Zijlstra (Intel)" , "Mike Rapoport (Microsoft)" , Nikolay Borisov , stable@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20250603111446.2609381-5-rppt@kernel.org> References: <20250603111446.2609381-5-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <174963422924.406.14483786184878538014.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the x86/urgent branch of tip: Commit-ID: a82b26451de126a5ae130361081986bc459afe9b Gitweb: https://git.kernel.org/tip/a82b26451de126a5ae130361081986bc4= 59afe9b Author: Peter Zijlstra (Intel) AuthorDate: Tue, 03 Jun 2025 14:14:44 +03:00 Committer: Peter Zijlstra CommitterDate: Wed, 11 Jun 2025 11:20:52 +02:00 x86/its: explicitly manage permissions for ITS pages execmem_alloc() sets permissions differently depending on the kernel configuration, CPU support for PSE and whether a page is allocated before or after mark_rodata_ro(). Add tracking for pages allocated for ITS when patching the core kernel and make sure the permissions for ITS pages are explicitly managed for both kernel and module allocations. Fixes: 872df34d7c51 ("x86/its: Use dynamic thunks for indirect branches") Signed-off-by: Peter Zijlstra (Intel) Co-developed-by: Mike Rapoport (Microsoft) Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Nikolay Borisov Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20250603111446.2609381-5-rppt@kernel.org --- arch/x86/kernel/alternative.c | 74 +++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index b50fe6c..6455f7f 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -116,6 +116,24 @@ static struct module *its_mod; #endif static void *its_page; static unsigned int its_offset; +struct its_array its_pages; + +static void *__its_alloc(struct its_array *pages) +{ + void *page __free(execmem) =3D execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SI= ZE); + if (!page) + return NULL; + + void *tmp =3D krealloc(pages->pages, (pages->num+1) * sizeof(void *), + GFP_KERNEL); + if (!tmp) + return NULL; + + pages->pages =3D tmp; + pages->pages[pages->num++] =3D page; + + return no_free_ptr(page); +} =20 /* Initialize a thunk with the "jmp *reg; int3" instructions. */ static void *its_init_thunk(void *thunk, int reg) @@ -151,6 +169,21 @@ static void *its_init_thunk(void *thunk, int reg) return thunk + offset; } =20 +static void its_pages_protect(struct its_array *pages) +{ + for (int i =3D 0; i < pages->num; i++) { + void *page =3D pages->pages[i]; + execmem_restore_rox(page, PAGE_SIZE); + } +} + +static void its_fini_core(void) +{ + if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) + its_pages_protect(&its_pages); + kfree(its_pages.pages); +} + #ifdef CONFIG_MODULES void its_init_mod(struct module *mod) { @@ -173,10 +206,8 @@ void its_fini_mod(struct module *mod) its_page =3D NULL; mutex_unlock(&text_mutex); =20 - for (int i =3D 0; i < mod->arch.its_pages.num; i++) { - void *page =3D mod->arch.its_pages.pages[i]; - execmem_restore_rox(page, PAGE_SIZE); - } + if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) + its_pages_protect(&mod->arch.its_pages); } =20 void its_free_mod(struct module *mod) @@ -194,28 +225,23 @@ void its_free_mod(struct module *mod) =20 static void *its_alloc(void) { - void *page __free(execmem) =3D execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SI= ZE); + struct its_array *pages =3D &its_pages; + void *page; =20 +#ifdef CONFIG_MODULE + if (its_mod) + pages =3D &its_mod->arch.its_pages; +#endif + + page =3D __its_alloc(pages); if (!page) return NULL; =20 -#ifdef CONFIG_MODULES - if (its_mod) { - struct its_array *pages =3D &its_mod->arch.its_pages; - void *tmp =3D krealloc(pages->pages, - (pages->num+1) * sizeof(void *), - GFP_KERNEL); - if (!tmp) - return NULL; - - pages->pages =3D tmp; - pages->pages[pages->num++] =3D page; + execmem_make_temp_rw(page, PAGE_SIZE); + if (pages =3D=3D &its_pages) + set_memory_x((unsigned long)page, 1); =20 - execmem_make_temp_rw(page, PAGE_SIZE); - } -#endif /* CONFIG_MODULES */ - - return no_free_ptr(page); + return page; } =20 static void *its_allocate_thunk(int reg) @@ -269,7 +295,9 @@ u8 *its_static_thunk(int reg) return thunk; } =20 -#endif +#else +static inline void its_fini_core(void) {} +#endif /* CONFIG_MITIGATION_ITS */ =20 /* * Nomenclature for variable names to simplify and clarify this code and e= ase @@ -2339,6 +2367,8 @@ void __init alternative_instructions(void) apply_retpolines(__retpoline_sites, __retpoline_sites_end); apply_returns(__return_sites, __return_sites_end); =20 + its_fini_core(); + /* * Adjust all CALL instructions to point to func()-10, including * those in .altinstr_replacement.