Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1005433ybb; Fri, 20 Mar 2020 11:40:32 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtm3VG3Y9xq3UeB4xvUJSBxJbi2NgTHCoUVX0HGmNX2On6gNzTL4wZ1Z9nv3y0bDe27SQRB X-Received: by 2002:a05:6830:1697:: with SMTP id k23mr8590440otr.155.1584729632771; Fri, 20 Mar 2020 11:40:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584729632; cv=none; d=google.com; s=arc-20160816; b=VyR7rGGWCGmIgzQ33kTol8Mmdq3PWNZb+QgS6VQPZIXA6pvx2a1lUxbx4zI6lX7XLB IAq2X1mKJvQPYNAZIu8Ke0yDzEPGPFbLC6+2Zcr2XSlrQg+B2AuEWBXv0xz8gBibU73i MgOxAKsP+RCvlue2eup153EyOJWTGTgA1tcsQeFcUJ5ObQ7xdCseo7tsHBvHEmEgt6V+ 3cwPUGL8cOplNzL+mTaZB2K3eZIjpMVOC8Bc7V+d+pn5N4kC/VNn+3IYd81v/CnSIgfg nvpO9mJjVP6mCprDPln87FxGq28R1Pth9L4xYTvRMxgAJjAR6SvFdgYabPQZ67Nxj5Se yjEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:subject:cc:to:from:date:user-agent:message-id; bh=Wyxs5w9BeHKOtx6Uz22nf67V7clK/OHwoKcP54CULoo=; b=vbn37Abh1+ZdsdHrZ0fIe6IcfXw+POVqJ3DGTFJpD8y5bTtppFICXRrj29xwEWgpoE E/6DexYXrBGvI3fd6S2XFXPk1VEzZx6szNJhFjq9f+3pusPxay5ltTL2SDfywansZma5 M1MEiH9bCadt76mG6kd9sKn/s+YjDV2MWHEzgu7P67I1BAvhoy1+w276EFLNMvxeD3un w3KQVoIdM31Rj7gLWdunfXAqOXLxUGA7QZH839mH0SWwuzqOuN+VhpB9BwfvU04MKsR2 bduKoO/2kGzs4N5T9mA5Rk6Kaap4HAFPsBhiuAQCTqt8fkhdv1w6wb/aEHvCHRccfc9I cfyw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c22si3896772oto.280.2020.03.20.11.40.11; Fri, 20 Mar 2020 11:40:32 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727742AbgCTSjV (ORCPT + 99 others); Fri, 20 Mar 2020 14:39:21 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:36872 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727022AbgCTSiS (ORCPT ); Fri, 20 Mar 2020 14:38:18 -0400 Received: from p5de0bf0b.dip0.t-ipconnect.de ([93.224.191.11] helo=nanos.tec.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1jFMXA-000228-32 for linux-kernel@vger.kernel.org; Fri, 20 Mar 2020 19:38:16 +0100 Received: from nanos.tec.linutronix.de (localhost [IPv6:::1]) by nanos.tec.linutronix.de (Postfix) with ESMTP id 7660A1039FF for ; Fri, 20 Mar 2020 19:38:15 +0100 (CET) Message-Id: <20200320180032.708673769@linutronix.de> User-Agent: quilt/0.65 Date: Fri, 20 Mar 2020 18:59:59 +0100 From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Paul McKenney , Josh Poimboeuf , "Joel Fernandes (Google)" , "Steven Rostedt (VMware)" , Masami Hiramatsu , Alexei Starovoitov , Frederic Weisbecker , Mathieu Desnoyers , Brian Gerst , Juergen Gross , Alexandre Chartre , Peter Zijlstra , Tom Lendacky , Paolo Bonzini , kvm@vger.kernel.org Subject: [patch V3 03/23] vmlinux.lds.h: Create section for protection against instrumentation References: <20200320175956.033706968@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-transfer-encoding: 8-bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some code pathes, especially the low level entry code, must be protected against instrumentation for various reasons: - Low level entry code can be a fragile beast, especially on x86. - With NO_HZ_FULL RCU state needs to be established before using it. Having a dedicated section for such code allows to validate with tooling that no unsafe functions are invoked. Add the .noinstr.text section and the noinstr attribute to mark functions. noinstr implies notrace. Kprobes will gain a section check later. Provide also two sets of markers: - instr_begin()/end() This is used to mark code inside in a noinstr function which calls into regular instrumentable text section as safe. - noinstr_call_begin()/end() Same as above but used to mark indirect calls which cannot be tracked by tooling and need to be audited manually. Signed-off-by: Thomas Gleixner --- include/asm-generic/sections.h | 3 +++ include/asm-generic/vmlinux.lds.h | 4 ++++ include/linux/compiler.h | 24 ++++++++++++++++++++++++ include/linux/compiler_types.h | 4 ++++ scripts/mod/modpost.c | 2 +- 5 files changed, 36 insertions(+), 1 deletion(-) --- a/include/asm-generic/sections.h +++ b/include/asm-generic/sections.h @@ -53,6 +53,9 @@ extern char __ctors_start[], __ctors_end /* Start and end of .opd section - used for function descriptors. */ extern char __start_opd[], __end_opd[]; +/* Start and end of instrumentation protected text section */ +extern char __noinstr_text_start[], __noinstr_text_end[]; + extern __visible const void __nosave_begin, __nosave_end; /* Function descriptor handling (if any). Override in asm/sections.h */ --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -550,6 +550,10 @@ #define TEXT_TEXT \ ALIGN_FUNCTION(); \ *(.text.hot TEXT_MAIN .text.fixup .text.unlikely) \ + ALIGN_FUNCTION(); \ + __noinstr_text_start = .; \ + *(.noinstr.text) \ + __noinstr_text_end = .; \ *(.text..refcount) \ *(.ref.text) \ MEM_KEEP(init.text*) \ --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -120,12 +120,36 @@ void ftrace_likely_update(struct ftrace_ /* Annotate a C jump table to allow objtool to follow the code flow */ #define __annotate_jump_table __section(.rodata..c_jump_table) +/* Begin/end of an instrumentation safe region */ +#define instr_begin() ({ \ + asm volatile("%c0:\n\t" \ + ".pushsection .discard.instr_begin\n\t" \ + ".long %c0b - .\n\t" \ + ".popsection\n\t" : : "i" (__COUNTER__)); \ +}) + +#define instr_end() ({ \ + asm volatile("%c0:\n\t" \ + ".pushsection .discard.instr_end\n\t" \ + ".long %c0b - .\n\t" \ + ".popsection\n\t" : : "i" (__COUNTER__)); \ +}) + #else #define annotate_reachable() #define annotate_unreachable() #define __annotate_jump_table +#define instr_begin() do { } while(0) +#define instr_end() do { } while(0) #endif +/* + * Annotation for audited indirect calls. Distinct from instr_begin() on + * purpose because the called function has to be noinstr as well. + */ +#define noinstr_call_begin() instr_begin() +#define noinstr_call_end() instr_end() + #ifndef ASM_UNREACHABLE # define ASM_UNREACHABLE #endif --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -118,6 +118,10 @@ struct ftrace_likely_data { #define notrace __attribute__((__no_instrument_function__)) #endif +/* Section for code which can't be instrumented at all */ +#define noinstr \ + noinline notrace __attribute((__section__(".noinstr.text"))) + /* * it doesn't make sense on ARM (currently the only user of __naked) * to trace naked functions because then mcount is called without --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -953,7 +953,7 @@ static void check_section(const char *mo #define DATA_SECTIONS ".data", ".data.rel" #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \ - ".kprobes.text", ".cpuidle.text" + ".kprobes.text", ".cpuidle.text", ".noinstr.text" #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \ ".fixup", ".entry.text", ".exception.text", ".text.*", \ ".coldtext"