xref: /linux/lib/bug.c (revision 0bcb517f0a70ae4fc59ce87bd27573043416aa94)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3   Generic support for BUG()
4 
5   This respects the following config options:
6 
7   CONFIG_BUG - emit BUG traps.  Nothing happens without this.
8   CONFIG_GENERIC_BUG - enable this code.
9   CONFIG_GENERIC_BUG_RELATIVE_POINTERS - use 32-bit relative pointers for bug_addr and file
10   CONFIG_DEBUG_BUGVERBOSE - emit full file+line information for each BUG
11 
12   CONFIG_BUG and CONFIG_DEBUG_BUGVERBOSE are potentially user-settable
13   (though they're generally always on).
14 
15   CONFIG_GENERIC_BUG is set by each architecture using this code.
16 
17   To use this, your architecture must:
18 
19   1. Set up the config options:
20      - Enable CONFIG_GENERIC_BUG if CONFIG_BUG
21 
22   2. Implement BUG (and optionally BUG_ON, WARN, WARN_ON)
23      - Define HAVE_ARCH_BUG
24      - Implement BUG() to generate a faulting instruction
25      - NOTE: struct bug_entry does not have "file" or "line" entries
26        when CONFIG_DEBUG_BUGVERBOSE is not enabled, so you must generate
27        the values accordingly.
28 
29   3. Implement the trap
30      - In the illegal instruction trap handler (typically), verify
31        that the fault was in kernel mode, and call report_bug()
32      - report_bug() will return whether it was a false alarm, a warning,
33        or an actual bug.
34      - You must implement the is_valid_bugaddr(bugaddr) callback which
35        returns true if the eip is a real kernel address, and it points
36        to the expected BUG trap instruction.
37 
38     Jeremy Fitzhardinge <jeremy@goop.org> 2006
39  */
40 
41 #define pr_fmt(fmt) fmt
42 
43 #include <linux/list.h>
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/bug.h>
47 #include <linux/sched.h>
48 #include <linux/rculist.h>
49 #include <linux/ftrace.h>
50 #include <linux/context_tracking.h>
51 
52 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
53 
bug_addr(const struct bug_entry * bug)54 static inline unsigned long bug_addr(const struct bug_entry *bug)
55 {
56 #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
57 	return (unsigned long)&bug->bug_addr_disp + bug->bug_addr_disp;
58 #else
59 	return bug->bug_addr;
60 #endif
61 }
62 
63 #ifdef CONFIG_MODULES
64 /* Updates are protected by module mutex */
65 static LIST_HEAD(module_bug_list);
66 
module_find_bug(unsigned long bugaddr)67 static struct bug_entry *module_find_bug(unsigned long bugaddr)
68 {
69 	struct bug_entry *bug;
70 	struct module *mod;
71 
72 	guard(rcu)();
73 	list_for_each_entry_rcu(mod, &module_bug_list, bug_list) {
74 		unsigned i;
75 
76 		bug = mod->bug_table;
77 		for (i = 0; i < mod->num_bugs; ++i, ++bug)
78 			if (bugaddr == bug_addr(bug))
79 				return bug;
80 	}
81 	return NULL;
82 }
83 
module_bug_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)84 void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
85 			 struct module *mod)
86 {
87 	char *secstrings;
88 	unsigned int i;
89 
90 	mod->bug_table = NULL;
91 	mod->num_bugs = 0;
92 
93 	/* Find the __bug_table section, if present */
94 	secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
95 	for (i = 1; i < hdr->e_shnum; i++) {
96 		if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
97 			continue;
98 		mod->bug_table = (void *) sechdrs[i].sh_addr;
99 		mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
100 		break;
101 	}
102 
103 	/*
104 	 * Strictly speaking this should have a spinlock to protect against
105 	 * traversals, but since we only traverse on BUG()s, a spinlock
106 	 * could potentially lead to deadlock and thus be counter-productive.
107 	 * Thus, this uses RCU to safely manipulate the bug list, since BUG
108 	 * must run in non-interruptive state.
109 	 */
110 	list_add_rcu(&mod->bug_list, &module_bug_list);
111 }
112 
module_bug_cleanup(struct module * mod)113 void module_bug_cleanup(struct module *mod)
114 {
115 	list_del_rcu(&mod->bug_list);
116 }
117 
118 #else
119 
module_find_bug(unsigned long bugaddr)120 static inline struct bug_entry *module_find_bug(unsigned long bugaddr)
121 {
122 	return NULL;
123 }
124 #endif
125 
bug_get_file_line(struct bug_entry * bug,const char ** file,unsigned int * line)126 void bug_get_file_line(struct bug_entry *bug, const char **file,
127 		       unsigned int *line)
128 {
129 #ifdef CONFIG_DEBUG_BUGVERBOSE
130 #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
131 	*file = (const char *)&bug->file_disp + bug->file_disp;
132 #else
133 	*file = bug->file;
134 #endif
135 	*line = bug->line;
136 #else
137 	*file = NULL;
138 	*line = 0;
139 #endif
140 }
141 
bug_get_format(struct bug_entry * bug)142 static const char *bug_get_format(struct bug_entry *bug)
143 {
144 	const char *format = NULL;
145 #ifdef HAVE_ARCH_BUG_FORMAT
146 #ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
147 	/*
148 	 * Allow an architecture to:
149 	 *  - relative encode NULL (difficult vs KASLR);
150 	 *  - use a literal 0 (there are no valid objects inside
151 	 *    the __bug_table itself to refer to after all);
152 	 *  - use an empty string.
153 	 */
154 	if (bug->format_disp)
155 		format = (const char *)&bug->format_disp + bug->format_disp;
156 	if (format && format[0] == '\0')
157 		format = NULL;
158 #else
159 	format = bug->format;
160 #endif
161 #endif
162 	return format;
163 }
164 
find_bug(unsigned long bugaddr)165 struct bug_entry *find_bug(unsigned long bugaddr)
166 {
167 	struct bug_entry *bug;
168 
169 	for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
170 		if (bugaddr == bug_addr(bug))
171 			return bug;
172 
173 	return module_find_bug(bugaddr);
174 }
175 
176 static __printf(1, 0)
__warn_printf(const char * fmt,struct pt_regs * regs)177 void __warn_printf(const char *fmt, struct pt_regs *regs)
178 {
179 	if (!fmt)
180 		return;
181 
182 #ifdef HAVE_ARCH_BUG_FORMAT_ARGS
183 	if (regs) {
184 		struct arch_va_list _args;
185 		va_list *args = __warn_args(&_args, regs);
186 
187 		if (args) {
188 			vprintk(fmt, *args);
189 			return;
190 		}
191 	}
192 #endif
193 
194 	printk("%s", fmt);
195 }
196 
__report_bug(struct bug_entry * bug,unsigned long bugaddr,struct pt_regs * regs)197 static enum bug_trap_type __report_bug(struct bug_entry *bug, unsigned long bugaddr, struct pt_regs *regs)
198 {
199 	bool warning, once, done, no_cut, has_args;
200 	const char *file, *fmt;
201 	unsigned line;
202 
203 	if (!bug) {
204 		if (!is_valid_bugaddr(bugaddr))
205 			return BUG_TRAP_TYPE_NONE;
206 
207 		bug = find_bug(bugaddr);
208 		if (!bug)
209 			return BUG_TRAP_TYPE_NONE;
210 	}
211 
212 	disable_trace_on_warning();
213 
214 	bug_get_file_line(bug, &file, &line);
215 	fmt = bug_get_format(bug);
216 
217 	warning  = bug->flags & BUGFLAG_WARNING;
218 	once     = bug->flags & BUGFLAG_ONCE;
219 	done     = bug->flags & BUGFLAG_DONE;
220 	no_cut   = bug->flags & BUGFLAG_NO_CUT_HERE;
221 	has_args = bug->flags & BUGFLAG_ARGS;
222 
223 	if (warning && once) {
224 		if (done)
225 			return BUG_TRAP_TYPE_WARN;
226 
227 		/*
228 		 * Since this is the only store, concurrency is not an issue.
229 		 */
230 		bug->flags |= BUGFLAG_DONE;
231 	}
232 
233 	/*
234 	 * BUG() and WARN_ON() families don't print a custom debug message
235 	 * before triggering the exception handler, so we must add the
236 	 * "cut here" line now. WARN() issues its own "cut here" before the
237 	 * extra debugging message it writes before triggering the handler.
238 	 */
239 	if (!no_cut) {
240 		printk(KERN_DEFAULT CUT_HERE);
241 		__warn_printf(fmt, has_args ? regs : NULL);
242 	}
243 
244 	if (warning) {
245 		/* this is a WARN_ON rather than BUG/BUG_ON */
246 		__warn(file, line, (void *)bugaddr, BUG_GET_TAINT(bug), regs,
247 		       NULL);
248 		return BUG_TRAP_TYPE_WARN;
249 	}
250 
251 	if (file)
252 		pr_crit("kernel BUG at %s:%u!\n", file, line);
253 	else
254 		pr_crit("Kernel BUG at %pB [verbose debug info unavailable]\n",
255 			(void *)bugaddr);
256 
257 	return BUG_TRAP_TYPE_BUG;
258 }
259 
report_bug_entry(struct bug_entry * bug,struct pt_regs * regs)260 enum bug_trap_type report_bug_entry(struct bug_entry *bug, struct pt_regs *regs)
261 {
262 	enum bug_trap_type ret;
263 	bool rcu = false;
264 
265 	rcu = warn_rcu_enter();
266 	ret = __report_bug(bug, bug_addr(bug), regs);
267 	warn_rcu_exit(rcu);
268 
269 	return ret;
270 }
271 
report_bug(unsigned long bugaddr,struct pt_regs * regs)272 enum bug_trap_type report_bug(unsigned long bugaddr, struct pt_regs *regs)
273 {
274 	enum bug_trap_type ret;
275 	bool rcu = false;
276 
277 	rcu = warn_rcu_enter();
278 	ret = __report_bug(NULL, bugaddr, regs);
279 	warn_rcu_exit(rcu);
280 
281 	return ret;
282 }
283 
clear_once_table(struct bug_entry * start,struct bug_entry * end)284 static void clear_once_table(struct bug_entry *start, struct bug_entry *end)
285 {
286 	struct bug_entry *bug;
287 
288 	for (bug = start; bug < end; bug++)
289 		bug->flags &= ~BUGFLAG_DONE;
290 }
291 
generic_bug_clear_once(void)292 void generic_bug_clear_once(void)
293 {
294 #ifdef CONFIG_MODULES
295 	struct module *mod;
296 
297 	scoped_guard(rcu) {
298 		list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
299 			clear_once_table(mod->bug_table,
300 					 mod->bug_table + mod->num_bugs);
301 	}
302 #endif
303 
304 	clear_once_table(__start___bug_table, __stop___bug_table);
305 }
306