From patchwork Wed Jul 5 18:12:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 116289 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp2055701vqx; Wed, 5 Jul 2023 11:25:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlHDzRbcxflDhKXH/oXqtIozs2sZ8a2bDMFOcMO1fU1raVxc7SKmGvSn17uygh3ztWXpIT94 X-Received: by 2002:a17:90b:3a8e:b0:263:4815:cb9a with SMTP id om14-20020a17090b3a8e00b002634815cb9amr16438028pjb.41.1688581508928; Wed, 05 Jul 2023 11:25:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688581508; cv=none; d=google.com; s=arc-20160816; b=KCIcFcgZ2Hdz5RSAAhbhRBbmDyUuorlsx922+b1PWkFtFs+8520YGlycF/UB5aSSEH q0J7PQ/Mi5QH6SmnoDbQbCX8PM5rBBbikZD4Nv3EIcjFm671tKpXhKMS8W4CdC0ptEzJ edxX2JXZYAOJWdifhgWVS/S8Fq6OjtvHYjTXq3nEZUSd5nRXG7hFKzyCOp4i3KpmSBJX IhfKdceY0ScY2q5dNhaR7dTGuUuZeNkgRi8NtkLTZPlMzj05r4nfgvnuslQWxhi8/PwB /o5dSLFBfKxCjlLscwNSa0nh+b/yQGZTzixorPTh47xrpHbCO9JDnHsztUmecOiSfFbe 7pQA== 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=J8rXW514UBch39kVc1ygiPYqeRYBiljQKIWLWtG0jLw=; fh=3UhQn3CgZPqYoVL3wUdj68RjB671pvt3bCy1XlPpML0=; b=I1AOrmcCZFK3HbnW0H0JaLQdqYJDRjK2mNgkXQmiBko5FZ3b/ng5vliFfl6t/hKGVJ TH9wbl7uVJUZNGZszgasUj44YQmjwo2nuKjhPwSJhjTj/2SksDvc9OkUPkT6RXYT4KSb DxXxbw9ZbtQbQrkvLI9tOc1HYo1z3idbLt0QOnBYxcA45cBi3PjGCxGpXaE1UGt6JPlv kn5A1/u8gn7bJHE7PxPftDqswFtM42RfyEQtmU+GqnF5ohrE5PpCr8kQPnBVdjsF3g6/ ObUQAKDTWvGJolXad47CXJhrdGCLuwpzKGIVJXICa4RCk6UYSgyGbB7iafowgIl1sjAt WOtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FX1r+ppN; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x7-20020a17090a970700b0025690b76c95si2093174pjo.10.2023.07.05.11.24.52; Wed, 05 Jul 2023 11:25:08 -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=FX1r+ppN; 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 S233411AbjGESSB (ORCPT + 99 others); Wed, 5 Jul 2023 14:18:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233369AbjGESRl (ORCPT ); Wed, 5 Jul 2023 14:17:41 -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 DC4B31BC1 for ; Wed, 5 Jul 2023 11:16:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688581005; 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=J8rXW514UBch39kVc1ygiPYqeRYBiljQKIWLWtG0jLw=; b=FX1r+ppNlRDeMGoNbaLNkLujoNP9R7FxFlVx8mczJNXxItZigQWKipehItWUWOwnlVMil8 E/yG3Ir2qaKxA3RqHwesRb9y7ebiVJJMprsjuymGtQt8DhMEm4UWGJ5s3dBoKpZI3437tr JT8Xuw7LNh4CrauuyrfdV6fNH1OY0qA= 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-101-9oeIA5OgNLqp_60MHHVNcQ-1; Wed, 05 Jul 2023 14:16:43 -0400 X-MC-Unique: 9oeIA5OgNLqp_60MHHVNcQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A8779810BB4; Wed, 5 Jul 2023 18:16:41 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.42.28.164]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A081B18EB4; Wed, 5 Jul 2023 18:16:36 +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 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" , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Josh Poimboeuf , 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 07/14] objtool: Warn about non __ro_after_init static key usage in .noinstr Date: Wed, 5 Jul 2023 19:12:49 +0100 Message-Id: <20230705181256.3539027-8-vschneid@redhat.com> In-Reply-To: <20230705181256.3539027-1-vschneid@redhat.com> References: <20230705181256.3539027-1-vschneid@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770606043997355033?= X-GMAIL-MSGID: =?utf-8?q?1770606043997355033?= 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 fe62232f218f9..7f8d210ec88c3 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2032,6 +2032,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); } @@ -3520,6 +3523,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) { @@ -3670,6 +3687,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 baa85c31526b3..830e6abf173a2 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 = key_reloc->addend; + + reloc_to_sec_off(key_reloc, &sec, &offset); + alt->key_sym = find_symbol_by_offset(sec, offset & ~2); } return 0;