Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp9808521rwp; Thu, 20 Jul 2023 10:00:00 -0700 (PDT) X-Google-Smtp-Source: APBJJlHSAurNPsyMx2QXYWjfDc7c1BOUqdbbtNMg4w6rnw8en9gckUXMeQYpYXSZXAojYO934ZCI X-Received: by 2002:a17:902:cf46:b0:1b8:9b1d:d7f1 with SMTP id e6-20020a170902cf4600b001b89b1dd7f1mr7244380plg.47.1689872399790; Thu, 20 Jul 2023 09:59:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689872399; cv=none; d=google.com; s=arc-20160816; b=p82XdsfgelZ9iBAlp5+5PLnqdIWfyzwcCirKxEeI43RSSmc7WxZtlusrmW/szCxHvP BWRt40x48D57grUp7ciTXefW/Ho+p2zW/pARm+AMfM5cW2idWNb72VmYoskPFHSdpAka NcxV8J0pgVoVeV5qpWDt7iZWV1ODG27AzdoH2pPnTnyyORYlKqkRGWf1XAWXJ9ld2XIm i8yT/+rb2uJ/iPc6yqxY/XO2LmT6yftD3/F5cim+a7QddK9pw7J7FyFxgf5AlFC2sFT4 OmP6SomFh8UOWphZlYUEGyQhAtvrcrIYncR/JHKIBzhCsnySUjCV6zkvoHg9BRlTBQ3f c3og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=w2k5nztFy3WXKlhL7LY6m+LcDW7MzG+nauWvTii+Ef4=; fh=NSUq75zeufIVjHRBHbml84yXVIvVKM/2W0B9xTkxwlc=; b=xUWLpVhP8zbpXubADICGQKcHCdyev3dJwdIcfY88mCy6STdVUVO8507GNB5iOn7uwU UYJ6Z+F/yzS+PLI7K/C95P79AQvWfmuPy3regknC2Vp+EZnq7FHxKzFB7+uAiLrdB7DT kEs1AmXUPB3DTzEqsPBl1n6pyQT4+TJFrIX6l9+KLNIaqCbTRH0oDxZwGDSeyrJj2W0/ KwB05Zyx2NpoF/2gAtImTlofrIVDiEdyZS47tBgCWAXeNmy7YQMhSUFJkrUdsymqLTYC UYECuw4Wrq++OnmfTZXov5gI3YQoawVFwHbAuBUIsvb23xVRxITfo4KPPZZjZqO6eMzJ zSBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=A9cIk7hV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u6-20020a170903124600b001b9d2010b27si1491124plh.119.2023.07.20.09.59.46; Thu, 20 Jul 2023 09:59:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=A9cIk7hV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231330AbjGTQfb (ORCPT + 99 others); Thu, 20 Jul 2023 12:35:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231868AbjGTQfM (ORCPT ); Thu, 20 Jul 2023 12:35:12 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBB8F199D for ; Thu, 20 Jul 2023 09:33:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689870829; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w2k5nztFy3WXKlhL7LY6m+LcDW7MzG+nauWvTii+Ef4=; b=A9cIk7hV7QKZoF+6s1x00u4oDJ4UYPgSPYNU1guRhg3Xp205VkA1MdHLzD4qrNZ/Lp33my bnBPEEye3dVSxgc276iTmzvA0mtyK7WSt/EQEiRKmMaVVZMD2LCrRMHRz3xQE0c7IHzomg NV+q9a8Iq9AFi6zrdLR6c6bYgtBhmbk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-77-dQqQ7nvGOe-_KMx6MDuCag-1; Thu, 20 Jul 2023 12:33:43 -0400 X-MC-Unique: dQqQ7nvGOe-_KMx6MDuCag-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F1ADA1044592; Thu, 20 Jul 2023 16:33:33 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.42.28.48]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F11B740C206F; Thu, 20 Jul 2023 16:33:25 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, x86@kernel.org, rcu@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Josh Poimboeuf , Steven Rostedt , Masami Hiramatsu , Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Paolo Bonzini , Wanpeng Li , Vitaly Kuznetsov , Andy Lutomirski , Peter Zijlstra , Frederic Weisbecker , "Paul E. McKenney" , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Josh Poimboeuf , Jason Baron , Kees Cook , Sami Tolvanen , Ard Biesheuvel , Nicholas Piggin , Juerg Haefliger , Nicolas Saenz Julienne , "Kirill A. Shutemov" , Nadav Amit , Dan Carpenter , Chuang Wang , Yang Jihong , Petr Mladek , "Jason A. Donenfeld" , Song Liu , Julian Pidancet , Tom Lendacky , Dionna Glaze , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Juri Lelli , Daniel Bristot de Oliveira , Marcelo Tosatti , Yair Podemsky Subject: [RFC PATCH v2 12/20] objtool: Warn about non __ro_after_init static key usage in .noinstr Date: Thu, 20 Jul 2023 17:30:48 +0100 Message-Id: <20230720163056.2564824-13-vschneid@redhat.com> In-Reply-To: <20230720163056.2564824-1-vschneid@redhat.com> References: <20230720163056.2564824-1-vschneid@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Later commits will depend on having no runtime-mutable text in early entry code. (ab)use the .noinstr section as a marker of early entry code and warn about static keys used in it that can be flipped at runtime. Suggested-by: Josh Poimboeuf Signed-off-by: Valentin Schneider --- tools/objtool/check.c | 20 ++++++++++++++++++++ tools/objtool/include/objtool/check.h | 1 + tools/objtool/include/objtool/special.h | 2 ++ tools/objtool/special.c | 3 +++ 4 files changed, 26 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index d308330f2910e..d973bb4df4341 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1968,6 +1968,9 @@ static int add_special_section_alts(struct objtool_file *file) alt->next = orig_insn->alts; orig_insn->alts = alt; + if (special_alt->key_sym) + orig_insn->key_sym = special_alt->key_sym; + list_del(&special_alt->list); free(special_alt); } @@ -3476,6 +3479,20 @@ static int validate_return(struct symbol *func, struct instruction *insn, struct return 0; } +static int validate_static_key(struct instruction *insn, struct insn_state *state) +{ + if (state->noinstr && state->instr <= 0) { + if ((strcmp(insn->key_sym->sec->name, ".data..ro_after_init"))) { + WARN_INSN(insn, + "Non __ro_after_init static key \"%s\" in .noinstr section", + insn->key_sym->name); + return 1; + } + } + + return 0; +} + static struct instruction *next_insn_to_validate(struct objtool_file *file, struct instruction *insn) { @@ -3625,6 +3642,9 @@ static int validate_branch(struct objtool_file *file, struct symbol *func, if (handle_insn_ops(insn, next_insn, &state)) return 1; + if (insn->key_sym) + validate_static_key(insn, &state); + switch (insn->type) { case INSN_RETURN: diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/objtool/check.h index daa46f1f0965a..35dd21f8f41e1 100644 --- a/tools/objtool/include/objtool/check.h +++ b/tools/objtool/include/objtool/check.h @@ -77,6 +77,7 @@ struct instruction { struct symbol *sym; struct stack_op *stack_ops; struct cfi_state *cfi; + struct symbol *key_sym; }; static inline struct symbol *insn_func(struct instruction *insn) diff --git a/tools/objtool/include/objtool/special.h b/tools/objtool/include/objtool/special.h index 86d4af9c5aa9d..0e61f34fe3a28 100644 --- a/tools/objtool/include/objtool/special.h +++ b/tools/objtool/include/objtool/special.h @@ -27,6 +27,8 @@ struct special_alt { struct section *new_sec; unsigned long new_off; + struct symbol *key_sym; + unsigned int orig_len, new_len; /* group only */ }; diff --git a/tools/objtool/special.c b/tools/objtool/special.c index 91b1950f5bd8a..1f76cfd815bf3 100644 --- a/tools/objtool/special.c +++ b/tools/objtool/special.c @@ -127,6 +127,9 @@ static int get_alt_entry(struct elf *elf, const struct special_entry *entry, return -1; } alt->key_addend = reloc_addend(key_reloc); + + reloc_to_sec_off(key_reloc, &sec, &offset); + alt->key_sym = find_symbol_by_offset(sec, offset & ~2); } return 0; -- 2.31.1