Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3569368pxp; Tue, 8 Mar 2022 17:42:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJyy1oz15/mH3wSvF8KQbseEKspIs2qDVJUR5RD1iMSuhWcEagk8ow4xtMuC/KQmgk5pAvCf X-Received: by 2002:aa7:8385:0:b0:4f6:ef47:e943 with SMTP id u5-20020aa78385000000b004f6ef47e943mr16673087pfm.38.1646790148800; Tue, 08 Mar 2022 17:42:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646790148; cv=none; d=google.com; s=arc-20160816; b=P4HGr3WTlktp8qe/3M96gFuuwMPpVu/ySC9dmjja0/PIqsGvVfH9fIvljY/SNtwSij 5iPkIFm0ODh4FO+SEn4sjuanwrGZ/9O7xy4IH4g2IYCgxe7KJurCYvmCw1E2x8Kug6mH FievpLX0t1BT8r/5rDmY5YHgkvjEKF/7h0i0qg5PFtO8AZENe4oFK+8JnKiqw9AfXA0d 3ttUBRi30ZdcvEBxkkLsCBNdQ6wHeAEpZgRtTGYgzmzRFEfe9T4X4Fi8m9Gyoe22v1zC TylO4RQWtbxNqWxNY14dAWMO81lqxApQ1mrOqctkhftMTPhCtqTUH8iz3qE1ka7nyCt+ GwnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id:dkim-signature; bh=bM/2CfoNUi7Kkgqfth4YTpf/0vnJo3PNRdu2me27tHQ=; b=IIOtukpuMTX0Z/HDPbKSUkDPScuyHQmIpPpxakXqBtS28PlsLqERlnTpvT4Vq1rHWZ Ud98t5/aAhxHVfy45L7tMvmd8kiUZtjqqgxY4TaB7Wht1xKZgazSH+03tPndd3AwQFF6 bh45pVJLiM1TTqK2I0Slxe0r9pg1Ey2K0LMgmgBog8oI2csTSO/0m3z+tt3jG2ZcBlbb e2kWZJ2zyVWd+VYamqD01AT3G9yt6J+4SlBAdj0z/dbbOFdCNJOjcvFea3WVgkuK2TQs zsxzsCZQ30by8uGhexaELiQJQLtm6flkQhs4mJhNIltbjh5C4AfB13EgmBb2Gqf6Nq7t FX0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=UomS5gg6; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id l24-20020a63da58000000b0037053f8a204si501815pgj.293.2022.03.08.17.42.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Mar 2022 17:42:28 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=UomS5gg6; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B56A423D189; Tue, 8 Mar 2022 16:37:06 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348817AbiCHP6k (ORCPT + 99 others); Tue, 8 Mar 2022 10:58:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348080AbiCHP4K (ORCPT ); Tue, 8 Mar 2022 10:56:10 -0500 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFAA44D27C for ; Tue, 8 Mar 2022 07:55:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=bM/2CfoNUi7Kkgqfth4YTpf/0vnJo3PNRdu2me27tHQ=; b=UomS5gg6jyVXEZ4UrCfSUt5Du7 0r/Fp7rUPBfZOFxAZjhRFqOjqDoibC350r2rH4T0winRKtI8bFwfARHmJRr2rt4d236jRSTVOLL0t vFfsOuP0WnzZb9mIWDR0OWdRwUStlEyUgmT65wOPgzJ15tgkZqq9Xv+fzv3Uwn7WHJUjfVlQSE0tx bLHl1UeSdUTn/VMbuBuPkRTC/DkoXkc/vC4+PzCx1faygR9IpR91DNwI8f1A3hcY/IaI0NTa/6GJf m2ol4lm5kVOp4VNHXWH/JWXUq9hZt0qmanUSy5aMlbscZZ6Cf9Urjsr26Upnly/+wu1g34jm3i94F 2i89ztWQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRcAd-00GUiq-H7; Tue, 08 Mar 2022 15:54:43 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 54F65302DDE; Tue, 8 Mar 2022 16:54:40 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 0802F2B5649E6; Tue, 8 Mar 2022 16:54:40 +0100 (CET) Message-ID: <20220308154319.763643193@infradead.org> User-Agent: quilt/0.66 Date: Tue, 08 Mar 2022 16:30:55 +0100 From: Peter Zijlstra To: x86@kernel.org, joao@overdrivepizza.com, hjl.tools@gmail.com, jpoimboe@redhat.com, andrew.cooper3@citrix.com Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, ndesaulniers@google.com, keescook@chromium.org, samitolvanen@google.com, mark.rutland@arm.com, alyssa.milburn@intel.com, mbenes@suse.cz, rostedt@goodmis.org, mhiramat@kernel.org, alexei.starovoitov@gmail.com Subject: [PATCH v4 44/45] objtool: Find unused ENDBR instructions References: <20220308153011.021123062@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Find all ENDBR instructions which are never referenced and stick them in a section such that the kernel can poison them, sealing the functions from ever being an indirect call target. This removes about 1-in-4 ENDBR instructions. Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/kernel/vmlinux.lds.S | 9 ++++ tools/objtool/check.c | 69 +++++++++++++++++++++++++++++++- tools/objtool/include/objtool/objtool.h | 1 tools/objtool/objtool.c | 1 4 files changed, 78 insertions(+), 2 deletions(-) --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -285,6 +285,15 @@ SECTIONS } #endif +#ifdef CONFIG_X86_KERNEL_IBT + . = ALIGN(8); + .ibt_endbr_seal : AT(ADDR(.ibt_endbr_seal) - LOAD_OFFSET) { + __ibt_endbr_seal = .; + *(.ibt_endbr_seal) + __ibt_endbr_seal_end = .; + } +#endif + /* * struct alt_inst entries. From the header (alternative.h): * "Alternative instructions for different CPU types or capabilities" --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -380,6 +380,7 @@ static int decode_instructions(struct ob memset(insn, 0, sizeof(*insn)); INIT_LIST_HEAD(&insn->alts); INIT_LIST_HEAD(&insn->stack_ops); + INIT_LIST_HEAD(&insn->call_node); insn->sec = sec; insn->offset = offset; @@ -409,8 +410,9 @@ static int decode_instructions(struct ob sym_for_each_insn(file, func, insn) { insn->func = func; - if (insn->type == INSN_ENDBR) { + if (insn->type == INSN_ENDBR && list_empty(&insn->call_node)) { if (insn->offset == insn->func->offset) { + list_add_tail(&insn->call_node, &file->endbr_list); file->nr_endbr++; } else { file->nr_endbr_int++; @@ -739,6 +741,58 @@ static int create_retpoline_sites_sectio return 0; } +static int create_ibt_endbr_seal_sections(struct objtool_file *file) +{ + struct instruction *insn; + struct section *sec; + int idx; + + sec = find_section_by_name(file->elf, ".ibt_endbr_seal"); + if (sec) { + WARN("file already has .ibt_endbr_seal, skipping"); + return 0; + } + + idx = 0; + list_for_each_entry(insn, &file->endbr_list, call_node) + idx++; + + if (stats) { + printf("ibt: ENDBR at function start: %d\n", file->nr_endbr); + printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int); + printf("ibt: superfluous ENDBR: %d\n", idx); + } + + if (!idx) + return 0; + + sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0, + sizeof(int), idx); + if (!sec) { + WARN("elf_create_section: .ibt_endbr_seal"); + return -1; + } + + idx = 0; + list_for_each_entry(insn, &file->endbr_list, call_node) { + + int *site = (int *)sec->data->d_buf + idx; + *site = 0; + + if (elf_add_reloc_to_insn(file->elf, sec, + idx * sizeof(int), + R_X86_64_PC32, + insn->sec, insn->offset)) { + WARN("elf_add_reloc_to_insn: .ibt_endbr_seal"); + return -1; + } + + idx++; + } + + return 0; +} + static int create_mcount_loc_sections(struct objtool_file *file) { struct section *sec; @@ -3097,8 +3151,12 @@ validate_ibt_reloc(struct objtool_file * if (!dest) return NULL; - if (dest->type == INSN_ENDBR) + if (dest->type == INSN_ENDBR) { + if (!list_empty(&dest->call_node)) + list_del_init(&dest->call_node); + return NULL; + } if (reloc->sym->static_call_tramp) return NULL; @@ -3805,6 +3863,13 @@ int check(struct objtool_file *file) if (ret < 0) goto out; warnings += ret; + } + + if (ibt) { + ret = create_ibt_endbr_seal_sections(file); + if (ret < 0) + goto out; + warnings += ret; } if (stats) { --- a/tools/objtool/include/objtool/objtool.h +++ b/tools/objtool/include/objtool/objtool.h @@ -26,6 +26,7 @@ struct objtool_file { struct list_head retpoline_call_list; struct list_head static_call_list; struct list_head mcount_loc_list; + struct list_head endbr_list; bool ignore_unreachables, c_file, hints, rodata; unsigned int nr_endbr; --- a/tools/objtool/objtool.c +++ b/tools/objtool/objtool.c @@ -128,6 +128,7 @@ struct objtool_file *objtool_open_read(c INIT_LIST_HEAD(&file.retpoline_call_list); INIT_LIST_HEAD(&file.static_call_list); INIT_LIST_HEAD(&file.mcount_loc_list); + INIT_LIST_HEAD(&file.endbr_list); file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment"); file.ignore_unreachables = no_unreachable; file.hints = false;