Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp655977pxb; Fri, 16 Apr 2021 15:01:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyIJZ+U9D0H0s4LFGzd9HJe2dVSfSP1MI8+bs+TeTSViu6XgAI1IJjow7c84xvdr00FVHMX X-Received: by 2002:a17:906:953:: with SMTP id j19mr10578886ejd.56.1618610474001; Fri, 16 Apr 2021 15:01:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618610473; cv=none; d=google.com; s=arc-20160816; b=fGVMj6EVNpRvVFbJpIsetE+JanUcQ116uv355VLw7rMYxanJ2YGm4Jxe3ZWwyffkAJ iR8fp4jw/51gAKhra9yovkn/Ukd9BebjTi/xkVN4JTLuMYpczymXDd2XjJLCFtN9Srzf VL+1SzMwVx1nZvF75WigAJbUoWERJwVdLH7jpzxrzhspLgJunSnetrPTGhrXnne4E5h7 epv4iNpypreUrEr9XAG2RlFzrB/k2f+CQFnYQPGvuspDs3UFmOjH5ifH5Ndh0JXX9klp tVfOzMfnH3RvuzCIMV04yqTy1cDAC4qwrjvcFhaidldE/CPcvKF9G49GjNiMLZlPGtsw u+Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=jDS7Yxe+QOQd/ON8hFRWD8N0+6UfcyzD361zMutG46I=; b=vPCIeov6KYj5Ye7fpVch8E/EBB+mXML5Gk8nY0rqcb5PODfVdkzB9Lbhr0B9DJp7/j DmwyyH+p+nG+9o/IVLjP+Vd+TB+BoZ0mKBrf6Ctm4SVpiaAhJ4kC/0MfKlESTNTNIpbP YL3g/ufJfAIX210oamBnuwUsEtbLy0/FmsjbklM9fvUalWKVSsHbDD1Ugm25nfTHDLlh X8sDXrBebDWEXF3Oh1V6PC0cZEl8EP4k8o/T1OYZ0zNPXg+QXJCvoOApDkAPF1YOizmf 1YpuNh4wDqTsFNt0DgA1jhlXFef7q72ku4Jebn5gruakx4rj3lKL2LmvXOCWRIcN3wco gWzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AkvSGo1f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m11si5519178edr.252.2021.04.16.15.00.50; Fri, 16 Apr 2021 15:01:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AkvSGo1f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343689AbhDPUjT (ORCPT + 99 others); Fri, 16 Apr 2021 16:39:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343657AbhDPUjQ (ORCPT ); Fri, 16 Apr 2021 16:39:16 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4457FC061574 for ; Fri, 16 Apr 2021 13:38:51 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id v2so7516490ybc.17 for ; Fri, 16 Apr 2021 13:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=jDS7Yxe+QOQd/ON8hFRWD8N0+6UfcyzD361zMutG46I=; b=AkvSGo1f82tRhgaVzheYzcz1/uHLFpEltaiLCHNc1FQJ0VIZvloEZRvttwz2Es0C9P JX2LNWBfwyju6TagtI3dBRt9bVrrtt+1P7QY4rJfTZoLt0evZuVLmewsroHlcVFWFes6 ADh3S57I42WpbAnMIk4L/prCP9SaBUiZQ8ocb7Q6NMptSk8qG71Dn5z7B70L32yWQ6y8 c5Kh1Lm6DJDe2uRNOjCN19N4L17xaiGLmFMzMU+u6iQ5rVgNtkCQWP1wdojuq1s05WYz tq3A2pH4uDwNo6aLLVcLGXMMn8lISDJn5fsSHhNteuW38VA7StOaYUBqlYnFxE/q/Oz4 LH0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=jDS7Yxe+QOQd/ON8hFRWD8N0+6UfcyzD361zMutG46I=; b=ah7VGZ8LouA31ooPSzEH2bZ8Rdg1PMJK0nD0PE5M/bJBV3+bNSMWMv7xoTXuZR/p/X B3ltn8+L5n9C5MBmM6yDZXOAnoNelkE/3W0r2vCDtjGl8snIWPkBLeXFt8KUREScItOU dDNqwnyBDYL463WXa01A10VFxXUcdM2xdhz5VP/ja8A6CE3GfSpOzZJdQciEbeHUbi5/ CBPDzbsxMwm+uFL1/bnVRF+6je8OI/nuxJxWoLvIEBaAVKhEXXqisMQ3xBckRgE+UtDF qqf7crRC0W6E7QIpxFO3m2Rt3bjxqt8pFEnHMuyGVpmItgXPjpztuA+V7zRp1KmOA5m6 7dAw== X-Gm-Message-State: AOAM53220jRa+JLd9Sz3wScFGtb/OLYN0aSzyKVtm1Q6YPn50IRvIec3 eHSHq+LNaU9JTJF+MApY0OufZidI57psHf3rKMY= X-Received: from samitolvanen1.mtv.corp.google.com ([2620:15c:201:2:e262:3d8e:cbf:6164]) (user=samitolvanen job=sendgmr) by 2002:a25:b993:: with SMTP id r19mr1340658ybg.445.1618605530534; Fri, 16 Apr 2021 13:38:50 -0700 (PDT) Date: Fri, 16 Apr 2021 13:38:31 -0700 In-Reply-To: <20210416203844.3803177-1-samitolvanen@google.com> Message-Id: <20210416203844.3803177-3-samitolvanen@google.com> Mime-Version: 1.0 References: <20210416203844.3803177-1-samitolvanen@google.com> X-Mailer: git-send-email 2.31.1.368.gbe11c130af-goog Subject: [PATCH 02/15] objtool: Add CONFIG_CFI_CLANG support From: Sami Tolvanen To: x86@kernel.org Cc: Kees Cook , Josh Poimboeuf , Peter Zijlstra , Nathan Chancellor , Nick Desaulniers , Sedat Dilek , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com, Sami Tolvanen Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With CONFIG_CFI_CLANG, the compiler replaces function references with references to the CFI jump table, which confuses objtool. This change, based on Josh's initial patch [1], goes through the list of relocations and replaces jump table symbols with the actual function symbols. [1] https://lore.kernel.org/r/d743f4b36e120c06506567a9f87a062ae03da47f.1611263462.git.jpoimboe@redhat.com/ Reported-by: Sedat Dilek Suggested-by: Josh Poimboeuf Signed-off-by: Sami Tolvanen --- tools/objtool/elf.c | 48 +++++++++++++++++++++++++++++ tools/objtool/include/objtool/elf.h | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index d08f5f3670f8..5cf2c61ce7b8 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -273,6 +273,10 @@ static int read_sections(struct elf *elf) } sec->len = sec->sh.sh_size; + /* Detect -fsanitize=cfi jump table sections */ + if (!strncmp(sec->name, ".text..L.cfi.jumptable", 22)) + sec->cfi_jt = true; + list_add_tail(&sec->list, &elf->sections); elf_hash_add(elf->section_hash, &sec->hash, sec->idx); elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name)); @@ -548,6 +552,48 @@ static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsi return 0; } +static int fix_cfi_relocs(const struct elf *elf) +{ + struct section *sec, *tmpsec; + struct reloc *reloc, *tmpreloc; + + list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { + list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) { + struct symbol *sym; + struct reloc *func_reloc; + + /* + * CONFIG_CFI_CLANG replaces function relocations to refer + * to an intermediate jump table. Undo the conversion so + * objtool can make sense of things. + */ + if (!reloc->sym->sec->cfi_jt) + continue; + + if (reloc->sym->type == STT_SECTION) + sym = find_func_by_offset(reloc->sym->sec, + reloc->addend); + else + sym = reloc->sym; + + if (!sym || !sym->sec) + continue; + + /* + * The jump table immediately jumps to the actual function, + * so look up the relocation there. + */ + func_reloc = find_reloc_by_dest_range(elf, sym->sec, sym->offset, 5); + if (!func_reloc || !func_reloc->sym) + continue; + + reloc->sym = func_reloc->sym; + } + } + + return 0; +} + static int read_relocs(struct elf *elf) { struct section *sec; @@ -608,6 +654,8 @@ static int read_relocs(struct elf *elf) tot_reloc += nr_reloc; } + fix_cfi_relocs(elf); + if (stats) { printf("max_reloc: %lu\n", max_reloc); printf("tot_reloc: %lu\n", tot_reloc); diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h index 45e5ede363b0..ef19578fc5e4 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -39,7 +39,7 @@ struct section { char *name; int idx; unsigned int len; - bool changed, text, rodata, noinstr; + bool changed, text, rodata, noinstr, cfi_jt; }; struct symbol { -- 2.31.1.368.gbe11c130af-goog