mirror of
https://github.com/torvalds/linux.git
synced 2026-01-25 15:03:52 +08:00
For X86_FEATURE_SMAP alternatives which replace NOP with STAC or CLAC,
uaccess validation skips the NOP branch to avoid following impossible
code paths, e.g. where a STAC would be patched but a CLAC wouldn't.
However, it's not safe to assume an X86_FEATURE_SMAP alternative is
patching STAC/CLAC. There can be other alternatives, like
static_cpu_has(), where both branches need to be validated.
Fix that by repurposing ANNOTATE_IGNORE_ALTERNATIVE for skipping either
original instructions or new ones. This is a more generic approach
which enables the removal of the feature checking hacks and the
insn->ignore bit.
Fixes the following warnings:
arch/x86/mm/fault.o: warning: objtool: do_user_addr_fault+0x8ec: __stack_chk_fail() missing __noreturn in .c/.h or NORETURN() in noreturns.h
arch/x86/mm/fault.o: warning: objtool: do_user_addr_fault+0x8f1: unreachable instruction
[ mingo: Fix up conflicts with recent x86 changes. ]
Fixes: ea24213d80 ("objtool: Add UACCESS validation")
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: https://lore.kernel.org/r/de0621ca242130156a55d5d74fed86994dfa4c9c.1742852846.git.jpoimboe@kernel.org
Closes: https://lore.kernel.org/oe-kbuild-all/202503181736.zkZUBv4N-lkp@intel.com/
175 lines
3.9 KiB
C
175 lines
3.9 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/*
|
|
* Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
|
|
*/
|
|
|
|
/*
|
|
* This file reads all the special sections which have alternate instructions
|
|
* which can be patched in or redirected to at runtime.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <arch/special.h>
|
|
#include <objtool/builtin.h>
|
|
#include <objtool/special.h>
|
|
#include <objtool/warn.h>
|
|
#include <objtool/endianness.h>
|
|
|
|
struct special_entry {
|
|
const char *sec;
|
|
bool group, jump_or_nop;
|
|
unsigned char size, orig, new;
|
|
unsigned char orig_len, new_len; /* group only */
|
|
unsigned char feature; /* ALTERNATIVE macro CPU feature */
|
|
unsigned char key; /* jump_label key */
|
|
};
|
|
|
|
static const struct special_entry entries[] = {
|
|
{
|
|
.sec = ".altinstructions",
|
|
.group = true,
|
|
.size = ALT_ENTRY_SIZE,
|
|
.orig = ALT_ORIG_OFFSET,
|
|
.orig_len = ALT_ORIG_LEN_OFFSET,
|
|
.new = ALT_NEW_OFFSET,
|
|
.new_len = ALT_NEW_LEN_OFFSET,
|
|
.feature = ALT_FEATURE_OFFSET,
|
|
},
|
|
{
|
|
.sec = "__jump_table",
|
|
.jump_or_nop = true,
|
|
.size = JUMP_ENTRY_SIZE,
|
|
.orig = JUMP_ORIG_OFFSET,
|
|
.new = JUMP_NEW_OFFSET,
|
|
.key = JUMP_KEY_OFFSET,
|
|
},
|
|
{
|
|
.sec = "__ex_table",
|
|
.size = EX_ENTRY_SIZE,
|
|
.orig = EX_ORIG_OFFSET,
|
|
.new = EX_NEW_OFFSET,
|
|
},
|
|
{},
|
|
};
|
|
|
|
void __weak arch_handle_alternative(struct special_alt *alt)
|
|
{
|
|
}
|
|
|
|
static void reloc_to_sec_off(struct reloc *reloc, struct section **sec,
|
|
unsigned long *off)
|
|
{
|
|
*sec = reloc->sym->sec;
|
|
*off = reloc->sym->offset + reloc_addend(reloc);
|
|
}
|
|
|
|
static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
|
|
struct section *sec, int idx,
|
|
struct special_alt *alt)
|
|
{
|
|
struct reloc *orig_reloc, *new_reloc;
|
|
unsigned long offset;
|
|
|
|
offset = idx * entry->size;
|
|
|
|
alt->group = entry->group;
|
|
alt->jump_or_nop = entry->jump_or_nop;
|
|
|
|
if (alt->group) {
|
|
alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
|
|
entry->orig_len);
|
|
alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
|
|
entry->new_len);
|
|
}
|
|
|
|
orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
|
|
if (!orig_reloc) {
|
|
WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
|
|
return -1;
|
|
}
|
|
|
|
reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
|
|
|
|
arch_handle_alternative(alt);
|
|
|
|
if (!entry->group || alt->new_len) {
|
|
new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
|
|
if (!new_reloc) {
|
|
WARN_FUNC("can't find new reloc",
|
|
sec, offset + entry->new);
|
|
return -1;
|
|
}
|
|
|
|
reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
|
|
|
|
/* _ASM_EXTABLE_EX hack */
|
|
if (alt->new_off >= 0x7ffffff0)
|
|
alt->new_off -= 0x7ffffff0;
|
|
}
|
|
|
|
if (entry->key) {
|
|
struct reloc *key_reloc;
|
|
|
|
key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key);
|
|
if (!key_reloc) {
|
|
WARN_FUNC("can't find key reloc",
|
|
sec, offset + entry->key);
|
|
return -1;
|
|
}
|
|
alt->key_addend = reloc_addend(key_reloc);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Read all the special sections and create a list of special_alt structs which
|
|
* describe all the alternate instructions which can be patched in or
|
|
* redirected to at runtime.
|
|
*/
|
|
int special_get_alts(struct elf *elf, struct list_head *alts)
|
|
{
|
|
const struct special_entry *entry;
|
|
struct section *sec;
|
|
unsigned int nr_entries;
|
|
struct special_alt *alt;
|
|
int idx, ret;
|
|
|
|
INIT_LIST_HEAD(alts);
|
|
|
|
for (entry = entries; entry->sec; entry++) {
|
|
sec = find_section_by_name(elf, entry->sec);
|
|
if (!sec)
|
|
continue;
|
|
|
|
if (sec->sh.sh_size % entry->size != 0) {
|
|
WARN("%s size not a multiple of %d",
|
|
sec->name, entry->size);
|
|
return -1;
|
|
}
|
|
|
|
nr_entries = sec->sh.sh_size / entry->size;
|
|
|
|
for (idx = 0; idx < nr_entries; idx++) {
|
|
alt = malloc(sizeof(*alt));
|
|
if (!alt) {
|
|
WARN("malloc failed");
|
|
return -1;
|
|
}
|
|
memset(alt, 0, sizeof(*alt));
|
|
|
|
ret = get_alt_entry(elf, entry, sec, idx, alt);
|
|
if (ret > 0)
|
|
continue;
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
list_add_tail(&alt->list, alts);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|