blob: 758c14e46a44ebbd1121e6080a1b53157ef8b3c2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
14#include <linux/proc_fs.h>
David Howells2f96b8c2013-04-12 00:10:25 +010015#include <linux/kcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/user.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/elf.h>
19#include <linux/elfcore.h>
Andrew Morton3c743a72013-04-29 15:08:08 -070020#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/vmalloc.h>
22#include <linux/highmem.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080023#include <linux/printk.h>
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -070024#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/io.h>
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070029#include <linux/list.h>
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -070030#include <linux/ioport.h>
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -070031#include <linux/memory.h>
Ingo Molnar29930022017-02-08 18:51:36 +010032#include <linux/sched/task.h>
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -070033#include <asm/sections.h>
David Howells59d80532013-04-11 13:34:43 +010034#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Magnus Damm36027602006-12-06 20:38:00 -080036#define CORE_STR "CORE"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Edgar E. Iglesias79885b22008-07-25 01:48:10 -070038#ifndef ELF_CORE_EFLAGS
39#define ELF_CORE_EFLAGS 0
40#endif
41
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +040042static struct proc_dir_entry *proc_root_kcore;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
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 Hiroyuki2ef43ec2009-09-22 16:45:41 -070052static LIST_HEAD(kclist_head);
Omar Sandoval0b172f82018-08-21 21:54:59 -070053static DECLARE_RWSEM(kclist_lock);
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -070054static int kcore_need_update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Omar Sandovala8dd9c42018-08-21 21:54:51 -070056/* This doesn't grab kclist_lock, so it should only be used at init time. */
57void __init kclist_add(struct kcore_list *new, void *addr, size_t size,
58 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 new->addr = (unsigned long)addr;
61 new->size = size;
KAMEZAWA Hiroyukic30bb2a2009-09-22 16:45:43 -070062 new->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070064 list_add_tail(&new->list, &kclist_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Omar Sandoval37e949b2018-08-21 21:55:09 -070067static size_t get_kcore_size(int *nphdr, size_t *phdrs_len, size_t *notes_len,
68 size_t *data_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 size_t try, size;
71 struct kcore_list *m;
72
73 *nphdr = 1; /* PT_NOTE */
74 size = 0;
75
KAMEZAWA Hiroyuki2ef43ec2009-09-22 16:45:41 -070076 list_for_each_entry(m, &kclist_head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 try = kc_vaddr_to_offset((size_t)m->addr + m->size);
78 if (try > size)
79 size = try;
80 *nphdr = *nphdr + 1;
81 }
Omar Sandoval37e949b2018-08-21 21:55:09 -070082
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 Torvalds1da177e2005-04-16 15:20:36 -070091}
92
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -070093#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 Sandovalb66fb002018-08-21 21:55:02 -070099static int kcore_ram_list(struct list_head *head)
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700100{
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700101 struct kcore_list *ent;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700102
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 Sandovalb66fb002018-08-21 21:55:02 -0700109 list_add(&ent->list, head);
110 return 0;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700111}
112
113#else /* !CONFIG_HIGHMEM */
114
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700115#ifdef CONFIG_SPARSEMEM_VMEMMAP
116/* calculate vmemmap's address from given system ram pfn and register it */
Djalal Harounib9082432012-03-23 15:02:52 -0700117static int
118get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700119{
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 Frederick108a8a12014-08-08 14:21:20 -0700128 end = PAGE_ALIGN(end);
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700129 /* 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 Harounib9082432012-03-23 15:02:52 -0700150static int
151get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
KAMEZAWA Hiroyuki26562c52009-09-22 16:45:49 -0700152{
153 return 1;
154}
155
156#endif
157
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700158static int
159kclist_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 Abbott39553332018-05-11 16:01:57 -0700163 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 Hiroyuki3089aa12009-09-22 16:45:48 -0700171
172 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
173 if (!ent)
174 return -ENOMEM;
Laura Abbott39553332018-05-11 16:01:57 -0700175 ent->addr = (unsigned long)page_to_virt(p);
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700176 ent->size = nr_pages << PAGE_SHIFT;
177
Laura Abbott39553332018-05-11 16:01:57 -0700178 if (!virt_addr_valid(ent->addr))
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700179 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 Abbott39553332018-05-11 16:01:57 -0700185 /*
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 Hiroyuki3089aa12009-09-22 16:45:48 -0700191 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 Hiroyuki26562c52009-09-22 16:45:49 -0700197
198 if (!get_sparsemem_vmemmap_info(ent, head)) {
199 list_del(&ent->list);
200 goto free_out;
201 }
202
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700203 return 0;
204free_out:
205 kfree(ent);
206 return 1;
207}
208
Omar Sandovalb66fb002018-08-21 21:55:02 -0700209static int kcore_ram_list(struct list_head *list)
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700210{
211 int nid, ret;
212 unsigned long end_pfn;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700213
214 /* Not inialized....update now */
215 /* find out "max pfn" */
216 end_pfn = 0;
Lai Jiangshan4ff1b2c2012-12-12 13:51:25 -0800217 for_each_node_state(nid, N_MEMORY) {
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700218 unsigned long node_end;
Xishi Qiu83285c72013-11-12 15:07:19 -0800219 node_end = node_end_pfn(nid);
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700220 if (end_pfn < node_end)
221 end_pfn = node_end;
222 }
223 /* scan 0 to max_pfn */
Omar Sandovalb66fb002018-08-21 21:55:02 -0700224 ret = walk_system_ram_range(0, end_pfn, list, kclist_add_private);
225 if (ret)
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700226 return -ENOMEM;
Omar Sandovalb66fb002018-08-21 21:55:02 -0700227 return 0;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700228}
229#endif /* CONFIG_HIGHMEM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Omar Sandovalb66fb002018-08-21 21:55:02 -0700231static int kcore_update_ram(void)
232{
233 LIST_HEAD(list);
234 LIST_HEAD(garbage);
235 int nphdr;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700236 size_t phdrs_len, notes_len, data_offset;
Omar Sandovalb66fb002018-08-21 21:55:02 -0700237 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 Sandoval37e949b2018-08-21 21:55:09 -0700258 proc_root_kcore->size = get_kcore_size(&nphdr, &phdrs_len, &notes_len,
259 &data_offset);
Omar Sandovalb66fb002018-08-21 21:55:02 -0700260
261out:
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 Sandoval37e949b2018-08-21 21:55:09 -0700270static void append_kcore_note(char *notes, size_t *i, const char *name,
271 unsigned int type, const void *desc,
272 size_t descsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Omar Sandoval37e949b2018-08-21 21:55:09 -0700274 struct elf_note *note = (struct elf_note *)&notes[*i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Omar Sandoval37e949b2018-08-21 21:55:09 -0700276 note->n_namesz = strlen(name) + 1;
277 note->n_descsz = descsz;
278 note->n_type = type;
279 *i += sizeof(*note);
280 memcpy(&notes[*i], name, note->n_namesz);
281 *i = ALIGN(*i + note->n_namesz, 4);
282 memcpy(&notes[*i], desc, descsz);
283 *i = ALIGN(*i + descsz, 4);
284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static ssize_t
287read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
288{
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200289 char *buf = file->private_data;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700290 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 Torvalds1da177e2005-04-16 15:20:36 -0700294 int nphdr;
295 unsigned long start;
Omar Sandoval3673fb02018-08-21 21:55:06 -0700296 size_t orig_buflen = buflen;
297 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Omar Sandoval0b172f82018-08-21 21:54:59 -0700299 down_read(&kclist_lock);
KAMEZAWA Hiroyuki678ad5d2009-09-22 16:45:51 -0700300
Omar Sandoval37e949b2018-08-21 21:55:09 -0700301 get_kcore_size(&nphdr, &phdrs_len, &notes_len, &data_offset);
302 phdrs_offset = sizeof(struct elfhdr);
303 notes_offset = phdrs_offset + phdrs_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Omar Sandoval37e949b2018-08-21 21:55:09 -0700305 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700327
Omar Sandoval37e949b2018-08-21 21:55:09 -0700328 tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos);
329 if (copy_to_user(buffer, (char *)&ehdr + *fpos, tsz)) {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700330 ret = -EFAULT;
331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Omar Sandoval37e949b2018-08-21 21:55:09 -0700333
334 buffer += tsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 buflen -= tsz;
336 *fpos += tsz;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Omar Sandoval37e949b2018-08-21 21:55:09 -0700339 /* 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 Sandoval3673fb02018-08-21 21:55:06 -0700346 goto out;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700347 }
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 Sandoval3673fb02018-08-21 21:55:06 -0700421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
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 Sandoval37e949b2018-08-21 21:55:09 -0700427 start = kc_offset_to_vaddr(*fpos - data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
429 tsz = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Omar Sandovalbf991c22018-08-21 21:55:13 -0700431 m = NULL;
Omar Sandoval37e949b2018-08-21 21:55:09 -0700432 while (buflen) {
Omar Sandovalbf991c22018-08-21 21:55:13 -0700433 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dan Carpenter4fd2c202010-03-23 13:35:42 -0700445 if (&m->list == &kclist_head) {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700446 if (clear_user(buffer, tsz)) {
447 ret = -EFAULT;
448 goto out;
449 }
Ard Biesheuvel737326a2017-06-14 12:43:54 +0200450 } else if (m->type == KCORE_VMALLOC) {
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200451 vread(buf, (char *)start, tsz);
KAMEZAWA Hiroyuki73d7c332009-09-21 17:02:35 -0700452 /* we have to zero-fill user buffer even if no read */
Omar Sandoval3673fb02018-08-21 21:55:06 -0700453 if (copy_to_user(buffer, buf, tsz)) {
454 ret = -EFAULT;
455 goto out;
456 }
Jia Zhang595dd462018-02-12 22:44:53 +0800457 } else if (m->type == KCORE_USER) {
458 /* User page is handled prior to normal kernel page: */
Omar Sandoval3673fb02018-08-21 21:55:06 -0700459 if (copy_to_user(buffer, (char *)start, tsz)) {
460 ret = -EFAULT;
461 goto out;
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 } else {
464 if (kern_addr_valid(start)) {
Jiri Olsadf04abf2016-09-08 09:57:08 +0200465 /*
466 * Using bounce buffer to bypass the
467 * hardened user copy kernel text checks.
468 */
Heiko Carstensd0290bc2018-02-06 15:37:13 -0800469 if (probe_kernel_read(buf, (void *) start, tsz)) {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700470 if (clear_user(buffer, tsz)) {
471 ret = -EFAULT;
472 goto out;
473 }
Heiko Carstensd0290bc2018-02-06 15:37:13 -0800474 } else {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700475 if (copy_to_user(buffer, buf, tsz)) {
476 ret = -EFAULT;
477 goto out;
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 } else {
Omar Sandoval3673fb02018-08-21 21:55:06 -0700481 if (clear_user(buffer, tsz)) {
482 ret = -EFAULT;
483 goto out;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 }
487 buflen -= tsz;
488 *fpos += tsz;
489 buffer += tsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 start += tsz;
491 tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
492 }
493
Omar Sandoval3673fb02018-08-21 21:55:06 -0700494out:
495 up_read(&kclist_lock);
496 if (ret)
497 return ret;
498 return orig_buflen - buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400500
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700501static int open_kcore(struct inode *inode, struct file *filp)
502{
503 if (!capable(CAP_SYS_RAWIO))
504 return -EPERM;
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200505
506 filp->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
507 if (!filp->private_data)
508 return -ENOMEM;
509
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700510 if (kcore_need_update)
511 kcore_update_ram();
KAMEZAWA Hiroyuki0d4c36a2009-09-22 16:45:52 -0700512 if (i_size_read(inode) != proc_root_kcore->size) {
Al Viro59551022016-01-22 15:40:57 -0500513 inode_lock(inode);
KAMEZAWA Hiroyuki0d4c36a2009-09-22 16:45:52 -0700514 i_size_write(inode, proc_root_kcore->size);
Al Viro59551022016-01-22 15:40:57 -0500515 inode_unlock(inode);
KAMEZAWA Hiroyuki0d4c36a2009-09-22 16:45:52 -0700516 }
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700517 return 0;
518}
519
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200520static int release_kcore(struct inode *inode, struct file *file)
521{
522 kfree(file->private_data);
523 return 0;
524}
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700525
526static const struct file_operations proc_kcore_operations = {
527 .read = read_kcore,
528 .open = open_kcore,
Jiri Olsaf5beeb12016-09-08 09:57:07 +0200529 .release = release_kcore,
Dave Andersonceff1a72011-01-12 17:00:36 -0800530 .llseek = default_llseek,
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700531};
532
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700533/* just remember that we have to update kcore */
534static 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 Hiroyuki3089aa12009-09-22 16:45:48 -0700540 kcore_need_update = 1;
Omar Sandovalbf531832018-08-21 21:54:55 -0700541 break;
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700542 }
543 return NOTIFY_OK;
544}
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700545
Andrew Morton3c743a72013-04-29 15:08:08 -0700546static struct notifier_block kcore_callback_nb __meminitdata = {
547 .notifier_call = kcore_callback,
548 .priority = 0,
549};
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700550
KAMEZAWA Hiroyukia0614da2009-09-22 16:45:44 -0700551static struct kcore_list kcore_vmalloc;
552
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -0700553#ifdef CONFIG_ARCH_PROC_KCORE_TEXT
554static 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 */
559static void __init proc_kcore_text_init(void)
560{
Wu Fengguang36e15262010-05-26 14:43:27 -0700561 kclist_add(&kcore_text, _text, _end - _text, KCORE_TEXT);
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -0700562}
563#else
564static void __init proc_kcore_text_init(void)
565{
566}
567#endif
568
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700569#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
570/*
571 * MODULES_VADDR has no intersection with VMALLOC_ADDR.
572 */
573struct kcore_list kcore_modules;
574static void __init add_modules_range(void)
575{
Baoquan Hebf3e2692014-10-09 15:25:56 -0700576 if (MODULES_VADDR != VMALLOC_START && MODULES_END != VMALLOC_END) {
577 kclist_add(&kcore_modules, (void *)MODULES_VADDR,
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700578 MODULES_END - MODULES_VADDR, KCORE_VMALLOC);
Baoquan Hebf3e2692014-10-09 15:25:56 -0700579 }
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700580}
581#else
582static void __init add_modules_range(void)
583{
584}
585#endif
586
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400587static int __init proc_kcore_init(void)
588{
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700589 proc_root_kcore = proc_create("kcore", S_IRUSR, NULL,
590 &proc_kcore_operations);
KAMEZAWA Hiroyuki90396f92009-09-22 16:45:50 -0700591 if (!proc_root_kcore) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800592 pr_err("couldn't create /proc/kcore\n");
KAMEZAWA Hiroyuki90396f92009-09-22 16:45:50 -0700593 return 0; /* Always returns 0. */
594 }
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700595 /* Store text area if it's special */
KAMEZAWA Hiroyuki94925872009-09-22 16:45:45 -0700596 proc_kcore_text_init();
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700597 /* Store vmalloc area */
KAMEZAWA Hiroyukia0614da2009-09-22 16:45:44 -0700598 kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
599 VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700600 add_modules_range();
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700601 /* Store direct-map area from physical memory map */
602 kcore_update_ram();
Andrew Morton3c743a72013-04-29 15:08:08 -0700603 register_hotmemory_notifier(&kcore_callback_nb);
KAMEZAWA Hiroyuki3089aa12009-09-22 16:45:48 -0700604
Alexey Dobriyan97ce5d62008-10-06 14:14:19 +0400605 return 0;
606}
Paul Gortmakerabaf3782014-01-23 15:55:45 -0800607fs_initcall(proc_kcore_init);