From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5047C433EF for ; Thu, 5 May 2022 21:56:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1386114AbiEEV77 (ORCPT ); Thu, 5 May 2022 17:59:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233742AbiEEV75 (ORCPT ); Thu, 5 May 2022 17:59:57 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1753A5C743; Thu, 5 May 2022 14:56:16 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1651787773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Xigc7SGokGXaGXENHUlGV+lz8hEFE/+jKrwKMtiGA38=; b=PSrYskSYyUHsNEO0GqK1VFP4p09LSXsNqDvhWx4yX+BCy0bhLEjJWkHBAyfK35Ml82+SLB QhIbjlbXkoGWzoOqsNE/QD0hU23fHRzFfzIBvZdDsZOJ/zNH09WX0Ns/YBZPTaT5Pifvgf AoND8IAnLQbJxr2U6F1tVnHsPUpW4xYj4bdwfo7zjTTsGjOENwAnmvrRUpr2wh6vXO8zu2 LDCXHhbd8KbvfMwx5O88EDcaxIUo+RFafdnJWtbGU6M0AkNTNvrWVw7zXUsvY/4/ZTBqct EXrRY+ty7Xb/YcaKhuki/G+Hcn0CirsxaVLpth4e8Vtd7Q5c8uEdBdPb2ZtV4w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1651787773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Xigc7SGokGXaGXENHUlGV+lz8hEFE/+jKrwKMtiGA38=; b=Rn3RCXeCWzxAluumapgAbVqbQIAk6BhZPfuwnrm8z/Sc3mHPD7EetWFlsTT3/u/QXAtx4s QDbwokMO61HhvgBA== To: Alexander Potapenko Cc: Alexander Viro , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev , Linux Memory Management List , Linux-Arch , LKML Subject: Re: [PATCH v3 28/46] kmsan: entry: handle register passing from uninstrumented code In-Reply-To: References: <20220426164315.625149-1-glider@google.com> <20220426164315.625149-29-glider@google.com> <87a6c6y7mg.ffs@tglx> <87y1zjlhmj.ffs@tglx> Date: Thu, 05 May 2022 23:56:12 +0200 Message-ID: <878rrfiqyr.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Alexander, On Thu, May 05 2022 at 20:04, Alexander Potapenko wrote: > On Tue, May 3, 2022 at 12:00 AM Thomas Gleixner wrote: >> > Regarding the regs, you are right. It should be enough to unpoison the >> > regs at idtentry prologue instead. >> > I tried that initially, but IIRC it required patching each of the >> > DEFINE_IDTENTRY_XXX macros, which already use instrumentation_begin(). >> >> Exactly 4 instances :) >> > > Not really, I had to add a call to `kmsan_unpoison_memory(regs, > sizeof(*regs));` to the following places in > arch/x86/include/asm/idtentry.h: > - DEFINE_IDTENTRY() > - DEFINE_IDTENTRY_ERRORCODE() > - DEFINE_IDTENTRY_RAW() > - DEFINE_IDTENTRY_RAW_ERRORCODE() > - DEFINE_IDTENTRY_IRQ() > - DEFINE_IDTENTRY_SYSVEC() > - DEFINE_IDTENTRY_SYSVEC_SIMPLE() > - DEFINE_IDTENTRY_DF() > > , but even that wasn't enough. For some reason I also had to unpoison > pt_regs directly in > DEFINE_IDTENTRY_SYSVEC(sysvec_apic_timer_interrupt) and > DEFINE_IDTENTRY_IRQ(common_interrupt). > In the latter case, this could have been caused by > asm_common_interrupt being entered from irq_entries_start(), but I am > not sure what is so special about sysvec_apic_timer_interrupt(). > > Ideally, it would be great to find that single point where pt_regs are > set up before being passed to all IDT entries. > I used to do that by inserting calls to kmsan_unpoison_memory right > into arch/x86/entry/entry_64.S > (https://github.com/google/kmsan/commit/3b0583f45f74f3a09f4c7e0e0588169cef918026), > but that required storing/restoring all GP registers. Maybe there's a > better way? Yes. Something like this should cover all exceptions and syscalls before anything instrumentable can touch @regs. Anything up to those points is either off-limit for instrumentation or does not deal with @regs. --- a/kernel/entry/common.c +++ b/kernel/entry/common.c @@ -24,6 +24,7 @@ static __always_inline void __enter_from user_exit_irqoff(); instrumentation_begin(); + unpoison(regs); trace_hardirqs_off_finish(); instrumentation_end(); } @@ -352,6 +353,7 @@ noinstr irqentry_state_t irqentry_enter( lockdep_hardirqs_off(CALLER_ADDR0); rcu_irq_enter(); instrumentation_begin(); + unpoison(regs); trace_hardirqs_off_finish(); instrumentation_end(); @@ -367,6 +369,7 @@ noinstr irqentry_state_t irqentry_enter( */ lockdep_hardirqs_off(CALLER_ADDR0); instrumentation_begin(); + unpoison(regs); rcu_irq_enter_check_tick(); trace_hardirqs_off_finish(); instrumentation_end(); @@ -452,6 +455,7 @@ irqentry_state_t noinstr irqentry_nmi_en rcu_nmi_enter(); instrumentation_begin(); + unpoison(regs); trace_hardirqs_off_finish(); ftrace_nmi_enter(); instrumentation_end(); As I said: 4 places :) > Fortunately, I don't think we need to insert extra instrumentation > into instrumentation_begin()/instrumentation_end() regions. > > What I have in mind is adding a bool flag to kmsan_context_state, that > the instrumentation sets to true before the function call. > When entering an instrumented function, KMSAN would check that flag > and set it to false, so that the context state can be only used once. > If a function is called from another instrumented function, the > context state is properly set up, and there is nothing to worry about. > If it is called from non-instrumented code (either noinstr or the > skipped files that have KMSAN_SANITIZE:=n), KMSAN would detect that > and wipe the context state before use. Sounds good. Thanks, tglx