^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Rewritten by Rusty Russell, on the backs of many others...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/extable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * mutex protecting text section modification (dynamic code patching).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * some users need to sleep (allocating memory...) while they hold this lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Note: Also protects SMP-alternatives modification on x86.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * NOT exported to modules - patching kernel text is a really delicate matter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) DEFINE_MUTEX(text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) extern struct exception_table_entry __start___ex_table[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) extern struct exception_table_entry __stop___ex_table[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* Cleared by build time tools if the table is already sorted. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u32 __initdata __visible main_extable_sort_needed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Sort the kernel's built-in exception table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) void __init sort_main_extable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (main_extable_sort_needed &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) &__stop___ex_table > &__start___ex_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) pr_notice("Sorting __ex_table...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) sort_extable(__start___ex_table, __stop___ex_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Given an address, look for it in the kernel exception table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) const
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct exception_table_entry *search_kernel_exception_table(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return search_extable(__start___ex_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) __stop___ex_table - __start___ex_table, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Given an address, look for it in the exception tables. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) const struct exception_table_entry *search_exception_tables(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) const struct exception_table_entry *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) e = search_kernel_exception_table(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) e = search_module_extables(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) e = search_bpf_extables(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int init_kernel_text(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (addr >= (unsigned long)_sinittext &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) addr < (unsigned long)_einittext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int notrace core_kernel_text(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (addr >= (unsigned long)_stext &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) addr < (unsigned long)_etext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (system_state < SYSTEM_RUNNING &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) init_kernel_text(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * core_kernel_data - tell if addr points to kernel data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @addr: address to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Returns true if @addr passed in is from the core kernel data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Note: On some archs it may return true for core RODATA, and false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * for others. But will always be true for core RW data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int core_kernel_data(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (addr >= (unsigned long)_sdata &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) addr < (unsigned long)_edata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int __kernel_text_address(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (kernel_text_address(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * There might be init symbols in saved stacktraces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * Give those symbols a chance to be printed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * backtraces (such as lockdep traces).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * Since we are after the module-symbols check, there's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * no danger of address overlap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (init_kernel_text(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int kernel_text_address(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) bool no_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (core_kernel_text(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * If a stack dump happens while RCU is not watching, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * RCU needs to be notified that it requires to start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * watching again. This can happen either by tracing that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * triggers a stack trace, or a WARN() that happens during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * coming back from idle, or cpu on or offlining.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * is_module_text_address() as well as the kprobe slots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * is_bpf_text_address() and is_bpf_image_address require
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * RCU to be watching.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) no_rcu = !rcu_is_watching();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Treat this like an NMI as it can happen anywhere */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (no_rcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) rcu_nmi_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (is_module_text_address(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (is_ftrace_trampoline(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (is_kprobe_optinsn_slot(addr) || is_kprobe_insn_slot(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (is_bpf_text_address(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (no_rcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) rcu_nmi_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * On some architectures (PPC64, IA64) function pointers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * are actually only tokens to some data that then holds the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * real function address. As a result, to find if a function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * pointer is part of the kernel text, we need to do some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * special dereferencing first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int func_ptr_is_kernel_text(void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) addr = (unsigned long) dereference_function_descriptor(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (core_kernel_text(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return is_module_text_address(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }