Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp3032421imj; Mon, 11 Feb 2019 12:38:17 -0800 (PST) X-Google-Smtp-Source: AHgI3IY0cX5hVjBFKR3TEpqbZxO6ILMq86IyIrcULOW3z3u5Pqe7BY4PbvUJmlguwbFW79RVl9ux X-Received: by 2002:a62:138f:: with SMTP id 15mr66362pft.219.1549917497434; Mon, 11 Feb 2019 12:38:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549917497; cv=none; d=google.com; s=arc-20160816; b=FqvysaShnBcHdE51cXNMXkpaD5j3dMhRPKOG97Tp+vowhTRWVjrrv+aLj01VqLNUKP HucIPw+L3Kped3/1Kxj2nqy8ipBU1tVBg5wEh5SJ+hGCc5BeVtuQIXdhl3TsZCmfWNQz 65kdEvjW5HFli1eYWTh5THEffDUFhK+od1TFS2mivn1bn8+4dzQnKYoNCcnxfu8lBcwp atfHf2oqFQCctRN61q1LwOVi2ctMJi662t+/1IWznZ/+QJvaow3lkBHIQRyKZgAegeSH LjN0Iobb8py9tPwcFOnY2jcdOsy/0UYddYPHOI4g2GBwiJMXmjUvGpjC7Dq5zcBtkYjL FvHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Yw+RK9f/S+johjl1gYKTeZptuEY0P9nfhJd0XlVQGCo=; b=MB8M75L0jlhIcKhSEEnig6GPLKigm3/EHA3PFnmaZ7V9udZCPSxr65tO4ZdZJnQjjA /kMzeFuwkZwlxii1bzORQWAX4I3J/LXMUblX1bQVneusgOqkjT6Ub04Euu6GELnnIy3n s192oSpDXu+gnLHJrqDfdonsIklsBVmGA6UW5/o0tsrjFX9sc/q/JBpbdt2cH5YzbOrP 7kS01aw6sztNTaLCSBtENxQMctOTuMf+euAzA+FQm/A9ZAGl1l3raopx1Vm5wvCmiCfC uWZt6uXCKPKk79b6PrsXMi37TB8Y41tn9MnWSMRsMljTzfHfTXbferU+8L0ur0k07t1D RIoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=dj10XbPz; 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=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 64si6069362pld.436.2019.02.11.12.38.01; Mon, 11 Feb 2019 12:38:17 -0800 (PST) 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=@alien8.de header.s=dkim header.b=dj10XbPz; 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=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731486AbfBKSaA (ORCPT + 99 others); Mon, 11 Feb 2019 13:30:00 -0500 Received: from mail.skyhub.de ([5.9.137.197]:44204 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727968AbfBKSaA (ORCPT ); Mon, 11 Feb 2019 13:30:00 -0500 Received: from zn.tnic (p200300EC2BC7A10074DEFDFE3AD6CF32.dip0.t-ipconnect.de [IPv6:2003:ec:2bc7:a100:74de:fdfe:3ad6:cf32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 1C0D71EC037B; Mon, 11 Feb 2019 19:29:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1549909798; 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: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=Yw+RK9f/S+johjl1gYKTeZptuEY0P9nfhJd0XlVQGCo=; b=dj10XbPzFbFnu9Nya4W5OkkNbXxJnnkj4Hr1SOpZ2asDUWMbDzAbKRWRj/tjz/qk4tl7WJ 0em21jvddg2A3nLTY/GpXAXUxfmJCaIUKjSFW1JC6HHKlJLE1/k9VIlZrqJLUkJ0JPA/M4 LmZjPdIjqu1x0Cyl6ehJazATSPz8JTc= Date: Mon, 11 Feb 2019 19:29:56 +0100 From: Borislav Petkov To: Rick Edgecombe Cc: Andy Lutomirski , Ingo Molnar , linux-kernel@vger.kernel.org, x86@kernel.org, hpa@zytor.com, Thomas Gleixner , Nadav Amit , Dave Hansen , Peter Zijlstra , linux_dti@icloud.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, akpm@linux-foundation.org, kernel-hardening@lists.openwall.com, linux-mm@kvack.org, will.deacon@arm.com, ard.biesheuvel@linaro.org, kristen@linux.intel.com, deneen.t.dock@intel.com, Nadav Amit , Kees Cook , Dave Hansen , Masami Hiramatsu Subject: Re: [PATCH v2 10/20] x86: avoid W^X being broken during modules loading Message-ID: <20190211182956.GN19618@zn.tnic> References: <20190129003422.9328-1-rick.p.edgecombe@intel.com> <20190129003422.9328-11-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190129003422.9328-11-rick.p.edgecombe@intel.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > Subject: Re: [PATCH v2 10/20] x86: avoid W^X being broken during modules loading For your next submission, please fix all your subjects: The tip tree preferred format for patch subject prefixes is 'subsys/component:', e.g. 'x86/apic:', 'x86/mm/fault:', 'sched/fair:', 'genirq/core:'. Please do not use file names or complete file paths as prefix. 'git log path/to/file' should give you a reasonable hint in most cases. The condensed patch description in the subject line should start with a uppercase letter and should be written in imperative tone. On Mon, Jan 28, 2019 at 04:34:12PM -0800, Rick Edgecombe wrote: > From: Nadav Amit > > When modules and BPF filters are loaded, there is a time window in > which some memory is both writable and executable. An attacker that has > already found another vulnerability (e.g., a dangling pointer) might be > able to exploit this behavior to overwrite kernel code. > > Prevent having writable executable PTEs in this stage. In addition, > avoiding having W+X mappings can also slightly simplify the patching of > modules code on initialization (e.g., by alternatives and static-key), > as would be done in the next patch. > > To avoid having W+X mappings, set them initially as RW (NX) and after > they are set as RO set them as X as well. Setting them as executable is > done as a separate step to avoid one core in which the old PTE is cached > (hence writable), and another which sees the updated PTE (executable), > which would break the W^X protection. > > Cc: Kees Cook > Cc: Peter Zijlstra > Cc: Dave Hansen > Cc: Masami Hiramatsu > Suggested-by: Thomas Gleixner > Suggested-by: Andy Lutomirski > Signed-off-by: Nadav Amit > Signed-off-by: Rick Edgecombe > --- > arch/x86/kernel/alternative.c | 28 +++++++++++++++++++++------- > arch/x86/kernel/module.c | 2 +- > include/linux/filter.h | 2 +- > kernel/module.c | 5 +++++ > 4 files changed, 28 insertions(+), 9 deletions(-) > > diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c > index 76d482a2b716..69f3e650ada8 100644 > --- a/arch/x86/kernel/alternative.c > +++ b/arch/x86/kernel/alternative.c > @@ -667,15 +667,29 @@ void __init alternative_instructions(void) > * handlers seeing an inconsistent instruction while you patch. > */ > void *__init_or_module text_poke_early(void *addr, const void *opcode, > - size_t len) > + size_t len) > { > unsigned long flags; > - local_irq_save(flags); > - memcpy(addr, opcode, len); > - local_irq_restore(flags); > - sync_core(); > - /* Could also do a CLFLUSH here to speed up CPU recovery; but > - that causes hangs on some VIA CPUs. */ > + > + if (static_cpu_has(X86_FEATURE_NX) && Not a fast path - boot_cpu_has() is fine here. -- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.