From patchwork Thu Oct 27 09:28:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 11694 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp129956wru; Thu, 27 Oct 2022 02:41:09 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5VNMFp4UTWwLESJJvSwqFRfLvLC0AJ9EGRnExsnxmdaSxcNwY578fJeaOhelg0ferz5iFn X-Received: by 2002:a17:907:3e01:b0:730:a690:a211 with SMTP id hp1-20020a1709073e0100b00730a690a211mr40793219ejc.596.1666863669070; Thu, 27 Oct 2022 02:41:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666863669; cv=none; d=google.com; s=arc-20160816; b=j2/MWdG5agib7wxnGy2vqc/Yii0+eRMVnNLYliOIg1DAGv2MC3XCxCq6nDeqrbywRJ LXDRD+MR1lCVMvx4NwP0sZVnKvIwigKyAV2QUp03O8kG9mmUeTXmZ9MimUYC2AaFCfAr fZ/q48k5UrPadjQ4wAf1B8IF6vRnu6esvSJgPryYDqQiE72NdjJCkx7+afLNt3jXT7+R DFbB2tVhV+yl3PlgRZv/TRIvwa2bgZek6ofhxgPCGT38yUkfABZBHpkNAp35uXe+w9P8 DrGh0t8smXLeJA3oos1doLFb1DLD1WlcHdcTS0hzUmYxPRe8BPruivHLDG1tr8fobo99 iBaQ== 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=DMAH9kfM4tEFqzW7U3XOFl7UPt7+R8Cga6pBO8g46JQ=; b=y98pVlkgjO2fzcK3yCsg62fKx2gh0NJo2+M8KOrP03DKWFtGxDaCdHuCgf0Rf4F8Yn 00l3haSh3ljmzCAYfQ/dxtYJvo+UY04okRGi4vwcaIVRkd0X4l1exOWEVIGNN2om94io zldDYjW+oMTwHqVU7fk2EPsKjki/jY+Ggl2dQ6xGgHSpuyedaRSEeaqsX+hBU4hsDODA 7g5BBfB/mzYn86VNWDbO7zpFsXxzIZmPejmQbR+njykS1lLLtzjA/S8+4kLYsyQHjGOi ug1yEUp3KWfhWcRrCEFrNMNpHJSkzJQIB/VgAc6dxUvk4SPqF339PrBx8B5ObHf+BDS6 mazw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=l4jn8iSI; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sd32-20020a1709076e2000b0072fb108db55si993473ejc.895.2022.10.27.02.40.44; Thu, 27 Oct 2022 02:41:09 -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=@infradead.org header.s=casper.20170209 header.b=l4jn8iSI; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234897AbiJ0JaI (ORCPT + 99 others); Thu, 27 Oct 2022 05:30:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235033AbiJ0J3o (ORCPT ); Thu, 27 Oct 2022 05:29:44 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE4D4BCAE for ; Thu, 27 Oct 2022 02:29:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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=DMAH9kfM4tEFqzW7U3XOFl7UPt7+R8Cga6pBO8g46JQ=; b=l4jn8iSIh7prip20Uz5pPewEvV gAa7AXNkok0jcvCOfRqC2iNwDfWiUIY7shqcgfyhSxBRXGqeuKTwaj75p4xhMsBYlOw5xW/74qv8+ fLeV3bUWua5x+jY7YDCaEG2fbLoTVFltJBlrLwREJCLDKP+K/MrWWu2k9O35zWUBTAoL07S9oCysA RirVDRX8+oY9nAzFYsEVZrIh1vQRj3LC5jkDDOdPGrXtxpBMR5VSK8Okrv4OJSRw7TFJtnrLjz3yy EBdx8JeyY2xWLRRA3YwAstCh3chPREbeufnOJDcL8QiwNwFi0M/4Qem/5pOTb9405V5M0lyTjZP76 zfyzZ4Eg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1onzCi-0005S9-EN; Thu, 27 Oct 2022 09:29:36 +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) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 8DB39300411; Thu, 27 Oct 2022 11:29:30 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 6C0DD2C450475; Thu, 27 Oct 2022 11:29:30 +0200 (CEST) Message-ID: <20221027092842.568039454@infradead.org> User-Agent: quilt/0.66 Date: Thu, 27 Oct 2022 11:28:13 +0200 From: Peter Zijlstra To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, Kees Cook , Sami Tolvanen , Joao Moreira , Josh Poimboeuf , Mark Rutland Subject: [PATCH 1/4] objtool: Add --cfi to generate the .cfi_sites section References: <20221027092812.185993858@infradead.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham 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?1747833238358900213?= X-GMAIL-MSGID: =?utf-8?q?1747833238358900213?= Add the location of all __cfi_##name symbols (as generated by kCFI) to a section such that we might re-write things at kernel boot. Notably; boot time re-hashing and FineIBT are the intended use of this. Signed-off-by: Peter Zijlstra (Intel) --- tools/objtool/builtin-check.c | 1 tools/objtool/check.c | 69 ++++++++++++++++++++++++++++++++ tools/objtool/include/objtool/builtin.h | 1 3 files changed, 71 insertions(+) --- a/tools/objtool/builtin-check.c +++ b/tools/objtool/builtin-check.c @@ -79,6 +79,7 @@ const struct option check_options[] = { OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"), OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"), OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"), + OPT_BOOLEAN(0 , "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"), OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump), OPT_GROUP("Options:"), --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -861,6 +861,68 @@ static int create_ibt_endbr_seal_section return 0; } +static int create_cfi_sections(struct objtool_file *file) +{ + struct section *sec, *s; + struct symbol *sym; + unsigned int *loc; + int idx; + + sec = find_section_by_name(file->elf, ".cfi_sites"); + if (sec) { + INIT_LIST_HEAD(&file->call_list); + WARN("file already has .cfi_sites section, skipping"); + return 0; + } + + idx = 0; + for_each_sec(file, s) { + if (!s->text) + continue; + + list_for_each_entry(sym, &s->symbol_list, list) { + if (sym->type != STT_FUNC) + continue; + + if (strncmp(sym->name, "__cfi_", 6)) + continue; + + idx++; + } + } + + sec = elf_create_section(file->elf, ".cfi_sites", 0, sizeof(unsigned int), idx); + if (!sec) + return -1; + + idx = 0; + for_each_sec(file, s) { + if (!s->text) + continue; + + list_for_each_entry(sym, &s->symbol_list, list) { + if (sym->type != STT_FUNC) + continue; + + if (strncmp(sym->name, "__cfi_", 6)) + continue; + + loc = (unsigned int *)sec->data->d_buf + idx; + memset(loc, 0, sizeof(unsigned int)); + + if (elf_add_reloc_to_insn(file->elf, sec, + idx * sizeof(unsigned int), + R_X86_64_PC32, + s, sym->offset)) + return -1; + + idx++; + } + } + + return 0; +} + static int create_mcount_loc_sections(struct objtool_file *file) { struct section *sec; @@ -4397,6 +4459,13 @@ int check(struct objtool_file *file) if (ret < 0) goto out; warnings += ret; + } + + if (opts.cfi) { + ret = create_cfi_sections(file); + if (ret < 0) + goto out; + warnings += ret; } if (opts.rethunk) { --- a/tools/objtool/include/objtool/builtin.h +++ b/tools/objtool/include/objtool/builtin.h @@ -26,6 +26,7 @@ struct opts { bool stackval; bool static_call; bool uaccess; + bool cfi; /* options: */ bool backtrace;