• 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/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  */
kallsyms_expand_symbol(unsigned int off,char * result,size_t maxlen)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  */
kallsyms_get_symbol_type(unsigned int off)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  */
get_symbol_offset(unsigned long pos)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 
kallsyms_sym_address(int idx)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 
cleanup_symbol_name(char * s)164 static bool cleanup_symbol_name(char *s)
165 {
166 	char *res;
167 
168 	if (!IS_ENABLED(CONFIG_LTO_CLANG))
169 		return false;
170 
171 	/*
172 	 * LLVM appends various suffixes for local functions and variables that
173 	 * must be promoted to global scope as part of LTO.  This can break
174 	 * hooking of static functions with kprobes. '.' is not a valid
175 	 * character in an identifier in C. Suffixes observed:
176 	 * - foo.llvm.[0-9a-f]+
177 	 * - foo.[0-9a-f]+
178 	 * - foo.[0-9a-f]+.cfi_jt
179 	 */
180 	res = strchr(s, '.');
181 	if (res) {
182 		*res = '\0';
183 		return true;
184 	}
185 
186 	if (!IS_ENABLED(CONFIG_CFI_CLANG) ||
187 	    !IS_ENABLED(CONFIG_LTO_CLANG_THIN) ||
188 	    CONFIG_CLANG_VERSION >= 130000)
189 		return false;
190 
191 	/*
192 	 * Prior to LLVM 13, the following suffixes were observed when thinLTO
193 	 * and CFI are both enabled:
194 	 * - foo$[0-9]+
195 	 */
196 	res = strrchr(s, '$');
197 	if (res) {
198 		*res = '\0';
199 		return true;
200 	}
201 
202 	return false;
203 }
204 
205 /* Lookup the address for this symbol. Returns 0 if not found. */
kallsyms_lookup_name(const char * name)206 unsigned long kallsyms_lookup_name(const char *name)
207 {
208 	char namebuf[KSYM_NAME_LEN];
209 	unsigned long i;
210 	unsigned int off;
211 
212 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
213 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
214 
215 		if (strcmp(namebuf, name) == 0)
216 			return kallsyms_sym_address(i);
217 
218 		if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
219 			return kallsyms_sym_address(i);
220 	}
221 	return module_kallsyms_lookup_name(name);
222 }
223 
kallsyms_on_each_symbol(int (* fn)(void *,const char *,struct module *,unsigned long),void * data)224 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
225 				      unsigned long),
226 			    void *data)
227 {
228 	char namebuf[KSYM_NAME_LEN];
229 	unsigned long i;
230 	unsigned int off;
231 	int ret;
232 
233 	for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
234 		off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf));
235 		ret = fn(data, namebuf, NULL, kallsyms_sym_address(i));
236 		if (ret != 0)
237 			return ret;
238 	}
239 	return module_kallsyms_on_each_symbol(fn, data);
240 }
241 
get_symbol_pos(unsigned long addr,unsigned long * symbolsize,unsigned long * offset)242 static unsigned long get_symbol_pos(unsigned long addr,
243 				    unsigned long *symbolsize,
244 				    unsigned long *offset)
245 {
246 	unsigned long symbol_start = 0, symbol_end = 0;
247 	unsigned long i, low, high, mid;
248 
249 	/* This kernel should never had been booted. */
250 	if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
251 		BUG_ON(!kallsyms_addresses);
252 	else
253 		BUG_ON(!kallsyms_offsets);
254 
255 	/* Do a binary search on the sorted kallsyms_addresses array. */
256 	low = 0;
257 	high = kallsyms_num_syms;
258 
259 	while (high - low > 1) {
260 		mid = low + (high - low) / 2;
261 		if (kallsyms_sym_address(mid) <= addr)
262 			low = mid;
263 		else
264 			high = mid;
265 	}
266 
267 	/*
268 	 * Search for the first aliased symbol. Aliased
269 	 * symbols are symbols with the same address.
270 	 */
271 	while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low))
272 		--low;
273 
274 	symbol_start = kallsyms_sym_address(low);
275 
276 	/* Search for next non-aliased symbol. */
277 	for (i = low + 1; i < kallsyms_num_syms; i++) {
278 		if (kallsyms_sym_address(i) > symbol_start) {
279 			symbol_end = kallsyms_sym_address(i);
280 			break;
281 		}
282 	}
283 
284 	/* If we found no next symbol, we use the end of the section. */
285 	if (!symbol_end) {
286 		if (is_kernel_inittext(addr))
287 			symbol_end = (unsigned long)_einittext;
288 		else if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
289 			symbol_end = (unsigned long)_end;
290 		else
291 			symbol_end = (unsigned long)_etext;
292 	}
293 
294 	if (symbolsize)
295 		*symbolsize = symbol_end - symbol_start;
296 	if (offset)
297 		*offset = addr - symbol_start;
298 
299 	return low;
300 }
301 
302 /*
303  * Lookup an address but don't bother to find any names.
304  */
kallsyms_lookup_size_offset(unsigned long addr,unsigned long * symbolsize,unsigned long * offset)305 int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
306 				unsigned long *offset)
307 {
308 	char namebuf[KSYM_NAME_LEN];
309 
310 	if (is_ksym_addr(addr)) {
311 		get_symbol_pos(addr, symbolsize, offset);
312 		return 1;
313 	}
314 	return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf) ||
315 	       !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
316 }
317 
318 /*
319  * Lookup an address
320  * - modname is set to NULL if it's in the kernel.
321  * - We guarantee that the returned name is valid until we reschedule even if.
322  *   It resides in a module.
323  * - We also guarantee that modname will be valid until rescheduled.
324  */
kallsyms_lookup(unsigned long addr,unsigned long * symbolsize,unsigned long * offset,char ** modname,char * namebuf)325 const char *kallsyms_lookup(unsigned long addr,
326 			    unsigned long *symbolsize,
327 			    unsigned long *offset,
328 			    char **modname, char *namebuf)
329 {
330 	const char *ret;
331 
332 	namebuf[KSYM_NAME_LEN - 1] = 0;
333 	namebuf[0] = 0;
334 
335 	if (is_ksym_addr(addr)) {
336 		unsigned long pos;
337 
338 		pos = get_symbol_pos(addr, symbolsize, offset);
339 		/* Grab name */
340 		kallsyms_expand_symbol(get_symbol_offset(pos),
341 				       namebuf, KSYM_NAME_LEN);
342 		if (modname)
343 			*modname = NULL;
344 
345 		ret = namebuf;
346 		goto found;
347 	}
348 
349 	/* See if it's in a module or a BPF JITed image. */
350 	ret = module_address_lookup(addr, symbolsize, offset,
351 				    modname, namebuf);
352 	if (!ret)
353 		ret = bpf_address_lookup(addr, symbolsize,
354 					 offset, modname, namebuf);
355 
356 	if (!ret)
357 		ret = ftrace_mod_address_lookup(addr, symbolsize,
358 						offset, modname, namebuf);
359 
360 found:
361 	cleanup_symbol_name(namebuf);
362 	return ret;
363 }
364 
lookup_symbol_name(unsigned long addr,char * symname)365 int lookup_symbol_name(unsigned long addr, char *symname)
366 {
367 	int res;
368 
369 	symname[0] = '\0';
370 	symname[KSYM_NAME_LEN - 1] = '\0';
371 
372 	if (is_ksym_addr(addr)) {
373 		unsigned long pos;
374 
375 		pos = get_symbol_pos(addr, NULL, NULL);
376 		/* Grab name */
377 		kallsyms_expand_symbol(get_symbol_offset(pos),
378 				       symname, KSYM_NAME_LEN);
379 		goto found;
380 	}
381 	/* See if it's in a module. */
382 	res = lookup_module_symbol_name(addr, symname);
383 	if (res)
384 		return res;
385 
386 found:
387 	cleanup_symbol_name(symname);
388 	return 0;
389 }
390 
lookup_symbol_attrs(unsigned long addr,unsigned long * size,unsigned long * offset,char * modname,char * name)391 int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
392 			unsigned long *offset, char *modname, char *name)
393 {
394 	int res;
395 
396 	name[0] = '\0';
397 	name[KSYM_NAME_LEN - 1] = '\0';
398 
399 	if (is_ksym_addr(addr)) {
400 		unsigned long pos;
401 
402 		pos = get_symbol_pos(addr, size, offset);
403 		/* Grab name */
404 		kallsyms_expand_symbol(get_symbol_offset(pos),
405 				       name, KSYM_NAME_LEN);
406 		modname[0] = '\0';
407 		goto found;
408 	}
409 	/* See if it's in a module. */
410 	res = lookup_module_symbol_attrs(addr, size, offset, modname, name);
411 	if (res)
412 		return res;
413 
414 found:
415 	cleanup_symbol_name(name);
416 	return 0;
417 }
418 
419 /* 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)420 static int __sprint_symbol(char *buffer, unsigned long address,
421 			   int symbol_offset, int add_offset)
422 {
423 	char *modname;
424 	const char *name;
425 	unsigned long offset, size;
426 	int len;
427 
428 	address += symbol_offset;
429 	name = kallsyms_lookup(address, &size, &offset, &modname, 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 
444 	return len;
445 }
446 
447 /**
448  * sprint_symbol - Look up a kernel symbol and return it in a text buffer
449  * @buffer: buffer to be stored
450  * @address: address to lookup
451  *
452  * This function looks up a kernel symbol with @address and stores its name,
453  * offset, size and module name to @buffer if possible. If no symbol was found,
454  * just saves its @address as is.
455  *
456  * This function returns the number of bytes stored in @buffer.
457  */
sprint_symbol(char * buffer,unsigned long address)458 int sprint_symbol(char *buffer, unsigned long address)
459 {
460 	return __sprint_symbol(buffer, address, 0, 1);
461 }
462 EXPORT_SYMBOL_GPL(sprint_symbol);
463 
464 /**
465  * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
466  * @buffer: buffer to be stored
467  * @address: address to lookup
468  *
469  * This function looks up a kernel symbol with @address and stores its name
470  * and module name to @buffer if possible. If no symbol was found, just saves
471  * its @address as is.
472  *
473  * This function returns the number of bytes stored in @buffer.
474  */
sprint_symbol_no_offset(char * buffer,unsigned long address)475 int sprint_symbol_no_offset(char *buffer, unsigned long address)
476 {
477 	return __sprint_symbol(buffer, address, 0, 0);
478 }
479 EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
480 
481 /**
482  * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer
483  * @buffer: buffer to be stored
484  * @address: address to lookup
485  *
486  * This function is for stack backtrace and does the same thing as
487  * sprint_symbol() but with modified/decreased @address. If there is a
488  * tail-call to the function marked "noreturn", gcc optimized out code after
489  * the call so that the stack-saved return address could point outside of the
490  * caller. This function ensures that kallsyms will find the original caller
491  * by decreasing @address.
492  *
493  * This function returns the number of bytes stored in @buffer.
494  */
sprint_backtrace(char * buffer,unsigned long address)495 int sprint_backtrace(char *buffer, unsigned long address)
496 {
497 	return __sprint_symbol(buffer, address, -1, 1);
498 }
499 
500 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
501 struct kallsym_iter {
502 	loff_t pos;
503 	loff_t pos_arch_end;
504 	loff_t pos_mod_end;
505 	loff_t pos_ftrace_mod_end;
506 	loff_t pos_bpf_end;
507 	unsigned long value;
508 	unsigned int nameoff; /* If iterating in core kernel symbols. */
509 	char type;
510 	char name[KSYM_NAME_LEN];
511 	char module_name[MODULE_NAME_LEN];
512 	int exported;
513 	int show_value;
514 };
515 
arch_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * name)516 int __weak arch_get_kallsym(unsigned int symnum, unsigned long *value,
517 			    char *type, char *name)
518 {
519 	return -EINVAL;
520 }
521 
get_ksymbol_arch(struct kallsym_iter * iter)522 static int get_ksymbol_arch(struct kallsym_iter *iter)
523 {
524 	int ret = arch_get_kallsym(iter->pos - kallsyms_num_syms,
525 				   &iter->value, &iter->type,
526 				   iter->name);
527 
528 	if (ret < 0) {
529 		iter->pos_arch_end = iter->pos;
530 		return 0;
531 	}
532 
533 	return 1;
534 }
535 
get_ksymbol_mod(struct kallsym_iter * iter)536 static int get_ksymbol_mod(struct kallsym_iter *iter)
537 {
538 	int ret = module_get_kallsym(iter->pos - iter->pos_arch_end,
539 				     &iter->value, &iter->type,
540 				     iter->name, iter->module_name,
541 				     &iter->exported);
542 	if (ret < 0) {
543 		iter->pos_mod_end = iter->pos;
544 		return 0;
545 	}
546 
547 	return 1;
548 }
549 
550 /*
551  * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace
552  * purposes. In that case "__builtin__ftrace" is used as a module name, even
553  * though "__builtin__ftrace" is not a module.
554  */
get_ksymbol_ftrace_mod(struct kallsym_iter * iter)555 static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
556 {
557 	int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end,
558 					 &iter->value, &iter->type,
559 					 iter->name, iter->module_name,
560 					 &iter->exported);
561 	if (ret < 0) {
562 		iter->pos_ftrace_mod_end = iter->pos;
563 		return 0;
564 	}
565 
566 	return 1;
567 }
568 
get_ksymbol_bpf(struct kallsym_iter * iter)569 static int get_ksymbol_bpf(struct kallsym_iter *iter)
570 {
571 	int ret;
572 
573 	strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN);
574 	iter->exported = 0;
575 	ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
576 			      &iter->value, &iter->type,
577 			      iter->name);
578 	if (ret < 0) {
579 		iter->pos_bpf_end = iter->pos;
580 		return 0;
581 	}
582 
583 	return 1;
584 }
585 
586 /*
587  * This uses "__builtin__kprobes" as a module name for symbols for pages
588  * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a
589  * module.
590  */
get_ksymbol_kprobe(struct kallsym_iter * iter)591 static int get_ksymbol_kprobe(struct kallsym_iter *iter)
592 {
593 	strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN);
594 	iter->exported = 0;
595 	return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end,
596 				  &iter->value, &iter->type,
597 				  iter->name) < 0 ? 0 : 1;
598 }
599 
600 /* Returns space to next name. */
get_ksymbol_core(struct kallsym_iter * iter)601 static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
602 {
603 	unsigned off = iter->nameoff;
604 
605 	iter->module_name[0] = '\0';
606 	iter->value = kallsyms_sym_address(iter->pos);
607 
608 	iter->type = kallsyms_get_symbol_type(off);
609 
610 	off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name));
611 
612 	return off - iter->nameoff;
613 }
614 
reset_iter(struct kallsym_iter * iter,loff_t new_pos)615 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
616 {
617 	iter->name[0] = '\0';
618 	iter->nameoff = get_symbol_offset(new_pos);
619 	iter->pos = new_pos;
620 	if (new_pos == 0) {
621 		iter->pos_arch_end = 0;
622 		iter->pos_mod_end = 0;
623 		iter->pos_ftrace_mod_end = 0;
624 		iter->pos_bpf_end = 0;
625 	}
626 }
627 
628 /*
629  * The end position (last + 1) of each additional kallsyms section is recorded
630  * in iter->pos_..._end as each section is added, and so can be used to
631  * determine which get_ksymbol_...() function to call next.
632  */
update_iter_mod(struct kallsym_iter * iter,loff_t pos)633 static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
634 {
635 	iter->pos = pos;
636 
637 	if ((!iter->pos_arch_end || iter->pos_arch_end > pos) &&
638 	    get_ksymbol_arch(iter))
639 		return 1;
640 
641 	if ((!iter->pos_mod_end || iter->pos_mod_end > pos) &&
642 	    get_ksymbol_mod(iter))
643 		return 1;
644 
645 	if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) &&
646 	    get_ksymbol_ftrace_mod(iter))
647 		return 1;
648 
649 	if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) &&
650 	    get_ksymbol_bpf(iter))
651 		return 1;
652 
653 	return get_ksymbol_kprobe(iter);
654 }
655 
656 /* Returns false if pos at or past end of file. */
update_iter(struct kallsym_iter * iter,loff_t pos)657 static int update_iter(struct kallsym_iter *iter, loff_t pos)
658 {
659 	/* Module symbols can be accessed randomly. */
660 	if (pos >= kallsyms_num_syms)
661 		return update_iter_mod(iter, pos);
662 
663 	/* If we're not on the desired position, reset to new position. */
664 	if (pos != iter->pos)
665 		reset_iter(iter, pos);
666 
667 	iter->nameoff += get_ksymbol_core(iter);
668 	iter->pos++;
669 
670 	return 1;
671 }
672 
s_next(struct seq_file * m,void * p,loff_t * pos)673 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
674 {
675 	(*pos)++;
676 
677 	if (!update_iter(m->private, *pos))
678 		return NULL;
679 	return p;
680 }
681 
s_start(struct seq_file * m,loff_t * pos)682 static void *s_start(struct seq_file *m, loff_t *pos)
683 {
684 	if (!update_iter(m->private, *pos))
685 		return NULL;
686 	return m->private;
687 }
688 
s_stop(struct seq_file * m,void * p)689 static void s_stop(struct seq_file *m, void *p)
690 {
691 }
692 
s_show(struct seq_file * m,void * p)693 static int s_show(struct seq_file *m, void *p)
694 {
695 	void *value;
696 	struct kallsym_iter *iter = m->private;
697 
698 	/* Some debugging symbols have no name.  Ignore them. */
699 	if (!iter->name[0])
700 		return 0;
701 
702 	value = iter->show_value ? (void *)iter->value : NULL;
703 
704 	if (iter->module_name[0]) {
705 		char type;
706 
707 		/*
708 		 * Label it "global" if it is exported,
709 		 * "local" if not exported.
710 		 */
711 		type = iter->exported ? toupper(iter->type) :
712 					tolower(iter->type);
713 		seq_printf(m, "%px %c %s\t[%s]\n", value,
714 			   type, iter->name, iter->module_name);
715 	} else
716 		seq_printf(m, "%px %c %s\n", value,
717 			   iter->type, iter->name);
718 	return 0;
719 }
720 
721 static const struct seq_operations kallsyms_op = {
722 	.start = s_start,
723 	.next = s_next,
724 	.stop = s_stop,
725 	.show = s_show
726 };
727 
kallsyms_for_perf(void)728 static inline int kallsyms_for_perf(void)
729 {
730 #ifdef CONFIG_PERF_EVENTS
731 	extern int sysctl_perf_event_paranoid;
732 	if (sysctl_perf_event_paranoid <= 1)
733 		return 1;
734 #endif
735 	return 0;
736 }
737 
738 /*
739  * We show kallsyms information even to normal users if we've enabled
740  * kernel profiling and are explicitly not paranoid (so kptr_restrict
741  * is clear, and sysctl_perf_event_paranoid isn't set).
742  *
743  * Otherwise, require CAP_SYSLOG (assuming kptr_restrict isn't set to
744  * block even that).
745  */
kallsyms_show_value(const struct cred * cred)746 bool kallsyms_show_value(const struct cred *cred)
747 {
748 	switch (kptr_restrict) {
749 	case 0:
750 		if (kallsyms_for_perf())
751 			return true;
752 		fallthrough;
753 	case 1:
754 		if (security_capable(cred, &init_user_ns, CAP_SYSLOG,
755 				     CAP_OPT_NOAUDIT) == 0)
756 			return true;
757 		fallthrough;
758 	default:
759 		return false;
760 	}
761 }
762 
kallsyms_open(struct inode * inode,struct file * file)763 static int kallsyms_open(struct inode *inode, struct file *file)
764 {
765 	/*
766 	 * We keep iterator in m->private, since normal case is to
767 	 * s_start from where we left off, so we avoid doing
768 	 * using get_symbol_offset for every symbol.
769 	 */
770 	struct kallsym_iter *iter;
771 	iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter));
772 	if (!iter)
773 		return -ENOMEM;
774 	reset_iter(iter, 0);
775 
776 	/*
777 	 * Instead of checking this on every s_show() call, cache
778 	 * the result here at open time.
779 	 */
780 	iter->show_value = kallsyms_show_value(file->f_cred);
781 	return 0;
782 }
783 
784 #ifdef	CONFIG_KGDB_KDB
kdb_walk_kallsyms(loff_t * pos)785 const char *kdb_walk_kallsyms(loff_t *pos)
786 {
787 	static struct kallsym_iter kdb_walk_kallsyms_iter;
788 	if (*pos == 0) {
789 		memset(&kdb_walk_kallsyms_iter, 0,
790 		       sizeof(kdb_walk_kallsyms_iter));
791 		reset_iter(&kdb_walk_kallsyms_iter, 0);
792 	}
793 	while (1) {
794 		if (!update_iter(&kdb_walk_kallsyms_iter, *pos))
795 			return NULL;
796 		++*pos;
797 		/* Some debugging symbols have no name.  Ignore them. */
798 		if (kdb_walk_kallsyms_iter.name[0])
799 			return kdb_walk_kallsyms_iter.name;
800 	}
801 }
802 #endif	/* CONFIG_KGDB_KDB */
803 
804 static const struct proc_ops kallsyms_proc_ops = {
805 	.proc_open	= kallsyms_open,
806 	.proc_read	= seq_read,
807 	.proc_lseek	= seq_lseek,
808 	.proc_release	= seq_release_private,
809 };
810 
kallsyms_init(void)811 static int __init kallsyms_init(void)
812 {
813 	proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops);
814 	return 0;
815 }
816 device_initcall(kallsyms_init);
817