3
0
mirror of https://github.com/Qortal/Brooklyn.git synced 2025-01-30 14:52:17 +00:00
Brooklyn/kernel/cfi.c

102 lines
2.2 KiB
C
Raw Normal View History

2022-04-02 13:17:33 +00:00
// SPDX-License-Identifier: GPL-2.0
/*
2022-12-14 06:30:32 +00:00
* Clang Control Flow Integrity (CFI) error handling.
2022-04-02 13:17:33 +00:00
*
2022-12-14 06:30:32 +00:00
* Copyright (C) 2022 Google LLC
2022-04-02 13:17:33 +00:00
*/
2022-12-14 06:30:32 +00:00
#include <linux/cfi.h>
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
enum bug_trap_type report_cfi_failure(struct pt_regs *regs, unsigned long addr,
unsigned long *target, u32 type)
2022-04-02 13:17:33 +00:00
{
2022-12-14 06:30:32 +00:00
if (target)
pr_err("CFI failure at %pS (target: %pS; expected type: 0x%08x)\n",
(void *)addr, (void *)*target, type);
2022-04-02 13:17:33 +00:00
else
2022-12-14 06:30:32 +00:00
pr_err("CFI failure at %pS (no target information)\n",
(void *)addr);
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
if (IS_ENABLED(CONFIG_CFI_PERMISSIVE)) {
__warn(NULL, 0, (void *)addr, 0, regs, NULL);
return BUG_TRAP_TYPE_WARN;
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
return BUG_TRAP_TYPE_BUG;
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
#ifdef CONFIG_ARCH_USES_CFI_TRAPS
static inline unsigned long trap_address(s32 *p)
2022-04-02 13:17:33 +00:00
{
2022-12-14 06:30:32 +00:00
return (unsigned long)((long)p + (long)*p);
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
static bool is_trap(unsigned long addr, s32 *start, s32 *end)
2022-04-02 13:17:33 +00:00
{
2022-12-14 06:30:32 +00:00
s32 *p;
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
for (p = start; p < end; ++p) {
if (trap_address(p) == addr)
return true;
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
return false;
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
#ifdef CONFIG_MODULES
/* Populates `kcfi_trap(_end)?` fields in `struct module`. */
void module_cfi_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
struct module *mod)
2022-04-02 13:17:33 +00:00
{
2022-12-14 06:30:32 +00:00
char *secstrings;
unsigned int i;
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
mod->kcfi_traps = NULL;
mod->kcfi_traps_end = NULL;
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
for (i = 1; i < hdr->e_shnum; i++) {
if (strcmp(secstrings + sechdrs[i].sh_name, "__kcfi_traps"))
continue;
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
mod->kcfi_traps = (s32 *)sechdrs[i].sh_addr;
mod->kcfi_traps_end = (s32 *)(sechdrs[i].sh_addr + sechdrs[i].sh_size);
break;
}
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
static bool is_module_cfi_trap(unsigned long addr)
2022-04-02 13:17:33 +00:00
{
struct module *mod;
2022-12-14 06:30:32 +00:00
bool found = false;
2022-04-02 13:17:33 +00:00
rcu_read_lock_sched_notrace();
2022-12-14 06:30:32 +00:00
mod = __module_address(addr);
if (mod)
found = is_trap(addr, mod->kcfi_traps, mod->kcfi_traps_end);
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
rcu_read_unlock_sched_notrace();
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
return found;
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
#else /* CONFIG_MODULES */
static inline bool is_module_cfi_trap(unsigned long addr)
2022-04-02 13:17:33 +00:00
{
2022-12-14 06:30:32 +00:00
return false;
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
#endif /* CONFIG_MODULES */
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
extern s32 __start___kcfi_traps[];
extern s32 __stop___kcfi_traps[];
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
bool is_cfi_trap(unsigned long addr)
2022-04-02 13:17:33 +00:00
{
2022-12-14 06:30:32 +00:00
if (is_trap(addr, __start___kcfi_traps, __stop___kcfi_traps))
return true;
2022-04-02 13:17:33 +00:00
2022-12-14 06:30:32 +00:00
return is_module_cfi_trap(addr);
2022-04-02 13:17:33 +00:00
}
2022-12-14 06:30:32 +00:00
#endif /* CONFIG_ARCH_USES_CFI_TRAPS */