FreeBSD/Linux Kernel Cross Reference
sys/kern/imgact_elf.c
1 /*-
2 * Copyright (c) 2000 David O'Brien
3 * Copyright (c) 1995-1996 Søren Schmidt
4 * Copyright (c) 1996 Peter Wemm
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_capsicum.h"
35 #include "opt_compat.h"
36 #include "opt_core.h"
37
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/exec.h>
41 #include <sys/fcntl.h>
42 #include <sys/imgact.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mman.h>
50 #include <sys/namei.h>
51 #include <sys/pioctl.h>
52 #include <sys/proc.h>
53 #include <sys/procfs.h>
54 #include <sys/racct.h>
55 #include <sys/resourcevar.h>
56 #include <sys/rwlock.h>
57 #include <sys/sbuf.h>
58 #include <sys/sf_buf.h>
59 #include <sys/smp.h>
60 #include <sys/systm.h>
61 #include <sys/signalvar.h>
62 #include <sys/stat.h>
63 #include <sys/sx.h>
64 #include <sys/syscall.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/vnode.h>
68 #include <sys/syslog.h>
69 #include <sys/eventhandler.h>
70 #include <sys/user.h>
71
72 #include <net/zlib.h>
73
74 #include <vm/vm.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_param.h>
77 #include <vm/pmap.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_extern.h>
81
82 #include <machine/elf.h>
83 #include <machine/md_var.h>
84
85 #define ELF_NOTE_ROUNDSIZE 4
86 #define OLD_EI_BRAND 8
87
88 static int __elfN(check_header)(const Elf_Ehdr *hdr);
89 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
90 const char *interp, int interp_name_len, int32_t *osrel);
91 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
92 u_long *entry, size_t pagesize);
93 static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
94 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
95 size_t pagesize);
96 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
97 static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
98 int32_t *osrel);
99 static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
100 static boolean_t __elfN(check_note)(struct image_params *imgp,
101 Elf_Brandnote *checknote, int32_t *osrel);
102 static vm_prot_t __elfN(trans_prot)(Elf_Word);
103 static Elf_Word __elfN(untrans_prot)(vm_prot_t);
104
105 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
106 "");
107
108 #ifdef COMPRESS_USER_CORES
109 static int compress_core(gzFile, char *, char *, unsigned int,
110 struct thread * td);
111 #endif
112 #define CORE_BUF_SIZE (16 * 1024)
113
114 int __elfN(fallback_brand) = -1;
115 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
116 fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0,
117 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
118 TUNABLE_INT("kern.elf" __XSTRING(__ELF_WORD_SIZE) ".fallback_brand",
119 &__elfN(fallback_brand));
120
121 static int elf_legacy_coredump = 0;
122 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
123 &elf_legacy_coredump, 0, "");
124
125 int __elfN(nxstack) =
126 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
127 1;
128 #else
129 0;
130 #endif
131 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
132 nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
133 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
134
135 #if __ELF_WORD_SIZE == 32
136 #if defined(__amd64__) || defined(__ia64__)
137 int i386_read_exec = 0;
138 SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
139 "enable execution from readable segments");
140 #endif
141 #endif
142
143 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
144
145 #define trunc_page_ps(va, ps) ((va) & ~(ps - 1))
146 #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1))
147 #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
148
149 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
150
151 Elf_Brandnote __elfN(freebsd_brandnote) = {
152 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
153 .hdr.n_descsz = sizeof(int32_t),
154 .hdr.n_type = 1,
155 .vendor = FREEBSD_ABI_VENDOR,
156 .flags = BN_TRANSLATE_OSREL,
157 .trans_osrel = __elfN(freebsd_trans_osrel)
158 };
159
160 static boolean_t
161 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
162 {
163 uintptr_t p;
164
165 p = (uintptr_t)(note + 1);
166 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
167 *osrel = *(const int32_t *)(p);
168
169 return (TRUE);
170 }
171
172 static const char GNU_ABI_VENDOR[] = "GNU";
173 static int GNU_KFREEBSD_ABI_DESC = 3;
174
175 Elf_Brandnote __elfN(kfreebsd_brandnote) = {
176 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
177 .hdr.n_descsz = 16, /* XXX at least 16 */
178 .hdr.n_type = 1,
179 .vendor = GNU_ABI_VENDOR,
180 .flags = BN_TRANSLATE_OSREL,
181 .trans_osrel = kfreebsd_trans_osrel
182 };
183
184 static boolean_t
185 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
186 {
187 const Elf32_Word *desc;
188 uintptr_t p;
189
190 p = (uintptr_t)(note + 1);
191 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
192
193 desc = (const Elf32_Word *)p;
194 if (desc[0] != GNU_KFREEBSD_ABI_DESC)
195 return (FALSE);
196
197 /*
198 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
199 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
200 */
201 *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
202
203 return (TRUE);
204 }
205
206 int
207 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
208 {
209 int i;
210
211 for (i = 0; i < MAX_BRANDS; i++) {
212 if (elf_brand_list[i] == NULL) {
213 elf_brand_list[i] = entry;
214 break;
215 }
216 }
217 if (i == MAX_BRANDS) {
218 printf("WARNING: %s: could not insert brandinfo entry: %p\n",
219 __func__, entry);
220 return (-1);
221 }
222 return (0);
223 }
224
225 int
226 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
227 {
228 int i;
229
230 for (i = 0; i < MAX_BRANDS; i++) {
231 if (elf_brand_list[i] == entry) {
232 elf_brand_list[i] = NULL;
233 break;
234 }
235 }
236 if (i == MAX_BRANDS)
237 return (-1);
238 return (0);
239 }
240
241 int
242 __elfN(brand_inuse)(Elf_Brandinfo *entry)
243 {
244 struct proc *p;
245 int rval = FALSE;
246
247 sx_slock(&allproc_lock);
248 FOREACH_PROC_IN_SYSTEM(p) {
249 if (p->p_sysent == entry->sysvec) {
250 rval = TRUE;
251 break;
252 }
253 }
254 sx_sunlock(&allproc_lock);
255
256 return (rval);
257 }
258
259 static Elf_Brandinfo *
260 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
261 int interp_name_len, int32_t *osrel)
262 {
263 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
264 Elf_Brandinfo *bi, *bi_m;
265 boolean_t ret;
266 int i;
267
268 /*
269 * We support four types of branding -- (1) the ELF EI_OSABI field
270 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
271 * branding w/in the ELF header, (3) path of the `interp_path'
272 * field, and (4) the ".note.ABI-tag" ELF section.
273 */
274
275 /* Look for an ".note.ABI-tag" ELF section */
276 bi_m = NULL;
277 for (i = 0; i < MAX_BRANDS; i++) {
278 bi = elf_brand_list[i];
279 if (bi == NULL)
280 continue;
281 if (hdr->e_machine == bi->machine && (bi->flags &
282 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
283 ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
284 /*
285 * If note checker claimed the binary, but the
286 * interpreter path in the image does not
287 * match default one for the brand, try to
288 * search for other brands with the same
289 * interpreter. Either there is better brand
290 * with the right interpreter, or, failing
291 * this, we return first brand which accepted
292 * our note and, optionally, header.
293 */
294 if (ret && bi_m == NULL && (strlen(bi->interp_path) +
295 1 != interp_name_len || strncmp(interp,
296 bi->interp_path, interp_name_len) != 0)) {
297 bi_m = bi;
298 ret = 0;
299 }
300 if (ret)
301 return (bi);
302 }
303 }
304 if (bi_m != NULL)
305 return (bi_m);
306
307 /* If the executable has a brand, search for it in the brand list. */
308 for (i = 0; i < MAX_BRANDS; i++) {
309 bi = elf_brand_list[i];
310 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
311 continue;
312 if (hdr->e_machine == bi->machine &&
313 (hdr->e_ident[EI_OSABI] == bi->brand ||
314 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
315 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
316 return (bi);
317 }
318
319 /* Lacking a known brand, search for a recognized interpreter. */
320 if (interp != NULL) {
321 for (i = 0; i < MAX_BRANDS; i++) {
322 bi = elf_brand_list[i];
323 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
324 continue;
325 if (hdr->e_machine == bi->machine &&
326 /* ELF image p_filesz includes terminating zero */
327 strlen(bi->interp_path) + 1 == interp_name_len &&
328 strncmp(interp, bi->interp_path, interp_name_len)
329 == 0)
330 return (bi);
331 }
332 }
333
334 /* Lacking a recognized interpreter, try the default brand */
335 for (i = 0; i < MAX_BRANDS; i++) {
336 bi = elf_brand_list[i];
337 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
338 continue;
339 if (hdr->e_machine == bi->machine &&
340 __elfN(fallback_brand) == bi->brand)
341 return (bi);
342 }
343 return (NULL);
344 }
345
346 static int
347 __elfN(check_header)(const Elf_Ehdr *hdr)
348 {
349 Elf_Brandinfo *bi;
350 int i;
351
352 if (!IS_ELF(*hdr) ||
353 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
354 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
355 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
356 hdr->e_phentsize != sizeof(Elf_Phdr) ||
357 hdr->e_version != ELF_TARG_VER)
358 return (ENOEXEC);
359
360 /*
361 * Make sure we have at least one brand for this machine.
362 */
363
364 for (i = 0; i < MAX_BRANDS; i++) {
365 bi = elf_brand_list[i];
366 if (bi != NULL && bi->machine == hdr->e_machine)
367 break;
368 }
369 if (i == MAX_BRANDS)
370 return (ENOEXEC);
371
372 return (0);
373 }
374
375 static int
376 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
377 vm_offset_t start, vm_offset_t end, vm_prot_t prot)
378 {
379 struct sf_buf *sf;
380 int error;
381 vm_offset_t off;
382
383 /*
384 * Create the page if it doesn't exist yet. Ignore errors.
385 */
386 vm_map_lock(map);
387 vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
388 VM_PROT_ALL, VM_PROT_ALL, 0);
389 vm_map_unlock(map);
390
391 /*
392 * Find the page from the underlying object.
393 */
394 if (object) {
395 sf = vm_imgact_map_page(object, offset);
396 if (sf == NULL)
397 return (KERN_FAILURE);
398 off = offset - trunc_page(offset);
399 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
400 end - start);
401 vm_imgact_unmap_page(sf);
402 if (error != 0)
403 return (KERN_FAILURE);
404 }
405
406 return (KERN_SUCCESS);
407 }
408
409 static int
410 __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
411 vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
412 {
413 struct sf_buf *sf;
414 vm_offset_t off;
415 vm_size_t sz;
416 int error, rv;
417
418 if (start != trunc_page(start)) {
419 rv = __elfN(map_partial)(map, object, offset, start,
420 round_page(start), prot);
421 if (rv)
422 return (rv);
423 offset += round_page(start) - start;
424 start = round_page(start);
425 }
426 if (end != round_page(end)) {
427 rv = __elfN(map_partial)(map, object, offset +
428 trunc_page(end) - start, trunc_page(end), end, prot);
429 if (rv)
430 return (rv);
431 end = trunc_page(end);
432 }
433 if (end > start) {
434 if (offset & PAGE_MASK) {
435 /*
436 * The mapping is not page aligned. This means we have
437 * to copy the data. Sigh.
438 */
439 rv = vm_map_find(map, NULL, 0, &start, end - start, 0,
440 VMFS_NO_SPACE, prot | VM_PROT_WRITE, VM_PROT_ALL,
441 0);
442 if (rv != KERN_SUCCESS)
443 return (rv);
444 if (object == NULL)
445 return (KERN_SUCCESS);
446 for (; start < end; start += sz) {
447 sf = vm_imgact_map_page(object, offset);
448 if (sf == NULL)
449 return (KERN_FAILURE);
450 off = offset - trunc_page(offset);
451 sz = end - start;
452 if (sz > PAGE_SIZE - off)
453 sz = PAGE_SIZE - off;
454 error = copyout((caddr_t)sf_buf_kva(sf) + off,
455 (caddr_t)start, sz);
456 vm_imgact_unmap_page(sf);
457 if (error != 0)
458 return (KERN_FAILURE);
459 offset += sz;
460 }
461 rv = KERN_SUCCESS;
462 } else {
463 vm_object_reference(object);
464 vm_map_lock(map);
465 rv = vm_map_insert(map, object, offset, start, end,
466 prot, VM_PROT_ALL, cow);
467 vm_map_unlock(map);
468 if (rv != KERN_SUCCESS)
469 vm_object_deallocate(object);
470 }
471 return (rv);
472 } else {
473 return (KERN_SUCCESS);
474 }
475 }
476
477 static int
478 __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
479 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
480 size_t pagesize)
481 {
482 struct sf_buf *sf;
483 size_t map_len;
484 vm_map_t map;
485 vm_object_t object;
486 vm_offset_t map_addr;
487 int error, rv, cow;
488 size_t copy_len;
489 vm_offset_t file_addr;
490
491 /*
492 * It's necessary to fail if the filsz + offset taken from the
493 * header is greater than the actual file pager object's size.
494 * If we were to allow this, then the vm_map_find() below would
495 * walk right off the end of the file object and into the ether.
496 *
497 * While I'm here, might as well check for something else that
498 * is invalid: filsz cannot be greater than memsz.
499 */
500 if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) {
501 uprintf("elf_load_section: truncated ELF file\n");
502 return (ENOEXEC);
503 }
504
505 object = imgp->object;
506 map = &imgp->proc->p_vmspace->vm_map;
507 map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
508 file_addr = trunc_page_ps(offset, pagesize);
509
510 /*
511 * We have two choices. We can either clear the data in the last page
512 * of an oversized mapping, or we can start the anon mapping a page
513 * early and copy the initialized data into that first page. We
514 * choose the second..
515 */
516 if (memsz > filsz)
517 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
518 else
519 map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
520
521 if (map_len != 0) {
522 /* cow flags: don't dump readonly sections in core */
523 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
524 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
525
526 rv = __elfN(map_insert)(map,
527 object,
528 file_addr, /* file offset */
529 map_addr, /* virtual start */
530 map_addr + map_len,/* virtual end */
531 prot,
532 cow);
533 if (rv != KERN_SUCCESS)
534 return (EINVAL);
535
536 /* we can stop now if we've covered it all */
537 if (memsz == filsz) {
538 return (0);
539 }
540 }
541
542
543 /*
544 * We have to get the remaining bit of the file into the first part
545 * of the oversized map segment. This is normally because the .data
546 * segment in the file is extended to provide bss. It's a neat idea
547 * to try and save a page, but it's a pain in the behind to implement.
548 */
549 copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
550 map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
551 map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
552 map_addr;
553
554 /* This had damn well better be true! */
555 if (map_len != 0) {
556 rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr +
557 map_len, VM_PROT_ALL, 0);
558 if (rv != KERN_SUCCESS) {
559 return (EINVAL);
560 }
561 }
562
563 if (copy_len != 0) {
564 vm_offset_t off;
565
566 sf = vm_imgact_map_page(object, offset + filsz);
567 if (sf == NULL)
568 return (EIO);
569
570 /* send the page fragment to user space */
571 off = trunc_page_ps(offset + filsz, pagesize) -
572 trunc_page(offset + filsz);
573 error = copyout((caddr_t)sf_buf_kva(sf) + off,
574 (caddr_t)map_addr, copy_len);
575 vm_imgact_unmap_page(sf);
576 if (error) {
577 return (error);
578 }
579 }
580
581 /*
582 * set it to the specified protection.
583 * XXX had better undo the damage from pasting over the cracks here!
584 */
585 vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
586 map_len), prot, FALSE);
587
588 return (0);
589 }
590
591 /*
592 * Load the file "file" into memory. It may be either a shared object
593 * or an executable.
594 *
595 * The "addr" reference parameter is in/out. On entry, it specifies
596 * the address where a shared object should be loaded. If the file is
597 * an executable, this value is ignored. On exit, "addr" specifies
598 * where the file was actually loaded.
599 *
600 * The "entry" reference parameter is out only. On exit, it specifies
601 * the entry point for the loaded file.
602 */
603 static int
604 __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
605 u_long *entry, size_t pagesize)
606 {
607 struct {
608 struct nameidata nd;
609 struct vattr attr;
610 struct image_params image_params;
611 } *tempdata;
612 const Elf_Ehdr *hdr = NULL;
613 const Elf_Phdr *phdr = NULL;
614 struct nameidata *nd;
615 struct vattr *attr;
616 struct image_params *imgp;
617 vm_prot_t prot;
618 u_long rbase;
619 u_long base_addr = 0;
620 int error, i, numsegs;
621
622 #ifdef CAPABILITY_MODE
623 /*
624 * XXXJA: This check can go away once we are sufficiently confident
625 * that the checks in namei() are correct.
626 */
627 if (IN_CAPABILITY_MODE(curthread))
628 return (ECAPMODE);
629 #endif
630
631 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
632 nd = &tempdata->nd;
633 attr = &tempdata->attr;
634 imgp = &tempdata->image_params;
635
636 /*
637 * Initialize part of the common data
638 */
639 imgp->proc = p;
640 imgp->attr = attr;
641 imgp->firstpage = NULL;
642 imgp->image_header = NULL;
643 imgp->object = NULL;
644 imgp->execlabel = NULL;
645
646 NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread);
647 if ((error = namei(nd)) != 0) {
648 nd->ni_vp = NULL;
649 goto fail;
650 }
651 NDFREE(nd, NDF_ONLY_PNBUF);
652 imgp->vp = nd->ni_vp;
653
654 /*
655 * Check permissions, modes, uid, etc on the file, and "open" it.
656 */
657 error = exec_check_permissions(imgp);
658 if (error)
659 goto fail;
660
661 error = exec_map_first_page(imgp);
662 if (error)
663 goto fail;
664
665 /*
666 * Also make certain that the interpreter stays the same, so set
667 * its VV_TEXT flag, too.
668 */
669 VOP_SET_TEXT(nd->ni_vp);
670
671 imgp->object = nd->ni_vp->v_object;
672
673 hdr = (const Elf_Ehdr *)imgp->image_header;
674 if ((error = __elfN(check_header)(hdr)) != 0)
675 goto fail;
676 if (hdr->e_type == ET_DYN)
677 rbase = *addr;
678 else if (hdr->e_type == ET_EXEC)
679 rbase = 0;
680 else {
681 error = ENOEXEC;
682 goto fail;
683 }
684
685 /* Only support headers that fit within first page for now */
686 if ((hdr->e_phoff > PAGE_SIZE) ||
687 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
688 error = ENOEXEC;
689 goto fail;
690 }
691
692 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
693 if (!aligned(phdr, Elf_Addr)) {
694 error = ENOEXEC;
695 goto fail;
696 }
697
698 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
699 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
700 /* Loadable segment */
701 prot = __elfN(trans_prot)(phdr[i].p_flags);
702 error = __elfN(load_section)(imgp, phdr[i].p_offset,
703 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
704 phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
705 if (error != 0)
706 goto fail;
707 /*
708 * Establish the base address if this is the
709 * first segment.
710 */
711 if (numsegs == 0)
712 base_addr = trunc_page(phdr[i].p_vaddr +
713 rbase);
714 numsegs++;
715 }
716 }
717 *addr = base_addr;
718 *entry = (unsigned long)hdr->e_entry + rbase;
719
720 fail:
721 if (imgp->firstpage)
722 exec_unmap_first_page(imgp);
723
724 if (nd->ni_vp)
725 vput(nd->ni_vp);
726
727 free(tempdata, M_TEMP);
728
729 return (error);
730 }
731
732 static int
733 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
734 {
735 struct thread *td;
736 const Elf_Ehdr *hdr;
737 const Elf_Phdr *phdr;
738 Elf_Auxargs *elf_auxargs;
739 struct vmspace *vmspace;
740 const char *err_str, *newinterp;
741 char *interp, *interp_buf, *path;
742 Elf_Brandinfo *brand_info;
743 struct sysentvec *sv;
744 vm_prot_t prot;
745 u_long text_size, data_size, total_size, text_addr, data_addr;
746 u_long seg_size, seg_addr, addr, baddr, et_dyn_addr, entry, proghdr;
747 int32_t osrel;
748 int error, i, n, interp_name_len, have_interp;
749
750 hdr = (const Elf_Ehdr *)imgp->image_header;
751
752 /*
753 * Do we have a valid ELF header ?
754 *
755 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
756 * if particular brand doesn't support it.
757 */
758 if (__elfN(check_header)(hdr) != 0 ||
759 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
760 return (-1);
761
762 /*
763 * From here on down, we return an errno, not -1, as we've
764 * detected an ELF file.
765 */
766
767 if ((hdr->e_phoff > PAGE_SIZE) ||
768 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
769 /* Only support headers in first page for now */
770 uprintf("Program headers not in the first page\n");
771 return (ENOEXEC);
772 }
773 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
774 if (!aligned(phdr, Elf_Addr)) {
775 uprintf("Unaligned program headers\n");
776 return (ENOEXEC);
777 }
778
779 n = error = 0;
780 baddr = 0;
781 osrel = 0;
782 text_size = data_size = total_size = text_addr = data_addr = 0;
783 entry = proghdr = 0;
784 interp_name_len = 0;
785 err_str = newinterp = NULL;
786 interp = interp_buf = NULL;
787 td = curthread;
788
789 for (i = 0; i < hdr->e_phnum; i++) {
790 switch (phdr[i].p_type) {
791 case PT_LOAD:
792 if (n == 0)
793 baddr = phdr[i].p_vaddr;
794 n++;
795 break;
796 case PT_INTERP:
797 /* Path to interpreter */
798 if (phdr[i].p_filesz < 2 ||
799 phdr[i].p_filesz > MAXPATHLEN) {
800 uprintf("Invalid PT_INTERP\n");
801 error = ENOEXEC;
802 goto ret;
803 }
804 if (interp != NULL) {
805 uprintf("Multiple PT_INTERP headers\n");
806 error = ENOEXEC;
807 goto ret;
808 }
809 interp_name_len = phdr[i].p_filesz;
810 if (phdr[i].p_offset > PAGE_SIZE ||
811 interp_name_len > PAGE_SIZE - phdr[i].p_offset) {
812 VOP_UNLOCK(imgp->vp, 0);
813 interp_buf = malloc(interp_name_len + 1, M_TEMP,
814 M_WAITOK);
815 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
816 error = vn_rdwr(UIO_READ, imgp->vp, interp_buf,
817 interp_name_len, phdr[i].p_offset,
818 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
819 NOCRED, NULL, td);
820 if (error != 0) {
821 uprintf("i/o error PT_INTERP\n");
822 goto ret;
823 }
824 interp_buf[interp_name_len] = '\0';
825 interp = interp_buf;
826 } else {
827 interp = __DECONST(char *, imgp->image_header) +
828 phdr[i].p_offset;
829 if (interp[interp_name_len - 1] != '\0') {
830 uprintf("Invalid PT_INTERP\n");
831 error = ENOEXEC;
832 goto ret;
833 }
834 }
835 break;
836 case PT_GNU_STACK:
837 if (__elfN(nxstack))
838 imgp->stack_prot =
839 __elfN(trans_prot)(phdr[i].p_flags);
840 imgp->stack_sz = phdr[i].p_memsz;
841 break;
842 }
843 }
844
845 brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
846 &osrel);
847 if (brand_info == NULL) {
848 uprintf("ELF binary type \"%u\" not known.\n",
849 hdr->e_ident[EI_OSABI]);
850 error = ENOEXEC;
851 goto ret;
852 }
853 if (hdr->e_type == ET_DYN) {
854 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
855 uprintf("Cannot execute shared object\n");
856 error = ENOEXEC;
857 goto ret;
858 }
859 /*
860 * Honour the base load address from the dso if it is
861 * non-zero for some reason.
862 */
863 if (baddr == 0)
864 et_dyn_addr = ET_DYN_LOAD_ADDR;
865 else
866 et_dyn_addr = 0;
867 } else
868 et_dyn_addr = 0;
869 sv = brand_info->sysvec;
870 if (interp != NULL && brand_info->interp_newpath != NULL)
871 newinterp = brand_info->interp_newpath;
872
873 /*
874 * Avoid a possible deadlock if the current address space is destroyed
875 * and that address space maps the locked vnode. In the common case,
876 * the locked vnode's v_usecount is decremented but remains greater
877 * than zero. Consequently, the vnode lock is not needed by vrele().
878 * However, in cases where the vnode lock is external, such as nullfs,
879 * v_usecount may become zero.
880 *
881 * The VV_TEXT flag prevents modifications to the executable while
882 * the vnode is unlocked.
883 */
884 VOP_UNLOCK(imgp->vp, 0);
885
886 error = exec_new_vmspace(imgp, sv);
887 imgp->proc->p_sysent = sv;
888
889 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
890 if (error != 0)
891 goto ret;
892
893 for (i = 0; i < hdr->e_phnum; i++) {
894 switch (phdr[i].p_type) {
895 case PT_LOAD: /* Loadable segment */
896 if (phdr[i].p_memsz == 0)
897 break;
898 prot = __elfN(trans_prot)(phdr[i].p_flags);
899 error = __elfN(load_section)(imgp, phdr[i].p_offset,
900 (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
901 phdr[i].p_memsz, phdr[i].p_filesz, prot,
902 sv->sv_pagesize);
903 if (error != 0)
904 goto ret;
905
906 /*
907 * If this segment contains the program headers,
908 * remember their virtual address for the AT_PHDR
909 * aux entry. Static binaries don't usually include
910 * a PT_PHDR entry.
911 */
912 if (phdr[i].p_offset == 0 &&
913 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
914 <= phdr[i].p_filesz)
915 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
916 et_dyn_addr;
917
918 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
919 seg_size = round_page(phdr[i].p_memsz +
920 phdr[i].p_vaddr + et_dyn_addr - seg_addr);
921
922 /*
923 * Make the largest executable segment the official
924 * text segment and all others data.
925 *
926 * Note that obreak() assumes that data_addr +
927 * data_size == end of data load area, and the ELF
928 * file format expects segments to be sorted by
929 * address. If multiple data segments exist, the
930 * last one will be used.
931 */
932
933 if (phdr[i].p_flags & PF_X && text_size < seg_size) {
934 text_size = seg_size;
935 text_addr = seg_addr;
936 } else {
937 data_size = seg_size;
938 data_addr = seg_addr;
939 }
940 total_size += seg_size;
941 break;
942 case PT_PHDR: /* Program header table info */
943 proghdr = phdr[i].p_vaddr + et_dyn_addr;
944 break;
945 default:
946 break;
947 }
948 }
949
950 if (data_addr == 0 && data_size == 0) {
951 data_addr = text_addr;
952 data_size = text_size;
953 }
954
955 entry = (u_long)hdr->e_entry + et_dyn_addr;
956
957 /*
958 * Check limits. It should be safe to check the
959 * limits after loading the segments since we do
960 * not actually fault in all the segments pages.
961 */
962 PROC_LOCK(imgp->proc);
963 if (data_size > lim_cur(imgp->proc, RLIMIT_DATA))
964 err_str = "Data segment size exceeds process limit";
965 else if (text_size > maxtsiz)
966 err_str = "Text segment size exceeds system limit";
967 else if (total_size > lim_cur(imgp->proc, RLIMIT_VMEM))
968 err_str = "Total segment size exceeds process limit";
969 else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
970 err_str = "Data segment size exceeds resource limit";
971 else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
972 err_str = "Total segment size exceeds resource limit";
973 if (err_str != NULL) {
974 PROC_UNLOCK(imgp->proc);
975 uprintf("%s\n", err_str);
976 error = ENOMEM;
977 goto ret;
978 }
979
980 vmspace = imgp->proc->p_vmspace;
981 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
982 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
983 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
984 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
985
986 /*
987 * We load the dynamic linker where a userland call
988 * to mmap(0, ...) would put it. The rationale behind this
989 * calculation is that it leaves room for the heap to grow to
990 * its maximum allowed size.
991 */
992 addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(imgp->proc,
993 RLIMIT_DATA));
994 PROC_UNLOCK(imgp->proc);
995
996 imgp->entry_addr = entry;
997
998 if (interp != NULL) {
999 have_interp = FALSE;
1000 VOP_UNLOCK(imgp->vp, 0);
1001 if (brand_info->emul_path != NULL &&
1002 brand_info->emul_path[0] != '\0') {
1003 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1004 snprintf(path, MAXPATHLEN, "%s%s",
1005 brand_info->emul_path, interp);
1006 error = __elfN(load_file)(imgp->proc, path, &addr,
1007 &imgp->entry_addr, sv->sv_pagesize);
1008 free(path, M_TEMP);
1009 if (error == 0)
1010 have_interp = TRUE;
1011 }
1012 if (!have_interp && newinterp != NULL &&
1013 (brand_info->interp_path == NULL ||
1014 strcmp(interp, brand_info->interp_path) == 0)) {
1015 error = __elfN(load_file)(imgp->proc, newinterp, &addr,
1016 &imgp->entry_addr, sv->sv_pagesize);
1017 if (error == 0)
1018 have_interp = TRUE;
1019 }
1020 if (!have_interp) {
1021 error = __elfN(load_file)(imgp->proc, interp, &addr,
1022 &imgp->entry_addr, sv->sv_pagesize);
1023 }
1024 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
1025 if (error != 0) {
1026 uprintf("ELF interpreter %s not found, error %d\n",
1027 interp, error);
1028 goto ret;
1029 }
1030 } else
1031 addr = et_dyn_addr;
1032
1033 /*
1034 * Construct auxargs table (used by the fixup routine)
1035 */
1036 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
1037 elf_auxargs->execfd = -1;
1038 elf_auxargs->phdr = proghdr;
1039 elf_auxargs->phent = hdr->e_phentsize;
1040 elf_auxargs->phnum = hdr->e_phnum;
1041 elf_auxargs->pagesz = PAGE_SIZE;
1042 elf_auxargs->base = addr;
1043 elf_auxargs->flags = 0;
1044 elf_auxargs->entry = entry;
1045
1046 imgp->auxargs = elf_auxargs;
1047 imgp->interpreted = 0;
1048 imgp->reloc_base = addr;
1049 imgp->proc->p_osrel = osrel;
1050 imgp->proc->p_elf_machine = hdr->e_machine;
1051 imgp->proc->p_elf_flags = hdr->e_flags;
1052
1053 ret:
1054 free(interp_buf, M_TEMP);
1055 return (error);
1056 }
1057
1058 #define suword __CONCAT(suword, __ELF_WORD_SIZE)
1059
1060 int
1061 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
1062 {
1063 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
1064 Elf_Addr *base;
1065 Elf_Addr *pos;
1066
1067 base = (Elf_Addr *)*stack_base;
1068 pos = base + (imgp->args->argc + imgp->args->envc + 2);
1069
1070 if (args->execfd != -1)
1071 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1072 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1073 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1074 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1075 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1076 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1077 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1078 AUXARGS_ENTRY(pos, AT_BASE, args->base);
1079 if (imgp->execpathp != 0)
1080 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1081 AUXARGS_ENTRY(pos, AT_OSRELDATE,
1082 imgp->proc->p_ucred->cr_prison->pr_osreldate);
1083 if (imgp->canary != 0) {
1084 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1085 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1086 }
1087 AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1088 if (imgp->pagesizes != 0) {
1089 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1090 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1091 }
1092 if (imgp->sysent->sv_timekeep_base != 0) {
1093 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1094 imgp->sysent->sv_timekeep_base);
1095 }
1096 AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
1097 != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
1098 imgp->sysent->sv_stackprot);
1099 AUXARGS_ENTRY(pos, AT_NULL, 0);
1100
1101 free(imgp->auxargs, M_TEMP);
1102 imgp->auxargs = NULL;
1103
1104 base--;
1105 suword(base, (long)imgp->args->argc);
1106 *stack_base = (register_t *)base;
1107 return (0);
1108 }
1109
1110 /*
1111 * Code for generating ELF core dumps.
1112 */
1113
1114 typedef void (*segment_callback)(vm_map_entry_t, void *);
1115
1116 /* Closure for cb_put_phdr(). */
1117 struct phdr_closure {
1118 Elf_Phdr *phdr; /* Program header to fill in */
1119 Elf_Off offset; /* Offset of segment in core file */
1120 };
1121
1122 /* Closure for cb_size_segment(). */
1123 struct sseg_closure {
1124 int count; /* Count of writable segments. */
1125 size_t size; /* Total size of all writable segments. */
1126 };
1127
1128 typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1129
1130 struct note_info {
1131 int type; /* Note type. */
1132 outfunc_t outfunc; /* Output function. */
1133 void *outarg; /* Argument for the output function. */
1134 size_t outsize; /* Output size. */
1135 TAILQ_ENTRY(note_info) link; /* Link to the next note info. */
1136 };
1137
1138 TAILQ_HEAD(note_info_list, note_info);
1139
1140 static void cb_put_phdr(vm_map_entry_t, void *);
1141 static void cb_size_segment(vm_map_entry_t, void *);
1142 static void each_writable_segment(struct thread *, segment_callback, void *);
1143 static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *,
1144 int, void *, size_t, struct note_info_list *, size_t, gzFile);
1145 static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1146 size_t *);
1147 static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1148 static void __elfN(putnote)(struct note_info *, struct sbuf *);
1149 static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1150 static int sbuf_drain_core_output(void *, const char *, int);
1151 static int sbuf_drain_count(void *arg, const char *data, int len);
1152
1153 static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1154 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1155 static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1156 static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1157 static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
1158 static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1159 static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1160 static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1161 static void note_procstat_files(void *, struct sbuf *, size_t *);
1162 static void note_procstat_groups(void *, struct sbuf *, size_t *);
1163 static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1164 static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1165 static void note_procstat_umask(void *, struct sbuf *, size_t *);
1166 static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
1167
1168 #ifdef COMPRESS_USER_CORES
1169 extern int compress_user_cores;
1170 extern int compress_user_cores_gzlevel;
1171 #endif
1172
1173 static int
1174 core_output(struct vnode *vp, void *base, size_t len, off_t offset,
1175 struct ucred *active_cred, struct ucred *file_cred,
1176 struct thread *td, char *core_buf, gzFile gzfile) {
1177
1178 int error;
1179 if (gzfile) {
1180 #ifdef COMPRESS_USER_CORES
1181 error = compress_core(gzfile, base, core_buf, len, td);
1182 #else
1183 panic("shouldn't be here");
1184 #endif
1185 } else {
1186 /*
1187 * EFAULT is a non-fatal error that we can get, for example,
1188 * if the segment is backed by a file but extends beyond its
1189 * end.
1190 */
1191 error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset,
1192 UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
1193 NULL, td);
1194 if (error == EFAULT) {
1195 log(LOG_WARNING, "Failed to fully fault in a core file "
1196 "segment at VA %p with size 0x%zx to be written at "
1197 "offset 0x%jx for process %s\n", base, len, offset,
1198 curproc->p_comm);
1199
1200 /*
1201 * Write a "real" zero byte at the end of the target
1202 * region in the case this is the last segment.
1203 * The intermediate space will be implicitly
1204 * zero-filled.
1205 */
1206 error = vn_rdwr_inchunks(UIO_WRITE, vp,
1207 __DECONST(void *, zero_region), 1, offset + len - 1,
1208 UIO_SYSSPACE, IO_UNIT | IO_DIRECT, active_cred,
1209 file_cred, NULL, td);
1210 }
1211 }
1212 return (error);
1213 }
1214
1215 /* Coredump output parameters for sbuf drain routine. */
1216 struct sbuf_drain_core_params {
1217 off_t offset;
1218 struct ucred *active_cred;
1219 struct ucred *file_cred;
1220 struct thread *td;
1221 struct vnode *vp;
1222 #ifdef COMPRESS_USER_CORES
1223 gzFile gzfile;
1224 #endif
1225 };
1226
1227 /*
1228 * Drain into a core file.
1229 */
1230 static int
1231 sbuf_drain_core_output(void *arg, const char *data, int len)
1232 {
1233 struct sbuf_drain_core_params *p;
1234 int error, locked;
1235
1236 p = (struct sbuf_drain_core_params *)arg;
1237
1238 /*
1239 * Some kern_proc out routines that print to this sbuf may
1240 * call us with the process lock held. Draining with the
1241 * non-sleepable lock held is unsafe. The lock is needed for
1242 * those routines when dumping a live process. In our case we
1243 * can safely release the lock before draining and acquire
1244 * again after.
1245 */
1246 locked = PROC_LOCKED(p->td->td_proc);
1247 if (locked)
1248 PROC_UNLOCK(p->td->td_proc);
1249 #ifdef COMPRESS_USER_CORES
1250 if (p->gzfile != Z_NULL)
1251 error = compress_core(p->gzfile, NULL, __DECONST(char *, data),
1252 len, p->td);
1253 else
1254 #endif
1255 error = vn_rdwr_inchunks(UIO_WRITE, p->vp,
1256 __DECONST(void *, data), len, p->offset, UIO_SYSSPACE,
1257 IO_UNIT | IO_DIRECT, p->active_cred, p->file_cred, NULL,
1258 p->td);
1259 if (locked)
1260 PROC_LOCK(p->td->td_proc);
1261 if (error != 0)
1262 return (-error);
1263 p->offset += len;
1264 return (len);
1265 }
1266
1267 /*
1268 * Drain into a counter.
1269 */
1270 static int
1271 sbuf_drain_count(void *arg, const char *data __unused, int len)
1272 {
1273 size_t *sizep;
1274
1275 sizep = (size_t *)arg;
1276 *sizep += len;
1277 return (len);
1278 }
1279
1280 int
1281 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1282 {
1283 struct ucred *cred = td->td_ucred;
1284 int error = 0;
1285 struct sseg_closure seginfo;
1286 struct note_info_list notelst;
1287 struct note_info *ninfo;
1288 void *hdr;
1289 size_t hdrsize, notesz, coresize;
1290
1291 gzFile gzfile = Z_NULL;
1292 char *core_buf = NULL;
1293 #ifdef COMPRESS_USER_CORES
1294 char gzopen_flags[8];
1295 char *p;
1296 int doing_compress = flags & IMGACT_CORE_COMPRESS;
1297 #endif
1298
1299 hdr = NULL;
1300 TAILQ_INIT(¬elst);
1301
1302 #ifdef COMPRESS_USER_CORES
1303 if (doing_compress) {
1304 p = gzopen_flags;
1305 *p++ = 'w';
1306 if (compress_user_cores_gzlevel >= 0 &&
1307 compress_user_cores_gzlevel <= 9)
1308 *p++ = '' + compress_user_cores_gzlevel;
1309 *p = 0;
1310 gzfile = gz_open("", gzopen_flags, vp);
1311 if (gzfile == Z_NULL) {
1312 error = EFAULT;
1313 goto done;
1314 }
1315 core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1316 if (!core_buf) {
1317 error = ENOMEM;
1318 goto done;
1319 }
1320 }
1321 #endif
1322
1323 /* Size the program segments. */
1324 seginfo.count = 0;
1325 seginfo.size = 0;
1326 each_writable_segment(td, cb_size_segment, &seginfo);
1327
1328 /*
1329 * Collect info about the core file header area.
1330 */
1331 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1332 __elfN(prepare_notes)(td, ¬elst, ¬esz);
1333 coresize = round_page(hdrsize + notesz) + seginfo.size;
1334
1335 #ifdef RACCT
1336 if (racct_enable) {
1337 PROC_LOCK(td->td_proc);
1338 error = racct_add(td->td_proc, RACCT_CORE, coresize);
1339 PROC_UNLOCK(td->td_proc);
1340 if (error != 0) {
1341 error = EFAULT;
1342 goto done;
1343 }
1344 }
1345 #endif
1346 if (coresize >= limit) {
1347 error = EFAULT;
1348 goto done;
1349 }
1350
1351 /*
1352 * Allocate memory for building the header, fill it up,
1353 * and write it out following the notes.
1354 */
1355 hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1356 error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize,
1357 ¬elst, notesz, gzfile);
1358
1359 /* Write the contents of all of the writable segments. */
1360 if (error == 0) {
1361 Elf_Phdr *php;
1362 off_t offset;
1363 int i;
1364
1365 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1366 offset = round_page(hdrsize + notesz);
1367 for (i = 0; i < seginfo.count; i++) {
1368 error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
1369 php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
1370 if (error != 0)
1371 break;
1372 offset += php->p_filesz;
1373 php++;
1374 }
1375 }
1376 if (error) {
1377 log(LOG_WARNING,
1378 "Failed to write core file for process %s (error %d)\n",
1379 curproc->p_comm, error);
1380 }
1381
1382 done:
1383 #ifdef COMPRESS_USER_CORES
1384 if (core_buf)
1385 free(core_buf, M_TEMP);
1386 if (gzfile)
1387 gzclose(gzfile);
1388 #endif
1389 while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) {
1390 TAILQ_REMOVE(¬elst, ninfo, link);
1391 free(ninfo, M_TEMP);
1392 }
1393 if (hdr != NULL)
1394 free(hdr, M_TEMP);
1395
1396 return (error);
1397 }
1398
1399 /*
1400 * A callback for each_writable_segment() to write out the segment's
1401 * program header entry.
1402 */
1403 static void
1404 cb_put_phdr(entry, closure)
1405 vm_map_entry_t entry;
1406 void *closure;
1407 {
1408 struct phdr_closure *phc = (struct phdr_closure *)closure;
1409 Elf_Phdr *phdr = phc->phdr;
1410
1411 phc->offset = round_page(phc->offset);
1412
1413 phdr->p_type = PT_LOAD;
1414 phdr->p_offset = phc->offset;
1415 phdr->p_vaddr = entry->start;
1416 phdr->p_paddr = 0;
1417 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1418 phdr->p_align = PAGE_SIZE;
1419 phdr->p_flags = __elfN(untrans_prot)(entry->protection);
1420
1421 phc->offset += phdr->p_filesz;
1422 phc->phdr++;
1423 }
1424
1425 /*
1426 * A callback for each_writable_segment() to gather information about
1427 * the number of segments and their total size.
1428 */
1429 static void
1430 cb_size_segment(entry, closure)
1431 vm_map_entry_t entry;
1432 void *closure;
1433 {
1434 struct sseg_closure *ssc = (struct sseg_closure *)closure;
1435
1436 ssc->count++;
1437 ssc->size += entry->end - entry->start;
1438 }
1439
1440 /*
1441 * For each writable segment in the process's memory map, call the given
1442 * function with a pointer to the map entry and some arbitrary
1443 * caller-supplied data.
1444 */
1445 static void
1446 each_writable_segment(td, func, closure)
1447 struct thread *td;
1448 segment_callback func;
1449 void *closure;
1450 {
1451 struct proc *p = td->td_proc;
1452 vm_map_t map = &p->p_vmspace->vm_map;
1453 vm_map_entry_t entry;
1454 vm_object_t backing_object, object;
1455 boolean_t ignore_entry;
1456
1457 vm_map_lock_read(map);
1458 for (entry = map->header.next; entry != &map->header;
1459 entry = entry->next) {
1460 /*
1461 * Don't dump inaccessible mappings, deal with legacy
1462 * coredump mode.
1463 *
1464 * Note that read-only segments related to the elf binary
1465 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1466 * need to arbitrarily ignore such segments.
1467 */
1468 if (elf_legacy_coredump) {
1469 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
1470 continue;
1471 } else {
1472 if ((entry->protection & VM_PROT_ALL) == 0)
1473 continue;
1474 }
1475
1476 /*
1477 * Dont include memory segment in the coredump if
1478 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1479 * madvise(2). Do not dump submaps (i.e. parts of the
1480 * kernel map).
1481 */
1482 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1483 continue;
1484
1485 if ((object = entry->object.vm_object) == NULL)
1486 continue;
1487
1488 /* Ignore memory-mapped devices and such things. */
1489 VM_OBJECT_RLOCK(object);
1490 while ((backing_object = object->backing_object) != NULL) {
1491 VM_OBJECT_RLOCK(backing_object);
1492 VM_OBJECT_RUNLOCK(object);
1493 object = backing_object;
1494 }
1495 ignore_entry = object->type != OBJT_DEFAULT &&
1496 object->type != OBJT_SWAP && object->type != OBJT_VNODE &&
1497 object->type != OBJT_PHYS;
1498 VM_OBJECT_RUNLOCK(object);
1499 if (ignore_entry)
1500 continue;
1501
1502 (*func)(entry, closure);
1503 }
1504 vm_map_unlock_read(map);
1505 }
1506
1507 /*
1508 * Write the core file header to the file, including padding up to
1509 * the page boundary.
1510 */
1511 static int
1512 __elfN(corehdr)(struct thread *td, struct vnode *vp, struct ucred *cred,
1513 int numsegs, void *hdr, size_t hdrsize, struct note_info_list *notelst,
1514 size_t notesz, gzFile gzfile)
1515 {
1516 struct sbuf_drain_core_params params;
1517 struct note_info *ninfo;
1518 struct sbuf *sb;
1519 int error;
1520
1521 /* Fill in the header. */
1522 bzero(hdr, hdrsize);
1523 __elfN(puthdr)(td, hdr, hdrsize, numsegs, notesz);
1524
1525 params.offset = 0;
1526 params.active_cred = cred;
1527 params.file_cred = NOCRED;
1528 params.td = td;
1529 params.vp = vp;
1530 #ifdef COMPRESS_USER_CORES
1531 params.gzfile = gzfile;
1532 #endif
1533 sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1534 sbuf_set_drain(sb, sbuf_drain_core_output, ¶ms);
1535 sbuf_start_section(sb, NULL);
1536 sbuf_bcat(sb, hdr, hdrsize);
1537 TAILQ_FOREACH(ninfo, notelst, link)
1538 __elfN(putnote)(ninfo, sb);
1539 /* Align up to a page boundary for the program segments. */
1540 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1541 error = sbuf_finish(sb);
1542 sbuf_delete(sb);
1543
1544 return (error);
1545 }
1546
1547 static void
1548 __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1549 size_t *sizep)
1550 {
1551 struct proc *p;
1552 struct thread *thr;
1553 size_t size;
1554
1555 p = td->td_proc;
1556 size = 0;
1557
1558 size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1559
1560 /*
1561 * To have the debugger select the right thread (LWP) as the initial
1562 * thread, we dump the state of the thread passed to us in td first.
1563 * This is the thread that causes the core dump and thus likely to
1564 * be the right thread one wants to have selected in the debugger.
1565 */
1566 thr = td;
1567 while (thr != NULL) {
1568 size += register_note(list, NT_PRSTATUS,
1569 __elfN(note_prstatus), thr);
1570 size += register_note(list, NT_FPREGSET,
1571 __elfN(note_fpregset), thr);
1572 size += register_note(list, NT_THRMISC,
1573 __elfN(note_thrmisc), thr);
1574 size += register_note(list, -1,
1575 __elfN(note_threadmd), thr);
1576
1577 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1578 TAILQ_NEXT(thr, td_plist);
1579 if (thr == td)
1580 thr = TAILQ_NEXT(thr, td_plist);
1581 }
1582
1583 size += register_note(list, NT_PROCSTAT_PROC,
1584 __elfN(note_procstat_proc), p);
1585 size += register_note(list, NT_PROCSTAT_FILES,
1586 note_procstat_files, p);
1587 size += register_note(list, NT_PROCSTAT_VMMAP,
1588 note_procstat_vmmap, p);
1589 size += register_note(list, NT_PROCSTAT_GROUPS,
1590 note_procstat_groups, p);
1591 size += register_note(list, NT_PROCSTAT_UMASK,
1592 note_procstat_umask, p);
1593 size += register_note(list, NT_PROCSTAT_RLIMIT,
1594 note_procstat_rlimit, p);
1595 size += register_note(list, NT_PROCSTAT_OSREL,
1596 note_procstat_osrel, p);
1597 size += register_note(list, NT_PROCSTAT_PSSTRINGS,
1598 __elfN(note_procstat_psstrings), p);
1599 size += register_note(list, NT_PROCSTAT_AUXV,
1600 __elfN(note_procstat_auxv), p);
1601
1602 *sizep = size;
1603 }
1604
1605 static void
1606 __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1607 size_t notesz)
1608 {
1609 Elf_Ehdr *ehdr;
1610 Elf_Phdr *phdr;
1611 struct phdr_closure phc;
1612
1613 ehdr = (Elf_Ehdr *)hdr;
1614 phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
1615
1616 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1617 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1618 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1619 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1620 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1621 ehdr->e_ident[EI_DATA] = ELF_DATA;
1622 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1623 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1624 ehdr->e_ident[EI_ABIVERSION] = 0;
1625 ehdr->e_ident[EI_PAD] = 0;
1626 ehdr->e_type = ET_CORE;
1627 ehdr->e_machine = td->td_proc->p_elf_machine;
1628 ehdr->e_version = EV_CURRENT;
1629 ehdr->e_entry = 0;
1630 ehdr->e_phoff = sizeof(Elf_Ehdr);
1631 ehdr->e_flags = td->td_proc->p_elf_flags;
1632 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1633 ehdr->e_phentsize = sizeof(Elf_Phdr);
1634 ehdr->e_phnum = numsegs + 1;
1635 ehdr->e_shentsize = sizeof(Elf_Shdr);
1636 ehdr->e_shnum = 0;
1637 ehdr->e_shstrndx = SHN_UNDEF;
1638
1639 /*
1640 * Fill in the program header entries.
1641 */
1642
1643 /* The note segement. */
1644 phdr->p_type = PT_NOTE;
1645 phdr->p_offset = hdrsize;
1646 phdr->p_vaddr = 0;
1647 phdr->p_paddr = 0;
1648 phdr->p_filesz = notesz;
1649 phdr->p_memsz = 0;
1650 phdr->p_flags = PF_R;
1651 phdr->p_align = ELF_NOTE_ROUNDSIZE;
1652 phdr++;
1653
1654 /* All the writable segments from the program. */
1655 phc.phdr = phdr;
1656 phc.offset = round_page(hdrsize + notesz);
1657 each_writable_segment(td, cb_put_phdr, &phc);
1658 }
1659
1660 static size_t
1661 register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1662 {
1663 struct note_info *ninfo;
1664 size_t size, notesize;
1665
1666 size = 0;
1667 out(arg, NULL, &size);
1668 ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1669 ninfo->type = type;
1670 ninfo->outfunc = out;
1671 ninfo->outarg = arg;
1672 ninfo->outsize = size;
1673 TAILQ_INSERT_TAIL(list, ninfo, link);
1674
1675 if (type == -1)
1676 return (size);
1677
1678 notesize = sizeof(Elf_Note) + /* note header */
1679 roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1680 /* note name */
1681 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
1682
1683 return (notesize);
1684 }
1685
1686 static size_t
1687 append_note_data(const void *src, void *dst, size_t len)
1688 {
1689 size_t padded_len;
1690
1691 padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
1692 if (dst != NULL) {
1693 bcopy(src, dst, len);
1694 bzero((char *)dst + len, padded_len - len);
1695 }
1696 return (padded_len);
1697 }
1698
1699 size_t
1700 __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
1701 {
1702 Elf_Note *note;
1703 char *buf;
1704 size_t notesize;
1705
1706 buf = dst;
1707 if (buf != NULL) {
1708 note = (Elf_Note *)buf;
1709 note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1710 note->n_descsz = size;
1711 note->n_type = type;
1712 buf += sizeof(*note);
1713 buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
1714 sizeof(FREEBSD_ABI_VENDOR));
1715 append_note_data(src, buf, size);
1716 if (descp != NULL)
1717 *descp = buf;
1718 }
1719
1720 notesize = sizeof(Elf_Note) + /* note header */
1721 roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1722 /* note name */
1723 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
1724
1725 return (notesize);
1726 }
1727
1728 static void
1729 __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1730 {
1731 Elf_Note note;
1732 ssize_t old_len, sect_len;
1733 size_t new_len, descsz, i;
1734
1735 if (ninfo->type == -1) {
1736 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1737 return;
1738 }
1739
1740 note.n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1741 note.n_descsz = ninfo->outsize;
1742 note.n_type = ninfo->type;
1743
1744 sbuf_bcat(sb, ¬e, sizeof(note));
1745 sbuf_start_section(sb, &old_len);
1746 sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR));
1747 sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1748 if (note.n_descsz == 0)
1749 return;
1750 sbuf_start_section(sb, &old_len);
1751 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1752 sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1753 if (sect_len < 0)
1754 return;
1755
1756 new_len = (size_t)sect_len;
1757 descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
1758 if (new_len < descsz) {
1759 /*
1760 * It is expected that individual note emitters will correctly
1761 * predict their expected output size and fill up to that size
1762 * themselves, padding in a format-specific way if needed.
1763 * However, in case they don't, just do it here with zeros.
1764 */
1765 for (i = 0; i < descsz - new_len; i++)
1766 sbuf_putc(sb, 0);
1767 } else if (new_len > descsz) {
1768 /*
1769 * We can't always truncate sb -- we may have drained some
1770 * of it already.
1771 */
1772 KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
1773 "read it (%zu > %zu). Since it is longer than "
1774 "expected, this coredump's notes are corrupt. THIS "
1775 "IS A BUG in the note_procstat routine for type %u.\n",
1776 __func__, (unsigned)note.n_type, new_len, descsz,
1777 (unsigned)note.n_type));
1778 }
1779 }
1780
1781 /*
1782 * Miscellaneous note out functions.
1783 */
1784
1785 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1786 #include <compat/freebsd32/freebsd32.h>
1787
1788 typedef struct prstatus32 elf_prstatus_t;
1789 typedef struct prpsinfo32 elf_prpsinfo_t;
1790 typedef struct fpreg32 elf_prfpregset_t;
1791 typedef struct fpreg32 elf_fpregset_t;
1792 typedef struct reg32 elf_gregset_t;
1793 typedef struct thrmisc32 elf_thrmisc_t;
1794 #define ELF_KERN_PROC_MASK KERN_PROC_MASK32
1795 typedef struct kinfo_proc32 elf_kinfo_proc_t;
1796 typedef uint32_t elf_ps_strings_t;
1797 #else
1798 typedef prstatus_t elf_prstatus_t;
1799 typedef prpsinfo_t elf_prpsinfo_t;
1800 typedef prfpregset_t elf_prfpregset_t;
1801 typedef prfpregset_t elf_fpregset_t;
1802 typedef gregset_t elf_gregset_t;
1803 typedef thrmisc_t elf_thrmisc_t;
1804 #define ELF_KERN_PROC_MASK 0
1805 typedef struct kinfo_proc elf_kinfo_proc_t;
1806 typedef vm_offset_t elf_ps_strings_t;
1807 #endif
1808
1809 static void
1810 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
1811 {
1812 struct sbuf sbarg;
1813 size_t len;
1814 char *cp, *end;
1815 struct proc *p;
1816 elf_prpsinfo_t *psinfo;
1817 int error;
1818
1819 p = (struct proc *)arg;
1820 if (sb != NULL) {
1821 KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1822 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
1823 psinfo->pr_version = PRPSINFO_VERSION;
1824 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1825 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1826 PROC_LOCK(p);
1827 if (p->p_args != NULL) {
1828 len = sizeof(psinfo->pr_psargs) - 1;
1829 if (len > p->p_args->ar_length)
1830 len = p->p_args->ar_length;
1831 memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
1832 PROC_UNLOCK(p);
1833 error = 0;
1834 } else {
1835 _PHOLD(p);
1836 PROC_UNLOCK(p);
1837 sbuf_new(&sbarg, psinfo->pr_psargs,
1838 sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
1839 error = proc_getargv(curthread, p, &sbarg);
1840 PRELE(p);
1841 if (sbuf_finish(&sbarg) == 0)
1842 len = sbuf_len(&sbarg) - 1;
1843 else
1844 len = sizeof(psinfo->pr_psargs) - 1;
1845 sbuf_delete(&sbarg);
1846 }
1847 if (error || len == 0)
1848 strlcpy(psinfo->pr_psargs, p->p_comm,
1849 sizeof(psinfo->pr_psargs));
1850 else {
1851 KASSERT(len < sizeof(psinfo->pr_psargs),
1852 ("len is too long: %zu vs %zu", len,
1853 sizeof(psinfo->pr_psargs)));
1854 cp = psinfo->pr_psargs;
1855 end = cp + len - 1;
1856 for (;;) {
1857 cp = memchr(cp, '\0', end - cp);
1858 if (cp == NULL)
1859 break;
1860 *cp = ' ';
1861 }
1862 }
1863 psinfo->pr_pid = p->p_pid;
1864 sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1865 free(psinfo, M_TEMP);
1866 }
1867 *sizep = sizeof(*psinfo);
1868 }
1869
1870 static void
1871 __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1872 {
1873 struct thread *td;
1874 elf_prstatus_t *status;
1875
1876 td = (struct thread *)arg;
1877 if (sb != NULL) {
1878 KASSERT(*sizep == sizeof(*status), ("invalid size"));
1879 status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
1880 status->pr_version = PRSTATUS_VERSION;
1881 status->pr_statussz = sizeof(elf_prstatus_t);
1882 status->pr_gregsetsz = sizeof(elf_gregset_t);
1883 status->pr_fpregsetsz = sizeof(elf_fpregset_t);
1884 status->pr_osreldate = osreldate;
1885 status->pr_cursig = td->td_proc->p_sig;
1886 status->pr_pid = td->td_tid;
1887 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1888 fill_regs32(td, &status->pr_reg);
1889 #else
1890 fill_regs(td, &status->pr_reg);
1891 #endif
1892 sbuf_bcat(sb, status, sizeof(*status));
1893 free(status, M_TEMP);
1894 }
1895 *sizep = sizeof(*status);
1896 }
1897
1898 static void
1899 __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
1900 {
1901 struct thread *td;
1902 elf_prfpregset_t *fpregset;
1903
1904 td = (struct thread *)arg;
1905 if (sb != NULL) {
1906 KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
1907 fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1908 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1909 fill_fpregs32(td, fpregset);
1910 #else
1911 fill_fpregs(td, fpregset);
1912 #endif
1913 sbuf_bcat(sb, fpregset, sizeof(*fpregset));
1914 free(fpregset, M_TEMP);
1915 }
1916 *sizep = sizeof(*fpregset);
1917 }
1918
1919 static void
1920 __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
1921 {
1922 struct thread *td;
1923 elf_thrmisc_t thrmisc;
1924
1925 td = (struct thread *)arg;
1926 if (sb != NULL) {
1927 KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
1928 bzero(&thrmisc._pad, sizeof(thrmisc._pad));
1929 strcpy(thrmisc.pr_tname, td->td_name);
1930 sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
1931 }
1932 *sizep = sizeof(thrmisc);
1933 }
1934
1935 /*
1936 * Allow for MD specific notes, as well as any MD
1937 * specific preparations for writing MI notes.
1938 */
1939 static void
1940 __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
1941 {
1942 struct thread *td;
1943 void *buf;
1944 size_t size;
1945
1946 td = (struct thread *)arg;
1947 size = *sizep;
1948 if (size != 0 && sb != NULL)
1949 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
1950 else
1951 buf = NULL;
1952 size = 0;
1953 __elfN(dump_thread)(td, buf, &size);
1954 KASSERT(sb == NULL || *sizep == size, ("invalid size"));
1955 if (size != 0 && sb != NULL)
1956 sbuf_bcat(sb, buf, size);
1957 free(buf, M_TEMP);
1958 *sizep = size;
1959 }
1960
1961 #ifdef KINFO_PROC_SIZE
1962 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
1963 #endif
1964
1965 static void
1966 __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
1967 {
1968 struct proc *p;
1969 size_t size;
1970 int structsize;
1971
1972 p = (struct proc *)arg;
1973 size = sizeof(structsize) + p->p_numthreads *
1974 sizeof(elf_kinfo_proc_t);
1975
1976 if (sb != NULL) {
1977 KASSERT(*sizep == size, ("invalid size"));
1978 structsize = sizeof(elf_kinfo_proc_t);
1979 sbuf_bcat(sb, &structsize, sizeof(structsize));
1980 PROC_LOCK(p);
1981 kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
1982 }
1983 *sizep = size;
1984 }
1985
1986 #ifdef KINFO_FILE_SIZE
1987 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
1988 #endif
1989
1990 static void
1991 note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
1992 {
1993 struct proc *p;
1994 size_t size, sect_sz, i;
1995 ssize_t start_len, sect_len;
1996 int structsize, filedesc_flags;
1997
1998 if (coredump_pack_fileinfo)
1999 filedesc_flags = KERN_FILEDESC_PACK_KINFO;
2000 else
2001 filedesc_flags = 0;
2002
2003 p = (struct proc *)arg;
2004 structsize = sizeof(struct kinfo_file);
2005 if (sb == NULL) {
2006 size = 0;
2007 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2008 sbuf_set_drain(sb, sbuf_drain_count, &size);
2009 sbuf_bcat(sb, &structsize, sizeof(structsize));
2010 PROC_LOCK(p);
2011 kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
2012 sbuf_finish(sb);
2013 sbuf_delete(sb);
2014 *sizep = size;
2015 } else {
2016 sbuf_start_section(sb, &start_len);
2017
2018 sbuf_bcat(sb, &structsize, sizeof(structsize));
2019 PROC_LOCK(p);
2020 kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
2021 filedesc_flags);
2022
2023 sect_len = sbuf_end_section(sb, start_len, 0, 0);
2024 if (sect_len < 0)
2025 return;
2026 sect_sz = sect_len;
2027
2028 KASSERT(sect_sz <= *sizep,
2029 ("kern_proc_filedesc_out did not respect maxlen; "
2030 "requested %zu, got %zu", *sizep - sizeof(structsize),
2031 sect_sz - sizeof(structsize)));
2032
2033 for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
2034 sbuf_putc(sb, 0);
2035 }
2036 }
2037
2038 #ifdef KINFO_VMENTRY_SIZE
2039 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2040 #endif
2041
2042 static void
2043 note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
2044 {
2045 struct proc *p;
2046 size_t size;
2047 int structsize, vmmap_flags;
2048
2049 if (coredump_pack_vmmapinfo)
2050 vmmap_flags = KERN_VMMAP_PACK_KINFO;
2051 else
2052 vmmap_flags = 0;
2053
2054 p = (struct proc *)arg;
2055 structsize = sizeof(struct kinfo_vmentry);
2056 if (sb == NULL) {
2057 size = 0;
2058 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2059 sbuf_set_drain(sb, sbuf_drain_count, &size);
2060 sbuf_bcat(sb, &structsize, sizeof(structsize));
2061 PROC_LOCK(p);
2062 kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
2063 sbuf_finish(sb);
2064 sbuf_delete(sb);
2065 *sizep = size;
2066 } else {
2067 sbuf_bcat(sb, &structsize, sizeof(structsize));
2068 PROC_LOCK(p);
2069 kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize),
2070 vmmap_flags);
2071 }
2072 }
2073
2074 static void
2075 note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
2076 {
2077 struct proc *p;
2078 size_t size;
2079 int structsize;
2080
2081 p = (struct proc *)arg;
2082 size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
2083 if (sb != NULL) {
2084 KASSERT(*sizep == size, ("invalid size"));
2085 structsize = sizeof(gid_t);
2086 sbuf_bcat(sb, &structsize, sizeof(structsize));
2087 sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2088 sizeof(gid_t));
2089 }
2090 *sizep = size;
2091 }
2092
2093 static void
2094 note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2095 {
2096 struct proc *p;
2097 size_t size;
2098 int structsize;
2099
2100 p = (struct proc *)arg;
2101 size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask);
2102 if (sb != NULL) {
2103 KASSERT(*sizep == size, ("invalid size"));
2104 structsize = sizeof(p->p_fd->fd_cmask);
2105 sbuf_bcat(sb, &structsize, sizeof(structsize));
2106 sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask));
2107 }
2108 *sizep = size;
2109 }
2110
2111 static void
2112 note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2113 {
2114 struct proc *p;
2115 struct rlimit rlim[RLIM_NLIMITS];
2116 size_t size;
2117 int structsize, i;
2118
2119 p = (struct proc *)arg;
2120 size = sizeof(structsize) + sizeof(rlim);
2121 if (sb != NULL) {
2122 KASSERT(*sizep == size, ("invalid size"));
2123 structsize = sizeof(rlim);
2124 sbuf_bcat(sb, &structsize, sizeof(structsize));
2125 PROC_LOCK(p);
2126 for (i = 0; i < RLIM_NLIMITS; i++)
2127 lim_rlimit(p, i, &rlim[i]);
2128 PROC_UNLOCK(p);
2129 sbuf_bcat(sb, rlim, sizeof(rlim));
2130 }
2131 *sizep = size;
2132 }
2133
2134 static void
2135 note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2136 {
2137 struct proc *p;
2138 size_t size;
2139 int structsize;
2140
2141 p = (struct proc *)arg;
2142 size = sizeof(structsize) + sizeof(p->p_osrel);
2143 if (sb != NULL) {
2144 KASSERT(*sizep == size, ("invalid size"));
2145 structsize = sizeof(p->p_osrel);
2146 sbuf_bcat(sb, &structsize, sizeof(structsize));
2147 sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2148 }
2149 *sizep = size;
2150 }
2151
2152 static void
2153 __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2154 {
2155 struct proc *p;
2156 elf_ps_strings_t ps_strings;
2157 size_t size;
2158 int structsize;
2159
2160 p = (struct proc *)arg;
2161 size = sizeof(structsize) + sizeof(ps_strings);
2162 if (sb != NULL) {
2163 KASSERT(*sizep == size, ("invalid size"));
2164 structsize = sizeof(ps_strings);
2165 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2166 ps_strings = PTROUT(p->p_sysent->sv_psstrings);
2167 #else
2168 ps_strings = p->p_sysent->sv_psstrings;
2169 #endif
2170 sbuf_bcat(sb, &structsize, sizeof(structsize));
2171 sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2172 }
2173 *sizep = size;
2174 }
2175
2176 static void
2177 __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2178 {
2179 struct proc *p;
2180 size_t size;
2181 int structsize;
2182
2183 p = (struct proc *)arg;
2184 if (sb == NULL) {
2185 size = 0;
2186 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2187 sbuf_set_drain(sb, sbuf_drain_count, &size);
2188 sbuf_bcat(sb, &structsize, sizeof(structsize));
2189 PHOLD(p);
2190 proc_getauxv(curthread, p, sb);
2191 PRELE(p);
2192 sbuf_finish(sb);
2193 sbuf_delete(sb);
2194 *sizep = size;
2195 } else {
2196 structsize = sizeof(Elf_Auxinfo);
2197 sbuf_bcat(sb, &structsize, sizeof(structsize));
2198 PHOLD(p);
2199 proc_getauxv(curthread, p, sb);
2200 PRELE(p);
2201 }
2202 }
2203
2204 static boolean_t
2205 __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
2206 int32_t *osrel, const Elf_Phdr *pnote)
2207 {
2208 const Elf_Note *note, *note0, *note_end;
2209 const char *note_name;
2210 char *buf;
2211 int i, error;
2212 boolean_t res;
2213
2214 /* We need some limit, might as well use PAGE_SIZE. */
2215 if (pnote == NULL || pnote->p_filesz > PAGE_SIZE)
2216 return (FALSE);
2217 ASSERT_VOP_LOCKED(imgp->vp, "parse_notes");
2218 if (pnote->p_offset > PAGE_SIZE ||
2219 pnote->p_filesz > PAGE_SIZE - pnote->p_offset) {
2220 VOP_UNLOCK(imgp->vp, 0);
2221 buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK);
2222 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
2223 error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz,
2224 pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED,
2225 curthread->td_ucred, NOCRED, NULL, curthread);
2226 if (error != 0) {
2227 uprintf("i/o error PT_NOTE\n");
2228 res = FALSE;
2229 goto ret;
2230 }
2231 note = note0 = (const Elf_Note *)buf;
2232 note_end = (const Elf_Note *)(buf + pnote->p_filesz);
2233 } else {
2234 note = note0 = (const Elf_Note *)(imgp->image_header +
2235 pnote->p_offset);
2236 note_end = (const Elf_Note *)(imgp->image_header +
2237 pnote->p_offset + pnote->p_filesz);
2238 buf = NULL;
2239 }
2240 for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
2241 if (!aligned(note, Elf32_Addr) || (const char *)note_end -
2242 (const char *)note < sizeof(Elf_Note)) {
2243 res = FALSE;
2244 goto ret;
2245 }
2246 if (note->n_namesz != checknote->hdr.n_namesz ||
2247 note->n_descsz != checknote->hdr.n_descsz ||
2248 note->n_type != checknote->hdr.n_type)
2249 goto nextnote;
2250 note_name = (const char *)(note + 1);
2251 if (note_name + checknote->hdr.n_namesz >=
2252 (const char *)note_end || strncmp(checknote->vendor,
2253 note_name, checknote->hdr.n_namesz) != 0)
2254 goto nextnote;
2255
2256 /*
2257 * Fetch the osreldate for binary
2258 * from the ELF OSABI-note if necessary.
2259 */
2260 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
2261 checknote->trans_osrel != NULL) {
2262 res = checknote->trans_osrel(note, osrel);
2263 goto ret;
2264 }
2265 res = TRUE;
2266 goto ret;
2267 nextnote:
2268 note = (const Elf_Note *)((const char *)(note + 1) +
2269 roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
2270 roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
2271 }
2272 res = FALSE;
2273 ret:
2274 free(buf, M_TEMP);
2275 return (res);
2276 }
2277
2278 /*
2279 * Try to find the appropriate ABI-note section for checknote,
2280 * fetch the osreldate for binary from the ELF OSABI-note. Only the
2281 * first page of the image is searched, the same as for headers.
2282 */
2283 static boolean_t
2284 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
2285 int32_t *osrel)
2286 {
2287 const Elf_Phdr *phdr;
2288 const Elf_Ehdr *hdr;
2289 int i;
2290
2291 hdr = (const Elf_Ehdr *)imgp->image_header;
2292 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
2293
2294 for (i = 0; i < hdr->e_phnum; i++) {
2295 if (phdr[i].p_type == PT_NOTE &&
2296 __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
2297 return (TRUE);
2298 }
2299 return (FALSE);
2300
2301 }
2302
2303 /*
2304 * Tell kern_execve.c about it, with a little help from the linker.
2305 */
2306 static struct execsw __elfN(execsw) = {
2307 __CONCAT(exec_, __elfN(imgact)),
2308 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2309 };
2310 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2311
2312 #ifdef COMPRESS_USER_CORES
2313 /*
2314 * Compress and write out a core segment for a user process.
2315 *
2316 * 'inbuf' is the starting address of a VM segment in the process' address
2317 * space that is to be compressed and written out to the core file. 'dest_buf'
2318 * is a buffer in the kernel's address space. The segment is copied from
2319 * 'inbuf' to 'dest_buf' first before being processed by the compression
2320 * routine gzwrite(). This copying is necessary because the content of the VM
2321 * segment may change between the compression pass and the crc-computation pass
2322 * in gzwrite(). This is because realtime threads may preempt the UNIX kernel.
2323 *
2324 * If inbuf is NULL it is assumed that data is already copied to 'dest_buf'.
2325 */
2326 static int
2327 compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len,
2328 struct thread *td)
2329 {
2330 int len_compressed;
2331 int error = 0;
2332 unsigned int chunk_len;
2333
2334 while (len) {
2335 if (inbuf != NULL) {
2336 chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len;
2337
2338 /*
2339 * We can get EFAULT error here. In that case zero out
2340 * the current chunk of the segment.
2341 */
2342 error = copyin(inbuf, dest_buf, chunk_len);
2343 if (error != 0) {
2344 bzero(dest_buf, chunk_len);
2345 error = 0;
2346 }
2347 inbuf += chunk_len;
2348 } else {
2349 chunk_len = len;
2350 }
2351 len_compressed = gzwrite(file, dest_buf, chunk_len);
2352
2353 EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
2354
2355 if ((unsigned int)len_compressed != chunk_len) {
2356 log(LOG_WARNING,
2357 "compress_core: length mismatch (0x%x returned, "
2358 "0x%x expected)\n", len_compressed, chunk_len);
2359 EVENTHANDLER_INVOKE(app_coredump_error, td,
2360 "compress_core: length mismatch %x -> %x",
2361 chunk_len, len_compressed);
2362 error = EFAULT;
2363 break;
2364 }
2365 len -= chunk_len;
2366 maybe_yield();
2367 }
2368
2369 return (error);
2370 }
2371 #endif /* COMPRESS_USER_CORES */
2372
2373 static vm_prot_t
2374 __elfN(trans_prot)(Elf_Word flags)
2375 {
2376 vm_prot_t prot;
2377
2378 prot = 0;
2379 if (flags & PF_X)
2380 prot |= VM_PROT_EXECUTE;
2381 if (flags & PF_W)
2382 prot |= VM_PROT_WRITE;
2383 if (flags & PF_R)
2384 prot |= VM_PROT_READ;
2385 #if __ELF_WORD_SIZE == 32
2386 #if defined(__amd64__) || defined(__ia64__)
2387 if (i386_read_exec && (flags & PF_R))
2388 prot |= VM_PROT_EXECUTE;
2389 #endif
2390 #endif
2391 return (prot);
2392 }
2393
2394 static Elf_Word
2395 __elfN(untrans_prot)(vm_prot_t prot)
2396 {
2397 Elf_Word flags;
2398
2399 flags = 0;
2400 if (prot & VM_PROT_EXECUTE)
2401 flags |= PF_X;
2402 if (prot & VM_PROT_READ)
2403 flags |= PF_R;
2404 if (prot & VM_PROT_WRITE)
2405 flags |= PF_W;
2406 return (flags);
2407 }
Cache object: 6486183c627090253b99cf9c41b1030f
|