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