Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/proc/kcore.c kernel ELF core dumper |
| 3 | * |
| 4 | * Modelled on fs/exec.c:aout_core_dump() |
| 5 | * Jeremy Fitzhardinge <jeremy@sw.oz.au> |
| 6 | * ELF version written by David Howells <David.Howells@nexor.co.uk> |
| 7 | * Modified and incorporated into 2.3.x by Tigran Aivazian <tigran@veritas.com> |
| 8 | * Support to dump vmalloc'd areas (ELF only), Tigran Aivazian <tigran@veritas.com> |
| 9 | * Safe accesses to vmalloc/direct-mapped discontiguous areas, Kanoj Sarcar <kanoj@sgi.com> |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/mm.h> |
| 13 | #include <linux/proc_fs.h> |
| 14 | #include <linux/user.h> |
| 15 | #include <linux/a.out.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/elf.h> |
| 18 | #include <linux/elfcore.h> |
| 19 | #include <linux/vmalloc.h> |
| 20 | #include <linux/highmem.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <asm/uaccess.h> |
| 23 | #include <asm/io.h> |
| 24 | |
Magnus Damm | 3602760 | 2006-12-06 20:38:00 -0800 | [diff] [blame] | 25 | #define CORE_STR "CORE" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | static int open_kcore(struct inode * inode, struct file * filp) |
| 28 | { |
| 29 | return capable(CAP_SYS_RAWIO) ? 0 : -EPERM; |
| 30 | } |
| 31 | |
| 32 | static ssize_t read_kcore(struct file *, char __user *, size_t, loff_t *); |
| 33 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 34 | const struct file_operations proc_kcore_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | .read = read_kcore, |
| 36 | .open = open_kcore, |
| 37 | }; |
| 38 | |
| 39 | #ifndef kc_vaddr_to_offset |
| 40 | #define kc_vaddr_to_offset(v) ((v) - PAGE_OFFSET) |
| 41 | #endif |
| 42 | #ifndef kc_offset_to_vaddr |
| 43 | #define kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET) |
| 44 | #endif |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* An ELF note in memory */ |
| 47 | struct memelfnote |
| 48 | { |
| 49 | const char *name; |
| 50 | int type; |
| 51 | unsigned int datasz; |
| 52 | void *data; |
| 53 | }; |
| 54 | |
| 55 | static struct kcore_list *kclist; |
| 56 | static DEFINE_RWLOCK(kclist_lock); |
| 57 | |
| 58 | void |
| 59 | kclist_add(struct kcore_list *new, void *addr, size_t size) |
| 60 | { |
| 61 | new->addr = (unsigned long)addr; |
| 62 | new->size = size; |
| 63 | |
| 64 | write_lock(&kclist_lock); |
| 65 | new->next = kclist; |
| 66 | kclist = new; |
| 67 | write_unlock(&kclist_lock); |
| 68 | } |
| 69 | |
| 70 | static size_t get_kcore_size(int *nphdr, size_t *elf_buflen) |
| 71 | { |
| 72 | size_t try, size; |
| 73 | struct kcore_list *m; |
| 74 | |
| 75 | *nphdr = 1; /* PT_NOTE */ |
| 76 | size = 0; |
| 77 | |
| 78 | for (m=kclist; m; m=m->next) { |
| 79 | try = kc_vaddr_to_offset((size_t)m->addr + m->size); |
| 80 | if (try > size) |
| 81 | size = try; |
| 82 | *nphdr = *nphdr + 1; |
| 83 | } |
| 84 | *elf_buflen = sizeof(struct elfhdr) + |
| 85 | (*nphdr + 2)*sizeof(struct elf_phdr) + |
Magnus Damm | 3602760 | 2006-12-06 20:38:00 -0800 | [diff] [blame] | 86 | 3 * ((sizeof(struct elf_note)) + |
| 87 | roundup(sizeof(CORE_STR), 4)) + |
| 88 | roundup(sizeof(struct elf_prstatus), 4) + |
| 89 | roundup(sizeof(struct elf_prpsinfo), 4) + |
| 90 | roundup(sizeof(struct task_struct), 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | *elf_buflen = PAGE_ALIGN(*elf_buflen); |
| 92 | return size + *elf_buflen; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /*****************************************************************************/ |
| 97 | /* |
| 98 | * determine size of ELF note |
| 99 | */ |
| 100 | static int notesize(struct memelfnote *en) |
| 101 | { |
| 102 | int sz; |
| 103 | |
| 104 | sz = sizeof(struct elf_note); |
Vivek Goyal | 632dd20 | 2006-09-29 02:01:45 -0700 | [diff] [blame] | 105 | sz += roundup((strlen(en->name) + 1), 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | sz += roundup(en->datasz, 4); |
| 107 | |
| 108 | return sz; |
| 109 | } /* end notesize() */ |
| 110 | |
| 111 | /*****************************************************************************/ |
| 112 | /* |
| 113 | * store a note in the header buffer |
| 114 | */ |
| 115 | static char *storenote(struct memelfnote *men, char *bufp) |
| 116 | { |
| 117 | struct elf_note en; |
| 118 | |
| 119 | #define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0) |
| 120 | |
Vivek Goyal | 632dd20 | 2006-09-29 02:01:45 -0700 | [diff] [blame] | 121 | en.n_namesz = strlen(men->name) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | en.n_descsz = men->datasz; |
| 123 | en.n_type = men->type; |
| 124 | |
| 125 | DUMP_WRITE(&en, sizeof(en)); |
| 126 | DUMP_WRITE(men->name, en.n_namesz); |
| 127 | |
| 128 | /* XXX - cast from long long to long to avoid need for libgcc.a */ |
| 129 | bufp = (char*) roundup((unsigned long)bufp,4); |
| 130 | DUMP_WRITE(men->data, men->datasz); |
| 131 | bufp = (char*) roundup((unsigned long)bufp,4); |
| 132 | |
| 133 | #undef DUMP_WRITE |
| 134 | |
| 135 | return bufp; |
| 136 | } /* end storenote() */ |
| 137 | |
| 138 | /* |
| 139 | * store an ELF coredump header in the supplied buffer |
| 140 | * nphdr is the number of elf_phdr to insert |
| 141 | */ |
| 142 | static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff) |
| 143 | { |
| 144 | struct elf_prstatus prstatus; /* NT_PRSTATUS */ |
| 145 | struct elf_prpsinfo prpsinfo; /* NT_PRPSINFO */ |
| 146 | struct elf_phdr *nhdr, *phdr; |
| 147 | struct elfhdr *elf; |
| 148 | struct memelfnote notes[3]; |
| 149 | off_t offset = 0; |
| 150 | struct kcore_list *m; |
| 151 | |
| 152 | /* setup ELF header */ |
| 153 | elf = (struct elfhdr *) bufp; |
| 154 | bufp += sizeof(struct elfhdr); |
| 155 | offset += sizeof(struct elfhdr); |
| 156 | memcpy(elf->e_ident, ELFMAG, SELFMAG); |
| 157 | elf->e_ident[EI_CLASS] = ELF_CLASS; |
| 158 | elf->e_ident[EI_DATA] = ELF_DATA; |
| 159 | elf->e_ident[EI_VERSION]= EV_CURRENT; |
| 160 | elf->e_ident[EI_OSABI] = ELF_OSABI; |
| 161 | memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); |
| 162 | elf->e_type = ET_CORE; |
| 163 | elf->e_machine = ELF_ARCH; |
| 164 | elf->e_version = EV_CURRENT; |
| 165 | elf->e_entry = 0; |
| 166 | elf->e_phoff = sizeof(struct elfhdr); |
| 167 | elf->e_shoff = 0; |
| 168 | #if defined(CONFIG_H8300) |
| 169 | elf->e_flags = ELF_FLAGS; |
| 170 | #else |
| 171 | elf->e_flags = 0; |
| 172 | #endif |
| 173 | elf->e_ehsize = sizeof(struct elfhdr); |
| 174 | elf->e_phentsize= sizeof(struct elf_phdr); |
| 175 | elf->e_phnum = nphdr; |
| 176 | elf->e_shentsize= 0; |
| 177 | elf->e_shnum = 0; |
| 178 | elf->e_shstrndx = 0; |
| 179 | |
| 180 | /* setup ELF PT_NOTE program header */ |
| 181 | nhdr = (struct elf_phdr *) bufp; |
| 182 | bufp += sizeof(struct elf_phdr); |
| 183 | offset += sizeof(struct elf_phdr); |
| 184 | nhdr->p_type = PT_NOTE; |
| 185 | nhdr->p_offset = 0; |
| 186 | nhdr->p_vaddr = 0; |
| 187 | nhdr->p_paddr = 0; |
| 188 | nhdr->p_filesz = 0; |
| 189 | nhdr->p_memsz = 0; |
| 190 | nhdr->p_flags = 0; |
| 191 | nhdr->p_align = 0; |
| 192 | |
| 193 | /* setup ELF PT_LOAD program header for every area */ |
| 194 | for (m=kclist; m; m=m->next) { |
| 195 | phdr = (struct elf_phdr *) bufp; |
| 196 | bufp += sizeof(struct elf_phdr); |
| 197 | offset += sizeof(struct elf_phdr); |
| 198 | |
| 199 | phdr->p_type = PT_LOAD; |
| 200 | phdr->p_flags = PF_R|PF_W|PF_X; |
| 201 | phdr->p_offset = kc_vaddr_to_offset(m->addr) + dataoff; |
| 202 | phdr->p_vaddr = (size_t)m->addr; |
| 203 | phdr->p_paddr = 0; |
| 204 | phdr->p_filesz = phdr->p_memsz = m->size; |
| 205 | phdr->p_align = PAGE_SIZE; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Set up the notes in similar form to SVR4 core dumps made |
| 210 | * with info from their /proc. |
| 211 | */ |
| 212 | nhdr->p_offset = offset; |
| 213 | |
| 214 | /* set up the process status */ |
Magnus Damm | 3602760 | 2006-12-06 20:38:00 -0800 | [diff] [blame] | 215 | notes[0].name = CORE_STR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | notes[0].type = NT_PRSTATUS; |
| 217 | notes[0].datasz = sizeof(struct elf_prstatus); |
| 218 | notes[0].data = &prstatus; |
| 219 | |
| 220 | memset(&prstatus, 0, sizeof(struct elf_prstatus)); |
| 221 | |
| 222 | nhdr->p_filesz = notesize(¬es[0]); |
| 223 | bufp = storenote(¬es[0], bufp); |
| 224 | |
| 225 | /* set up the process info */ |
Magnus Damm | 3602760 | 2006-12-06 20:38:00 -0800 | [diff] [blame] | 226 | notes[1].name = CORE_STR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | notes[1].type = NT_PRPSINFO; |
| 228 | notes[1].datasz = sizeof(struct elf_prpsinfo); |
| 229 | notes[1].data = &prpsinfo; |
| 230 | |
| 231 | memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo)); |
| 232 | prpsinfo.pr_state = 0; |
| 233 | prpsinfo.pr_sname = 'R'; |
| 234 | prpsinfo.pr_zomb = 0; |
| 235 | |
| 236 | strcpy(prpsinfo.pr_fname, "vmlinux"); |
| 237 | strncpy(prpsinfo.pr_psargs, saved_command_line, ELF_PRARGSZ); |
| 238 | |
| 239 | nhdr->p_filesz += notesize(¬es[1]); |
| 240 | bufp = storenote(¬es[1], bufp); |
| 241 | |
| 242 | /* set up the task structure */ |
Magnus Damm | 3602760 | 2006-12-06 20:38:00 -0800 | [diff] [blame] | 243 | notes[2].name = CORE_STR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | notes[2].type = NT_TASKSTRUCT; |
| 245 | notes[2].datasz = sizeof(struct task_struct); |
| 246 | notes[2].data = current; |
| 247 | |
| 248 | nhdr->p_filesz += notesize(¬es[2]); |
| 249 | bufp = storenote(¬es[2], bufp); |
| 250 | |
| 251 | } /* end elf_kcore_store_hdr() */ |
| 252 | |
| 253 | /*****************************************************************************/ |
| 254 | /* |
| 255 | * read from the ELF header and then kernel memory |
| 256 | */ |
| 257 | static ssize_t |
| 258 | read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) |
| 259 | { |
| 260 | ssize_t acc = 0; |
| 261 | size_t size, tsz; |
| 262 | size_t elf_buflen; |
| 263 | int nphdr; |
| 264 | unsigned long start; |
| 265 | |
| 266 | read_lock(&kclist_lock); |
| 267 | proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen); |
| 268 | if (buflen == 0 || *fpos >= size) { |
| 269 | read_unlock(&kclist_lock); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /* trim buflen to not go beyond EOF */ |
| 274 | if (buflen > size - *fpos) |
| 275 | buflen = size - *fpos; |
| 276 | |
| 277 | /* construct an ELF core header if we'll need some of it */ |
| 278 | if (*fpos < elf_buflen) { |
| 279 | char * elf_buf; |
| 280 | |
| 281 | tsz = elf_buflen - *fpos; |
| 282 | if (buflen < tsz) |
| 283 | tsz = buflen; |
Panagiotis Issaris | f8314dc | 2006-09-27 01:49:37 -0700 | [diff] [blame] | 284 | elf_buf = kzalloc(elf_buflen, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (!elf_buf) { |
| 286 | read_unlock(&kclist_lock); |
| 287 | return -ENOMEM; |
| 288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen); |
| 290 | read_unlock(&kclist_lock); |
| 291 | if (copy_to_user(buffer, elf_buf + *fpos, tsz)) { |
| 292 | kfree(elf_buf); |
| 293 | return -EFAULT; |
| 294 | } |
| 295 | kfree(elf_buf); |
| 296 | buflen -= tsz; |
| 297 | *fpos += tsz; |
| 298 | buffer += tsz; |
| 299 | acc += tsz; |
| 300 | |
| 301 | /* leave now if filled buffer already */ |
| 302 | if (buflen == 0) |
| 303 | return acc; |
| 304 | } else |
| 305 | read_unlock(&kclist_lock); |
| 306 | |
| 307 | /* |
| 308 | * Check to see if our file offset matches with any of |
| 309 | * the addresses in the elf_phdr on our list. |
| 310 | */ |
| 311 | start = kc_offset_to_vaddr(*fpos - elf_buflen); |
| 312 | if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen) |
| 313 | tsz = buflen; |
| 314 | |
| 315 | while (buflen) { |
| 316 | struct kcore_list *m; |
| 317 | |
| 318 | read_lock(&kclist_lock); |
| 319 | for (m=kclist; m; m=m->next) { |
| 320 | if (start >= m->addr && start < (m->addr+m->size)) |
| 321 | break; |
| 322 | } |
| 323 | read_unlock(&kclist_lock); |
| 324 | |
| 325 | if (m == NULL) { |
| 326 | if (clear_user(buffer, tsz)) |
| 327 | return -EFAULT; |
| 328 | } else if ((start >= VMALLOC_START) && (start < VMALLOC_END)) { |
| 329 | char * elf_buf; |
| 330 | struct vm_struct *m; |
| 331 | unsigned long curstart = start; |
| 332 | unsigned long cursize = tsz; |
| 333 | |
Panagiotis Issaris | f8314dc | 2006-09-27 01:49:37 -0700 | [diff] [blame] | 334 | elf_buf = kzalloc(tsz, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | if (!elf_buf) |
| 336 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
| 338 | read_lock(&vmlist_lock); |
| 339 | for (m=vmlist; m && cursize; m=m->next) { |
| 340 | unsigned long vmstart; |
| 341 | unsigned long vmsize; |
| 342 | unsigned long msize = m->size - PAGE_SIZE; |
| 343 | |
| 344 | if (((unsigned long)m->addr + msize) < |
| 345 | curstart) |
| 346 | continue; |
| 347 | if ((unsigned long)m->addr > (curstart + |
| 348 | cursize)) |
| 349 | break; |
| 350 | vmstart = (curstart < (unsigned long)m->addr ? |
| 351 | (unsigned long)m->addr : curstart); |
| 352 | if (((unsigned long)m->addr + msize) > |
| 353 | (curstart + cursize)) |
| 354 | vmsize = curstart + cursize - vmstart; |
| 355 | else |
| 356 | vmsize = (unsigned long)m->addr + |
| 357 | msize - vmstart; |
| 358 | curstart = vmstart + vmsize; |
| 359 | cursize -= vmsize; |
| 360 | /* don't dump ioremap'd stuff! (TA) */ |
| 361 | if (m->flags & VM_IOREMAP) |
| 362 | continue; |
| 363 | memcpy(elf_buf + (vmstart - start), |
| 364 | (char *)vmstart, vmsize); |
| 365 | } |
| 366 | read_unlock(&vmlist_lock); |
| 367 | if (copy_to_user(buffer, elf_buf, tsz)) { |
| 368 | kfree(elf_buf); |
| 369 | return -EFAULT; |
| 370 | } |
| 371 | kfree(elf_buf); |
| 372 | } else { |
| 373 | if (kern_addr_valid(start)) { |
| 374 | unsigned long n; |
| 375 | |
| 376 | n = copy_to_user(buffer, (char *)start, tsz); |
| 377 | /* |
| 378 | * We cannot distingush between fault on source |
| 379 | * and fault on destination. When this happens |
| 380 | * we clear too and hope it will trigger the |
| 381 | * EFAULT again. |
| 382 | */ |
| 383 | if (n) { |
| 384 | if (clear_user(buffer + tsz - n, |
Adam B. Jerome | 0635170 | 2006-07-12 09:03:07 -0700 | [diff] [blame] | 385 | n)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return -EFAULT; |
| 387 | } |
| 388 | } else { |
| 389 | if (clear_user(buffer, tsz)) |
| 390 | return -EFAULT; |
| 391 | } |
| 392 | } |
| 393 | buflen -= tsz; |
| 394 | *fpos += tsz; |
| 395 | buffer += tsz; |
| 396 | acc += tsz; |
| 397 | start += tsz; |
| 398 | tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen); |
| 399 | } |
| 400 | |
| 401 | return acc; |
| 402 | } |