Re: [PATCH v4 06/10] x86/alternative: use temporary mm for text poking

From: Nadav Amit
Date: Sun Nov 11 2018 - 15:41:20 EST


From: Damian Tometzki
Sent: November 11, 2018 at 7:11:42 PM GMT
> To: Nadav Amit <namit@xxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>, linux-kernel@xxxxxxxxxxxxxxx>, x86@xxxxxxxxxx>, H. Peter Anvin <hpa@xxxxxxxxx>, Thomas Gleixner <tglx@xxxxxxxxxxxxx>, Borislav Petkov <bp@xxxxxxxxx>, Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>, Andy Lutomirski <luto@xxxxxxxxxx>, Kees Cook <keescook@xxxxxxxxxxxx>, Peter Zijlstra <peterz@xxxxxxxxxxxxx>, Dave Hansen <dave.hansen@xxxxxxxxx>, Masami Hiramatsu <mhiramat@xxxxxxxxxx>
> Subject: Re: [PATCH v4 06/10] x86/alternative: use temporary mm for text poking
>
>
> On Sa, 10. Nov 15:17, Nadav Amit wrote:
>> text_poke() can potentially compromise the security as it sets temporary
>> PTEs in the fixmap. These PTEs might be used to rewrite the kernel code
>> from other cores accidentally or maliciously, if an attacker gains the
>> ability to write onto kernel memory.
>>
>> Moreover, since remote TLBs are not flushed after the temporary PTEs are
>> removed, the time-window in which the code is writable is not limited if
>> the fixmap PTEs - maliciously or accidentally - are cached in the TLB.
>> To address these potential security hazards, we use a temporary mm for
>> patching the code.
>>
>> Finally, text_poke() is also not conservative enough when mapping pages,
>> as it always tries to map 2 pages, even when a single one is sufficient.
>> So try to be more conservative, and do not map more than needed.
>>
>> Cc: Andy Lutomirski <luto@xxxxxxxxxx>
>> Cc: Kees Cook <keescook@xxxxxxxxxxxx>
>> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
>> Cc: Dave Hansen <dave.hansen@xxxxxxxxx>
>> Cc: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
>> Signed-off-by: Nadav Amit <namit@xxxxxxxxxx>
>> ---
>> arch/x86/include/asm/fixmap.h | 2 -
>> arch/x86/kernel/alternative.c | 112 +++++++++++++++++++++++++++-------
>> 2 files changed, 89 insertions(+), 25 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h
>> index 50ba74a34a37..9da8cccdf3fb 100644
>> --- a/arch/x86/include/asm/fixmap.h
>> +++ b/arch/x86/include/asm/fixmap.h
>> @@ -103,8 +103,6 @@ enum fixed_addresses {
>> #ifdef CONFIG_PARAVIRT
>> FIX_PARAVIRT_BOOTMAP,
>> #endif
>
> Hello Nadav,
>
> with the remove of FIX_TEXT_POKE1 and FIX_TEXT_POKE0 i get the following
> build error:
>
> /home/damian/kernel/linux/arch/x86/xen/mmu_pv.c:2321:7: Fehler: ÂFIX_TEXT_POKE0Â nicht deklariert (erstmalige Verwendung in dieser Funktion); meinten Sie ÂFIX_TBOOT_BASEÂ?
> case FIX_TEXT_POKE0:
> ^~~~~~~~~~~~~~
> FIX_TBOOT_BASE
> /home/damian/kernel/linux/arch/x86/xen/mmu_pv.c:2321:7: Anmerkung: jeder nicht deklarierte Bezeichner wird nur einmal fÃr jede Funktion, in der er vorkommt, gemeldet
> /home/damian/kernel/linux/arch/x86/xen/mmu_pv.c:2322:7: Fehler: ÂFIX_TEXT_POKE1Â nicht deklariert (erstmalige Verwendung in dieser Funktion); meinten Sie ÂFIX_TBOOT_BASEÂ?
> case FIX_TEXT_POKE1:
> ^~~~~~~~~~~~~~
> FIX_TBOOT_BASE

Thanks for letting me know. Iâll simply remove them in v5.

Regards,
Nadav