linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nick Desaulniers <ndesaulniers@google.com>
To: Sami Tolvanen <samitolvanen@google.com>
Cc: x86@kernel.org, Kees Cook <keescook@chromium.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Sedat Dilek <sedat.dilek@gmail.com>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	llvm@lists.linux.dev
Subject: Re: [PATCH v4 01/15] objtool: Add CONFIG_CFI_CLANG support
Date: Thu, 30 Sep 2021 11:40:32 -0700	[thread overview]
Message-ID: <CAKwvOdm1QZxZYzyc0RR0ry530Hqezca-_Xv0t1e0oa7rN21rjg@mail.gmail.com> (raw)
In-Reply-To: <20210930180531.1190642-2-samitolvanen@google.com>

On Thu, Sep 30, 2021 at 11:05 AM Sami Tolvanen <samitolvanen@google.com> wrote:
>
> 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 <sedat.dilek@gmail.com>
> Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
> Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
> ---
>  tools/objtool/arch/x86/decode.c      | 17 ++++++++++
>  tools/objtool/elf.c                  | 51 ++++++++++++++++++++++++++++
>  tools/objtool/include/objtool/arch.h |  3 ++
>  tools/objtool/include/objtool/elf.h  |  2 +-
>  4 files changed, 72 insertions(+), 1 deletion(-)
>
> diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
> index 3172983bf808..9b043220b0af 100644
> --- a/tools/objtool/arch/x86/decode.c
> +++ b/tools/objtool/arch/x86/decode.c
> @@ -63,6 +63,23 @@ bool arch_callee_saved_reg(unsigned char reg)
>         }
>  }
>
> +unsigned long arch_cfi_section_reloc_offset(struct reloc *reloc)
> +{
> +       if (!reloc->addend)
> +               return 0;
> +
> +       if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32)
> +               return reloc->addend + 4;
> +
> +       return reloc->addend;
> +}
> +
> +unsigned long arch_cfi_jump_reloc_offset(unsigned long offset)
> +{
> +       /* offset to the relocation in a jmp instruction */

Thanks for the added comment. ;)

Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> +       return offset + 1;
> +}
> +
>  unsigned long arch_dest_reloc_offset(int addend)
>  {
>         return addend + 4;
> diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
> index 8676c7598728..05a5f51aad2c 100644
> --- a/tools/objtool/elf.c
> +++ b/tools/objtool/elf.c
> @@ -18,6 +18,7 @@
>  #include <errno.h>
>  #include <objtool/builtin.h>
>
> +#include <objtool/arch.h>
>  #include <objtool/elf.h>
>  #include <objtool/warn.h>
>
> @@ -291,6 +292,10 @@ static int read_sections(struct elf *elf)
>                 if (sec->sh.sh_flags & SHF_EXECINSTR)
>                         elf->text_size += sec->len;
>
> +               /* 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(section, &sec->hash, sec->idx);
>                 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
> @@ -576,6 +581,49 @@ static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsi
>         return 0;
>  }
>
> +/*
> + * CONFIG_CFI_CLANG replaces function relocations to refer to an intermediate
> + * jump table. Undo the conversion so objtool can make sense of things.
> + */
> +static int fix_cfi_relocs(const struct elf *elf)
> +{
> +       struct section *sec;
> +       struct reloc *reloc;
> +
> +       list_for_each_entry(sec, &elf->sections, list) {
> +               list_for_each_entry(reloc, &sec->reloc_list, list) {
> +                       struct reloc *cfi_reloc;
> +                       unsigned long offset;
> +
> +                       if (!reloc->sym->sec->cfi_jt)
> +                               continue;
> +
> +                       if (reloc->sym->type == STT_SECTION)
> +                               offset = arch_cfi_section_reloc_offset(reloc);
> +                       else
> +                               offset = reloc->sym->offset;
> +
> +                       /*
> +                        * The jump table immediately jumps to the actual function,
> +                        * so look up the relocation there.
> +                        */
> +                       offset = arch_cfi_jump_reloc_offset(offset);
> +                       cfi_reloc = find_reloc_by_dest(elf, reloc->sym->sec, offset);
> +
> +                       if (!cfi_reloc || !cfi_reloc->sym) {
> +                               WARN("can't find a CFI jump table relocation at %s+0x%lx",
> +                                       reloc->sym->sec->name, offset);
> +                               return -1;
> +                       }
> +
> +                       reloc->sym = cfi_reloc->sym;
> +                       reloc->addend = 0;
> +               }
> +       }
> +
> +       return 0;
> +}
> +
>  static int read_relocs(struct elf *elf)
>  {
>         struct section *sec;
> @@ -639,6 +687,9 @@ static int read_relocs(struct elf *elf)
>                 tot_reloc += nr_reloc;
>         }
>
> +       if (fix_cfi_relocs(elf))
> +               return -1;
> +
>         if (stats) {
>                 printf("max_reloc: %lu\n", max_reloc);
>                 printf("tot_reloc: %lu\n", tot_reloc);
> diff --git a/tools/objtool/include/objtool/arch.h b/tools/objtool/include/objtool/arch.h
> index 589ff58426ab..93bde8aaf2e3 100644
> --- a/tools/objtool/include/objtool/arch.h
> +++ b/tools/objtool/include/objtool/arch.h
> @@ -81,6 +81,9 @@ unsigned long arch_jump_destination(struct instruction *insn);
>
>  unsigned long arch_dest_reloc_offset(int addend);
>
> +unsigned long arch_cfi_section_reloc_offset(struct reloc *reloc);
> +unsigned long arch_cfi_jump_reloc_offset(unsigned long offset);
> +
>  const char *arch_nop_insn(int len);
>  const char *arch_ret_insn(int len);
>
> diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
> index c3857fadee7a..e8d838217c77 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.33.0.800.g4c38ced690-goog
>


-- 
Thanks,
~Nick Desaulniers

  reply	other threads:[~2021-09-30 18:40 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-30 18:05 [PATCH v4 00/15] x86: Add support for Clang CFI Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 01/15] objtool: Add CONFIG_CFI_CLANG support Sami Tolvanen
2021-09-30 18:40   ` Nick Desaulniers [this message]
2021-10-06  3:36   ` Josh Poimboeuf
2021-10-06 16:18     ` Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 02/15] objtool: Add ASM_STACK_FRAME_NON_STANDARD Sami Tolvanen
2021-10-06  3:37   ` Josh Poimboeuf
2021-09-30 18:05 ` [PATCH v4 03/15] linkage: Add DECLARE_ASM_FUNC_SYMBOL Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 04/15] cfi: Add DEFINE_CFI_IMMEDIATE_RETURN_STUB Sami Tolvanen
2021-09-30 18:50   ` Nick Desaulniers
2021-10-01 20:07     ` Sami Tolvanen
2021-10-04 13:50   ` Peter Zijlstra
2021-10-04 19:10     ` Sami Tolvanen
2021-10-05  6:59       ` Peter Zijlstra
2021-10-05 20:29         ` Sami Tolvanen
2021-10-05 20:56           ` Peter Zijlstra
2021-10-05 21:53             ` Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 05/15] tracepoint: Exclude tp_stub_func from CFI checking Sami Tolvanen
2021-09-30 18:50   ` Nick Desaulniers
2021-10-01 20:08     ` Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 06/15] ftrace: Use an opaque type for functions not callable from C Sami Tolvanen
2021-10-06  3:29   ` Josh Poimboeuf
2021-10-06 13:02     ` Steven Rostedt
2021-10-06 13:54       ` Josh Poimboeuf
2021-10-06 14:16         ` Steven Rostedt
2021-10-06 16:31       ` Sami Tolvanen
2021-10-06 16:58         ` Steven Rostedt
2021-10-06 17:45           ` Sami Tolvanen
2021-10-06 20:43             ` Josh Poimboeuf
2021-10-06 21:10               ` Steven Rostedt
2021-10-06 21:23                 ` Josh Poimboeuf
2021-10-06 23:14                   ` Sami Tolvanen
2021-10-07  0:56                     ` Steven Rostedt
2021-09-30 18:05 ` [PATCH v4 07/15] lkdtm: Disable UNSET_SMEP with CFI Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 08/15] lkdtm: Use an opaque type for lkdtm_rodata_do_nothing Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 09/15] x86: Use an opaque type for functions not callable from C Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 10/15] x86/purgatory: Disable CFI Sami Tolvanen
2021-09-30 19:05   ` Nick Desaulniers
2021-09-30 18:05 ` [PATCH v4 11/15] x86, relocs: Ignore __typeid__ relocations Sami Tolvanen
2021-10-06  3:31   ` Josh Poimboeuf
2021-10-06 16:17     ` Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 12/15] x86, module: " Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 13/15] x86, cpu: Use LTO for cpu.c with CFI Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 14/15] x86, kprobes: Fix optprobe_template_func type mismatch Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 15/15] x86, build: Allow CONFIG_CFI_CLANG to be selected Sami Tolvanen
2021-09-30 18:38 ` [PATCH v4 00/15] x86: Add support for Clang CFI Nick Desaulniers
2021-10-05 20:36 ` Josh Poimboeuf
2021-10-05 21:52   ` Sami Tolvanen
2021-10-06  2:42     ` Josh Poimboeuf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAKwvOdm1QZxZYzyc0RR0ry530Hqezca-_Xv0t1e0oa7rN21rjg@mail.gmail.com \
    --to=ndesaulniers@google.com \
    --cc=jpoimboe@redhat.com \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=nathan@kernel.org \
    --cc=peterz@infradead.org \
    --cc=samitolvanen@google.com \
    --cc=sedat.dilek@gmail.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).