31 #include <sys/cdefs.h>
34 #include "opt_capsicum.h"
35 #include "opt_compat.h"
38 #include <sys/param.h>
39 #include <sys/capability.h>
41 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/imgact.h>
44 #include <sys/imgact_elf.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
51 #include <sys/namei.h>
52 #include <sys/pioctl.h>
54 #include <sys/procfs.h>
55 #include <sys/racct.h>
56 #include <sys/resourcevar.h>
58 #include <sys/sf_buf.h>
60 #include <sys/systm.h>
61 #include <sys/signalvar.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>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_param.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_extern.h>
82 #include <machine/elf.h>
83 #include <machine/md_var.h>
85 #define ELF_NOTE_ROUNDSIZE 4
86 #define OLD_EI_BRAND 8
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);
94 vm_offset_t offset, caddr_t vmaddr,
size_t memsz,
size_t filsz,
95 vm_prot_t prot,
size_t pagesize);
101 Elf_Brandnote *checknote, int32_t *osrel);
108 #ifdef COMPRESS_USER_CORES
109 static int compress_core(gzFile,
char *,
char *,
unsigned int,
112 #define CORE_BUF_SIZE (16 * 1024)
114 int __elfN(fallback_brand) = -1;
116 fallback_brand, CTLFLAG_RW, &
__elfN(fallback_brand), 0,
121 static int elf_legacy_coredump = 0;
123 &elf_legacy_coredump, 0,
"");
126 #if defined(__amd64__) || defined(__powerpc64__)
132 nxstack, CTLFLAG_RW, &
__elfN(nxstack), 0,
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");
143 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
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))
149 static const char BSDSUNIX_ABI_VENDOR[] =
"BSDSUniX";
151 Elf_Brandnote
__elfN(bsdsunix_brandnote) = {
152 .hdr.n_namesz =
sizeof(BSDSUNIX_ABI_VENDOR),
153 .hdr.n_descsz =
sizeof(int32_t),
155 .vendor = BSDSUNIX_ABI_VENDOR,
156 .flags = BN_TRANSLATE_OSREL,
165 p = (uintptr_t)(note + 1);
167 *osrel = *(
const int32_t *)(p);
175 Elf_Brandnote
__elfN(kbsdsunix_brandnote) = {
179 .vendor = GNU_ABI_VENDOR,
180 .flags = BN_TRANSLATE_OSREL,
187 const Elf32_Word *desc;
190 p = (uintptr_t)(note + 1);
193 desc = (
const Elf32_Word *)p;
194 if (desc[0] != GNU_KBSDSUNIX_ABI_DESC)
201 *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
211 for (i = 0; i < MAX_BRANDS; i++) {
212 if (elf_brand_list[i] == NULL) {
213 elf_brand_list[i] = entry;
217 if (i == MAX_BRANDS) {
218 printf(
"WARNING: %s: could not insert brandinfo entry: %p\n",
230 for (i = 0; i < MAX_BRANDS; i++) {
231 if (elf_brand_list[i] == entry) {
232 elf_brand_list[i] = NULL;
248 FOREACH_PROC_IN_SYSTEM(p) {
249 if (p->p_sysent == entry->sysvec) {
259 static Elf_Brandinfo *
261 int interp_name_len, int32_t *osrel)
263 const Elf_Ehdr *hdr = (
const Elf_Ehdr *)imgp->image_header;
276 for (i = 0; i < MAX_BRANDS; i++) {
277 bi = elf_brand_list[i];
280 if (hdr->e_machine == bi->machine && (bi->flags &
281 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
289 for (i = 0; i < MAX_BRANDS; i++) {
290 bi = elf_brand_list[i];
291 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
293 if (hdr->e_machine == bi->machine &&
294 (hdr->e_ident[EI_OSABI] == bi->brand ||
296 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
301 if (interp != NULL) {
302 for (i = 0; i < MAX_BRANDS; i++) {
303 bi = elf_brand_list[i];
304 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
306 if (hdr->e_machine == bi->machine &&
308 strlen(bi->interp_path) + 1 == interp_name_len &&
309 strncmp(interp, bi->interp_path, interp_name_len)
316 for (i = 0; i < MAX_BRANDS; i++) {
317 bi = elf_brand_list[i];
318 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
320 if (hdr->e_machine == bi->machine &&
321 __elfN(fallback_brand) == bi->brand)
334 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
335 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
336 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
337 hdr->e_phentsize !=
sizeof(Elf_Phdr) ||
338 hdr->e_version != ELF_TARG_VER)
345 for (i = 0; i < MAX_BRANDS; i++) {
346 bi = elf_brand_list[i];
347 if (bi != NULL && bi->machine == hdr->e_machine)
358 vm_offset_t
start, vm_offset_t end, vm_prot_t prot)
368 vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
369 VM_PROT_ALL, VM_PROT_ALL, 0);
376 sf = vm_imgact_map_page(
object, offset);
378 return (KERN_FAILURE);
379 off = offset - trunc_page(offset);
380 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
382 vm_imgact_unmap_page(sf);
384 return (KERN_FAILURE);
388 return (KERN_SUCCESS);
393 vm_offset_t
start, vm_offset_t end, vm_prot_t prot,
int cow)
400 if (start != trunc_page(start)) {
402 round_page(start), prot);
405 offset += round_page(start) -
start;
406 start = round_page(start);
408 if (end != round_page(end)) {
410 trunc_page(end) -
start, trunc_page(end), end, prot);
413 end = trunc_page(end);
416 if (offset & PAGE_MASK) {
421 rv = vm_map_find(map, NULL, 0, &start, end - start,
422 FALSE, prot | VM_PROT_WRITE, VM_PROT_ALL, 0);
426 return (KERN_SUCCESS);
427 for (; start < end; start += sz) {
428 sf = vm_imgact_map_page(
object, offset);
430 return (KERN_FAILURE);
431 off = offset - trunc_page(offset);
433 if (sz > PAGE_SIZE - off)
434 sz = PAGE_SIZE - off;
435 error = copyout((caddr_t)sf_buf_kva(sf) + off,
437 vm_imgact_unmap_page(sf);
439 return (KERN_FAILURE);
445 vm_object_reference(
object);
447 rv = vm_map_insert(map,
object, offset, start, end,
448 prot, VM_PROT_ALL, cow);
450 if (rv != KERN_SUCCESS)
451 vm_object_deallocate(
object);
455 return (KERN_SUCCESS);
461 vm_object_t object, vm_offset_t offset,
462 caddr_t vmaddr,
size_t memsz,
size_t filsz, vm_prot_t prot,
467 vm_offset_t map_addr;
470 vm_offset_t file_addr;
481 if ((off_t)filsz + offset >
object->un_pager.vnp.vnp_size ||
483 uprintf(
"elf_load_section: truncated ELF file\n");
497 map_len =
trunc_page_ps(offset + filsz, pagesize) - file_addr;
499 map_len =
round_page_ps(offset + filsz, pagesize) - file_addr;
503 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
504 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
513 if (rv != KERN_SUCCESS)
517 if (memsz == filsz) {
529 copy_len = (offset + filsz) -
trunc_page_ps(offset + filsz, pagesize);
530 map_addr =
trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
531 map_len =
round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
537 map_addr + map_len, VM_PROT_ALL, 0);
538 if (rv != KERN_SUCCESS) {
546 sf = vm_imgact_map_page(
object, offset + filsz);
552 trunc_page(offset + filsz);
553 error = copyout((caddr_t)sf_buf_kva(sf) + off,
554 (caddr_t)map_addr, copy_len);
555 vm_imgact_unmap_page(sf);
565 vm_map_protect(&vmspace->vm_map, trunc_page(map_addr),
566 round_page(map_addr + map_len), prot, FALSE);
585 u_long *entry,
size_t pagesize)
590 struct image_params image_params;
592 const Elf_Ehdr *hdr = NULL;
593 const Elf_Phdr *phdr = NULL;
594 struct nameidata *nd;
595 struct vmspace *vmspace = p->p_vmspace;
597 struct image_params *imgp;
600 u_long base_addr = 0;
601 int vfslocked, error, i, numsegs;
603 #ifdef CAPABILITY_MODE
608 if (IN_CAPABILITY_MODE(curthread))
612 tempdata =
malloc(
sizeof(*tempdata), M_TEMP, M_WAITOK);
614 attr = &tempdata->attr;
615 imgp = &tempdata->image_params;
622 imgp->firstpage = NULL;
623 imgp->image_header = NULL;
625 imgp->execlabel = NULL;
627 NDINIT(nd, LOOKUP, MPSAFE|LOCKLEAF|FOLLOW, UIO_SYSSPACE, file,
630 if ((error =
namei(nd)) != 0) {
634 vfslocked = NDHASGIANT(nd);
635 NDFREE(nd, NDF_ONLY_PNBUF);
636 imgp->vp = nd->ni_vp;
653 VOP_SET_TEXT(nd->ni_vp);
655 imgp->object = nd->ni_vp->v_object;
657 hdr = (
const Elf_Ehdr *)imgp->image_header;
660 if (hdr->e_type == ET_DYN)
662 else if (hdr->e_type == ET_EXEC)
670 if ((hdr->e_phoff > PAGE_SIZE) ||
671 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
676 phdr = (
const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
677 if (!
aligned(phdr, Elf_Addr)) {
682 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
683 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
687 imgp->object, phdr[i].p_offset,
688 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
689 phdr[i].p_memsz, phdr[i].p_filesz, prot,
697 base_addr = trunc_page(phdr[i].p_vaddr +
703 *entry = (
unsigned long)hdr->e_entry + rbase;
712 VFS_UNLOCK_GIANT(vfslocked);
713 free(tempdata, M_TEMP);
721 const Elf_Ehdr *hdr = (
const Elf_Ehdr *)imgp->image_header;
722 const Elf_Phdr *phdr;
723 Elf_Auxargs *elf_auxargs;
724 struct vmspace *vmspace;
726 u_long text_size = 0, data_size = 0, total_size = 0;
727 u_long text_addr = 0, data_addr = 0;
728 u_long seg_size, seg_addr;
729 u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
731 int error = 0, i, n, interp_name_len = 0;
732 const char *interp = NULL, *newinterp = NULL;
733 Elf_Brandinfo *brand_info;
735 struct sysentvec *sv;
744 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
752 if ((hdr->e_phoff > PAGE_SIZE) ||
753 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
757 phdr = (
const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
762 for (i = 0; i < hdr->e_phnum; i++) {
763 switch (phdr[i].p_type) {
766 baddr = phdr[i].p_vaddr;
771 if (phdr[i].p_filesz > MAXPATHLEN ||
772 phdr[i].p_offset > PAGE_SIZE ||
773 phdr[i].p_filesz > PAGE_SIZE - phdr[i].p_offset)
775 interp = imgp->image_header + phdr[i].p_offset;
776 interp_name_len = phdr[i].p_filesz;
788 if (brand_info == NULL) {
789 uprintf(
"ELF binary type \"%u\" not known.\n",
790 hdr->e_ident[EI_OSABI]);
793 if (hdr->e_type == ET_DYN) {
794 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0)
801 et_dyn_addr = ET_DYN_LOAD_ADDR;
806 sv = brand_info->sysvec;
807 if (interp != NULL && brand_info->interp_newpath != NULL)
808 newinterp = brand_info->interp_newpath;
818 VOP_UNLOCK(imgp->vp, 0);
821 imgp->proc->p_sysent = sv;
823 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
827 vmspace = imgp->proc->p_vmspace;
829 for (i = 0; i < hdr->e_phnum; i++) {
830 switch (phdr[i].p_type) {
832 if (phdr[i].p_memsz == 0)
836 imgp->object, phdr[i].p_offset,
837 (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
838 phdr[i].p_memsz, phdr[i].p_filesz, prot,
839 sv->sv_pagesize)) != 0)
848 if (phdr[i].p_offset == 0 &&
849 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
851 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
854 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
855 seg_size = round_page(phdr[i].p_memsz +
856 phdr[i].p_vaddr + et_dyn_addr - seg_addr);
869 if (phdr[i].p_flags & PF_X && text_size < seg_size) {
870 text_size = seg_size;
871 text_addr = seg_addr;
873 data_size = seg_size;
874 data_addr = seg_addr;
876 total_size += seg_size;
879 proghdr = phdr[i].p_vaddr + et_dyn_addr;
886 if (data_addr == 0 && data_size == 0) {
887 data_addr = text_addr;
888 data_size = text_size;
891 entry = (u_long)hdr->e_entry + et_dyn_addr;
898 PROC_LOCK(imgp->proc);
899 if (data_size >
lim_cur(imgp->proc, RLIMIT_DATA) ||
901 total_size >
lim_cur(imgp->proc, RLIMIT_VMEM) ||
902 racct_set(imgp->proc, RACCT_DATA, data_size) != 0 ||
903 racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) {
904 PROC_UNLOCK(imgp->proc);
908 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
909 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
910 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
911 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
919 addr = round_page((vm_offset_t)imgp->proc->p_vmspace->vm_daddr +
920 lim_max(imgp->proc, RLIMIT_DATA));
921 PROC_UNLOCK(imgp->proc);
923 imgp->entry_addr = entry;
925 if (interp != NULL) {
926 int have_interp = FALSE;
927 VOP_UNLOCK(imgp->vp, 0);
928 if (brand_info->emul_path != NULL &&
929 brand_info->emul_path[0] !=
'\0') {
932 brand_info->emul_path, interp);
934 &imgp->entry_addr, sv->sv_pagesize);
939 if (!have_interp && newinterp != NULL) {
941 &imgp->entry_addr, sv->sv_pagesize);
947 &imgp->entry_addr, sv->sv_pagesize);
949 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
951 uprintf(
"ELF interpreter %s not found\n", interp);
960 elf_auxargs =
malloc(
sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
961 elf_auxargs->execfd = -1;
962 elf_auxargs->phdr = proghdr;
963 elf_auxargs->phent = hdr->e_phentsize;
964 elf_auxargs->phnum = hdr->e_phnum;
965 elf_auxargs->pagesz = PAGE_SIZE;
966 elf_auxargs->base = addr;
967 elf_auxargs->flags = 0;
968 elf_auxargs->entry = entry;
970 imgp->auxargs = elf_auxargs;
971 imgp->interpreted = 0;
972 imgp->reloc_base = addr;
973 imgp->proc->p_osrel = osrel;
978 #define suword __CONCAT(suword, __ELF_WORD_SIZE)
983 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
987 base = (Elf_Addr *)*stack_base;
988 pos = base + (imgp->args->argc + imgp->args->envc + 2);
990 if (args->execfd != -1)
991 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
992 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
993 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
994 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
995 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
996 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
997 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
998 AUXARGS_ENTRY(pos, AT_BASE, args->base);
999 if (imgp->execpathp != 0)
1000 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1001 AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate);
1002 if (imgp->canary != 0) {
1003 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1004 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1006 AUXARGS_ENTRY(pos, AT_NCPUS,
mp_ncpus);
1007 if (imgp->pagesizes != 0) {
1008 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1009 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1011 if (imgp->sysent->sv_timekeep_base != 0) {
1012 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1013 imgp->sysent->sv_timekeep_base);
1015 AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
1016 != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
1017 imgp->sysent->sv_stackprot);
1018 AUXARGS_ENTRY(pos, AT_NULL, 0);
1020 free(imgp->auxargs, M_TEMP);
1021 imgp->auxargs = NULL;
1024 suword(base, (
long)imgp->args->argc);
1025 *stack_base = (register_t *)base;
1062 static
int __elfN(
corehdr)(struct thread *, struct vnode *, struct ucred *,
1063 int,
void *,
size_t, struct note_info_list *,
size_t, gzFile);
1066 static
void __elfN(
puthdr)(struct thread *,
void *,
size_t,
int,
size_t);
1067 static
void __elfN(
putnote)(struct note_info *, struct sbuf *);
1087 #ifdef COMPRESS_USER_CORES
1088 extern int compress_user_cores;
1089 extern int compress_user_cores_gzlevel;
1094 struct ucred *active_cred,
struct ucred *file_cred,
1095 struct thread *td,
char *core_buf, gzFile gzfile) {
1099 #ifdef COMPRESS_USER_CORES
1100 error = compress_core(gzfile, base, core_buf, len, td);
1102 panic(
"shouldn't be here");
1106 UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
1119 #ifdef COMPRESS_USER_CORES
1143 locked = PROC_LOCKED(p->
td->td_proc);
1145 PROC_UNLOCK(p->
td->td_proc);
1146 #ifdef COMPRESS_USER_CORES
1147 if (p->gzfile != Z_NULL)
1148 error = compress_core(p->gzfile, NULL, __DECONST(
char *, data),
1153 __DECONST(
void *, data), len, p->
offset, UIO_SYSSPACE,
1157 PROC_LOCK(p->
td->td_proc);
1172 sizep = (
size_t *)arg;
1180 struct ucred *cred = td->td_ucred;
1183 struct note_info_list notelst;
1184 struct note_info *ninfo;
1186 size_t hdrsize, notesz, coresize;
1188 gzFile gzfile = Z_NULL;
1189 char *core_buf = NULL;
1190 #ifdef COMPRESS_USER_CORES
1191 char gzopen_flags[8];
1193 int doing_compress = flags & IMGACT_CORE_COMPRESS;
1197 TAILQ_INIT(¬elst);
1199 #ifdef COMPRESS_USER_CORES
1200 if (doing_compress) {
1203 if (compress_user_cores_gzlevel >= 0 &&
1204 compress_user_cores_gzlevel <= 9)
1205 *p++ =
'0' + compress_user_cores_gzlevel;
1207 gzfile =
gz_open(
"", gzopen_flags, vp);
1208 if (gzfile == Z_NULL) {
1228 hdrsize =
sizeof(Elf_Ehdr) +
sizeof(Elf_Phdr) * (1 + seginfo.
count);
1230 coresize = round_page(hdrsize + notesz) + seginfo.
size;
1233 PROC_LOCK(td->td_proc);
1234 error =
racct_add(td->td_proc, RACCT_CORE, coresize);
1235 PROC_UNLOCK(td->td_proc);
1241 if (coresize >= limit) {
1250 hdr =
malloc(hdrsize, M_TEMP, M_WAITOK);
1256 ¬elst, notesz, gzfile);
1264 php = (Elf_Phdr *)((
char *)hdr +
sizeof(Elf_Ehdr)) + 1;
1265 offset = round_page(hdrsize + notesz);
1266 for (i = 0; i < seginfo.
count; i++) {
1267 error =
core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
1268 php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
1271 offset += php->p_filesz;
1277 "Failed to write core file for process %s (error %d)\n",
1278 curproc->p_comm, error);
1282 #ifdef COMPRESS_USER_CORES
1284 free(core_buf, M_TEMP);
1288 while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) {
1289 TAILQ_REMOVE(¬elst, ninfo, link);
1290 free(ninfo, M_TEMP);
1304 vm_map_entry_t entry;
1312 phdr->p_type = PT_LOAD;
1313 phdr->p_offset = phc->
offset;
1314 phdr->p_vaddr = entry->start;
1316 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1317 phdr->p_align = PAGE_SIZE;
1320 phc->
offset += phdr->p_filesz;
1330 vm_map_entry_t entry;
1336 ssc->
size += entry->end - entry->start;
1347 segment_callback func;
1350 struct proc *p = td->td_proc;
1351 vm_map_t map = &p->p_vmspace->vm_map;
1352 vm_map_entry_t entry;
1353 vm_object_t backing_object, object;
1354 boolean_t ignore_entry;
1356 vm_map_lock_read(map);
1357 for (entry = map->header.next; entry != &map->header;
1358 entry = entry->next) {
1367 if (elf_legacy_coredump) {
1368 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
1371 if ((entry->protection & VM_PROT_ALL) == 0)
1381 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1384 if ((
object = entry->object.vm_object) == NULL)
1388 VM_OBJECT_LOCK(
object);
1389 while ((backing_object = object->backing_object) != NULL) {
1390 VM_OBJECT_LOCK(backing_object);
1391 VM_OBJECT_UNLOCK(
object);
1392 object = backing_object;
1394 ignore_entry =
object->type != OBJT_DEFAULT &&
1395 object->type != OBJT_SWAP &&
object->type != OBJT_VNODE &&
1396 object->type != OBJT_PHYS;
1397 VM_OBJECT_UNLOCK(
object);
1401 (*func)(entry, closure);
1403 vm_map_unlock_read(map);
1412 int numsegs,
void *hdr,
size_t hdrsize,
struct note_info_list *notelst,
1413 size_t notesz, gzFile gzfile)
1416 struct note_info *ninfo;
1421 bzero(hdr, hdrsize);
1429 #ifdef COMPRESS_USER_CORES
1430 params.gzfile = gzfile;
1436 TAILQ_FOREACH(ninfo, notelst, link)
1466 while (thr != NULL) {
1476 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1477 TAILQ_NEXT(thr, td_plist);
1479 thr = TAILQ_NEXT(thr, td_plist);
1512 ehdr = (Elf_Ehdr *)hdr;
1513 phdr = (Elf_Phdr *)((
char *)hdr +
sizeof(Elf_Ehdr));
1515 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1516 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1517 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1518 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1519 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1520 ehdr->e_ident[EI_DATA] = ELF_DATA;
1521 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1522 ehdr->e_ident[EI_OSABI] = ELFOSABI_BSDSUNIX;
1523 ehdr->e_ident[EI_ABIVERSION] = 0;
1524 ehdr->e_ident[EI_PAD] = 0;
1525 ehdr->e_type = ET_CORE;
1526 #if defined(COMPAT_32BIT) && __ELF_WORD_SIZE == 32
1527 ehdr->e_machine = ELF_ARCH32;
1529 ehdr->e_machine = ELF_ARCH;
1531 ehdr->e_version = EV_CURRENT;
1533 ehdr->e_phoff =
sizeof(Elf_Ehdr);
1535 ehdr->e_ehsize =
sizeof(Elf_Ehdr);
1536 ehdr->e_phentsize =
sizeof(Elf_Phdr);
1537 ehdr->e_phnum = numsegs + 1;
1538 ehdr->e_shentsize =
sizeof(Elf_Shdr);
1540 ehdr->e_shstrndx = SHN_UNDEF;
1547 phdr->p_type = PT_NOTE;
1548 phdr->p_offset = hdrsize;
1551 phdr->p_filesz = notesz;
1553 phdr->p_flags = PF_R;
1559 phc.
offset = round_page(hdrsize + notesz);
1566 struct note_info *ninfo;
1567 size_t size, notesize;
1570 out(arg, NULL, &size);
1571 ninfo =
malloc(
sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1576 TAILQ_INSERT_TAIL(list, ninfo, link);
1581 notesize =
sizeof(Elf_Note) +
1594 if (ninfo->
type == -1) {
1600 note.n_descsz = ninfo->
outsize;
1601 note.n_type = ninfo->
type;
1605 sbuf_bcat(sb,
"BSDSUniX", note.n_namesz);
1607 if (note.n_descsz == 0)
1618 #if defined(COMPAT_32BIT) && __ELF_WORD_SIZE == 32
1619 #include <compat/compat32bit/compat32bit.h>
1627 #define ELF_KERN_PROC_MASK KERN_PROC_MASK32
1637 #define ELF_KERN_PROC_MASK 0
1646 elf_prpsinfo_t *psinfo;
1648 p = (
struct proc *)arg;
1650 KASSERT(*sizep ==
sizeof(*psinfo), (
"invalid size"));
1651 psinfo =
malloc(
sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
1652 psinfo->pr_version = PRPSINFO_VERSION;
1654 strlcpy(psinfo->pr_fname, p->p_comm,
sizeof(psinfo->pr_fname));
1659 strlcpy(psinfo->pr_psargs, p->p_comm,
1660 sizeof(psinfo->pr_psargs));
1663 free(psinfo, M_TEMP);
1665 *sizep =
sizeof(*psinfo);
1672 elf_prstatus_t *status;
1674 td = (
struct thread *)arg;
1676 KASSERT(*sizep ==
sizeof(*status), (
"invalid size"));
1677 status =
malloc(
sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
1678 status->pr_version = PRSTATUS_VERSION;
1682 status->pr_osreldate = osreldate;
1683 status->pr_cursig = td->td_proc->p_sig;
1684 status->pr_pid = td->td_tid;
1685 #if defined(COMPAT_32BIT) && __ELF_WORD_SIZE == 32
1686 fill_regs32(td, &status->pr_reg);
1688 fill_regs(td, &status->pr_reg);
1691 free(status, M_TEMP);
1693 *sizep =
sizeof(*status);
1700 elf_prfpregset_t *fpregset;
1702 td = (
struct thread *)arg;
1704 KASSERT(*sizep ==
sizeof(*fpregset), (
"invalid size"));
1705 fpregset =
malloc(
sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1706 #if defined(COMPAT_32BIT) && __ELF_WORD_SIZE == 32
1707 fill_fpregs32(td, fpregset);
1709 fill_fpregs(td, fpregset);
1711 sbuf_bcat(sb, fpregset,
sizeof(*fpregset));
1712 free(fpregset, M_TEMP);
1714 *sizep =
sizeof(*fpregset);
1721 elf_thrmisc_t thrmisc;
1723 td = (
struct thread *)arg;
1725 KASSERT(*sizep ==
sizeof(thrmisc), (
"invalid size"));
1726 bzero(&thrmisc._pad,
sizeof(thrmisc._pad));
1727 strcpy(thrmisc.pr_tname, td->td_name);
1728 sbuf_bcat(sb, &thrmisc,
sizeof(thrmisc));
1730 *sizep =
sizeof(thrmisc);
1744 td = (
struct thread *)arg;
1746 if (size != 0 && sb != NULL)
1747 buf =
malloc(size, M_TEMP, M_ZERO | M_WAITOK);
1752 KASSERT(*sizep == size, (
"invalid size"));
1753 if (size != 0 && sb != NULL)
1759 #ifdef KINFO_PROC_SIZE
1760 CTASSERT(
sizeof(
struct kinfo_proc) == KINFO_PROC_SIZE);
1770 p = (
struct proc *)arg;
1771 size =
sizeof(structsize) + p->p_numthreads *
1775 KASSERT(*sizep == size, (
"invalid size"));
1777 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1784 #ifdef KINFO_FILE_SIZE
1785 CTASSERT(
sizeof(
struct kinfo_file) == KINFO_FILE_SIZE);
1795 p = (
struct proc *)arg;
1798 sb =
sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1800 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1807 structsize =
sizeof(
struct kinfo_file);
1808 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1814 #ifdef KINFO_VMENTRY_SIZE
1815 CTASSERT(
sizeof(
struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1825 p = (
struct proc *)arg;
1828 sb =
sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1830 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1837 structsize =
sizeof(
struct kinfo_vmentry);
1838 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1851 p = (
struct proc *)arg;
1852 size =
sizeof(structsize) + p->p_ucred->cr_ngroups *
sizeof(gid_t);
1854 KASSERT(*sizep == size, (
"invalid size"));
1855 structsize =
sizeof(gid_t);
1856 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1857 sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
1870 p = (
struct proc *)arg;
1871 size =
sizeof(structsize) +
sizeof(p->p_fd->fd_cmask);
1873 KASSERT(*sizep == size, (
"invalid size"));
1874 structsize =
sizeof(p->p_fd->fd_cmask);
1875 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1876 sbuf_bcat(sb, &p->p_fd->fd_cmask,
sizeof(p->p_fd->fd_cmask));
1885 struct rlimit rlim[RLIM_NLIMITS];
1889 p = (
struct proc *)arg;
1890 size =
sizeof(structsize) +
sizeof(rlim);
1892 KASSERT(*sizep == size, (
"invalid size"));
1893 structsize =
sizeof(rlim);
1894 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1896 for (i = 0; i < RLIM_NLIMITS; i++)
1911 p = (
struct proc *)arg;
1912 size =
sizeof(structsize) +
sizeof(p->p_osrel);
1914 KASSERT(*sizep == size, (
"invalid size"));
1915 structsize =
sizeof(p->p_osrel);
1916 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1917 sbuf_bcat(sb, &p->p_osrel,
sizeof(p->p_osrel));
1926 elf_ps_strings_t ps_strings;
1930 p = (
struct proc *)arg;
1931 size =
sizeof(structsize) +
sizeof(ps_strings);
1933 KASSERT(*sizep == size, (
"invalid size"));
1934 structsize =
sizeof(ps_strings);
1935 #if defined(COMPAT_32BIT) && __ELF_WORD_SIZE == 32
1936 ps_strings = PTROUT(p->p_sysent->sv_psstrings);
1938 ps_strings = p->p_sysent->sv_psstrings;
1940 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1941 sbuf_bcat(sb, &ps_strings,
sizeof(ps_strings));
1953 p = (
struct proc *)arg;
1956 sb =
sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1958 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1966 structsize =
sizeof(Elf_Auxinfo);
1967 sbuf_bcat(sb, &structsize,
sizeof(structsize));
1976 int32_t *osrel,
const Elf_Phdr *pnote)
1978 const Elf_Note *note, *note0, *note_end;
1979 const char *note_name;
1982 if (pnote == NULL || pnote->p_offset > PAGE_SIZE ||
1983 pnote->p_filesz > PAGE_SIZE - pnote->p_offset)
1986 note = note0 = (
const Elf_Note *)(imgp->image_header + pnote->p_offset);
1987 note_end = (
const Elf_Note *)(imgp->image_header +
1988 pnote->p_offset + pnote->p_filesz);
1989 for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
1990 if (!
aligned(note, Elf32_Addr) || (
const char *)note_end -
1991 (
const char *)note <
sizeof(Elf_Note))
1993 if (note->n_namesz != checknote->hdr.n_namesz ||
1994 note->n_descsz != checknote->hdr.n_descsz ||
1995 note->n_type != checknote->hdr.n_type)
1997 note_name = (
const char *)(note + 1);
1998 if (note_name + checknote->hdr.n_namesz >=
1999 (
const char *)note_end || strncmp(checknote->vendor,
2000 note_name, checknote->hdr.n_namesz) != 0)
2007 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
2008 checknote->trans_osrel != NULL)
2009 return (checknote->trans_osrel(note, osrel));
2013 note = (
const Elf_Note *)((
const char *)(note + 1) +
2030 const Elf_Phdr *phdr;
2031 const Elf_Ehdr *hdr;
2034 hdr = (
const Elf_Ehdr *)imgp->image_header;
2035 phdr = (
const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
2037 for (i = 0; i < hdr->e_phnum; i++) {
2038 if (phdr[i].p_type == PT_NOTE &&
2055 #ifdef COMPRESS_USER_CORES
2070 compress_core (gzFile file,
char *inbuf,
char *dest_buf,
unsigned int len,
2075 unsigned int chunk_len;
2078 if (inbuf != NULL) {
2080 copyin(inbuf, dest_buf, chunk_len);
2085 len_compressed =
gzwrite(file, dest_buf, chunk_len);
2087 EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
2089 if ((
unsigned int)len_compressed != chunk_len) {
2091 "compress_core: length mismatch (0x%x returned, "
2092 "0x%x expected)\n", len_compressed, chunk_len);
2093 EVENTHANDLER_INVOKE(app_coredump_error, td,
2094 "compress_core: length mismatch %x -> %x",
2095 chunk_len, len_compressed);
2114 prot |= VM_PROT_EXECUTE;
2116 prot |= VM_PROT_WRITE;
2118 prot |= VM_PROT_READ;
2119 #if __ELF_WORD_SIZE == 32
2120 #if defined(__amd64__) || defined(__ia64__)
2121 if (i386_read_exec && (flags & PF_R))
2122 prot |= VM_PROT_EXECUTE;
2134 if (prot & VM_PROT_EXECUTE)
2136 if (prot & VM_PROT_READ)
2138 if (prot & VM_PROT_WRITE)
rlim_t lim_max(struct proc *p, int which)
static boolean_t __elfN() bsdsunix_trans_osrel(const Elf_Note *note, int32_t *osrel)
prfpregset_t elf_fpregset_t
static int __CONCAT(exec_, __elfN(imgact))
static void note_procstat_umask(void *, struct sbuf *, size_t *)
int __elfN() coredump(struct thread *td, struct vnode *vp, off_t limit, int flags)
int exec_check_permissions(struct image_params *imgp)
static int core_output(struct vnode *vp, void *base, size_t len, off_t offset, struct ucred *active_cred, struct ucred *file_cred, struct thread *td, char *core_buf, gzFile gzfile)
EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw))
rlim_t lim_cur(struct proc *p, int which)
int racct_add(struct proc *p, int resource, uint64_t amount)
static int __elfN() corehdr(struct thread *, struct vnode *, struct ucred *, int, void *, size_t, struct note_info_list *, size_t, gzFile)
int vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, void *base, size_t len, off_t offset, enum uio_seg segflg, int ioflg, struct ucred *active_cred, struct ucred *file_cred, size_t *aresid, struct thread *td)
TUNABLE_INT("kern.eventtimer.singlemul",&singlemul)
void NDFREE(struct nameidata *ndp, const u_int flags)
static void __elfN() putnote(struct note_info *, struct sbuf *)
int snprintf(char *str, size_t size, const char *format,...)
static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL,"cpufreq debugging")
int racct_set(struct proc *p, int resource, uint64_t amount)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
int __elfN() remove_brand_entry(Elf_Brandinfo *entry)
CTASSERT(MAXSHELLCMDLEN >=MAXINTERP+3)
void panic(const char *fmt,...)
void exec_unmap_first_page(struct image_params *imgp)
static void __elfN() note_procstat_auxv(void *, struct sbuf *, size_t *)
TAILQ_HEAD(note_info_list, note_info)
static void __elfN() prepare_notes(struct thread *, struct note_info_list *, size_t *)
static int __elfN() map_partial(vm_map_t map, vm_object_t object, vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot)
static int GNU_KBSDSUNIX_ABI_DESC
SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD,&boothowto, 0,"Boot control flags, passed from loader")
void(* outfunc_t)(void *, struct sbuf *, size_t *)
int uprintf(const char *fmt,...)
static int __elfN() check_header(const Elf_Ehdr *hdr)
vm_offset_t elf_ps_strings_t
static boolean_t kbsdsunix_trans_osrel(const Elf_Note *note, int32_t *osrel)
static void __elfN() puthdr(struct thread *, void *, size_t, int, size_t)
static void note_procstat_files(void *, struct sbuf *, size_t *)
struct ucred * active_cred
static Elf_Word __elfN() untrans_prot(vm_prot_t prot)
ssize_t sbuf_end_section(struct sbuf *s, ssize_t old_len, size_t pad, int c)
static Elf_Brandinfo *__elfN() get_brandinfo(struct image_params *imgp, const char *interp, int interp_name_len, int32_t *osrel)
void vput(struct vnode *vp)
static boolean_t __elfN() parse_notes(struct image_params *imgp, Elf_Brandnote *checknote, int32_t *osrel, const Elf_Phdr *pnote)
int ZEXPORT gzwrite(gzFile file, const voidp buf, unsigned len)
static boolean_t __elfN() check_note(struct image_params *imgp, Elf_Brandnote *checknote, int32_t *osrel)
int kern_proc_filedesc_out(struct proc *p, struct sbuf *sb, ssize_t maxlen)
static void __elfN() note_procstat_proc(void *, struct sbuf *, size_t *)
gzFile gz_open(char *path, const char *mode, struct vnode *vp) const
static void note_procstat_vmmap(void *, struct sbuf *, size_t *)
int exec_new_vmspace(struct image_params *imgp, struct sysentvec *sv)
void sbuf_set_drain(struct sbuf *s, sbuf_drain_func *func, void *ctx)
static size_t register_note(struct note_info_list *, int, outfunc_t, void *)
int kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
int namei(struct nameidata *ndp)
int __elfN() insert_brand_entry(Elf_Brandinfo *entry)
static const struct execsw ** execsw
static void each_writable_segment(struct thread *, segment_callback, void *)
static void cb_put_phdr(vm_map_entry_t, void *)
static int __elfN() load_section(struct vmspace *vmspace, vm_object_t object, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, size_t pagesize)
prfpregset_t elf_prfpregset_t
static void __elfN() note_threadmd(void *, struct sbuf *, size_t *)
#define ELF_NOTE_ROUNDSIZE
int __elfN() brand_inuse(Elf_Brandinfo *entry)
void log(int level, const char *fmt,...)
void lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
struct sbuf * sbuf_new(struct sbuf *s, char *buf, int length, int flags)
Elf_Brandnote __elfN(kbsdsunix_brandnote)
#define ELF_KERN_PROC_MASK
static void __elfN() note_prstatus(void *, struct sbuf *, size_t *)
int kern_proc_vmmap_out(struct proc *p, struct sbuf *sb)
static void __elfN() note_procstat_psstrings(void *, struct sbuf *, size_t *)
static void note_procstat_osrel(void *, struct sbuf *, size_t *)
void free(void *addr, struct malloc_type *mtp)
int printf(const char *fmt,...)
static void __elfN() note_fpregset(void *, struct sbuf *, size_t *)
static int sbuf_drain_core_output(void *, const char *, int)
void sbuf_delete(struct sbuf *s)
static void note_procstat_groups(void *, struct sbuf *, size_t *)
static void cb_size_segment(vm_map_entry_t, void *)
static int __elfN() map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
struct kinfo_proc elf_kinfo_proc_t
void sbuf_start_section(struct sbuf *s, ssize_t *old_lenp)
prpsinfo_t elf_prpsinfo_t
int __elfN() bsdsunix_fixup(register_t **stack_base, struct image_params *imgp)
int sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
int sbuf_finish(struct sbuf *s)
int ZEXPORT gzclose(gzFile file)
prstatus_t elf_prstatus_t
static vm_prot_t __elfN() trans_prot(Elf_Word flags)
static int __elfN() load_file(struct proc *p, const char *file, u_long *addr, u_long *entry, size_t pagesize)
static void __elfN() note_thrmisc(void *, struct sbuf *, size_t *)
static void note_procstat_rlimit(void *, struct sbuf *, size_t *)
#define trunc_page_ps(va, ps)
#define round_page_ps(va, ps)
static const char GNU_ABI_VENDOR[]
static int sbuf_drain_count(void *arg, const char *data, int len)
int proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
static void __elfN() note_prpsinfo(void *, struct sbuf *, size_t *)
void(* segment_callback)(vm_map_entry_t, void *)
int exec_map_first_page(struct image_params *imgp)