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