Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * fs/proc/kcore.c kernel ELF core dumper |
| 4 | * |
| 5 | * Modelled on fs/exec.c:aout_core_dump() |
| 6 | * Jeremy Fitzhardinge <jeremy@sw.oz.au> |
| 7 | * ELF version written by David Howells <David.Howells@nexor.co.uk> |
| 8 | * Modified and incorporated into 2.3.x by Tigran Aivazian <tigran@veritas.com> |
| 9 | * Support to dump vmalloc'd areas (ELF only), Tigran Aivazian <tigran@veritas.com> |
| 10 | * Safe accesses to vmalloc/direct-mapped discontiguous areas, Kanoj Sarcar <kanoj@sgi.com> |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/mm.h> |
| 14 | #include <linux/proc_fs.h> |
David Howells | 2f96b8c | 2013-04-12 00:10:25 +0100 | [diff] [blame] | 15 | #include <linux/kcore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/user.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 17 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/elf.h> |
| 19 | #include <linux/elfcore.h> |
Andrew Morton | 3c743a7 | 2013-04-29 15:08:08 -0700 | [diff] [blame] | 20 | #include <linux/notifier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/highmem.h> |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 23 | #include <linux/printk.h> |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 24 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/io.h> |
KAMEZAWA Hiroyuki | 2ef43ec | 2009-09-22 16:45:41 -0700 | [diff] [blame] | 29 | #include <linux/list.h> |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 30 | #include <linux/ioport.h> |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 31 | #include <linux/memory.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 32 | #include <linux/sched/task.h> |
KAMEZAWA Hiroyuki | 9492587 | 2009-09-22 16:45:45 -0700 | [diff] [blame] | 33 | #include <asm/sections.h> |
David Howells | 59d8053 | 2013-04-11 13:34:43 +0100 | [diff] [blame] | 34 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Magnus Damm | 3602760 | 2006-12-06 20:38:00 -0800 | [diff] [blame] | 36 | #define CORE_STR "CORE" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Edgar E. Iglesias | 79885b2 | 2008-07-25 01:48:10 -0700 | [diff] [blame] | 38 | #ifndef ELF_CORE_EFLAGS |
| 39 | #define ELF_CORE_EFLAGS 0 |
| 40 | #endif |
| 41 | |
Alexey Dobriyan | 97ce5d6 | 2008-10-06 14:14:19 +0400 | [diff] [blame] | 42 | static struct proc_dir_entry *proc_root_kcore; |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #ifndef kc_vaddr_to_offset |
| 46 | #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET) |
| 47 | #endif |
| 48 | #ifndef kc_offset_to_vaddr |
| 49 | #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET) |
| 50 | #endif |
| 51 | |
KAMEZAWA Hiroyuki | 2ef43ec | 2009-09-22 16:45:41 -0700 | [diff] [blame] | 52 | static LIST_HEAD(kclist_head); |
Omar Sandoval | 0b172f8 | 2018-08-21 21:54:59 -0700 | [diff] [blame] | 53 | static DECLARE_RWSEM(kclist_lock); |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 54 | static int kcore_need_update = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Omar Sandoval | a8dd9c4 | 2018-08-21 21:54:51 -0700 | [diff] [blame] | 56 | /* This doesn't grab kclist_lock, so it should only be used at init time. */ |
| 57 | void __init kclist_add(struct kcore_list *new, void *addr, size_t size, |
| 58 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | new->addr = (unsigned long)addr; |
| 61 | new->size = size; |
KAMEZAWA Hiroyuki | c30bb2a | 2009-09-22 16:45:43 -0700 | [diff] [blame] | 62 | new->type = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
KAMEZAWA Hiroyuki | 2ef43ec | 2009-09-22 16:45:41 -0700 | [diff] [blame] | 64 | list_add_tail(&new->list, &kclist_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 67 | static size_t get_kcore_size(int *nphdr, size_t *phdrs_len, size_t *notes_len, |
| 68 | size_t *data_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | size_t try, size; |
| 71 | struct kcore_list *m; |
| 72 | |
| 73 | *nphdr = 1; /* PT_NOTE */ |
| 74 | size = 0; |
| 75 | |
KAMEZAWA Hiroyuki | 2ef43ec | 2009-09-22 16:45:41 -0700 | [diff] [blame] | 76 | list_for_each_entry(m, &kclist_head, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | try = kc_vaddr_to_offset((size_t)m->addr + m->size); |
| 78 | if (try > size) |
| 79 | size = try; |
| 80 | *nphdr = *nphdr + 1; |
| 81 | } |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 82 | |
| 83 | *phdrs_len = *nphdr * sizeof(struct elf_phdr); |
| 84 | *notes_len = (3 * (sizeof(struct elf_note) + ALIGN(sizeof(CORE_STR), 4)) + |
| 85 | ALIGN(sizeof(struct elf_prstatus), 4) + |
| 86 | ALIGN(sizeof(struct elf_prpsinfo), 4) + |
| 87 | ALIGN(arch_task_struct_size, 4)); |
| 88 | *data_offset = PAGE_ALIGN(sizeof(struct elfhdr) + *phdrs_len + |
| 89 | *notes_len); |
| 90 | return *data_offset + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 93 | #ifdef CONFIG_HIGHMEM |
| 94 | /* |
| 95 | * If no highmem, we can assume [0...max_low_pfn) continuous range of memory |
| 96 | * because memory hole is not as big as !HIGHMEM case. |
| 97 | * (HIGHMEM is special because part of memory is _invisible_ from the kernel.) |
| 98 | */ |
Omar Sandoval | b66fb00 | 2018-08-21 21:55:02 -0700 | [diff] [blame] | 99 | static int kcore_ram_list(struct list_head *head) |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 100 | { |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 101 | struct kcore_list *ent; |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 102 | |
| 103 | ent = kmalloc(sizeof(*ent), GFP_KERNEL); |
| 104 | if (!ent) |
| 105 | return -ENOMEM; |
| 106 | ent->addr = (unsigned long)__va(0); |
| 107 | ent->size = max_low_pfn << PAGE_SHIFT; |
| 108 | ent->type = KCORE_RAM; |
Omar Sandoval | b66fb00 | 2018-08-21 21:55:02 -0700 | [diff] [blame] | 109 | list_add(&ent->list, head); |
| 110 | return 0; |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | #else /* !CONFIG_HIGHMEM */ |
| 114 | |
KAMEZAWA Hiroyuki | 26562c5 | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 115 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 116 | /* calculate vmemmap's address from given system ram pfn and register it */ |
Djalal Harouni | b908243 | 2012-03-23 15:02:52 -0700 | [diff] [blame] | 117 | static int |
| 118 | get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head) |
KAMEZAWA Hiroyuki | 26562c5 | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 119 | { |
| 120 | unsigned long pfn = __pa(ent->addr) >> PAGE_SHIFT; |
| 121 | unsigned long nr_pages = ent->size >> PAGE_SHIFT; |
| 122 | unsigned long start, end; |
| 123 | struct kcore_list *vmm, *tmp; |
| 124 | |
| 125 | |
| 126 | start = ((unsigned long)pfn_to_page(pfn)) & PAGE_MASK; |
| 127 | end = ((unsigned long)pfn_to_page(pfn + nr_pages)) - 1; |
Fabian Frederick | 108a8a1 | 2014-08-08 14:21:20 -0700 | [diff] [blame] | 128 | end = PAGE_ALIGN(end); |
KAMEZAWA Hiroyuki | 26562c5 | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 129 | /* overlap check (because we have to align page */ |
| 130 | list_for_each_entry(tmp, head, list) { |
| 131 | if (tmp->type != KCORE_VMEMMAP) |
| 132 | continue; |
| 133 | if (start < tmp->addr + tmp->size) |
| 134 | if (end > tmp->addr) |
| 135 | end = tmp->addr; |
| 136 | } |
| 137 | if (start < end) { |
| 138 | vmm = kmalloc(sizeof(*vmm), GFP_KERNEL); |
| 139 | if (!vmm) |
| 140 | return 0; |
| 141 | vmm->addr = start; |
| 142 | vmm->size = end - start; |
| 143 | vmm->type = KCORE_VMEMMAP; |
| 144 | list_add_tail(&vmm->list, head); |
| 145 | } |
| 146 | return 1; |
| 147 | |
| 148 | } |
| 149 | #else |
Djalal Harouni | b908243 | 2012-03-23 15:02:52 -0700 | [diff] [blame] | 150 | static int |
| 151 | get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head) |
KAMEZAWA Hiroyuki | 26562c5 | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 152 | { |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | #endif |
| 157 | |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 158 | static int |
| 159 | kclist_add_private(unsigned long pfn, unsigned long nr_pages, void *arg) |
| 160 | { |
| 161 | struct list_head *head = (struct list_head *)arg; |
| 162 | struct kcore_list *ent; |
Laura Abbott | 3955333 | 2018-05-11 16:01:57 -0700 | [diff] [blame] | 163 | struct page *p; |
| 164 | |
| 165 | if (!pfn_valid(pfn)) |
| 166 | return 1; |
| 167 | |
| 168 | p = pfn_to_page(pfn); |
| 169 | if (!memmap_valid_within(pfn, p, page_zone(p))) |
| 170 | return 1; |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 171 | |
| 172 | ent = kmalloc(sizeof(*ent), GFP_KERNEL); |
| 173 | if (!ent) |
| 174 | return -ENOMEM; |
Laura Abbott | 3955333 | 2018-05-11 16:01:57 -0700 | [diff] [blame] | 175 | ent->addr = (unsigned long)page_to_virt(p); |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 176 | ent->size = nr_pages << PAGE_SHIFT; |
| 177 | |
Laura Abbott | 3955333 | 2018-05-11 16:01:57 -0700 | [diff] [blame] | 178 | if (!virt_addr_valid(ent->addr)) |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 179 | goto free_out; |
| 180 | |
| 181 | /* cut not-mapped area. ....from ppc-32 code. */ |
| 182 | if (ULONG_MAX - ent->addr < ent->size) |
| 183 | ent->size = ULONG_MAX - ent->addr; |
| 184 | |
Laura Abbott | 3955333 | 2018-05-11 16:01:57 -0700 | [diff] [blame] | 185 | /* |
| 186 | * We've already checked virt_addr_valid so we know this address |
| 187 | * is a valid pointer, therefore we can check against it to determine |
| 188 | * if we need to trim |
| 189 | */ |
| 190 | if (VMALLOC_START > ent->addr) { |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 191 | if (VMALLOC_START - ent->addr < ent->size) |
| 192 | ent->size = VMALLOC_START - ent->addr; |
| 193 | } |
| 194 | |
| 195 | ent->type = KCORE_RAM; |
| 196 | list_add_tail(&ent->list, head); |
KAMEZAWA Hiroyuki | 26562c5 | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 197 | |
| 198 | if (!get_sparsemem_vmemmap_info(ent, head)) { |
| 199 | list_del(&ent->list); |
| 200 | goto free_out; |
| 201 | } |
| 202 | |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | free_out: |
| 205 | kfree(ent); |
| 206 | return 1; |
| 207 | } |
| 208 | |
Omar Sandoval | b66fb00 | 2018-08-21 21:55:02 -0700 | [diff] [blame] | 209 | static int kcore_ram_list(struct list_head *list) |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 210 | { |
| 211 | int nid, ret; |
| 212 | unsigned long end_pfn; |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 213 | |
| 214 | /* Not inialized....update now */ |
| 215 | /* find out "max pfn" */ |
| 216 | end_pfn = 0; |
Lai Jiangshan | 4ff1b2c | 2012-12-12 13:51:25 -0800 | [diff] [blame] | 217 | for_each_node_state(nid, N_MEMORY) { |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 218 | unsigned long node_end; |
Xishi Qiu | 83285c7 | 2013-11-12 15:07:19 -0800 | [diff] [blame] | 219 | node_end = node_end_pfn(nid); |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 220 | if (end_pfn < node_end) |
| 221 | end_pfn = node_end; |
| 222 | } |
| 223 | /* scan 0 to max_pfn */ |
Omar Sandoval | b66fb00 | 2018-08-21 21:55:02 -0700 | [diff] [blame] | 224 | ret = walk_system_ram_range(0, end_pfn, list, kclist_add_private); |
| 225 | if (ret) |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 226 | return -ENOMEM; |
Omar Sandoval | b66fb00 | 2018-08-21 21:55:02 -0700 | [diff] [blame] | 227 | return 0; |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 228 | } |
| 229 | #endif /* CONFIG_HIGHMEM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Omar Sandoval | b66fb00 | 2018-08-21 21:55:02 -0700 | [diff] [blame] | 231 | static int kcore_update_ram(void) |
| 232 | { |
| 233 | LIST_HEAD(list); |
| 234 | LIST_HEAD(garbage); |
| 235 | int nphdr; |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 236 | size_t phdrs_len, notes_len, data_offset; |
Omar Sandoval | b66fb00 | 2018-08-21 21:55:02 -0700 | [diff] [blame] | 237 | struct kcore_list *tmp, *pos; |
| 238 | int ret = 0; |
| 239 | |
| 240 | down_write(&kclist_lock); |
| 241 | if (!xchg(&kcore_need_update, 0)) |
| 242 | goto out; |
| 243 | |
| 244 | ret = kcore_ram_list(&list); |
| 245 | if (ret) { |
| 246 | /* Couldn't get the RAM list, try again next time. */ |
| 247 | WRITE_ONCE(kcore_need_update, 1); |
| 248 | list_splice_tail(&list, &garbage); |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | list_for_each_entry_safe(pos, tmp, &kclist_head, list) { |
| 253 | if (pos->type == KCORE_RAM || pos->type == KCORE_VMEMMAP) |
| 254 | list_move(&pos->list, &garbage); |
| 255 | } |
| 256 | list_splice_tail(&list, &kclist_head); |
| 257 | |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 258 | proc_root_kcore->size = get_kcore_size(&nphdr, &phdrs_len, ¬es_len, |
| 259 | &data_offset); |
Omar Sandoval | b66fb00 | 2018-08-21 21:55:02 -0700 | [diff] [blame] | 260 | |
| 261 | out: |
| 262 | up_write(&kclist_lock); |
| 263 | list_for_each_entry_safe(pos, tmp, &garbage, list) { |
| 264 | list_del(&pos->list); |
| 265 | kfree(pos); |
| 266 | } |
| 267 | return ret; |
| 268 | } |
| 269 | |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 270 | static void append_kcore_note(char *notes, size_t *i, const char *name, |
| 271 | unsigned int type, const void *desc, |
| 272 | size_t descsz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 274 | struct elf_note *note = (struct elf_note *)¬es[*i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 276 | note->n_namesz = strlen(name) + 1; |
| 277 | note->n_descsz = descsz; |
| 278 | note->n_type = type; |
| 279 | *i += sizeof(*note); |
| 280 | memcpy(¬es[*i], name, note->n_namesz); |
| 281 | *i = ALIGN(*i + note->n_namesz, 4); |
| 282 | memcpy(¬es[*i], desc, descsz); |
| 283 | *i = ALIGN(*i + descsz, 4); |
| 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | static ssize_t |
| 287 | read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) |
| 288 | { |
Jiri Olsa | f5beeb1 | 2016-09-08 09:57:07 +0200 | [diff] [blame] | 289 | char *buf = file->private_data; |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 290 | size_t phdrs_offset, notes_offset, data_offset; |
| 291 | size_t phdrs_len, notes_len; |
| 292 | struct kcore_list *m; |
| 293 | size_t tsz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | int nphdr; |
| 295 | unsigned long start; |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 296 | size_t orig_buflen = buflen; |
| 297 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Omar Sandoval | 0b172f8 | 2018-08-21 21:54:59 -0700 | [diff] [blame] | 299 | down_read(&kclist_lock); |
KAMEZAWA Hiroyuki | 678ad5d | 2009-09-22 16:45:51 -0700 | [diff] [blame] | 300 | |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 301 | get_kcore_size(&nphdr, &phdrs_len, ¬es_len, &data_offset); |
| 302 | phdrs_offset = sizeof(struct elfhdr); |
| 303 | notes_offset = phdrs_offset + phdrs_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 305 | /* ELF file header. */ |
| 306 | if (buflen && *fpos < sizeof(struct elfhdr)) { |
| 307 | struct elfhdr ehdr = { |
| 308 | .e_ident = { |
| 309 | [EI_MAG0] = ELFMAG0, |
| 310 | [EI_MAG1] = ELFMAG1, |
| 311 | [EI_MAG2] = ELFMAG2, |
| 312 | [EI_MAG3] = ELFMAG3, |
| 313 | [EI_CLASS] = ELF_CLASS, |
| 314 | [EI_DATA] = ELF_DATA, |
| 315 | [EI_VERSION] = EV_CURRENT, |
| 316 | [EI_OSABI] = ELF_OSABI, |
| 317 | }, |
| 318 | .e_type = ET_CORE, |
| 319 | .e_machine = ELF_ARCH, |
| 320 | .e_version = EV_CURRENT, |
| 321 | .e_phoff = sizeof(struct elfhdr), |
| 322 | .e_flags = ELF_CORE_EFLAGS, |
| 323 | .e_ehsize = sizeof(struct elfhdr), |
| 324 | .e_phentsize = sizeof(struct elf_phdr), |
| 325 | .e_phnum = nphdr, |
| 326 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 328 | tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos); |
| 329 | if (copy_to_user(buffer, (char *)&ehdr + *fpos, tsz)) { |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 330 | ret = -EFAULT; |
| 331 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 333 | |
| 334 | buffer += tsz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | buflen -= tsz; |
| 336 | *fpos += tsz; |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 339 | /* ELF program headers. */ |
| 340 | if (buflen && *fpos < phdrs_offset + phdrs_len) { |
| 341 | struct elf_phdr *phdrs, *phdr; |
| 342 | |
| 343 | phdrs = kzalloc(phdrs_len, GFP_KERNEL); |
| 344 | if (!phdrs) { |
| 345 | ret = -ENOMEM; |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 346 | goto out; |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | phdrs[0].p_type = PT_NOTE; |
| 350 | phdrs[0].p_offset = notes_offset; |
| 351 | phdrs[0].p_filesz = notes_len; |
| 352 | |
| 353 | phdr = &phdrs[1]; |
| 354 | list_for_each_entry(m, &kclist_head, list) { |
| 355 | phdr->p_type = PT_LOAD; |
| 356 | phdr->p_flags = PF_R | PF_W | PF_X; |
| 357 | phdr->p_offset = kc_vaddr_to_offset(m->addr) + data_offset; |
| 358 | phdr->p_vaddr = (size_t)m->addr; |
| 359 | if (m->type == KCORE_RAM) |
| 360 | phdr->p_paddr = __pa(m->addr); |
| 361 | else if (m->type == KCORE_TEXT) |
| 362 | phdr->p_paddr = __pa_symbol(m->addr); |
| 363 | else |
| 364 | phdr->p_paddr = (elf_addr_t)-1; |
| 365 | phdr->p_filesz = phdr->p_memsz = m->size; |
| 366 | phdr->p_align = PAGE_SIZE; |
| 367 | phdr++; |
| 368 | } |
| 369 | |
| 370 | tsz = min_t(size_t, buflen, phdrs_offset + phdrs_len - *fpos); |
| 371 | if (copy_to_user(buffer, (char *)phdrs + *fpos - phdrs_offset, |
| 372 | tsz)) { |
| 373 | kfree(phdrs); |
| 374 | ret = -EFAULT; |
| 375 | goto out; |
| 376 | } |
| 377 | kfree(phdrs); |
| 378 | |
| 379 | buffer += tsz; |
| 380 | buflen -= tsz; |
| 381 | *fpos += tsz; |
| 382 | } |
| 383 | |
| 384 | /* ELF note segment. */ |
| 385 | if (buflen && *fpos < notes_offset + notes_len) { |
| 386 | struct elf_prstatus prstatus = {}; |
| 387 | struct elf_prpsinfo prpsinfo = { |
| 388 | .pr_sname = 'R', |
| 389 | .pr_fname = "vmlinux", |
| 390 | }; |
| 391 | char *notes; |
| 392 | size_t i = 0; |
| 393 | |
| 394 | strlcpy(prpsinfo.pr_psargs, saved_command_line, |
| 395 | sizeof(prpsinfo.pr_psargs)); |
| 396 | |
| 397 | notes = kzalloc(notes_len, GFP_KERNEL); |
| 398 | if (!notes) { |
| 399 | ret = -ENOMEM; |
| 400 | goto out; |
| 401 | } |
| 402 | |
| 403 | append_kcore_note(notes, &i, CORE_STR, NT_PRSTATUS, &prstatus, |
| 404 | sizeof(prstatus)); |
| 405 | append_kcore_note(notes, &i, CORE_STR, NT_PRPSINFO, &prpsinfo, |
| 406 | sizeof(prpsinfo)); |
| 407 | append_kcore_note(notes, &i, CORE_STR, NT_TASKSTRUCT, current, |
| 408 | arch_task_struct_size); |
| 409 | |
| 410 | tsz = min_t(size_t, buflen, notes_offset + notes_len - *fpos); |
| 411 | if (copy_to_user(buffer, notes + *fpos - notes_offset, tsz)) { |
| 412 | kfree(notes); |
| 413 | ret = -EFAULT; |
| 414 | goto out; |
| 415 | } |
| 416 | kfree(notes); |
| 417 | |
| 418 | buffer += tsz; |
| 419 | buflen -= tsz; |
| 420 | *fpos += tsz; |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Check to see if our file offset matches with any of |
| 425 | * the addresses in the elf_phdr on our list. |
| 426 | */ |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 427 | start = kc_offset_to_vaddr(*fpos - data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen) |
| 429 | tsz = buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Omar Sandoval | bf991c2 | 2018-08-21 21:55:13 -0700 | [diff] [blame^] | 431 | m = NULL; |
Omar Sandoval | 37e949b | 2018-08-21 21:55:09 -0700 | [diff] [blame] | 432 | while (buflen) { |
Omar Sandoval | bf991c2 | 2018-08-21 21:55:13 -0700 | [diff] [blame^] | 433 | /* |
| 434 | * If this is the first iteration or the address is not within |
| 435 | * the previous entry, search for a matching entry. |
| 436 | */ |
| 437 | if (!m || start < m->addr || start >= m->addr + m->size) { |
| 438 | list_for_each_entry(m, &kclist_head, list) { |
| 439 | if (start >= m->addr && |
| 440 | start < m->addr + m->size) |
| 441 | break; |
| 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Dan Carpenter | 4fd2c20 | 2010-03-23 13:35:42 -0700 | [diff] [blame] | 445 | if (&m->list == &kclist_head) { |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 446 | if (clear_user(buffer, tsz)) { |
| 447 | ret = -EFAULT; |
| 448 | goto out; |
| 449 | } |
Ard Biesheuvel | 737326a | 2017-06-14 12:43:54 +0200 | [diff] [blame] | 450 | } else if (m->type == KCORE_VMALLOC) { |
Jiri Olsa | f5beeb1 | 2016-09-08 09:57:07 +0200 | [diff] [blame] | 451 | vread(buf, (char *)start, tsz); |
KAMEZAWA Hiroyuki | 73d7c33 | 2009-09-21 17:02:35 -0700 | [diff] [blame] | 452 | /* we have to zero-fill user buffer even if no read */ |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 453 | if (copy_to_user(buffer, buf, tsz)) { |
| 454 | ret = -EFAULT; |
| 455 | goto out; |
| 456 | } |
Jia Zhang | 595dd46 | 2018-02-12 22:44:53 +0800 | [diff] [blame] | 457 | } else if (m->type == KCORE_USER) { |
| 458 | /* User page is handled prior to normal kernel page: */ |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 459 | if (copy_to_user(buffer, (char *)start, tsz)) { |
| 460 | ret = -EFAULT; |
| 461 | goto out; |
| 462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } else { |
| 464 | if (kern_addr_valid(start)) { |
Jiri Olsa | df04abf | 2016-09-08 09:57:08 +0200 | [diff] [blame] | 465 | /* |
| 466 | * Using bounce buffer to bypass the |
| 467 | * hardened user copy kernel text checks. |
| 468 | */ |
Heiko Carstens | d0290bc | 2018-02-06 15:37:13 -0800 | [diff] [blame] | 469 | if (probe_kernel_read(buf, (void *) start, tsz)) { |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 470 | if (clear_user(buffer, tsz)) { |
| 471 | ret = -EFAULT; |
| 472 | goto out; |
| 473 | } |
Heiko Carstens | d0290bc | 2018-02-06 15:37:13 -0800 | [diff] [blame] | 474 | } else { |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 475 | if (copy_to_user(buffer, buf, tsz)) { |
| 476 | ret = -EFAULT; |
| 477 | goto out; |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | } else { |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 481 | if (clear_user(buffer, tsz)) { |
| 482 | ret = -EFAULT; |
| 483 | goto out; |
| 484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | buflen -= tsz; |
| 488 | *fpos += tsz; |
| 489 | buffer += tsz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | start += tsz; |
| 491 | tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen); |
| 492 | } |
| 493 | |
Omar Sandoval | 3673fb0 | 2018-08-21 21:55:06 -0700 | [diff] [blame] | 494 | out: |
| 495 | up_read(&kclist_lock); |
| 496 | if (ret) |
| 497 | return ret; |
| 498 | return orig_buflen - buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
Alexey Dobriyan | 97ce5d6 | 2008-10-06 14:14:19 +0400 | [diff] [blame] | 500 | |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 501 | static int open_kcore(struct inode *inode, struct file *filp) |
| 502 | { |
| 503 | if (!capable(CAP_SYS_RAWIO)) |
| 504 | return -EPERM; |
Jiri Olsa | f5beeb1 | 2016-09-08 09:57:07 +0200 | [diff] [blame] | 505 | |
| 506 | filp->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 507 | if (!filp->private_data) |
| 508 | return -ENOMEM; |
| 509 | |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 510 | if (kcore_need_update) |
| 511 | kcore_update_ram(); |
KAMEZAWA Hiroyuki | 0d4c36a | 2009-09-22 16:45:52 -0700 | [diff] [blame] | 512 | if (i_size_read(inode) != proc_root_kcore->size) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 513 | inode_lock(inode); |
KAMEZAWA Hiroyuki | 0d4c36a | 2009-09-22 16:45:52 -0700 | [diff] [blame] | 514 | i_size_write(inode, proc_root_kcore->size); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 515 | inode_unlock(inode); |
KAMEZAWA Hiroyuki | 0d4c36a | 2009-09-22 16:45:52 -0700 | [diff] [blame] | 516 | } |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
Jiri Olsa | f5beeb1 | 2016-09-08 09:57:07 +0200 | [diff] [blame] | 520 | static int release_kcore(struct inode *inode, struct file *file) |
| 521 | { |
| 522 | kfree(file->private_data); |
| 523 | return 0; |
| 524 | } |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 525 | |
| 526 | static const struct file_operations proc_kcore_operations = { |
| 527 | .read = read_kcore, |
| 528 | .open = open_kcore, |
Jiri Olsa | f5beeb1 | 2016-09-08 09:57:07 +0200 | [diff] [blame] | 529 | .release = release_kcore, |
Dave Anderson | ceff1a7 | 2011-01-12 17:00:36 -0800 | [diff] [blame] | 530 | .llseek = default_llseek, |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 531 | }; |
| 532 | |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 533 | /* just remember that we have to update kcore */ |
| 534 | static int __meminit kcore_callback(struct notifier_block *self, |
| 535 | unsigned long action, void *arg) |
| 536 | { |
| 537 | switch (action) { |
| 538 | case MEM_ONLINE: |
| 539 | case MEM_OFFLINE: |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 540 | kcore_need_update = 1; |
Omar Sandoval | bf53183 | 2018-08-21 21:54:55 -0700 | [diff] [blame] | 541 | break; |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 542 | } |
| 543 | return NOTIFY_OK; |
| 544 | } |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 545 | |
Andrew Morton | 3c743a7 | 2013-04-29 15:08:08 -0700 | [diff] [blame] | 546 | static struct notifier_block kcore_callback_nb __meminitdata = { |
| 547 | .notifier_call = kcore_callback, |
| 548 | .priority = 0, |
| 549 | }; |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 550 | |
KAMEZAWA Hiroyuki | a0614da | 2009-09-22 16:45:44 -0700 | [diff] [blame] | 551 | static struct kcore_list kcore_vmalloc; |
| 552 | |
KAMEZAWA Hiroyuki | 9492587 | 2009-09-22 16:45:45 -0700 | [diff] [blame] | 553 | #ifdef CONFIG_ARCH_PROC_KCORE_TEXT |
| 554 | static struct kcore_list kcore_text; |
| 555 | /* |
| 556 | * If defined, special segment is used for mapping kernel text instead of |
| 557 | * direct-map area. We need to create special TEXT section. |
| 558 | */ |
| 559 | static void __init proc_kcore_text_init(void) |
| 560 | { |
Wu Fengguang | 36e1526 | 2010-05-26 14:43:27 -0700 | [diff] [blame] | 561 | kclist_add(&kcore_text, _text, _end - _text, KCORE_TEXT); |
KAMEZAWA Hiroyuki | 9492587 | 2009-09-22 16:45:45 -0700 | [diff] [blame] | 562 | } |
| 563 | #else |
| 564 | static void __init proc_kcore_text_init(void) |
| 565 | { |
| 566 | } |
| 567 | #endif |
| 568 | |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 569 | #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) |
| 570 | /* |
| 571 | * MODULES_VADDR has no intersection with VMALLOC_ADDR. |
| 572 | */ |
| 573 | struct kcore_list kcore_modules; |
| 574 | static void __init add_modules_range(void) |
| 575 | { |
Baoquan He | bf3e269 | 2014-10-09 15:25:56 -0700 | [diff] [blame] | 576 | if (MODULES_VADDR != VMALLOC_START && MODULES_END != VMALLOC_END) { |
| 577 | kclist_add(&kcore_modules, (void *)MODULES_VADDR, |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 578 | MODULES_END - MODULES_VADDR, KCORE_VMALLOC); |
Baoquan He | bf3e269 | 2014-10-09 15:25:56 -0700 | [diff] [blame] | 579 | } |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 580 | } |
| 581 | #else |
| 582 | static void __init add_modules_range(void) |
| 583 | { |
| 584 | } |
| 585 | #endif |
| 586 | |
Alexey Dobriyan | 97ce5d6 | 2008-10-06 14:14:19 +0400 | [diff] [blame] | 587 | static int __init proc_kcore_init(void) |
| 588 | { |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 589 | proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, |
| 590 | &proc_kcore_operations); |
KAMEZAWA Hiroyuki | 90396f9 | 2009-09-22 16:45:50 -0700 | [diff] [blame] | 591 | if (!proc_root_kcore) { |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 592 | pr_err("couldn't create /proc/kcore\n"); |
KAMEZAWA Hiroyuki | 90396f9 | 2009-09-22 16:45:50 -0700 | [diff] [blame] | 593 | return 0; /* Always returns 0. */ |
| 594 | } |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 595 | /* Store text area if it's special */ |
KAMEZAWA Hiroyuki | 9492587 | 2009-09-22 16:45:45 -0700 | [diff] [blame] | 596 | proc_kcore_text_init(); |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 597 | /* Store vmalloc area */ |
KAMEZAWA Hiroyuki | a0614da | 2009-09-22 16:45:44 -0700 | [diff] [blame] | 598 | kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, |
| 599 | VMALLOC_END - VMALLOC_START, KCORE_VMALLOC); |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 600 | add_modules_range(); |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 601 | /* Store direct-map area from physical memory map */ |
| 602 | kcore_update_ram(); |
Andrew Morton | 3c743a7 | 2013-04-29 15:08:08 -0700 | [diff] [blame] | 603 | register_hotmemory_notifier(&kcore_callback_nb); |
KAMEZAWA Hiroyuki | 3089aa1 | 2009-09-22 16:45:48 -0700 | [diff] [blame] | 604 | |
Alexey Dobriyan | 97ce5d6 | 2008-10-06 14:14:19 +0400 | [diff] [blame] | 605 | return 0; |
| 606 | } |
Paul Gortmaker | abaf378 | 2014-01-23 15:55:45 -0800 | [diff] [blame] | 607 | fs_initcall(proc_kcore_init); |