Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4737206iob; Sun, 8 May 2022 23:46:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8+yAfG9hSbZWFq84vAssvOtFIkgRqMrhR6pnMLzm0tGn0pzPus48hiDNU+ZDgv+WOgQez X-Received: by 2002:a17:90b:1091:b0:1d8:b371:4b29 with SMTP id gj17-20020a17090b109100b001d8b3714b29mr24582272pjb.234.1652078785005; Sun, 08 May 2022 23:46:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652078785; cv=none; d=google.com; s=arc-20160816; b=pscSjKbKJXMTovo425qS/eU2SQ8KV35tcCLrR8d0HqlBqDt8iy9gbGp3jxmOG/GjLP P6Ugupqo6F0CQUjmlZJt4Z4be1oAuqdFeUGrz5bvR9kFUNhgA7TWuKpez280+LpAvcYt UnQ6HAX6YmxtwNR610/y5Sfe1v824iWRpO8RzPmAsKIK11v5LD5/KDqdRMsO6QHu5ocB 44YKrg5cNZVkPIi6Mpc99O98g4PL67ZAc8gH3jySTawDISKptSDejOsn1yY6k+eA96FJ vds6AdMGemGnmNSQa9XTcGmErvx+XGxffRSHIhhkIycLsg4aggcg9q9oD45D28M1GuAG gEDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=Xigc7SGokGXaGXENHUlGV+lz8hEFE/+jKrwKMtiGA38=; b=y0H/m5S0RnGno+tZVo/PC80rLfNTARqSviAcM+LikW5t0U6Lu8vcnS35AUlpZTDt7Z HRIQrs6DseWtuz8j8LjLREYxB2cn1SskHlFZo5sJkZYWO4nwb70CdcVxlvhVGKBZNHQG URsDxzG0cF0BFmCLxPJZ8161tmKwMs1+iP3oD0tT/MLHU7oTe61Xz2DUclqsKpfmEnck E9RziWmIvcsotcZR0pJhHIizCH0OTs/bBDjxjVe97plm47gmc8cKtSsvZMa6ItOymv1l HWGeB5e60fXPn53+3sTcbosav0r9HXDjQgFZcjvUndxIZiyc5/K90b4bs+QzaIAT1jS2 HMxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=PSrYskSY; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Rn3RCXeC; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id l14-20020a170903244e00b0015d284e10a2si12170715pls.150.2022.05.08.23.46.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 23:46:24 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=PSrYskSY; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Rn3RCXeC; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E8DA9F6880; Sun, 8 May 2022 23:41:14 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1386105AbiEEV77 (ORCPT + 99 others); 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 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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