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