Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp750687pxb; Fri, 22 Apr 2022 10:21:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzWZ4kUE5SwSgZLU7t0blNfqzRMyRqKBSKDPMS/luTe0epknT5e3FQE3qv3166PySE+GvZ X-Received: by 2002:a63:e912:0:b0:39d:f8f:ca7 with SMTP id i18-20020a63e912000000b0039d0f8f0ca7mr4687289pgh.121.1650648095844; Fri, 22 Apr 2022 10:21:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650648095; cv=none; d=google.com; s=arc-20160816; b=UnpZ4yGHvjIpDgg6cn/DGA+sfF0GBsZMQhOFZ/2Y/0qsHlxRUphwC09HbLb+B378WG 8YrONhXC4zppjQD98AGtcOuNwqpQVxyhXHi5qGedjqsXaorg/xetibkoPVbSC6ik/lem POkBxh/EP2PM3amsrPciA/gZNiqIgOtvDVlAgKxwt3micA5Db+R6Hj4d1Nai8PeCXp4o LO2ZnTc5VmPQmsZJllvAxXaWKSBC7w3JrvM7ncgdRxmPBV3Cev64mOmypBKNUDlVOBTd 7WCFEhvXMc4LxVuKyYK+pc2vVIvY8g+suq5wtaYx4MSyRtW85V+vfuxAlqP4u/iDCp1I 8WVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=wFQUfjiPHZnqxP126pqF+VGxShtY5rmGdalhBuAkoT8=; b=yqRmTRYGUBCWBTzOD2u0ggrarEI2bCk+KBGFHgajM2u4KVb/cLhg2fClrp1h7mYAjX pl0hQRubTpOmwk9/NeTAeGrIvu/1qjDhtIHIO8ePR2Y0zUW9/vK56OqZsWNtIRopCDpO 3WDMAk4yikbv2zBkuTXFMVDTe99SdQKrqF3OrofS216s4vu0AL7VqS3Dr1m79nTBAmrf lBAoJdwCAbK8zTtPkxMrhZfIBgL5fbeBtltb2fd6rB3NiST0vxpWQMEMY7mzzw1aWTKo 7DY2ROmbTJUzkW0I5EjPiktS2xKoTCCItDWDyAjDkvLT9hwC/pmK6rKBZCxl/7LtXXQ3 LZJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=HAeEwNrb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id cb10-20020a056a00430a00b0050ad3a1c167si5831902pfb.175.2022.04.22.10.21.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 10:21:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=HAeEwNrb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 6EE47A5E8F; Fri, 22 Apr 2022 10:16:23 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446866AbiDVKxn (ORCPT + 99 others); Fri, 22 Apr 2022 06:53:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1446657AbiDVKxj (ORCPT ); Fri, 22 Apr 2022 06:53:39 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBFC052B27 for ; Fri, 22 Apr 2022 03:50:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=wFQUfjiPHZnqxP126pqF+VGxShtY5rmGdalhBuAkoT8=; b=HAeEwNrbJ+4/2lMEnr0sjlWnOF psR4t35/cLkOhlhHhkc8Jj9l0aUbgMl3SOJCcLhbjWzR9P5QlqNHnlpJN60tlLHgAMaqmu+N7yDxi 9NOsUbwqs+ND/CqNSdO4goYhEYNIaCp75GMlJtYquV9nrgM+n63cLE9V+phgVtaEhiJ6SIObw92DR 93FIaNuey27JNJm1iFvVsSb1MPMS1/WYD3SmFsVJ1M0CeEOaRHrg9jrqJ71rJbG4kvVQ+rgQ26jBX tW1jg6CcjGnfezXEmGEIWiDQi56aZOTwhIl1aM8180XLHeNFsJfUvS0V8Ls5dU7U8L39UArCrTI3a dDqVWnNA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=worktop.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nhqs3-005zx4-D0; Fri, 22 Apr 2022 10:50:39 +0000 Received: by worktop.programming.kicks-ass.net (Postfix, from userid 1000) id 3BAC898618A; Fri, 22 Apr 2022 12:50:37 +0200 (CEST) Date: Fri, 22 Apr 2022 12:50:37 +0200 From: Peter Zijlstra To: Miroslav Benes Cc: Josh Poimboeuf , x86@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 15/25] objtool: Rework ibt and extricate from stack validation Message-ID: <20220422105037.GV2731@worktop.programming.kicks-ass.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 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 On Wed, Apr 20, 2022 at 07:25:16PM +0200, Miroslav Benes wrote: > A nit and it was there even before this patch... > > > -static struct instruction * > > -validate_ibt_reloc(struct objtool_file *file, struct reloc *reloc) > > -{ > > - struct instruction *dest; > > - struct section *sec; > > - unsigned long off; > > - > > - sec = reloc->sym->sec; > > - off = reloc->sym->offset; > > - > > - if ((reloc->sec->base->sh.sh_flags & SHF_EXECINSTR) && > > - (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32)) > > - off += arch_dest_reloc_offset(reloc->addend); > > here... > > > +static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn) > > +{ > > ... > > + off = reloc->sym->offset; > > + if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32) > > + off += arch_dest_reloc_offset(reloc->addend); > > + else > > + off += reloc->addend; > > it looks kind of strange to have arch_dest_reloc_offset() and still > reference arch-specific relocation types here. On the other hand it seems > difficult to achieve complete arch-agnostic code, so take it just as a > note and maybe someone porting objtool to a different architecture will > split the code, make it all arch-independent and all will be nice and > shiny. Something like so perhaps? Seems to build and boot x86_64-defconfig. --- tools/objtool/arch/x86/decode.c | 9 +++++++-- tools/objtool/check.c | 18 +++++++----------- tools/objtool/include/objtool/arch.h | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c index 8b990a52aada..775e1963ecfc 100644 --- a/tools/objtool/arch/x86/decode.c +++ b/tools/objtool/arch/x86/decode.c @@ -63,9 +63,14 @@ bool arch_callee_saved_reg(unsigned char reg) } } -unsigned long arch_dest_reloc_offset(int addend) +unsigned long arch_dest_reloc_offset(struct reloc *reloc) { - return addend + 4; + unsigned long offset = reloc->sym->offset + reloc->addend; + + if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32) + offset += 4; + + return offset; } unsigned long arch_jump_destination(struct instruction *insn) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 2063f9fea1a2..5752013dd6e8 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1295,7 +1295,7 @@ static int add_jump_destinations(struct objtool_file *file) dest_off = arch_jump_destination(insn); } else if (reloc->sym->type == STT_SECTION) { dest_sec = reloc->sym->sec; - dest_off = arch_dest_reloc_offset(reloc->addend); + dest_off = arch_dest_reloc_offset(reloc); } else if (reloc->sym->retpoline_thunk) { add_retpoline_call(file, insn); continue; @@ -1308,8 +1308,7 @@ static int add_jump_destinations(struct objtool_file *file) continue; } else if (reloc->sym->sec->idx) { dest_sec = reloc->sym->sec; - dest_off = reloc->sym->sym.st_value + - arch_dest_reloc_offset(reloc->addend); + dest_off = arch_dest_reloc_offset(reloc); } else { /* non-func asm code jumping to another file */ continue; @@ -1413,7 +1412,7 @@ static int add_call_destinations(struct objtool_file *file) } } else if (reloc->sym->type == STT_SECTION) { - dest_off = arch_dest_reloc_offset(reloc->addend); + dest_off = arch_dest_reloc_offset(reloc); dest = find_call_destination(reloc->sym->sec, dest_off); if (!dest) { WARN_FUNC("can't find call dest symbol at %s+0x%lx", @@ -3031,6 +3030,7 @@ static inline const char *call_dest_name(struct instruction *insn) static bool pv_call_dest(struct objtool_file *file, struct instruction *insn) { struct symbol *target; + unsigned long offset; struct reloc *rel; int idx; @@ -3038,7 +3038,8 @@ static bool pv_call_dest(struct objtool_file *file, struct instruction *insn) if (!rel || strcmp(rel->sym->name, "pv_ops")) return false; - idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *)); + offset = arch_dest_reloc_offset(rel) - rel->sym->offset; + idx = offset / sizeof(void *); if (file->pv_ops[idx].clean) return true; @@ -3709,12 +3710,7 @@ static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn if (reloc->sym->static_call_tramp) continue; - off = reloc->sym->offset; - if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32) - off += arch_dest_reloc_offset(reloc->addend); - else - off += reloc->addend; - + off = arch_dest_reloc_offset(reloc); dest = find_insn(file, reloc->sym->sec, off); if (!dest) continue; diff --git a/tools/objtool/include/objtool/arch.h b/tools/objtool/include/objtool/arch.h index 9b19cc304195..57562eaa0967 100644 --- a/tools/objtool/include/objtool/arch.h +++ b/tools/objtool/include/objtool/arch.h @@ -81,7 +81,7 @@ bool arch_callee_saved_reg(unsigned char reg); unsigned long arch_jump_destination(struct instruction *insn); -unsigned long arch_dest_reloc_offset(int addend); +unsigned long arch_dest_reloc_offset(struct reloc *reloc); const char *arch_nop_insn(int len); const char *arch_ret_insn(int len);