From patchwork Thu Jul 20 16:30:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 13320796 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 588BAEB64DD for ; Thu, 20 Jul 2023 16:33:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E848A28013A; Thu, 20 Jul 2023 12:33:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E35A028004C; Thu, 20 Jul 2023 12:33:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CAF2628013A; Thu, 20 Jul 2023 12:33:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BC34C28004C for ; Thu, 20 Jul 2023 12:33:51 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 96A7A120110 for ; Thu, 20 Jul 2023 16:33:51 +0000 (UTC) X-FDA: 81032536662.24.497B3B7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf26.hostedemail.com (Postfix) with ESMTP id C4AFF140015 for ; Thu, 20 Jul 2023 16:33:49 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=A9cIk7hV; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.hostedemail.com: domain of vschneid@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689870829; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=w2k5nztFy3WXKlhL7LY6m+LcDW7MzG+nauWvTii+Ef4=; b=BoR5aQ55o6sDQLASNn8DVPrdB12Yu60R+B7x0wNELh2kyM1T4InRDuGlnfiAVrODT9bPeM hE7fkp6NndCiFvdMc+AnJ4I0j1YRAR/RCFfRkTmCPI3A8hUtEwc31exemp8hCf3cc62AZr YaV7SvdW91eu648IrhvigAN3f89Bm+4= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=A9cIk7hV; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf26.hostedemail.com: domain of vschneid@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689870829; a=rsa-sha256; cv=none; b=x9T0Q1PkbJeAY2mlHEJaLfIgvvzExwmPaqAfifnjyYgjXiOAnRSFeztUPQ1xmdbjOFE8UL Wt0kIvOYD++cFRddQFaylCHeUag/WJmD/fbu5ex/lFaMav4w6VCf9bdQREGG4ygFG5hy6J nEEftR5uxbtEwQsPnsxpde5lJCrTLmk= 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_Wei=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 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Rspam-User: X-Stat-Signature: 8jhyqn1z5hcnbj6dcuxfezp514i3s68z X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C4AFF140015 X-HE-Tag: 1689870829-306628 X-HE-Meta: U2FsdGVkX193TVBdcU2HVWIASUFsRXXFrFoOEKh+ET80felfr7NIEvp7V18rGK49lhe0HHhq8dYdnjkfab2AaTxy9mkIVb/dMwuyrouMx/yB4PvreQrARULuUzPReaoUlXACSJef3jOBcT20pVleeTBv6R8gJZ53a6QkK/PsVDodhRGvlzj1rpmg+FQWBQKXXmT/osgUkVxPiWaVHmzg5M3UpRA04VwPnhiyQx46cNEqrbCWrTMAI/00HWqAzwhs5mN63FVVmWZavsF4ahxRtax4U1xlU9Rv8gOBdfm5L8pnQb5npH6g+duRGK453IJ4pQll5XeqmOnQIGwaHV8OHxVrfNzmr92y+di/ReBWeBohwZEcU1IJRJCpqbhRHbLMGycz1IowrzaizzC2b84Odt5j0CaQVGQMzgn5nwXiKSLrDndpn5LvIBWJHpfW7fcxVzXBL1UbJO8hs3XcdK61/MCpwdggfi2ERN5XsQDtHlHB6p9I4J/ylPjnvWHdnUtO8X6agtl73yIuX/3fcHXJ3my/TUu9uXUpLoJbZC72lDkcu8RhwxKGkZCLLrCe1XqNm1Txm0G+PVRWFMOhSv7J58Jgq2thUOTbKeeHj/+Opb8QVIAzCKfUt9qv+5Xb+1s9lLur8/yF693GocSeuRJ0nTLcAsOTc7Ltlo4p2hMtyi/YKNY5ndSWpkihyG17XaOa0y+4DGz1iA82gv9xYwYqlQNBV+KgVOwhA7M/mcIeqt8o2hkIq3UuXiRRuXeVGF4EuhgG8XkzCtgOGhR7xgF1IALNwoeif+jJRXfCVIgRFZ3AHKce9VbXy/bzoltVSajP3DDnZSXJahBtBLTuxOTZmnYMH2fURjtAG5s6VUM6gR+2wWNm9yd57ngX1M5qwQHaOx5ibYbFrKIDghNHiXA/HjerYP46YE77V9+O6dWQ5247/D/kJSRmBWed6cw/us5EpSg+CXvvDYWLV6GDF3k snY6uQl3 XYuvgwpuSF+mSNG2kd65j1VKsgDZLlhv2YitNPqv3FBLygON1S8dAABFCrSyuNuS1bVVMj9UPuB9zqxx+2eENoDUwzzH7u4knQA6p+NTW3K8dndrzce4Pr4PVTCWsNFOBrIkMo4EqAr1rE4294byYSFLUtxkOc870NEjXh1XQXabfHd195EVXxEdZmyzcyrEKN5BmfKWa7ZDmStTGzcFO8CqDZdX3YTkQSiprAVncxWux8Szh8ihselFdzAUeEkrvcjGwiyXQ8FMpqsiOOuY+7fS9sTtH7OdUQKY/ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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;