blob: f5834488b67d564d91b5534c210fc7fcd8e95969 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Omar Sandoval23c85092018-08-21 21:55:20 -070013#include <linux/crash_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
15#include <linux/proc_fs.h>
David Howells2f96b8c2013-04-12 00:10:25 +010016#include <linux/kcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/user.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/elf.h>
20#include <linux/elfcore.h>
Andrew Morton3c743a72013-04-29 15:08:08 -070021#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/vmalloc.h>
23#include <linux/highmem.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080024#include <linux/printk.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070025#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/io.h>
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070030#include <linux/list.h>
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -070031#include <linux/ioport.h>
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -070032#include <linux/memory.h>
Ingo Molnar29930022017-02-08 18:51:36 +010033#include <linux/sched/task.h>
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -070034#include <asm/sections.h>
David Howells59d80532013-04-11 13:34:43 +010035#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Magnus Damm36027602006-12-06 20:38:00 -080037#define CORE_STR "CORE"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Edgar E. Iglesias79885b22008-07-25 01:48:10 -070039#ifndef ELF_CORE_EFLAGS
40#define ELF_CORE_EFLAGS 0
41#endif
42
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +040043static struct proc_dir_entry *proc_root_kcore;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#ifndef kc_vaddr_to_offset
47#define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET)
48#endif
49#ifndef kc_offset_to_vaddr
50#define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
51#endif
52
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070053static LIST_HEAD(kclist_head);
Omar Sandoval0b172f82018-08-21 21:54:59 -070054static DECLARE_RWSEM(kclist_lock);
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -070055static int kcore_need_update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Kairui Songffc85992019-03-08 11:05:08 +080057/*
58 * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error
59 * Same as oldmem_pfn_is_ram in vmcore
60 */
61static int (*mem_pfn_is_ram)(unsigned long pfn);
62
63int __init register_mem_pfn_is_ram(int (*fn)(unsigned long pfn))
64{
65 if (mem_pfn_is_ram)
66 return -EBUSY;
67 mem_pfn_is_ram = fn;
68 return 0;
69}
70
71static int pfn_is_ram(unsigned long pfn)
72{
73 if (mem_pfn_is_ram)
74 return mem_pfn_is_ram(pfn);
75 else
76 return 1;
77}
78
Omar Sandovala8dd9c42018-08-21 21:54:51 -070079/* This doesn't grab kclist_lock, so it should only be used at init time. */
80void __init kclist_add(struct kcore_list *new, void *addr, size_t size,
81 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 new->addr = (unsigned long)addr;
84 new->size = size;
KAMEZAWA Hiroyukic30bb2a2009-09-22 16:45:43 -070085 new->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070087 list_add_tail(&new->list, &kclist_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Omar Sandoval37e949b2018-08-21 21:55:09 -070090static size_t get_kcore_size(int *nphdr, size_t *phdrs_len, size_t *notes_len,
91 size_t *data_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 size_t try, size;
94 struct kcore_list *m;
95
96 *nphdr = 1; /* PT_NOTE */
97 size = 0;
98
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070099 list_for_each_entry(m, &kclist_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
101 if (try > size)
102 size = try;
103 *nphdr = *nphdr + 1;
104 }
Omar Sandoval37e949b2018-08-21 21:55:09 -0700105
106 *phdrs_len = *nphdr * sizeof(struct elf_phdr);
Omar Sandoval23c85092018-08-21 21:55:20 -0700107 *notes_len = (4 * sizeof(struct elf_note) +
108 3 * ALIGN(sizeof(CORE_STR), 4) +
109 VMCOREINFO_NOTE_NAME_BYTES +
Omar Sandoval37e949b2018-08-21 21:55:09 -0700110 ALIGN(sizeof(struct elf_prstatus), 4) +
111 ALIGN(sizeof(struct elf_prpsinfo), 4) +
Omar Sandoval23c85092018-08-21 21:55:20 -0700112 ALIGN(arch_task_struct_size, 4) +
113 ALIGN(vmcoreinfo_size, 4));
Omar Sandoval37e949b2018-08-21 21:55:09 -0700114 *data_offset = PAGE_ALIGN(sizeof(struct elfhdr) + *phdrs_len +
115 *notes_len);
116 return *data_offset + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700119#ifdef CONFIG_HIGHMEM
120/*
121 * If no highmem, we can assume [0...max_low_pfn) continuous range of memory
122 * because memory hole is not as big as !HIGHMEM case.
123 * (HIGHMEM is special because part of memory is _invisible_ from the kernel.)
124 */
Omar Sandovalb66fb002018-08-21 21:55:02 -0700125static int kcore_ram_list(struct list_head *head)
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700126{
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700127 struct kcore_list *ent;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700128
129 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
130 if (!ent)
131 return -ENOMEM;
132 ent->addr = (unsigned long)__va(0);
133 ent->size = max_low_pfn << PAGE_SHIFT;
134 ent->type = KCORE_RAM;
Omar Sandovalb66fb002018-08-21 21:55:02 -0700135 list_add(&ent->list, head);
136 return 0;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700137}
138
139#else /* !CONFIG_HIGHMEM */
140
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700141#ifdef CONFIG_SPARSEMEM_VMEMMAP
142/* calculate vmemmap's address from given system ram pfn and register it */
Djalal Harounib9082432012-03-23 15:02:52 -0700143static int
144get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700145{
146 unsigned long pfn = __pa(ent->addr) >> PAGE_SHIFT;
147 unsigned long nr_pages = ent->size >> PAGE_SHIFT;
148 unsigned long start, end;
149 struct kcore_list *vmm, *tmp;
150
151
152 start = ((unsigned long)pfn_to_page(pfn)) & PAGE_MASK;
153 end = ((unsigned long)pfn_to_page(pfn + nr_pages)) - 1;
Fabian Frederick108a8a12014-08-08 14:21:20 -0700154 end = PAGE_ALIGN(end);
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700155 /* overlap check (because we have to align page */
156 list_for_each_entry(tmp, head, list) {
157 if (tmp->type != KCORE_VMEMMAP)
158 continue;
159 if (start < tmp->addr + tmp->size)
160 if (end > tmp->addr)
161 end = tmp->addr;
162 }
163 if (start < end) {
164 vmm = kmalloc(sizeof(*vmm), GFP_KERNEL);
165 if (!vmm)
166 return 0;
167 vmm->addr = start;
168 vmm->size = end - start;
169 vmm->type = KCORE_VMEMMAP;
170 list_add_tail(&vmm->list, head);
171 }
172 return 1;
173
174}
175#else
Djalal Harounib9082432012-03-23 15:02:52 -0700176static int
177get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700178{
179 return 1;
180}
181
182#endif
183
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700184static int
185kclist_add_private(unsigned long pfn, unsigned long nr_pages, void *arg)
186{
187 struct list_head *head = (struct list_head *)arg;
188 struct kcore_list *ent;
Laura Abbott39553332018-05-11 16:01:57 -0700189 struct page *p;
190
191 if (!pfn_valid(pfn))
192 return 1;
193
194 p = pfn_to_page(pfn);
195 if (!memmap_valid_within(pfn, p, page_zone(p)))
196 return 1;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700197
198 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
199 if (!ent)
200 return -ENOMEM;
Laura Abbott39553332018-05-11 16:01:57 -0700201 ent->addr = (unsigned long)page_to_virt(p);
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700202 ent->size = nr_pages << PAGE_SHIFT;
203
Laura Abbott39553332018-05-11 16:01:57 -0700204 if (!virt_addr_valid(ent->addr))
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700205 goto free_out;
206
207 /* cut not-mapped area. ....from ppc-32 code. */
208 if (ULONG_MAX - ent->addr < ent->size)
209 ent->size = ULONG_MAX - ent->addr;
210
Laura Abbott39553332018-05-11 16:01:57 -0700211 /*
212 * We've already checked virt_addr_valid so we know this address
213 * is a valid pointer, therefore we can check against it to determine
214 * if we need to trim
215 */
216 if (VMALLOC_START > ent->addr) {
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700217 if (VMALLOC_START - ent->addr < ent->size)
218 ent->size = VMALLOC_START - ent->addr;
219 }
220
221 ent->type = KCORE_RAM;
222 list_add_tail(&ent->list, head);
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700223
224 if (!get_sparsemem_vmemmap_info(ent, head)) {
225 list_del(&ent->list);
226 goto free_out;
227 }
228
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700229 return 0;
230free_out:
231 kfree(ent);
232 return 1;
233}
234
Omar Sandovalb66fb002018-08-21 21:55:02 -0700235static int kcore_ram_list(struct list_head *list)
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700236{
237 int nid, ret;
238 unsigned long end_pfn;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700239
240 /* Not inialized....update now */
241 /* find out "max pfn" */
242 end_pfn = 0;
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -0800243 for_each_node_state(nid, N_MEMORY) {
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700244 unsigned long node_end;
Xishi Qiu83285c72013-11-12 15:07:19 -0800245 node_end = node_end_pfn(nid);
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700246 if (end_pfn < node_end)
247 end_pfn = node_end;
248 }
249 /* scan 0 to max_pfn */
Omar Sandovalb66fb002018-08-21 21:55:02 -0700250 ret = walk_system_ram_range(0, end_pfn, list, kclist_add_private);
251 if (ret)
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700252 return -ENOMEM;
Omar Sandovalb66fb002018-08-21 21:55:02 -0700253 return 0;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700254}
255#endif /* CONFIG_HIGHMEM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Omar Sandovalb66fb002018-08-21 21:55:02 -0700257static int kcore_update_ram(void)
258{
259 LIST_HEAD(list);
260 LIST_HEAD(garbage);
261 int nphdr;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700262 size_t phdrs_len, notes_len, data_offset;
Omar Sandovalb66fb002018-08-21 21:55:02 -0700263 struct kcore_list *tmp, *pos;
264 int ret = 0;
265
266 down_write(&kclist_lock);
267 if (!xchg(&kcore_need_update, 0))
268 goto out;
269
270 ret = kcore_ram_list(&list);
271 if (ret) {
272 /* Couldn't get the RAM list, try again next time. */
273 WRITE_ONCE(kcore_need_update, 1);
274 list_splice_tail(&list, &garbage);
275 goto out;
276 }
277
278 list_for_each_entry_safe(pos, tmp, &kclist_head, list) {
279 if (pos->type == KCORE_RAM || pos->type == KCORE_VMEMMAP)
280 list_move(&pos->list, &garbage);
281 }
282 list_splice_tail(&list, &kclist_head);
283
Omar Sandoval37e949b2018-08-21 21:55:09 -0700284 proc_root_kcore->size = get_kcore_size(&nphdr, &phdrs_len, &notes_len,
285 &data_offset);
Omar Sandovalb66fb002018-08-21 21:55:02 -0700286
287out:
288 up_write(&kclist_lock);
289 list_for_each_entry_safe(pos, tmp, &garbage, list) {
290 list_del(&pos->list);
291 kfree(pos);
292 }
293 return ret;
294}
295
Omar Sandoval37e949b2018-08-21 21:55:09 -0700296static void append_kcore_note(char *notes, size_t *i, const char *name,
297 unsigned int type, const void *desc,
298 size_t descsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Omar Sandoval37e949b2018-08-21 21:55:09 -0700300 struct elf_note *note = (struct elf_note *)&notes[*i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Omar Sandoval37e949b2018-08-21 21:55:09 -0700302 note->n_namesz = strlen(name) + 1;
303 note->n_descsz = descsz;
304 note->n_type = type;
305 *i += sizeof(*note);
306 memcpy(&notes[*i], name, note->n_namesz);
307 *i = ALIGN(*i + note->n_namesz, 4);
308 memcpy(&notes[*i], desc, descsz);
309 *i = ALIGN(*i + descsz, 4);
310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static ssize_t
313read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
314{
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200315 char *buf = file->private_data;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700316 size_t phdrs_offset, notes_offset, data_offset;
317 size_t phdrs_len, notes_len;
318 struct kcore_list *m;
319 size_t tsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int nphdr;
321 unsigned long start;
Omar Sandoval3673fb02018-08-21 21:55:06 -0700322 size_t orig_buflen = buflen;
323 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Omar Sandoval0b172f82018-08-21 21:54:59 -0700325 down_read(&kclist_lock);
KAMEZAWA Hiroyuki678ad5d2009-09-22 16:45:51 -0700326
Omar Sandoval37e949b2018-08-21 21:55:09 -0700327 get_kcore_size(&nphdr, &phdrs_len, &notes_len, &data_offset);
328 phdrs_offset = sizeof(struct elfhdr);
329 notes_offset = phdrs_offset + phdrs_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Omar Sandoval37e949b2018-08-21 21:55:09 -0700331 /* ELF file header. */
332 if (buflen && *fpos < sizeof(struct elfhdr)) {
333 struct elfhdr ehdr = {
334 .e_ident = {
335 [EI_MAG0] = ELFMAG0,
336 [EI_MAG1] = ELFMAG1,
337 [EI_MAG2] = ELFMAG2,
338 [EI_MAG3] = ELFMAG3,
339 [EI_CLASS] = ELF_CLASS,
340 [EI_DATA] = ELF_DATA,
341 [EI_VERSION] = EV_CURRENT,
342 [EI_OSABI] = ELF_OSABI,
343 },
344 .e_type = ET_CORE,
345 .e_machine = ELF_ARCH,
346 .e_version = EV_CURRENT,
347 .e_phoff = sizeof(struct elfhdr),
348 .e_flags = ELF_CORE_EFLAGS,
349 .e_ehsize = sizeof(struct elfhdr),
350 .e_phentsize = sizeof(struct elf_phdr),
351 .e_phnum = nphdr,
352 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Omar Sandoval37e949b2018-08-21 21:55:09 -0700354 tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos);
355 if (copy_to_user(buffer, (char *)&ehdr + *fpos, tsz)) {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700356 ret = -EFAULT;
357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Omar Sandoval37e949b2018-08-21 21:55:09 -0700359
360 buffer += tsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 buflen -= tsz;
362 *fpos += tsz;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Omar Sandoval37e949b2018-08-21 21:55:09 -0700365 /* ELF program headers. */
366 if (buflen && *fpos < phdrs_offset + phdrs_len) {
367 struct elf_phdr *phdrs, *phdr;
368
369 phdrs = kzalloc(phdrs_len, GFP_KERNEL);
370 if (!phdrs) {
371 ret = -ENOMEM;
Omar Sandoval3673fb02018-08-21 21:55:06 -0700372 goto out;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700373 }
374
375 phdrs[0].p_type = PT_NOTE;
376 phdrs[0].p_offset = notes_offset;
377 phdrs[0].p_filesz = notes_len;
378
379 phdr = &phdrs[1];
380 list_for_each_entry(m, &kclist_head, list) {
381 phdr->p_type = PT_LOAD;
382 phdr->p_flags = PF_R | PF_W | PF_X;
383 phdr->p_offset = kc_vaddr_to_offset(m->addr) + data_offset;
Linus Torvaldsd207ea82018-08-26 11:25:21 -0700384 if (m->type == KCORE_REMAP)
385 phdr->p_vaddr = (size_t)m->vaddr;
386 else
387 phdr->p_vaddr = (size_t)m->addr;
388 if (m->type == KCORE_RAM || m->type == KCORE_REMAP)
Omar Sandoval37e949b2018-08-21 21:55:09 -0700389 phdr->p_paddr = __pa(m->addr);
390 else if (m->type == KCORE_TEXT)
391 phdr->p_paddr = __pa_symbol(m->addr);
392 else
393 phdr->p_paddr = (elf_addr_t)-1;
394 phdr->p_filesz = phdr->p_memsz = m->size;
395 phdr->p_align = PAGE_SIZE;
396 phdr++;
397 }
398
399 tsz = min_t(size_t, buflen, phdrs_offset + phdrs_len - *fpos);
400 if (copy_to_user(buffer, (char *)phdrs + *fpos - phdrs_offset,
401 tsz)) {
402 kfree(phdrs);
403 ret = -EFAULT;
404 goto out;
405 }
406 kfree(phdrs);
407
408 buffer += tsz;
409 buflen -= tsz;
410 *fpos += tsz;
411 }
412
413 /* ELF note segment. */
414 if (buflen && *fpos < notes_offset + notes_len) {
415 struct elf_prstatus prstatus = {};
416 struct elf_prpsinfo prpsinfo = {
417 .pr_sname = 'R',
418 .pr_fname = "vmlinux",
419 };
420 char *notes;
421 size_t i = 0;
422
423 strlcpy(prpsinfo.pr_psargs, saved_command_line,
424 sizeof(prpsinfo.pr_psargs));
425
426 notes = kzalloc(notes_len, GFP_KERNEL);
427 if (!notes) {
428 ret = -ENOMEM;
429 goto out;
430 }
431
432 append_kcore_note(notes, &i, CORE_STR, NT_PRSTATUS, &prstatus,
433 sizeof(prstatus));
434 append_kcore_note(notes, &i, CORE_STR, NT_PRPSINFO, &prpsinfo,
435 sizeof(prpsinfo));
436 append_kcore_note(notes, &i, CORE_STR, NT_TASKSTRUCT, current,
437 arch_task_struct_size);
Omar Sandoval23c85092018-08-21 21:55:20 -0700438 /*
439 * vmcoreinfo_size is mostly constant after init time, but it
440 * can be changed by crash_save_vmcoreinfo(). Racing here with a
441 * panic on another CPU before the machine goes down is insanely
442 * unlikely, but it's better to not leave potential buffer
443 * overflows lying around, regardless.
444 */
445 append_kcore_note(notes, &i, VMCOREINFO_NOTE_NAME, 0,
446 vmcoreinfo_data,
447 min(vmcoreinfo_size, notes_len - i));
Omar Sandoval37e949b2018-08-21 21:55:09 -0700448
449 tsz = min_t(size_t, buflen, notes_offset + notes_len - *fpos);
450 if (copy_to_user(buffer, notes + *fpos - notes_offset, tsz)) {
451 kfree(notes);
452 ret = -EFAULT;
453 goto out;
454 }
455 kfree(notes);
456
457 buffer += tsz;
458 buflen -= tsz;
459 *fpos += tsz;
Omar Sandoval3673fb02018-08-21 21:55:06 -0700460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /*
463 * Check to see if our file offset matches with any of
464 * the addresses in the elf_phdr on our list.
465 */
Omar Sandoval37e949b2018-08-21 21:55:09 -0700466 start = kc_offset_to_vaddr(*fpos - data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
468 tsz = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Omar Sandovalbf991c22018-08-21 21:55:13 -0700470 m = NULL;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700471 while (buflen) {
Omar Sandovalbf991c22018-08-21 21:55:13 -0700472 /*
473 * If this is the first iteration or the address is not within
474 * the previous entry, search for a matching entry.
475 */
476 if (!m || start < m->addr || start >= m->addr + m->size) {
477 list_for_each_entry(m, &kclist_head, list) {
478 if (start >= m->addr &&
479 start < m->addr + m->size)
480 break;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Dan Carpenter4fd2c202010-03-23 13:35:42 -0700484 if (&m->list == &kclist_head) {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700485 if (clear_user(buffer, tsz)) {
486 ret = -EFAULT;
487 goto out;
488 }
Dominique Martineta1b3d2f2018-09-20 12:22:35 -0700489 m = NULL; /* skip the list anchor */
Kairui Songffc85992019-03-08 11:05:08 +0800490 } else if (!pfn_is_ram(__pa(start) >> PAGE_SHIFT)) {
491 if (clear_user(buffer, tsz)) {
492 ret = -EFAULT;
493 goto out;
494 }
Ard Biesheuvel737326a2017-06-14 12:43:54 +0200495 } else if (m->type == KCORE_VMALLOC) {
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200496 vread(buf, (char *)start, tsz);
KAMEZAWA Hiroyuki73d7c332009-09-21 17:02:35 -0700497 /* we have to zero-fill user buffer even if no read */
Omar Sandoval3673fb02018-08-21 21:55:06 -0700498 if (copy_to_user(buffer, buf, tsz)) {
499 ret = -EFAULT;
500 goto out;
501 }
Jia Zhang595dd462018-02-12 22:44:53 +0800502 } else if (m->type == KCORE_USER) {
503 /* User page is handled prior to normal kernel page: */
Omar Sandoval3673fb02018-08-21 21:55:06 -0700504 if (copy_to_user(buffer, (char *)start, tsz)) {
505 ret = -EFAULT;
506 goto out;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 } else {
509 if (kern_addr_valid(start)) {
Jiri Olsadf04abf2016-09-08 09:57:08 +0200510 /*
511 * Using bounce buffer to bypass the
512 * hardened user copy kernel text checks.
513 */
Heiko Carstensd0290bc2018-02-06 15:37:13 -0800514 if (probe_kernel_read(buf, (void *) start, tsz)) {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700515 if (clear_user(buffer, tsz)) {
516 ret = -EFAULT;
517 goto out;
518 }
Heiko Carstensd0290bc2018-02-06 15:37:13 -0800519 } else {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700520 if (copy_to_user(buffer, buf, tsz)) {
521 ret = -EFAULT;
522 goto out;
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 } else {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700526 if (clear_user(buffer, tsz)) {
527 ret = -EFAULT;
528 goto out;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 }
532 buflen -= tsz;
533 *fpos += tsz;
534 buffer += tsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 start += tsz;
536 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
537 }
538
Omar Sandoval3673fb02018-08-21 21:55:06 -0700539out:
540 up_read(&kclist_lock);
541 if (ret)
542 return ret;
543 return orig_buflen - buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400545
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700546static int open_kcore(struct inode *inode, struct file *filp)
547{
548 if (!capable(CAP_SYS_RAWIO))
549 return -EPERM;
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200550
551 filp->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
552 if (!filp->private_data)
553 return -ENOMEM;
554
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700555 if (kcore_need_update)
556 kcore_update_ram();
KAMEZAWA Hiroyuki0d4c36a2009-09-22 16:45:52 -0700557 if (i_size_read(inode) != proc_root_kcore->size) {
Al Viro59551022016-01-22 15:40:57 -0500558 inode_lock(inode);
KAMEZAWA Hiroyuki0d4c36a2009-09-22 16:45:52 -0700559 i_size_write(inode, proc_root_kcore->size);
Al Viro59551022016-01-22 15:40:57 -0500560 inode_unlock(inode);
KAMEZAWA Hiroyuki0d4c36a2009-09-22 16:45:52 -0700561 }
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700562 return 0;
563}
564
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200565static int release_kcore(struct inode *inode, struct file *file)
566{
567 kfree(file->private_data);
568 return 0;
569}
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700570
571static const struct file_operations proc_kcore_operations = {
572 .read = read_kcore,
573 .open = open_kcore,
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200574 .release = release_kcore,
Dave Andersonceff1a72011-01-12 17:00:36 -0800575 .llseek = default_llseek,
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700576};
577
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700578/* just remember that we have to update kcore */
579static int __meminit kcore_callback(struct notifier_block *self,
580 unsigned long action, void *arg)
581{
582 switch (action) {
583 case MEM_ONLINE:
584 case MEM_OFFLINE:
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700585 kcore_need_update = 1;
Omar Sandovalbf531832018-08-21 21:54:55 -0700586 break;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700587 }
588 return NOTIFY_OK;
589}
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700590
Andrew Morton3c743a72013-04-29 15:08:08 -0700591static struct notifier_block kcore_callback_nb __meminitdata = {
592 .notifier_call = kcore_callback,
593 .priority = 0,
594};
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700595
KAMEZAWA Hiroyukia0614da2009-09-22 16:45:44 -0700596static struct kcore_list kcore_vmalloc;
597
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -0700598#ifdef CONFIG_ARCH_PROC_KCORE_TEXT
599static struct kcore_list kcore_text;
600/*
601 * If defined, special segment is used for mapping kernel text instead of
602 * direct-map area. We need to create special TEXT section.
603 */
604static void __init proc_kcore_text_init(void)
605{
Wu Fengguang36e15262010-05-26 14:43:27 -0700606 kclist_add(&kcore_text, _text, _end - _text, KCORE_TEXT);
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -0700607}
608#else
609static void __init proc_kcore_text_init(void)
610{
611}
612#endif
613
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700614#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
615/*
616 * MODULES_VADDR has no intersection with VMALLOC_ADDR.
617 */
YueHaibingeebf3642019-03-28 20:44:09 -0700618static struct kcore_list kcore_modules;
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700619static void __init add_modules_range(void)
620{
Baoquan Hebf3e2692014-10-09 15:25:56 -0700621 if (MODULES_VADDR != VMALLOC_START && MODULES_END != VMALLOC_END) {
622 kclist_add(&kcore_modules, (void *)MODULES_VADDR,
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700623 MODULES_END - MODULES_VADDR, KCORE_VMALLOC);
Baoquan Hebf3e2692014-10-09 15:25:56 -0700624 }
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700625}
626#else
627static void __init add_modules_range(void)
628{
629}
630#endif
631
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400632static int __init proc_kcore_init(void)
633{
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700634 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL,
635 &proc_kcore_operations);
KAMEZAWA Hiroyuki90396f92009-09-22 16:45:50 -0700636 if (!proc_root_kcore) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800637 pr_err("couldn't create /proc/kcore\n");
KAMEZAWA Hiroyuki90396f92009-09-22 16:45:50 -0700638 return 0; /* Always returns 0. */
639 }
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700640 /* Store text area if it's special */
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -0700641 proc_kcore_text_init();
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700642 /* Store vmalloc area */
KAMEZAWA Hiroyukia0614da2009-09-22 16:45:44 -0700643 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
644 VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700645 add_modules_range();
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700646 /* Store direct-map area from physical memory map */
647 kcore_update_ram();
Andrew Morton3c743a72013-04-29 15:08:08 -0700648 register_hotmemory_notifier(&kcore_callback_nb);
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700649
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400650 return 0;
651}
Paul Gortmakerabaf3782014-01-23 15:55:45 -0800652fs_initcall(proc_kcore_init);