From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEF77C433E6 for ; Thu, 21 Jan 2021 21:36:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B36B4206BE for ; Thu, 21 Jan 2021 21:36:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726632AbhAUVgX (ORCPT ); Thu, 21 Jan 2021 16:36:23 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:21565 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726671AbhAUVbn (ORCPT ); Thu, 21 Jan 2021 16:31:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611264617; 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=k08HNHiRc4Ee5BfnUqUAZvZx09rIAfRRg0UVTMRN5co=; b=YXfV2fXW/HxFROkghXTLaKixch8lRV/Wd6sPsLwTS58f5AhxqshAjkHdt4Dah3E8CphLQH GfxW0JpvH39NhyvGUSxiPSiBeRbkEYeW36nyI3FJ78Udi/1XIZQHAIUXqPXoNeIWU2Zits hNVJPKetpCb33bFnM8o+bR/+RYSNwrs= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-193-uDN5AjirMou5u4YjXGdbTw-1; Thu, 21 Jan 2021 16:30:13 -0500 X-MC-Unique: uDN5AjirMou5u4YjXGdbTw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 148111842140; Thu, 21 Jan 2021 21:30:12 +0000 (UTC) Received: from treble.redhat.com (ovpn-116-102.rdu2.redhat.com [10.10.116.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0DF6119C59; Thu, 21 Jan 2021 21:30:09 +0000 (UTC) From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Sami Tolvanen , Sedat Dilek , Kees Cook , Nick Desaulniers , clang-built-linux@googlegroups.com, Miroslav Benes Subject: [PATCH v2 11/20] objtool: Add CONFIG_CFI_CLANG support Date: Thu, 21 Jan 2021 15:29:27 -0600 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The upcoming CONFIG_CFI_CLANG support uses -fsanitize=cfi, the non-canonical version of which hijacks function entry by changing function relocation references to point to an intermediary jump table. For example: Relocation section '.rela.discard.func_stack_frame_non_standard' at offset 0x37e018 contains 6 entries: Offset Info Type Symbol's Value Symbol's Name + Addend 0000000000000000 0002944700000002 R_X86_64_PC32 00000000000023f0 do_suspend_lowlevel + 0 0000000000000008 0003c11900000001 R_X86_64_64 0000000000000008 xen_cpuid$e69bc59f4fade3b6f2b579b3934137df.cfi_jt + 0 0000000000000010 0003980900000001 R_X86_64_64 0000000000000060 machine_real_restart.cfi_jt + 0 0000000000000018 0003962b00000001 R_X86_64_64 0000000000000e18 kretprobe_trampoline.cfi_jt + 0 0000000000000020 000028f300000001 R_X86_64_64 0000000000000000 .rodata + 12 0000000000000028 000349f400000001 R_X86_64_64 0000000000000018 __crash_kexec.cfi_jt + 0 0000000000000060 : 60: e9 00 00 00 00 jmpq 65 61: R_X86_64_PLT32 machine_real_restart-0x4 65: cc int3 66: cc int3 67: cc int3 This breaks objtool vmlinux validation in many ways, including static call site detection and the STACK_FRAME_NON_STANDARD() macro. Fix it by converting those relocations' symbol references back to their original non-jump-table versions. Note this doesn't change the actual relocations in the object itself, it just changes objtool's view of them. Reported-by: Sedat Dilek Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 28 ++++++++++++++++++++++++++++ tools/objtool/include/objtool/elf.h | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index e7d9c29fe78c..18b6fc4ca1f8 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -382,6 +382,11 @@ static int read_sections(struct elf *elf) } sec->len = sec->sh.sh_size; + /* Detect -fsanitize=cfi related sections */ + if (!strcmp(sec->name, ".text.__cfi_check") || + !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)); @@ -613,6 +618,29 @@ static int read_relocs(struct elf *elf) return -1; } + /* + * Deal with -fsanitize=cfi (CONFIG_CFI_CLANG), which + * hijacks function entry by arbitrarily changing a lot + * of relocation symbol references to refer to an + * intermediate jump table. Undo that conversion so + * objtool can make sense of things. + */ + if (reloc->sym->sec->cfi_jt) { + struct symbol *func, *sym; + + if (reloc->sym->type == STT_SECTION) + sym = find_func_by_offset(reloc->sym->sec, + reloc->addend); + else + sym = reloc->sym; + + if (find_unsuffixed_func(elf, sym, ".cfi_jt", &func)) + return -1; + + if (func) + reloc->sym = func; + } + elf_add_reloc(elf, reloc); nr_reloc++; } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h index e6890cc70a25..bcc524d73f51 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.29.2