Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp3576304imm; Fri, 24 Aug 2018 21:25:41 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaQSrV3zCvvZPLFM0zjTj/JrRA3dNcY+8mKtJCc2rrg6/xBjXKCHXDWc3lMe59sCZ/3ATHw X-Received: by 2002:a63:3e8b:: with SMTP id l133-v6mr4147625pga.355.1535171141661; Fri, 24 Aug 2018 21:25:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535171141; cv=none; d=google.com; s=arc-20160816; b=leJKUtRlxUWWRpsce6H1z6ZaQUt+3fbXFpXHqD/cjMkx+h85tGaWygvrrwjSpHypXP 210Ez9DVR+UmceJsreR17KEHY/ZmS6MCnYf7lxScZFsljkZkvRMlHfQXID627oz69tt2 HCoUmRJI8tPqizyKGu+Iw+sc9eC1AUiwYeUR1U3ttw52j4ro8O6Al/28FJnYx+f06ugx 0hnGrV4sPnVeGD7CwtfWaWRH9nRhRpnX9rx64EofzL4NPD5CnFxS1r4iU1PLiC8DbLLp IfZGFq4EflnyhJVDM8OjOdKY+StEyJB1DlY4OL4vrM3FnL6qP+/27gJuaOBcM/3FylOz Dt7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=NefMuo5qmeGEaKFZazgwMAHqaWXdmf8JXLDhlU0Z+Vg=; b=u9/VmPya+adwrw/urOctrg0I3YjWO7+N80Y3MreOA+PtrfHWCjAS6g6th01WAInNhM CDnMOojmBHVWSdKd05XLuBm47x/H0rKd3KnG1zYEJiem7ya6a7uCQOovFVHVNAWqalIj yyOJYZREIeHn6WQsiNg1klDyLIRgilmpdXT8gP7KDLydYp61ldb/k1juDpayYMidvg4O sIObBETWIxdRok+y6/p89A7cjPowa76XpWiwUiGy6h31kjDKYnsIyArg2kv0S1FQ7gpz WfSZhZSP9GknwRY5SRMqim29A0oRjOSSjtp5kjKMQu08akc0Cjl68o++4d7gm5EPxYKE w3Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vziqK7zk; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d13-v6si8168933plr.196.2018.08.24.21.25.25; Fri, 24 Aug 2018 21:25:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vziqK7zk; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726672AbeHYIBW (ORCPT + 99 others); Sat, 25 Aug 2018 04:01:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:55906 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726110AbeHYIBW (ORCPT ); Sat, 25 Aug 2018 04:01:22 -0400 Received: from mail-wm0-f51.google.com (mail-wm0-f51.google.com [74.125.82.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AEDEA214AB for ; Sat, 25 Aug 2018 04:23:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1535171029; bh=6zT9xyiKyS2ziClSfORdmfeZ4q27DCZxuAAy7ioWLOg=; h=In-Reply-To:References:From:Date:Subject:To:Cc:From; b=vziqK7zk2OgdfQy6bmm4Birm9sMTXfAjv85bYzlTNP4E41t+s4ioUf+oQyD9x2mi5 sTwoOuWt0RKRvtFGSiMcy3wQpPNo68+yv3U+HTGt3fG9cddWIPRQoMuje1Wv0q5miP 4M9w+Ur1PgTkIIwSgRWipjOGrWkbP1gi+SWeinSE= Received: by mail-wm0-f51.google.com with SMTP id y2-v6so3235401wma.1 for ; Fri, 24 Aug 2018 21:23:48 -0700 (PDT) X-Gm-Message-State: APzg51CbD7sLDEb9xufXu8DqCsYU5bRKct5V9ZT/6/Gd5aF2rUc51h1x p+y+mqPBZjb9vg6PFuPRZXMecBM3fzPX0N7WY1+TtA== X-Received: by 2002:a1c:ef0f:: with SMTP id n15-v6mr279711wmh.116.1535171027145; Fri, 24 Aug 2018 21:23:47 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a1c:548:0:0:0:0:0 with HTTP; Fri, 24 Aug 2018 21:23:26 -0700 (PDT) In-Reply-To: <8E0D8C66-6F21-4890-8984-B6B3082D4CC5@gmail.com> References: <20180822153012.173508681@infradead.org> <20180822154046.823850812@infradead.org> <20180822155527.GF24124@hirez.programming.kicks-ass.net> <20180823134525.5f12b0d3@roar.ozlabs.ibm.com> <776104d4c8e4fc680004d69e3a4c2594b638b6d1.camel@au1.ibm.com> <20180823133958.GA1496@brain-police> <20180824084717.GK24124@hirez.programming.kicks-ass.net> <20180824180438.GS24124@hirez.programming.kicks-ass.net> <56A9902F-44BE-4520-A17C-26650FCC3A11@gmail.com> <9A38D3F4-2F75-401D-8B4D-83A844C9061B@gmail.com> <8E0D8C66-6F21-4890-8984-B6B3082D4CC5@gmail.com> From: Andy Lutomirski Date: Fri, 24 Aug 2018 21:23:26 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: TLB flushes on fixmap changes To: Nadav Amit Cc: Linus Torvalds , Paolo Bonzini , Jiri Kosina , Masami Hiramatsu , Peter Zijlstra , Will Deacon , Benjamin Herrenschmidt , Nick Piggin , Andrew Lutomirski , "the arch/x86 maintainers" , Borislav Petkov , Rik van Riel , Jann Horn , Adin Scannell , Dave Hansen , Linux Kernel Mailing List , linux-mm , David Miller , Martin Schwidefsky , Michael Ellerman Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 24, 2018 at 7:29 PM, wrote: > > > On August 24, 2018 5:58:43 PM PDT, Linus Torvalds wrote: >>Adding a few people to the cc. >> >>On Fri, Aug 24, 2018 at 1:24 PM Nadav Amit >>wrote: >>> > >>> > Can you actually find something that changes the fixmaps after boot >>> > (again, ignoring kmap)? >>> >>> At least the alternatives mechanism appears to do so. >>> >>> IIUC the following path is possible when adding a module: >>> >>> jump_label_add_module() >>> ->__jump_label_update() >>> ->arch_jump_label_transform() >>> ->__jump_label_transform() >>> ->text_poke_bp() >>> ->text_poke() >>> ->set_fixmap() >> >>Yeah, that looks a bit iffy. >> >>But making the tlb flush global wouldn't help. This is running on a >>local core, and if there are other CPU's that can do this at the same >>time, then they'd just fight about the same mapping. >> >>Honestly, I think it's ok just because I *hope* this is all serialized >>anyway (jump_label_lock? But what about other users of text_poke?). > > The users should hold text_mutex. > >> >>But I'd be a lot happier about it if it either used an explicit lock >>to make sure, or used per-cpu fixmap entries. > > My concern is that despite the lock, one core would do a speculative page walk and cache a translation that soon after would become stale. > >> >>And the tlb flush is done *after* the address is used, which is bogus >>anyway. > > It seems to me that it is intended to remove the mapping that might be a security issue. > > But anyhow, set_fixmap and clear_fixmap perform a local TLB flush, (in __set_pte_vaddr()) so locally things should be fine. > >> >>> And a similar path can happen when static_key_enable/disable() is >>called. >> >>Same comments. >> >>How about replacing that >> >> local_irq_save(flags); >> ... do critical things here ... >> local_irq_restore(flags); >> >>in text_poke() with >> >> static DEFINE_SPINLOCK(poke_lock); >> >> spin_lock_irqsave(&poke_lock, flags); >> ... do critical things here ... >> spin_unlock_irqrestore(&poke_lock, flags); >> >>and moving the local_flush_tlb() to after the set_fixmaps, but before >>the access through the virtual address. >> >>But changing things to do a global tlb flush would just be wrong. > > As I noted, I think that locking and local flushes as they are right now are fine (besides the redundant flush). > > My concern is merely that speculative page walks on other cores would cache stale entries. > > This is almost certainly a bug, or even two bugs. Bug 1: why on Earth do we flush in __set_pte_vaddr()? We should flush when *clearing* or when modifying an existing fixmap entry. Right now, if we do text_poke() after boot, then the TLB entry will stick around and will be a nice exploit target. Bug 2: what you're describing. It's racy. Couldn't text_poke() use kmap_atomic()? Or, even better, just change CR3?