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