extable.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Rewritten by Rusty Russell, on the backs of many others...
  2. Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <linux/ftrace.h>
  16. #include <linux/memory.h>
  17. #include <linux/extable.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/init.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/filter.h>
  23. #include <asm/sections.h>
  24. #include <linux/uaccess.h>
  25. /*
  26. * mutex protecting text section modification (dynamic code patching).
  27. * some users need to sleep (allocating memory...) while they hold this lock.
  28. *
  29. * NOT exported to modules - patching kernel text is a really delicate matter.
  30. */
  31. DEFINE_MUTEX(text_mutex);
  32. extern struct exception_table_entry __start___ex_table[];
  33. extern struct exception_table_entry __stop___ex_table[];
  34. /* Cleared by build time tools if the table is already sorted. */
  35. u32 __initdata __visible main_extable_sort_needed = 1;
  36. /* Sort the kernel's built-in exception table */
  37. void __init sort_main_extable(void)
  38. {
  39. if (main_extable_sort_needed && __stop___ex_table > __start___ex_table) {
  40. pr_notice("Sorting __ex_table...\n");
  41. sort_extable(__start___ex_table, __stop___ex_table);
  42. }
  43. }
  44. /* Given an address, look for it in the exception tables. */
  45. const struct exception_table_entry *search_exception_tables(unsigned long addr)
  46. {
  47. const struct exception_table_entry *e;
  48. e = search_extable(__start___ex_table,
  49. __stop___ex_table - __start___ex_table, addr);
  50. if (!e)
  51. e = search_module_extables(addr);
  52. return e;
  53. }
  54. static inline int init_kernel_text(unsigned long addr)
  55. {
  56. if (addr >= (unsigned long)_sinittext &&
  57. addr < (unsigned long)_einittext)
  58. return 1;
  59. return 0;
  60. }
  61. int notrace core_kernel_text(unsigned long addr)
  62. {
  63. if (addr >= (unsigned long)_stext &&
  64. addr < (unsigned long)_etext)
  65. return 1;
  66. if (system_state < SYSTEM_RUNNING &&
  67. init_kernel_text(addr))
  68. return 1;
  69. return 0;
  70. }
  71. /**
  72. * core_kernel_data - tell if addr points to kernel data
  73. * @addr: address to test
  74. *
  75. * Returns true if @addr passed in is from the core kernel data
  76. * section.
  77. *
  78. * Note: On some archs it may return true for core RODATA, and false
  79. * for others. But will always be true for core RW data.
  80. */
  81. int core_kernel_data(unsigned long addr)
  82. {
  83. if (addr >= (unsigned long)_sdata &&
  84. addr < (unsigned long)_edata)
  85. return 1;
  86. return 0;
  87. }
  88. int __kernel_text_address(unsigned long addr)
  89. {
  90. if (kernel_text_address(addr))
  91. return 1;
  92. /*
  93. * There might be init symbols in saved stacktraces.
  94. * Give those symbols a chance to be printed in
  95. * backtraces (such as lockdep traces).
  96. *
  97. * Since we are after the module-symbols check, there's
  98. * no danger of address overlap:
  99. */
  100. if (init_kernel_text(addr))
  101. return 1;
  102. return 0;
  103. }
  104. int kernel_text_address(unsigned long addr)
  105. {
  106. bool no_rcu;
  107. int ret = 1;
  108. if (core_kernel_text(addr))
  109. return 1;
  110. /*
  111. * If a stack dump happens while RCU is not watching, then
  112. * RCU needs to be notified that it requires to start
  113. * watching again. This can happen either by tracing that
  114. * triggers a stack trace, or a WARN() that happens during
  115. * coming back from idle, or cpu on or offlining.
  116. *
  117. * is_module_text_address() as well as the kprobe slots
  118. * and is_bpf_text_address() require RCU to be watching.
  119. */
  120. no_rcu = !rcu_is_watching();
  121. /* Treat this like an NMI as it can happen anywhere */
  122. if (no_rcu)
  123. rcu_nmi_enter();
  124. if (is_module_text_address(addr))
  125. goto out;
  126. if (is_ftrace_trampoline(addr))
  127. goto out;
  128. if (is_kprobe_optinsn_slot(addr) || is_kprobe_insn_slot(addr))
  129. goto out;
  130. if (is_bpf_text_address(addr))
  131. goto out;
  132. ret = 0;
  133. out:
  134. if (no_rcu)
  135. rcu_nmi_exit();
  136. return ret;
  137. }
  138. /*
  139. * On some architectures (PPC64, IA64) function pointers
  140. * are actually only tokens to some data that then holds the
  141. * real function address. As a result, to find if a function
  142. * pointer is part of the kernel text, we need to do some
  143. * special dereferencing first.
  144. */
  145. int func_ptr_is_kernel_text(void *ptr)
  146. {
  147. unsigned long addr;
  148. addr = (unsigned long) dereference_function_descriptor(ptr);
  149. if (core_kernel_text(addr))
  150. return 1;
  151. return is_module_text_address(addr);
  152. }