xref: /openbmc/linux/kernel/kallsyms.c (revision 1d7a0395)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * kallsyms.c: in-kernel printing of symbolic oopses and stack traces.
4  *
5  * Rewritten and vastly simplified by Rusty Russell for in-kernel
6  * module loader:
7  *   Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
8  *
9  * ChangeLog:
10  *
11  * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
12  *      Changed the compression method from stem compression to "table lookup"
13  *      compression (see scripts/kallsyms.c for a more complete description)
14  */
15 #include <linux/kallsyms.h>
16 #include <linux/init.h>
17 #include <linux/seq_file.h>
18 #include <linux/fs.h>
19 #include <linux/kdb.h>
20 #include <linux/err.h>
21 #include <linux/proc_fs.h>
22 #include <linux/sched.h>	/* for cond_resched */
23 #include <linux/ctype.h>
24 #include <linux/slab.h>
25 #include <linux/filter.h>
26 #include <linux/ftrace.h>
27 #include <linux/kprobes.h>
28 #include <linux/compiler.h>
29 
30 /*
31  * These will be re-linked against their real values
32  * during the second link stage.
33  */
34 extern const unsigned long kallsyms_addresses[] __weak;
35 extern const int kallsyms_offsets[] __weak;
36 extern const u8 kallsyms_names[] __weak;
37 
38 /*
39  * Tell the compiler that the count isn't in the small data section if the arch
40  * has one (eg: FRV).
41  */
42 extern const unsigned int kallsyms_num_syms
43 __section(".rodata") __attribute__((weak));
44 
45 extern const unsigned long kallsyms_relative_base
46 __section(".rodata") __attribute__((weak));
47 
48 extern const char kallsyms_token_table[] __weak;
49 extern const u16 kallsyms_token_index[] __weak;
50 
51 extern const unsigned int kallsyms_markers[] __weak;
52 
53 /*
54  * Expand a compressed symbol data into the resulting uncompressed string,
55  * if uncompressed string is too long (>= maxlen), it will be truncated,
56  * given the offset to where the symbol is in the compressed stream.
57  */
58 static unsigned int kallsyms_expand_symbol(unsigned int off,
59 					   char *result, size_t maxlen)
60 {
61 	int len, skipped_first = 0;
62 	const char *tptr;
63 	const u8 *data;
64 
65 	/* Get the compressed symbol length from the first symbol byte. */
66 	data = &kallsyms_names[off];
67 	len = *data;
68 	data++;
69 
70 	/*
71 	 * Update the offset to return the offset for the next symbol on
72 	 * the compressed stream.
73 	 */
74 	off += len + 1;
75 
76 	/*
77 	 * For every byte on the compressed symbol data, copy the table
78 	 * entry for that byte.
79 	 */
80 	while (len) {
81 		tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
82 		data++;
83 		len--;
84 
85 		while (*tptr) {
86 			if (skipped_first) {
87 				if (maxlen <= 1)
88 					goto tail;
89 				*result = *tptr;
90 				result++;
91 				maxlen--;
92 			} else
93 				skipped_first = 1;
94 			tptr++;
95 		}
96 	}
97 
98 tail:
99 	if (maxlen)
100 		*result = '\0';
101 
102 	/* Return to offset to the next symbol. */
103 	return off;
104 }
105 
106 /*
107  * Get symbol type information. This is encoded as a single char at the
108  * beginning of the symbol name.
109  */
110 static char kallsyms_get_symbol_type(unsigned int off)
111 {
112 	/*
113 	 * Get just the first code, look it up in the token table,
114 	 * and return the first char from this token.
115 	 */
116 	return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]];
117 }
118 
119 
120 /*
121  * Find the offset on the compressed stream given and index in the
122  * kallsyms array.
123  */
124 static unsigned int get_symbol_offset(unsigned long pos)
125 {
126 	const u8 *name;
127 	int i;
128 
129 	/*
130 	 * Use the closest marker we have. We have markers every 256 positions,
131 	 * so that should be close enough.
132 	 */
133 	name = &kallsyms_names[kallsyms_markers[pos >> 8]];
134 
135 	/*
136 	 * Sequentially scan all the symbols up to the point we're searching
137 	 * for. Every symbol is stored in a [<len>][<len> bytes of data] format,
138 	 * so we just need to add the len to the current pointer for every
139 	 * symbol we wish to skip.
140 	 */
141 	for (i = 0; i < (pos & 0xFF); i++)
142 		name = name + (*name) + 1;
143 
144 	return name - kallsyms_names;
145 }
146 
147 static unsigned long kallsyms_sym_address(int idx)
148 {
149 	if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
150 		return kallsyms_addresses[idx];
151 
152 	/* values are unsigned offsets if --absolute-percpu is not in effect */
153 	if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU))
154 		return kallsyms_relative_base + (u32)kallsyms_offsets[idx];
155 
156 	/* ...otherwise, positive offsets are absolute values */
157 	if (kallsyms_offsets[idx] >= 0)
158 		return kallsyms_offsets[idx];
159 
160 	/* ...and negative offsets are relative to kallsyms_relative_base - 1 */
161 	return kallsyms_relative_base - 1 - kallsyms_offsets[idx];
162 }
163 
164 #if defined(CONFIG_CFI_CLANG) && defined(CONFIG_LTO_CLANG_THIN)
165 /*
166  * LLVM appends a hash to static function names when ThinLTO and CFI are
167  * both enabled, i.e. foo() becomes foo$707af9a22804d33c81801f27dcfe489b.
168  * This causes confusion and potentially breaks user space tools, so we
169  * strip the suffix from expanded symbol names.
170  */
171 static inline bool cleanup_symbol_name(char *s)
172 {
173 	char *res;
174 
175 	res = strrchr(s, '$');
176 	if (res)
177 		*res = '\0';
178 
179 	return res != NULL;
180 }
181 #else
182 static inline bool cleanup_symbol_name(char *s) { return false; }
183 #endif
184 
185 /* Lookup the address for this symbol. Returns 0 if not found. */
186 unsigned long kallsyms_lookup_name(const char *name)
187 {
188 	char namebuf[KSYM_NAME_LEN];
189 	unsigned long i;
190 	unsigned int off;
191 
192 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
193 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
194 
195 		if (strcmp(namebuf, name) == 0)
196 			return kallsyms_sym_address(i);
197 
198 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
199 			return kallsyms_sym_address(i);
200 	}
201 	return module_kallsyms_lookup_name(name);
202 }
203 
204 #ifdef CONFIG_LIVEPATCH
205 /*
206  * Iterate over all symbols in vmlinux.  For symbols from modules use
207  * module_kallsyms_on_each_symbol instead.
208  */
209 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
210 				      unsigned long),
211 			    void *data)
212 {
213 	char namebuf[KSYM_NAME_LEN];
214 	unsigned long i;
215 	unsigned int off;
216 	int ret;
217 
218 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
219 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
220 		ret = fn(data, namebuf, NULL, kallsyms_sym_address(i));
221 		if (ret != 0)
222 			return ret;
223 	}
224 	return 0;
225 }
226 #endif /* CONFIG_LIVEPATCH */
227 
228 static unsigned long get_symbol_pos(unsigned long addr,
229 				    unsigned long *symbolsize,
230 				    unsigned long *offset)
231 {
232 	unsigned long symbol_start = 0, symbol_end = 0;
233 	unsigned long i, low, high, mid;
234 
235 	/* This kernel should never had been booted. */
236 	if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
237 		BUG_ON(!kallsyms_addresses);
238 	else
239 		BUG_ON(!kallsyms_offsets);
240 
241 	/* Do a binary search on the sorted kallsyms_addresses array. */
242 	low = 0;
243 	high = kallsyms_num_syms;
244 
245 	while (high - low > 1) {
246 		mid = low + (high - low) / 2;
247 		if (kallsyms_sym_address(mid) <= addr)
248 			low = mid;
249 		else
250 			high = mid;
251 	}
252 
253 	/*
254 	 * Search for the first aliased symbol. Aliased
255 	 * symbols are symbols with the same address.
256 	 */
257 	while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low))
258 		--low;
259 
260 	symbol_start = kallsyms_sym_address(low);
261 
262 	/* Search for next non-aliased symbol. */
263 	for (i = low + 1; i < kallsyms_num_syms; i++) {
264 		if (kallsyms_sym_address(i) > symbol_start) {
265 			symbol_end = kallsyms_sym_address(i);
266 			break;
267 		}
268 	}
269 
270 	/* If we found no next symbol, we use the end of the section. */
271 	if (!symbol_end) {
272 		if (is_kernel_inittext(addr))
273 			symbol_end = (unsigned long)_einittext;
274 		else if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
275 			symbol_end = (unsigned long)_end;
276 		else
277 			symbol_end = (unsigned long)_etext;
278 	}
279 
280 	if (symbolsize)
281 		*symbolsize = symbol_end - symbol_start;
282 	if (offset)
283 		*offset = addr - symbol_start;
284 
285 	return low;
286 }
287 
288 /*
289  * Lookup an address but don't bother to find any names.
290  */
291 int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
292 				unsigned long *offset)
293 {
294 	char namebuf[KSYM_NAME_LEN];
295 
296 	if (is_ksym_addr(addr)) {
297 		get_symbol_pos(addr, symbolsize, offset);
298 		return 1;
299 	}
300 	return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf) ||
301 	       !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
302 }
303 
304 /*
305  * Lookup an address
306  * - modname is set to NULL if it's in the kernel.
307  * - We guarantee that the returned name is valid until we reschedule even if.
308  *   It resides in a module.
309  * - We also guarantee that modname will be valid until rescheduled.
310  */
311 const char *kallsyms_lookup(unsigned long addr,
312 			    unsigned long *symbolsize,
313 			    unsigned long *offset,
314 			    char **modname, char *namebuf)
315 {
316 	const char *ret;
317 
318 	namebuf[KSYM_NAME_LEN - 1] = 0;
319 	namebuf[0] = 0;
320 
321 	if (is_ksym_addr(addr)) {
322 		unsigned long pos;
323 
324 		pos = get_symbol_pos(addr, symbolsize, offset);
325 		/* Grab name */
326 		kallsyms_expand_symbol(get_symbol_offset(pos),
327 				       namebuf, KSYM_NAME_LEN);
328 		if (modname)
329 			*modname = NULL;
330 
331 		ret = namebuf;
332 		goto found;
333 	}
334 
335 	/* See if it's in a module or a BPF JITed image. */
336 	ret = module_address_lookup(addr, symbolsize, offset,
337 				    modname, namebuf);
338 	if (!ret)
339 		ret = bpf_address_lookup(addr, symbolsize,
340 					 offset, modname, namebuf);
341 
342 	if (!ret)
343 		ret = ftrace_mod_address_lookup(addr, symbolsize,
344 						offset, modname, namebuf);
345 
346 found:
347 	cleanup_symbol_name(namebuf);
348 	return ret;
349 }
350 
351 int lookup_symbol_name(unsigned long addr, char *symname)
352 {
353 	int res;
354 
355 	symname[0] = '\0';
356 	symname[KSYM_NAME_LEN - 1] = '\0';
357 
358 	if (is_ksym_addr(addr)) {
359 		unsigned long pos;
360 
361 		pos = get_symbol_pos(addr, NULL, NULL);
362 		/* Grab name */
363 		kallsyms_expand_symbol(get_symbol_offset(pos),
364 				       symname, KSYM_NAME_LEN);
365 		goto found;
366 	}
367 	/* See if it's in a module. */
368 	res = lookup_module_symbol_name(addr, symname);
369 	if (res)
370 		return res;
371 
372 found:
373 	cleanup_symbol_name(symname);
374 	return 0;
375 }
376 
377 int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
378 			unsigned long *offset, char *modname, char *name)
379 {
380 	int res;
381 
382 	name[0] = '\0';
383 	name[KSYM_NAME_LEN - 1] = '\0';
384 
385 	if (is_ksym_addr(addr)) {
386 		unsigned long pos;
387 
388 		pos = get_symbol_pos(addr, size, offset);
389 		/* Grab name */
390 		kallsyms_expand_symbol(get_symbol_offset(pos),
391 				       name, KSYM_NAME_LEN);
392 		modname[0] = '\0';
393 		goto found;
394 	}
395 	/* See if it's in a module. */
396 	res = lookup_module_symbol_attrs(addr, size, offset, modname, name);
397 	if (res)
398 		return res;
399 
400 found:
401 	cleanup_symbol_name(name);
402 	return 0;
403 }
404 
405 /* Look up a kernel symbol and return it in a text buffer. */
406 static int __sprint_symbol(char *buffer, unsigned long address,
407 			   int symbol_offset, int add_offset)
408 {
409 	char *modname;
410 	const char *name;
411 	unsigned long offset, size;
412 	int len;
413 
414 	address += symbol_offset;
415 	name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
416 	if (!name)
417 		return sprintf(buffer, "0x%lx", address - symbol_offset);
418 
419 	if (name != buffer)
420 		strcpy(buffer, name);
421 	len = strlen(buffer);
422 	offset -= symbol_offset;
423 
424 	if (add_offset)
425 		len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
426 
427 	if (modname)
428 		len += sprintf(buffer + len, " [%s]", modname);
429 
430 	return len;
431 }
432 
433 /**
434  * sprint_symbol - Look up a kernel symbol and return it in a text buffer
435  * @buffer: buffer to be stored
436  * @address: address to lookup
437  *
438  * This function looks up a kernel symbol with @address and stores its name,
439  * offset, size and module name to @buffer if possible. If no symbol was found,
440  * just saves its @address as is.
441  *
442  * This function returns the number of bytes stored in @buffer.
443  */
444 int sprint_symbol(char *buffer, unsigned long address)
445 {
446 	return __sprint_symbol(buffer, address, 0, 1);
447 }
448 EXPORT_SYMBOL_GPL(sprint_symbol);
449 
450 /**
451  * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
452  * @buffer: buffer to be stored
453  * @address: address to lookup
454  *
455  * This function looks up a kernel symbol with @address and stores its name
456  * and module name to @buffer if possible. If no symbol was found, just saves
457  * its @address as is.
458  *
459  * This function returns the number of bytes stored in @buffer.
460  */
461 int sprint_symbol_no_offset(char *buffer, unsigned long address)
462 {
463 	return __sprint_symbol(buffer, address, 0, 0);
464 }
465 EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
466 
467 /**
468  * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer
469  * @buffer: buffer to be stored
470  * @address: address to lookup
471  *
472  * This function is for stack backtrace and does the same thing as
473  * sprint_symbol() but with modified/decreased @address. If there is a
474  * tail-call to the function marked "noreturn", gcc optimized out code after
475  * the call so that the stack-saved return address could point outside of the
476  * caller. This function ensures that kallsyms will find the original caller
477  * by decreasing @address.
478  *
479  * This function returns the number of bytes stored in @buffer.
480  */
481 int sprint_backtrace(char *buffer, unsigned long address)
482 {
483 	return __sprint_symbol(buffer, address, -1, 1);
484 }
485 
486 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
487 struct kallsym_iter {
488 	loff_t pos;
489 	loff_t pos_arch_end;
490 	loff_t pos_mod_end;
491 	loff_t pos_ftrace_mod_end;
492 	loff_t pos_bpf_end;
493 	unsigned long value;
494 	unsigned int nameoff; /* If iterating in core kernel symbols. */
495 	char type;
496 	char name[KSYM_NAME_LEN];
497 	char module_name[MODULE_NAME_LEN];
498 	int exported;
499 	int show_value;
500 };
501 
502 int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value,
503 			    char *type, char *name)
504 {
505 	return -EINVAL;
506 }
507 
508 static int get_ksymbol_arch(struct kallsym_iter *iter)
509 {
510 	int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms,
511 				   &iter->value, &iter->type,
512 				   iter->name);
513 
514 	if (ret < 0) {
515 		iter->pos_arch_end = iter->pos;
516 		return 0;
517 	}
518 
519 	return 1;
520 }
521 
522 static int get_ksymbol_mod(struct kallsym_iter *iter)
523 {
524 	int ret = module_get_kallsym(iter->pos - iter->pos_arch_end,
525 				     &iter->value, &iter->type,
526 				     iter->name, iter->module_name,
527 				     &iter->exported);
528 	if (ret < 0) {
529 		iter->pos_mod_end = iter->pos;
530 		return 0;
531 	}
532 
533 	return 1;
534 }
535 
536 /*
537  * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace
538  * purposes. In that case "__builtin__ftrace" is used as a module name, even
539  * though "__builtin__ftrace" is not a module.
540  */
541 static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
542 {
543 	int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end,
544 					 &iter->value, &iter->type,
545 					 iter->name, iter->module_name,
546 					 &iter->exported);
547 	if (ret < 0) {
548 		iter->pos_ftrace_mod_end = iter->pos;
549 		return 0;
550 	}
551 
552 	return 1;
553 }
554 
555 static int get_ksymbol_bpf(struct kallsym_iter *iter)
556 {
557 	int ret;
558 
559 	strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN);
560 	iter->exported = 0;
561 	ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
562 			      &iter->value, &iter->type,
563 			      iter->name);
564 	if (ret < 0) {
565 		iter->pos_bpf_end = iter->pos;
566 		return 0;
567 	}
568 
569 	return 1;
570 }
571 
572 /*
573  * This uses "__builtin__kprobes" as a module name for symbols for pages
574  * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a
575  * module.
576  */
577 static int get_ksymbol_kprobe(struct kallsym_iter *iter)
578 {
579 	strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN);
580 	iter->exported = 0;
581 	return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end,
582 				  &iter->value, &iter->type,
583 				  iter->name) < 0 ? 0 : 1;
584 }
585 
586 /* Returns space to next name. */
587 static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
588 {
589 	unsigned off = iter->nameoff;
590 
591 	iter->module_name[0] = '\0';
592 	iter->value = kallsyms_sym_address(iter->pos);
593 
594 	iter->type = kallsyms_get_symbol_type(off);
595 
596 	off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name));
597 
598 	return off - iter->nameoff;
599 }
600 
601 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
602 {
603 	iter->name[0] = '\0';
604 	iter->nameoff = get_symbol_offset(new_pos);
605 	iter->pos = new_pos;
606 	if (new_pos == 0) {
607 		iter->pos_arch_end = 0;
608 		iter->pos_mod_end = 0;
609 		iter->pos_ftrace_mod_end = 0;
610 		iter->pos_bpf_end = 0;
611 	}
612 }
613 
614 /*
615  * The end position (last + 1) of each additional kallsyms section is recorded
616  * in iter->pos_..._end as each section is added, and so can be used to
617  * determine which get_ksymbol_...() function to call next.
618  */
619 static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
620 {
621 	iter->pos = pos;
622 
623 	if ((!iter->pos_arch_end || iter->pos_arch_end > pos) &&
624 	    get_ksymbol_arch(iter))
625 		return 1;
626 
627 	if ((!iter->pos_mod_end || iter->pos_mod_end > pos) &&
628 	    get_ksymbol_mod(iter))
629 		return 1;
630 
631 	if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) &&
632 	    get_ksymbol_ftrace_mod(iter))
633 		return 1;
634 
635 	if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) &&
636 	    get_ksymbol_bpf(iter))
637 		return 1;
638 
639 	return get_ksymbol_kprobe(iter);
640 }
641 
642 /* Returns false if pos at or past end of file. */
643 static int update_iter(struct kallsym_iter *iter, loff_t pos)
644 {
645 	/* Module symbols can be accessed randomly. */
646 	if (pos >= kallsyms_num_syms)
647 		return update_iter_mod(iter, pos);
648 
649 	/* If we're not on the desired position, reset to new position. */
650 	if (pos != iter->pos)
651 		reset_iter(iter, pos);
652 
653 	iter->nameoff += get_ksymbol_core(iter);
654 	iter->pos++;
655 
656 	return 1;
657 }
658 
659 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
660 {
661 	(*pos)++;
662 
663 	if (!update_iter(m->private, *pos))
664 		return NULL;
665 	return p;
666 }
667 
668 static void *s_start(struct seq_file *m, loff_t *pos)
669 {
670 	if (!update_iter(m->private, *pos))
671 		return NULL;
672 	return m->private;
673 }
674 
675 static void s_stop(struct seq_file *m, void *p)
676 {
677 }
678 
679 static int s_show(struct seq_file *m, void *p)
680 {
681 	void *value;
682 	struct kallsym_iter *iter = m->private;
683 
684 	/* Some debugging symbols have no name.  Ignore them. */
685 	if (!iter->name[0])
686 		return 0;
687 
688 	value = iter->show_value ? (void *)iter->value : NULL;
689 
690 	if (iter->module_name[0]) {
691 		char type;
692 
693 		/*
694 		 * Label it "global" if it is exported,
695 		 * "local" if not exported.
696 		 */
697 		type = iter->exported ? toupper(iter->type) :
698 					tolower(iter->type);
699 		seq_printf(m, "%px %c %s\t[%s]\n", value,
700 			   type, iter->name, iter->module_name);
701 	} else
702 		seq_printf(m, "%px %c %s\n", value,
703 			   iter->type, iter->name);
704 	return 0;
705 }
706 
707 static const struct seq_operations kallsyms_op = {
708 	.start = s_start,
709 	.next = s_next,
710 	.stop = s_stop,
711 	.show = s_show
712 };
713 
714 static inline int kallsyms_for_perf(void)
715 {
716 #ifdef CONFIG_PERF_EVENTS
717 	extern int sysctl_perf_event_paranoid;
718 	if (sysctl_perf_event_paranoid <= 1)
719 		return 1;
720 #endif
721 	return 0;
722 }
723 
724 /*
725  * We show kallsyms information even to normal users if we've enabled
726  * kernel profiling and are explicitly not paranoid (so kptr_restrict
727  * is clear, and sysctl_perf_event_paranoid isn't set).
728  *
729  * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to
730  * block even that).
731  */
732 bool kallsyms_show_value(const struct cred *cred)
733 {
734 	switch (kptr_restrict) {
735 	case 0:
736 		if (kallsyms_for_perf())
737 			return true;
738 		fallthrough;
739 	case 1:
740 		if (security_capable(cred, &init_user_ns, CAP_SYSLOG,
741 				     CAP_OPT_NOAUDIT) == 0)
742 			return true;
743 		fallthrough;
744 	default:
745 		return false;
746 	}
747 }
748 
749 static int kallsyms_open(struct inode *inode, struct file *file)
750 {
751 	/*
752 	 * We keep iterator in m->private, since normal case is to
753 	 * s_start from where we left off, so we avoid doing
754 	 * using get_symbol_offset for every symbol.
755 	 */
756 	struct kallsym_iter *iter;
757 	iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
758 	if (!iter)
759 		return -ENOMEM;
760 	reset_iter(iter, 0);
761 
762 	/*
763 	 * Instead of checking this on every s_show() call, cache
764 	 * the result here at open time.
765 	 */
766 	iter->show_value = kallsyms_show_value(file->f_cred);
767 	return 0;
768 }
769 
770 #ifdef	CONFIG_KGDB_KDB
771 const char *kdb_walk_kallsyms(loff_t *pos)
772 {
773 	static struct kallsym_iter kdb_walk_kallsyms_iter;
774 	if (*pos == 0) {
775 		memset(&kdb_walk_kallsyms_iter, 0,
776 		       sizeof(kdb_walk_kallsyms_iter));
777 		reset_iter(&kdb_walk_kallsyms_iter, 0);
778 	}
779 	while (1) {
780 		if (!update_iter(&kdb_walk_kallsyms_iter, *pos))
781 			return NULL;
782 		++*pos;
783 		/* Some debugging symbols have no name.  Ignore them. */
784 		if (kdb_walk_kallsyms_iter.name[0])
785 			return kdb_walk_kallsyms_iter.name;
786 	}
787 }
788 #endif	/* CONFIG_KGDB_KDB */
789 
790 static const struct proc_ops kallsyms_proc_ops = {
791 	.proc_open	= kallsyms_open,
792 	.proc_read	= seq_read,
793 	.proc_lseek	= seq_lseek,
794 	.proc_release	= seq_release_private,
795 };
796 
797 static int __init kallsyms_init(void)
798 {
799 	proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops);
800 	return 0;
801 }
802 device_initcall(kallsyms_init);
803