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: 13302573 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 33F58EB64DA for ; Wed, 5 Jul 2023 18:16:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC2FC8E0002; Wed, 5 Jul 2023 14:16:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C71C48D0001; Wed, 5 Jul 2023 14:16:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B128F8E0002; Wed, 5 Jul 2023 14:16:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9D3148D0001 for ; Wed, 5 Jul 2023 14:16:51 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7421A406D7 for ; Wed, 5 Jul 2023 18:16:51 +0000 (UTC) X-FDA: 80978364222.01.29A23C4 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 921B914001A for ; Wed, 5 Jul 2023 18:16:49 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=I7UinYmQ; spf=pass (imf09.hostedemail.com: domain of vschneid@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688581009; 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=J8rXW514UBch39kVc1ygiPYqeRYBiljQKIWLWtG0jLw=; b=zKi/nGeDQEXBOErt8eybd/Jn6QncqoWmOMLV3aYwEGAGC8pDwyUpE3bSbtmrewhhf+gTRe 9U/ttxnrG/rgQdI8oPdYgOKaxUQKpS8o4khvyQonQfLfLg95nKCf8JvgqnjQ+AXokISO17 dLoBrPnbPToRINOVvNbovDwmTuoNsl0= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=I7UinYmQ; spf=pass (imf09.hostedemail.com: domain of vschneid@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688581009; a=rsa-sha256; cv=none; b=Xz1gbmk4bBri1D4nnXYHcBWHsgYYsblxNrYwDQGam4BJJh/x+qJNcRxM6nOZM5EI08XYZe dG4ovkVgQFH7SuFwBQuaLJg5Tzi2aX0PhTxIZy8RMol5jCnxNlgBDG6IdwK5mRPvDC53Jr og2xeN6tssnMauGxflnrZJLG/63IWII= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688581008; 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=I7UinYmQgjgJ2HOf3bbSdyTs/TK92r583Deu53WQg5rmHkQf7HHx87pahiyO18qvC9iwbr HL32biXYE7D3nNywj9fUHDk4xwwZreBJEAHYyiBTD/IL8kojjCOaf5cS/XkY+BJtGg8E9h YOF/rYpoCQjJ4QABrk/iDthAYj0ywMY= 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-Rspamd-Queue-Id: 921B914001A X-Rspam-User: X-Stat-Signature: xsr5ph8e9zoa3w8zpqr3jbpn5j8hqd8f X-Rspamd-Server: rspam01 X-HE-Tag: 1688581009-872950 X-HE-Meta: U2FsdGVkX1/Tou4uXHsibKtf7RDr8e7BNNw3xc46abFdU9FU/RL6cvt0hS3xs8VBImTqRDEIkWbONO06I1HC7XnxDhtApYACWj6qPRwzpMFuvRZy8vvCF5psSIPdQJ7ko9DPaGt+VVOqf2WzQyHiQBYebET+aWjZiKmEppaClrqg3nHZJ6ruDcsgrBvtw0dxRfIaETbxjz8sFlFVw4uBXE0BpNmzrKmRA0UKKbbhSDchlgGPv0nydKYic8NQEZ/+BBsHYFeVRdLFng59uoht2GNG5TNu2FdrlS5A/DGEG+R1jvl2+VGDMf0454B9I20DWpwCUpDYsTe6PGdAZGzshZ4Br71Ta+FyOezzawgDzT6zAhoQdLGlSSCWdvXfgQaYmoEz/7FVjfZp8OzBIvC1DIOD8Qvy/nRV6BtcFnWWgZpuYlE8KMY0TYputU+Io+IIf1yI95cHzrVPp0oKn0k1j/ulvWFHbQOkgVyUu3VLvv68qV9ci+TuZlcs+hQdbtPkSNvEjhZFV37dFk+bdGiOzH1U2nUvzf6fYHqsQqE5EGaQNCrKB2zyoj78LtbBZn0ZZSj4E7TxcWsTx/9EtBtBTWqFXxjduMfIhp9PDFuinAjuM53J3/41VFPb0/PMFjvkCVTBhRsNDCLJJLRZnXuah77UXGi1S7GmWVxTaYmi5Tt1ZY7YgB+ZkPzwKtQL3Na9oTE0ECdrbNkHdy87iJ6zRHRwLT4J2Iln7vTobGMs+TXlWMt5gnumZNZA+6eTYN2l9BkH0KdRWD+6JsqX29yOl/N8eQvHaCDRdG/tvECmIj3wj4ffADYKd+0NnhRUmqSFy2ctd02DhmvrkMqmFR53uAEliaFgsMmI/npy8FJGopTdnZ7OY5TwcYtR+D5Ymua/RrfAcwBU/T3bacHwKr5x8c1oV78daAFXV/fJDTqIhf7kCfj6DrF1ZBO+7B/rj1gNDzQiVG/kZvFF2HOGnXb 4xuRis3k /xPoKfNlKC7MvpGBxe675bmHrKaxWMpqZ4r00fzv34OmIG9qGKxTiO80DB7rEGygwoPVdevg8xOnwChTRrf+fkd5fLwBwX1vnPZ8LLVqUlcDWwW3x1KQExQi4E6M1AlYG4pHFTG6EFvDZc/kgUH8LIP7F2kdFxYIKrtXtLMObwPtxKOD9DKOMy6+dYy7VLooJu9ytGc0EdswPxhIek7rJ1yEvWuVIJTzhf2Ov 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 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;