ep_remove() (via ep_remove_file()) cleared file->f_ep under
file->f_lock but then kept using @file inside the critical section
(is_file_epoll(), hlist_del_rcu() through the head, spin_unlock).
A concurrent __fput() taking the eventpoll_release() fastpath in
that window observed the transient NULL, skipped
eventpoll_release_file() and ran to f_op->release / file_free().
For the epoll-watches-epoll case, f_op->release is
ep_eventpoll_release() -> ep_clear_and_put() -> ep_free(), which
kfree()s the watched struct eventpoll. Its embedded ->refs
hlist_head is exactly where epi->fllink.pprev points, so the
subsequent hlist_del_rcu()'s "*pprev = next" scribbles into freed
kmalloc-192 memory.
In addition, struct file is SLAB_TYPESAFE_BY_RCU, so the slot
backing @file could be recycled by alloc_empty_file() --
reinitializing f_lock and f_ep -- while ep_remove() is still
nominally inside that lock. The upshot is an attacker-controllable
kmem_cache_free() against the wrong slab cache.
Pin @file via epi_fget() at the top of ep_remove() and gate the
critical section on the pin succeeding. With the pin held @file
cannot reach refcount zero, which holds __fput() off and
transitively keeps the watched struct eventpoll alive across the
hlist_del_rcu() and the f_lock use, closing both UAFs.
If the pin fails @file has already reached refcount zero and its
__fput() is in flight. Because we bailed before clearing f_ep,
that path takes the eventpoll_release() slow path into
eventpoll_release_file() and blocks on ep->mtx until the waiter
side's ep_clear_and_put() drops it. The bailed epi's share of
ep->refcount stays intact, so the trailing ep_refcount_dec_and_test()
in ep_clear_and_put() cannot free the eventpoll out from under
eventpoll_release_file(); the orphaned epi is then cleaned up
there.
A successful pin also proves we are not racing
eventpoll_release_file() on this epi, so drop the now-redundant
re-check of epi->dying under f_lock. The cheap lockless
READ_ONCE(epi->dying) fast-path bailout stays.
Bug: 509838226
Fixes: 58c9b016e128 ("epoll: use refcount to reduce ep_mutex contention")
Reported-by: Jaeyoung Chung <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Christian Brauner (Amutable) <[email protected]>
(cherry picked from commit a6dc643c69311677c574a0f17a3f4d66a5f3744b)
Signed-off-by: Lee Jones <[email protected]>
Change-Id: I030311c67841921a1309e94b8c9f798d73bd4a74
116 lines
2.8 KiB
C
116 lines
2.8 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Clang Control Flow Integrity (CFI) error handling.
|
|
*
|
|
* Copyright (C) 2022 Google LLC
|
|
*/
|
|
|
|
#include <linux/bpf.h>
|
|
#include <linux/cfi_types.h>
|
|
#include <linux/cfi.h>
|
|
|
|
enum bug_trap_type report_cfi_failure(struct pt_regs *regs, unsigned long addr,
|
|
unsigned long *target, u32 type)
|
|
{
|
|
if (target)
|
|
pr_err("CFI failure at %pS (target: %pS; expected type: 0x%08x)\n",
|
|
(void *)addr, (void *)*target, type);
|
|
else
|
|
pr_err("CFI failure at %pS (no target information)\n",
|
|
(void *)addr);
|
|
|
|
if (IS_ENABLED(CONFIG_CFI_PERMISSIVE)) {
|
|
__warn(NULL, 0, (void *)addr, 0, regs, NULL);
|
|
return BUG_TRAP_TYPE_WARN;
|
|
}
|
|
|
|
return BUG_TRAP_TYPE_BUG;
|
|
}
|
|
|
|
/*
|
|
* Declare two non-existent functions with types that match bpf_func_t and
|
|
* bpf_callback_t pointers, and use DEFINE_CFI_TYPE to define type hash
|
|
* variables for each function type. The cfi_bpf_* variables are used by
|
|
* arch-specific BPF JIT implementations to ensure indirectly callable JIT
|
|
* code has matching CFI type hashes.
|
|
*/
|
|
extern typeof(*(bpf_func_t)0) __bpf_prog_runX;
|
|
DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX);
|
|
|
|
extern typeof(*(bpf_callback_t)0) __bpf_callback_fn;
|
|
DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn);
|
|
|
|
#ifdef CONFIG_ARCH_USES_CFI_TRAPS
|
|
static inline unsigned long trap_address(s32 *p)
|
|
{
|
|
return (unsigned long)((long)p + (long)*p);
|
|
}
|
|
|
|
static bool is_trap(unsigned long addr, s32 *start, s32 *end)
|
|
{
|
|
s32 *p;
|
|
|
|
for (p = start; p < end; ++p) {
|
|
if (trap_address(p) == addr)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
#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)
|
|
{
|
|
char *secstrings;
|
|
unsigned int i;
|
|
|
|
mod->kcfi_traps = NULL;
|
|
mod->kcfi_traps_end = NULL;
|
|
|
|
secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
|
|
|
|
for (i = 1; i < hdr->e_shnum; i++) {
|
|
if (strcmp(secstrings + sechdrs[i].sh_name, "__kcfi_traps"))
|
|
continue;
|
|
|
|
mod->kcfi_traps = (s32 *)sechdrs[i].sh_addr;
|
|
mod->kcfi_traps_end = (s32 *)(sechdrs[i].sh_addr + sechdrs[i].sh_size);
|
|
break;
|
|
}
|
|
}
|
|
|
|
static bool is_module_cfi_trap(unsigned long addr)
|
|
{
|
|
struct module *mod;
|
|
bool found = false;
|
|
|
|
rcu_read_lock_sched_notrace();
|
|
|
|
mod = __module_address(addr);
|
|
if (mod)
|
|
found = is_trap(addr, mod->kcfi_traps, mod->kcfi_traps_end);
|
|
|
|
rcu_read_unlock_sched_notrace();
|
|
|
|
return found;
|
|
}
|
|
#else /* CONFIG_MODULES */
|
|
static inline bool is_module_cfi_trap(unsigned long addr)
|
|
{
|
|
return false;
|
|
}
|
|
#endif /* CONFIG_MODULES */
|
|
|
|
extern s32 __start___kcfi_traps[];
|
|
extern s32 __stop___kcfi_traps[];
|
|
|
|
bool is_cfi_trap(unsigned long addr)
|
|
{
|
|
if (is_trap(addr, __start___kcfi_traps, __stop___kcfi_traps))
|
|
return true;
|
|
|
|
return is_module_cfi_trap(addr);
|
|
}
|
|
#endif /* CONFIG_ARCH_USES_CFI_TRAPS */
|