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