1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2017 Dell EMC
5 * Copyright (c) 2000-2001, 2003 David O'Brien
6 * Copyright (c) 1995-1996 Søren Schmidt
7 * Copyright (c) 1996 Peter Wemm
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer
15 * in this position and unchanged.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "opt_capsicum.h"
35
36 #include <sys/param.h>
37 #include <sys/capsicum.h>
38 #include <sys/compressor.h>
39 #include <sys/exec.h>
40 #include <sys/fcntl.h>
41 #include <sys/imgact.h>
42 #include <sys/imgact_elf.h>
43 #include <sys/jail.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mman.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/procfs.h>
52 #include <sys/ptrace.h>
53 #include <sys/racct.h>
54 #include <sys/reg.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/ucoredump.h>
68 #include <sys/vnode.h>
69 #include <sys/syslog.h>
70 #include <sys/eventhandler.h>
71 #include <sys/user.h>
72
73 #include <vm/vm.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_param.h>
76 #include <vm/pmap.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_extern.h>
80
81 #include <machine/elf.h>
82 #include <machine/md_var.h>
83
84 #define ELF_NOTE_ROUNDSIZE 4
85 #define OLD_EI_BRAND 8
86
87 /*
88 * ELF_ABI_NAME is a string name of the ELF ABI. ELF_ABI_ID is used
89 * to build variable names.
90 */
91 #define ELF_ABI_NAME __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
92 #define ELF_ABI_ID __CONCAT(elf, __ELF_WORD_SIZE)
93
94 static int __elfN(check_header)(const Elf_Ehdr *hdr);
95 static const Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
96 const Elf_Phdr *phdr, const char *interp, int32_t *osrel, uint32_t *fctl0);
97 static int __elfN(load_interp_file)(struct thread *td, const char *file, u_long *addr,
98 u_long *entry);
99 static int __elfN(load_section)(const struct image_params *imgp,
100 vm_ooffset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
101 vm_prot_t prot);
102 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
103 static bool __elfN(freebsd_trans_osrel)(const Elf_Note *note,
104 int32_t *osrel);
105 static bool kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
106 static bool __elfN(check_note)(struct image_params *imgp, const Elf_Phdr *phdr,
107 const Elf_Brandnote *checknote, int32_t *osrel, bool *has_fctl0,
108 uint32_t *fctl0);
109 static vm_prot_t __elfN(trans_prot)(Elf_Word);
110 static Elf_Word __elfN(untrans_prot)(vm_prot_t);
111 static size_t __elfN(prepare_register_notes)(struct thread *td,
112 struct note_info_list *list, struct thread *target_td);
113
114 SYSCTL_NODE(_kern, OID_AUTO, ELF_ABI_ID, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
115 "");
116
117 #define ELF_NODE_OID __CONCAT(_kern_, ELF_ABI_ID)
118
119 int __elfN(fallback_brand) = -1;
120 SYSCTL_INT(ELF_NODE_OID, OID_AUTO,
121 fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0,
122 ELF_ABI_NAME " brand of last resort");
123
124 static int elf_legacy_coredump = 0;
125 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
126 &elf_legacy_coredump, 0,
127 "include all and only RW pages in core dumps");
128
129 int __elfN(nxstack) =
130 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \
131 defined(__arm__) || defined(__aarch64__) || \
132 defined(__riscv)
133 1;
134 #else
135 0;
136 #endif
137 SYSCTL_INT(ELF_NODE_OID, OID_AUTO,
138 nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
139 ELF_ABI_NAME ": support PT_GNU_STACK for non-executable stack control");
140
141 #if defined(__amd64__)
142 static int __elfN(vdso) = 1;
143 SYSCTL_INT(ELF_NODE_OID, OID_AUTO,
144 vdso, CTLFLAG_RWTUN, &__elfN(vdso), 0,
145 ELF_ABI_NAME ": enable vdso preloading");
146 #else
147 static int __elfN(vdso) = 0;
148 #endif
149
150 #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__))
151 int i386_read_exec = 0;
152 SYSCTL_INT(ELF_NODE_OID, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
153 "enable execution from readable segments");
154 #endif
155
156 static u_long __elfN(pie_base) = ET_DYN_LOAD_ADDR;
157 static int
sysctl_pie_base(SYSCTL_HANDLER_ARGS)158 sysctl_pie_base(SYSCTL_HANDLER_ARGS)
159 {
160 u_long val;
161 int error;
162
163 val = __elfN(pie_base);
164 error = sysctl_handle_long(oidp, &val, 0, req);
165 if (error != 0 || req->newptr == NULL)
166 return (error);
167 if ((val & PAGE_MASK) != 0)
168 return (EINVAL);
169 __elfN(pie_base) = val;
170 return (0);
171 }
172 SYSCTL_PROC(ELF_NODE_OID, OID_AUTO, pie_base,
173 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
174 sysctl_pie_base, "LU",
175 "PIE load base without randomization");
176
177 SYSCTL_NODE(ELF_NODE_OID, OID_AUTO, aslr,
178 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
179 "");
180 #define ASLR_NODE_OID __CONCAT(ELF_NODE_OID, _aslr)
181
182 /*
183 * Enable ASLR by default for 64-bit non-PIE binaries. 32-bit architectures
184 * have limited address space (which can cause issues for applications with
185 * high memory use) so we leave it off there.
186 */
187 static int __elfN(aslr_enabled) = __ELF_WORD_SIZE == 64;
188 SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, enable, CTLFLAG_RWTUN,
189 &__elfN(aslr_enabled), 0,
190 ELF_ABI_NAME ": enable address map randomization");
191
192 /*
193 * Enable ASLR by default for 64-bit PIE binaries.
194 */
195 static int __elfN(pie_aslr_enabled) = __ELF_WORD_SIZE == 64;
196 SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, pie_enable, CTLFLAG_RWTUN,
197 &__elfN(pie_aslr_enabled), 0,
198 ELF_ABI_NAME ": enable address map randomization for PIE binaries");
199
200 /*
201 * Sbrk is deprecated and it can be assumed that in most cases it will not be
202 * used anyway. This setting is valid only with ASLR enabled, and allows ASLR
203 * to use the bss grow region.
204 */
205 static int __elfN(aslr_honor_sbrk) = 0;
206 SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, honor_sbrk, CTLFLAG_RW,
207 &__elfN(aslr_honor_sbrk), 0,
208 ELF_ABI_NAME ": assume sbrk is used");
209
210 static int __elfN(aslr_stack) = __ELF_WORD_SIZE == 64;
211 SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, stack, CTLFLAG_RWTUN,
212 &__elfN(aslr_stack), 0,
213 ELF_ABI_NAME ": enable stack address randomization");
214
215 static int __elfN(aslr_shared_page) = __ELF_WORD_SIZE == 64;
216 SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, shared_page, CTLFLAG_RWTUN,
217 &__elfN(aslr_shared_page), 0,
218 ELF_ABI_NAME ": enable shared page address randomization");
219
220 static int __elfN(sigfastblock) = 1;
221 SYSCTL_INT(ELF_NODE_OID, OID_AUTO, sigfastblock,
222 CTLFLAG_RWTUN, &__elfN(sigfastblock), 0,
223 "enable sigfastblock for new processes");
224
225 static bool __elfN(allow_wx) = true;
226 SYSCTL_BOOL(ELF_NODE_OID, OID_AUTO, allow_wx,
227 CTLFLAG_RWTUN, &__elfN(allow_wx), 0,
228 "Allow pages to be mapped simultaneously writable and executable");
229
230 static u_int __elfN(phnums) = 128;
231 SYSCTL_UINT(ELF_NODE_OID, OID_AUTO, phnums,
232 CTLFLAG_RWTUN, &__elfN(phnums), 0,
233 "Max number of program headers to accept");
234
235 static const Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
236
237 #define aligned(a, t) (rounddown2((u_long)(a), sizeof(t)) == (u_long)(a))
238
239 const Elf_Brandnote __elfN(freebsd_brandnote) = {
240 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
241 .hdr.n_descsz = sizeof(int32_t),
242 .hdr.n_type = NT_FREEBSD_ABI_TAG,
243 .vendor = FREEBSD_ABI_VENDOR,
244 .flags = BN_TRANSLATE_OSREL,
245 .trans_osrel = __elfN(freebsd_trans_osrel)
246 };
247
248 static bool
__elfN(freebsd_trans_osrel)249 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
250 {
251 uintptr_t p;
252
253 p = (uintptr_t)(note + 1);
254 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
255 *osrel = *(const int32_t *)(p);
256
257 return (true);
258 }
259
260 static int GNU_KFREEBSD_ABI_DESC = 3;
261
262 const Elf_Brandnote __elfN(kfreebsd_brandnote) = {
263 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
264 .hdr.n_descsz = 16, /* XXX at least 16 */
265 .hdr.n_type = 1,
266 .vendor = GNU_ABI_VENDOR,
267 .flags = BN_TRANSLATE_OSREL,
268 .trans_osrel = kfreebsd_trans_osrel
269 };
270
271 static bool
kfreebsd_trans_osrel(const Elf_Note * note,int32_t * osrel)272 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
273 {
274 const Elf32_Word *desc;
275 uintptr_t p;
276
277 p = (uintptr_t)(note + 1);
278 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
279
280 desc = (const Elf32_Word *)p;
281 if (desc[0] != GNU_KFREEBSD_ABI_DESC)
282 return (false);
283
284 /*
285 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
286 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
287 */
288 *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
289
290 return (true);
291 }
292
293 int
__elfN(insert_brand_entry)294 __elfN(insert_brand_entry)(const Elf_Brandinfo *entry)
295 {
296 int i;
297
298 for (i = 0; i < MAX_BRANDS; i++) {
299 if (elf_brand_list[i] == NULL) {
300 elf_brand_list[i] = entry;
301 break;
302 }
303 }
304 if (i == MAX_BRANDS) {
305 printf("WARNING: %s: could not insert brandinfo entry: %p\n",
306 __func__, entry);
307 return (-1);
308 }
309 return (0);
310 }
311
312 int
__elfN(remove_brand_entry)313 __elfN(remove_brand_entry)(const Elf_Brandinfo *entry)
314 {
315 int i;
316
317 for (i = 0; i < MAX_BRANDS; i++) {
318 if (elf_brand_list[i] == entry) {
319 elf_brand_list[i] = NULL;
320 break;
321 }
322 }
323 if (i == MAX_BRANDS)
324 return (-1);
325 return (0);
326 }
327
328 bool
__elfN(brand_inuse)329 __elfN(brand_inuse)(const Elf_Brandinfo *entry)
330 {
331 struct proc *p;
332 bool rval = false;
333
334 sx_slock(&allproc_lock);
335 FOREACH_PROC_IN_SYSTEM(p) {
336 if (p->p_sysent == entry->sysvec) {
337 rval = true;
338 break;
339 }
340 }
341 sx_sunlock(&allproc_lock);
342
343 return (rval);
344 }
345
346 static const Elf_Brandinfo *
__elfN(get_brandinfo)347 __elfN(get_brandinfo)(struct image_params *imgp, const Elf_Phdr *phdr,
348 const char *interp, int32_t *osrel, uint32_t *fctl0)
349 {
350 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
351 const Elf_Brandinfo *bi, *bi_m;
352 bool ret, has_fctl0;
353 int i, interp_name_len;
354
355 interp_name_len = interp != NULL ? strlen(interp) + 1 : 0;
356
357 /*
358 * We support four types of branding -- (1) the ELF EI_OSABI field
359 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
360 * branding w/in the ELF header, (3) path of the `interp_path'
361 * field, and (4) the ".note.ABI-tag" ELF section.
362 */
363
364 /* Look for an ".note.ABI-tag" ELF section */
365 bi_m = NULL;
366 for (i = 0; i < MAX_BRANDS; i++) {
367 bi = elf_brand_list[i];
368 if (bi == NULL)
369 continue;
370 if (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)
371 continue;
372 if (hdr->e_machine == bi->machine && (bi->flags &
373 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
374 has_fctl0 = false;
375 *fctl0 = 0;
376 *osrel = 0;
377 ret = __elfN(check_note)(imgp, phdr, bi->brand_note,
378 osrel, &has_fctl0, fctl0);
379 /* Give brand a chance to veto check_note's guess */
380 if (ret && bi->header_supported) {
381 ret = bi->header_supported(imgp, osrel,
382 has_fctl0 ? fctl0 : NULL);
383 }
384 /*
385 * If note checker claimed the binary, but the
386 * interpreter path in the image does not
387 * match default one for the brand, try to
388 * search for other brands with the same
389 * interpreter. Either there is better brand
390 * with the right interpreter, or, failing
391 * this, we return first brand which accepted
392 * our note and, optionally, header.
393 */
394 if (ret && bi_m == NULL && interp != NULL &&
395 (bi->interp_path == NULL ||
396 (strlen(bi->interp_path) + 1 != interp_name_len ||
397 strncmp(interp, bi->interp_path, interp_name_len)
398 != 0))) {
399 bi_m = bi;
400 ret = 0;
401 }
402 if (ret)
403 return (bi);
404 }
405 }
406 if (bi_m != NULL)
407 return (bi_m);
408
409 /* If the executable has a brand, search for it in the brand list. */
410 for (i = 0; i < MAX_BRANDS; i++) {
411 bi = elf_brand_list[i];
412 if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
413 (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
414 continue;
415 if (hdr->e_machine == bi->machine &&
416 (hdr->e_ident[EI_OSABI] == bi->brand ||
417 (bi->compat_3_brand != NULL &&
418 strcmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
419 bi->compat_3_brand) == 0))) {
420 /* Looks good, but give brand a chance to veto */
421 if (bi->header_supported == NULL ||
422 bi->header_supported(imgp, NULL, NULL)) {
423 /*
424 * Again, prefer strictly matching
425 * interpreter path.
426 */
427 if (interp_name_len == 0 &&
428 bi->interp_path == NULL)
429 return (bi);
430 if (bi->interp_path != NULL &&
431 strlen(bi->interp_path) + 1 ==
432 interp_name_len && strncmp(interp,
433 bi->interp_path, interp_name_len) == 0)
434 return (bi);
435 if (bi_m == NULL)
436 bi_m = bi;
437 }
438 }
439 }
440 if (bi_m != NULL)
441 return (bi_m);
442
443 /* No known brand, see if the header is recognized by any brand */
444 for (i = 0; i < MAX_BRANDS; i++) {
445 bi = elf_brand_list[i];
446 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY ||
447 bi->header_supported == NULL)
448 continue;
449 if (hdr->e_machine == bi->machine) {
450 ret = bi->header_supported(imgp, NULL, NULL);
451 if (ret)
452 return (bi);
453 }
454 }
455
456 /* Lacking a known brand, search for a recognized interpreter. */
457 if (interp != NULL) {
458 for (i = 0; i < MAX_BRANDS; i++) {
459 bi = elf_brand_list[i];
460 if (bi == NULL || (bi->flags &
461 (BI_BRAND_NOTE_MANDATORY | BI_BRAND_ONLY_STATIC))
462 != 0)
463 continue;
464 if (hdr->e_machine == bi->machine &&
465 bi->interp_path != NULL &&
466 /* ELF image p_filesz includes terminating zero */
467 strlen(bi->interp_path) + 1 == interp_name_len &&
468 strncmp(interp, bi->interp_path, interp_name_len)
469 == 0 && (bi->header_supported == NULL ||
470 bi->header_supported(imgp, NULL, NULL)))
471 return (bi);
472 }
473 }
474
475 /* Lacking a recognized interpreter, try the default brand */
476 for (i = 0; i < MAX_BRANDS; i++) {
477 bi = elf_brand_list[i];
478 if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
479 (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
480 continue;
481 if (hdr->e_machine == bi->machine &&
482 __elfN(fallback_brand) == bi->brand &&
483 (bi->header_supported == NULL ||
484 bi->header_supported(imgp, NULL, NULL)))
485 return (bi);
486 }
487 return (NULL);
488 }
489
490 static bool
__elfN(phdr_in_zero_page)491 __elfN(phdr_in_zero_page)(const Elf_Ehdr *hdr)
492 {
493 return (hdr->e_phoff <= PAGE_SIZE &&
494 (u_int)hdr->e_phentsize * hdr->e_phnum <= PAGE_SIZE - hdr->e_phoff);
495 }
496
497 static int
__elfN(check_header)498 __elfN(check_header)(const Elf_Ehdr *hdr)
499 {
500 const Elf_Brandinfo *bi;
501 int i;
502
503 if (!IS_ELF(*hdr) ||
504 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
505 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
506 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
507 hdr->e_phentsize != sizeof(Elf_Phdr) ||
508 hdr->e_version != ELF_TARG_VER)
509 return (ENOEXEC);
510
511 /*
512 * Make sure we have at least one brand for this machine.
513 */
514
515 for (i = 0; i < MAX_BRANDS; i++) {
516 bi = elf_brand_list[i];
517 if (bi != NULL && bi->machine == hdr->e_machine)
518 break;
519 }
520 if (i == MAX_BRANDS)
521 return (ENOEXEC);
522
523 return (0);
524 }
525
526 static int
__elfN(map_partial)527 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
528 vm_offset_t start, vm_offset_t end, vm_prot_t prot)
529 {
530 struct sf_buf *sf;
531 int error;
532 vm_offset_t off;
533
534 /*
535 * Create the page if it doesn't exist yet. Ignore errors.
536 */
537 vm_map_fixed(map, NULL, 0, trunc_page(start), round_page(end) -
538 trunc_page(start), VM_PROT_ALL, VM_PROT_ALL, MAP_CHECK_EXCL);
539
540 /*
541 * Find the page from the underlying object.
542 */
543 if (object != NULL) {
544 sf = vm_imgact_map_page(object, offset);
545 if (sf == NULL)
546 return (KERN_FAILURE);
547 off = offset - trunc_page(offset);
548 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
549 end - start);
550 vm_imgact_unmap_page(sf);
551 if (error != 0)
552 return (KERN_FAILURE);
553 }
554
555 return (KERN_SUCCESS);
556 }
557
558 static int
__elfN(map_insert)559 __elfN(map_insert)(const struct image_params *imgp, vm_map_t map,
560 vm_object_t object, vm_ooffset_t offset, vm_offset_t start, vm_offset_t end,
561 vm_prot_t prot, int cow)
562 {
563 struct sf_buf *sf;
564 vm_offset_t off;
565 vm_size_t sz;
566 int error, locked, rv;
567
568 if (start != trunc_page(start)) {
569 rv = __elfN(map_partial)(map, object, offset, start,
570 round_page(start), prot);
571 if (rv != KERN_SUCCESS)
572 return (rv);
573 offset += round_page(start) - start;
574 start = round_page(start);
575 }
576 if (end != round_page(end)) {
577 rv = __elfN(map_partial)(map, object, offset +
578 trunc_page(end) - start, trunc_page(end), end, prot);
579 if (rv != KERN_SUCCESS)
580 return (rv);
581 end = trunc_page(end);
582 }
583 if (start >= end)
584 return (KERN_SUCCESS);
585 if ((offset & PAGE_MASK) != 0) {
586 /*
587 * The mapping is not page aligned. This means that we have
588 * to copy the data.
589 */
590 rv = vm_map_fixed(map, NULL, 0, start, end - start,
591 prot | VM_PROT_WRITE, VM_PROT_ALL, MAP_CHECK_EXCL);
592 if (rv != KERN_SUCCESS)
593 return (rv);
594 if (object == NULL)
595 return (KERN_SUCCESS);
596 for (; start < end; start += sz) {
597 sf = vm_imgact_map_page(object, offset);
598 if (sf == NULL)
599 return (KERN_FAILURE);
600 off = offset - trunc_page(offset);
601 sz = end - start;
602 if (sz > PAGE_SIZE - off)
603 sz = PAGE_SIZE - off;
604 error = copyout((caddr_t)sf_buf_kva(sf) + off,
605 (caddr_t)start, sz);
606 vm_imgact_unmap_page(sf);
607 if (error != 0)
608 return (KERN_FAILURE);
609 offset += sz;
610 }
611 } else {
612 vm_object_reference(object);
613 rv = vm_map_fixed(map, object, offset, start, end - start,
614 prot, VM_PROT_ALL, cow | MAP_CHECK_EXCL |
615 (object != NULL ? MAP_VN_EXEC : 0));
616 if (rv != KERN_SUCCESS) {
617 locked = VOP_ISLOCKED(imgp->vp);
618 VOP_UNLOCK(imgp->vp);
619 vm_object_deallocate(object);
620 vn_lock(imgp->vp, locked | LK_RETRY);
621 return (rv);
622 } else if (object != NULL) {
623 MPASS(imgp->vp->v_object == object);
624 VOP_SET_TEXT_CHECKED(imgp->vp);
625 }
626 }
627 return (KERN_SUCCESS);
628 }
629
630 static int
__elfN(load_section)631 __elfN(load_section)(const struct image_params *imgp, vm_ooffset_t offset,
632 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
633 {
634 struct sf_buf *sf;
635 size_t map_len;
636 vm_map_t map;
637 vm_object_t object;
638 vm_offset_t map_addr;
639 int error, rv, cow;
640 size_t copy_len;
641 vm_ooffset_t file_addr;
642
643 /*
644 * It's necessary to fail if the filsz + offset taken from the
645 * header is greater than the actual file pager object's size.
646 * If we were to allow this, then the vm_map_find() below would
647 * walk right off the end of the file object and into the ether.
648 *
649 * While I'm here, might as well check for something else that
650 * is invalid: filsz cannot be greater than memsz.
651 */
652 if ((filsz != 0 && (off_t)filsz + offset > imgp->attr->va_size) ||
653 filsz > memsz) {
654 uprintf("elf_load_section: truncated ELF file\n");
655 return (ENOEXEC);
656 }
657
658 object = imgp->object;
659 map = &imgp->proc->p_vmspace->vm_map;
660 map_addr = trunc_page((vm_offset_t)vmaddr);
661 file_addr = trunc_page(offset);
662
663 /*
664 * We have two choices. We can either clear the data in the last page
665 * of an oversized mapping, or we can start the anon mapping a page
666 * early and copy the initialized data into that first page. We
667 * choose the second.
668 */
669 if (filsz == 0)
670 map_len = 0;
671 else if (memsz > filsz)
672 map_len = trunc_page(offset + filsz) - file_addr;
673 else
674 map_len = round_page(offset + filsz) - file_addr;
675
676 if (map_len != 0) {
677 /* cow flags: don't dump readonly sections in core */
678 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
679 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
680
681 rv = __elfN(map_insert)(imgp, map, object, file_addr,
682 map_addr, map_addr + map_len, prot, cow);
683 if (rv != KERN_SUCCESS)
684 return (EINVAL);
685
686 /* we can stop now if we've covered it all */
687 if (memsz == filsz)
688 return (0);
689 }
690
691 /*
692 * We have to get the remaining bit of the file into the first part
693 * of the oversized map segment. This is normally because the .data
694 * segment in the file is extended to provide bss. It's a neat idea
695 * to try and save a page, but it's a pain in the behind to implement.
696 */
697 copy_len = filsz == 0 ? 0 : (offset + filsz) - trunc_page(offset +
698 filsz);
699 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
700 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
701
702 /* This had damn well better be true! */
703 if (map_len != 0) {
704 rv = __elfN(map_insert)(imgp, map, NULL, 0, map_addr,
705 map_addr + map_len, prot, 0);
706 if (rv != KERN_SUCCESS)
707 return (EINVAL);
708 }
709
710 if (copy_len != 0) {
711 sf = vm_imgact_map_page(object, offset + filsz);
712 if (sf == NULL)
713 return (EIO);
714
715 /* send the page fragment to user space */
716 error = copyout(sf_buf_kva(sf), (caddr_t)map_addr,
717 copy_len);
718 vm_imgact_unmap_page(sf);
719 if (error != 0)
720 return (error);
721 }
722
723 /*
724 * Remove write access to the page if it was only granted by map_insert
725 * to allow copyout.
726 */
727 if ((prot & VM_PROT_WRITE) == 0)
728 vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
729 map_len), prot, 0, VM_MAP_PROTECT_SET_PROT);
730
731 return (0);
732 }
733
734 static int
__elfN(load_sections)735 __elfN(load_sections)(const struct image_params *imgp, const Elf_Ehdr *hdr,
736 const Elf_Phdr *phdr, u_long rbase, u_long *base_addrp)
737 {
738 vm_prot_t prot;
739 u_long base_addr;
740 bool first;
741 int error, i;
742
743 ASSERT_VOP_LOCKED(imgp->vp, __func__);
744
745 base_addr = 0;
746 first = true;
747
748 for (i = 0; i < hdr->e_phnum; i++) {
749 if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
750 continue;
751
752 /* Loadable segment */
753 prot = __elfN(trans_prot)(phdr[i].p_flags);
754 error = __elfN(load_section)(imgp, phdr[i].p_offset,
755 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
756 phdr[i].p_memsz, phdr[i].p_filesz, prot);
757 if (error != 0)
758 return (error);
759
760 /*
761 * Establish the base address if this is the first segment.
762 */
763 if (first) {
764 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
765 first = false;
766 }
767 }
768
769 if (base_addrp != NULL)
770 *base_addrp = base_addr;
771
772 return (0);
773 }
774
775 /*
776 * Load the file "file" into memory. It may be either a shared object
777 * or an executable.
778 *
779 * The "addr" reference parameter is in/out. On entry, it specifies
780 * the address where a shared object should be loaded. If the file is
781 * an executable, this value is ignored. On exit, "addr" specifies
782 * where the file was actually loaded.
783 *
784 * The "entry" reference parameter is out only. On exit, it specifies
785 * the entry point for the loaded file.
786 */
787 static int
__elfN(load_interp_file)788 __elfN(load_interp_file)(struct thread *td, const char *file, u_long *addr,
789 u_long *entry)
790 {
791 struct {
792 struct nameidata nd;
793 struct vattr attr;
794 struct image_params image_params;
795 } *tempdata = NULL;
796 const Elf_Ehdr *hdr = NULL;
797 const Elf_Phdr *phdr = NULL;
798 struct nameidata *nd;
799 struct vattr *attr;
800 struct image_params *imgp;
801 void *m_phdrs = NULL;
802 u_long rbase;
803 u_long base_addr = 0;
804 int error;
805
806 #ifdef CAPABILITY_MODE
807 /*
808 * XXXJA: This check can go away once we are sufficiently confident
809 * that the checks in namei() are correct.
810 */
811 if (IN_CAPABILITY_MODE(td))
812 return (ECAPMODE);
813 #endif
814
815 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK | M_ZERO);
816 nd = &tempdata->nd;
817 attr = &tempdata->attr;
818 imgp = &tempdata->image_params;
819
820 /*
821 * Initialize part of the common data
822 */
823 imgp->td = td;
824 imgp->proc = td->td_proc;
825 imgp->attr = attr;
826 imgp->interpreted = IMGACT_INTERP_ELF; /* ignored by do_execve */
827
828 NDINIT(nd, LOOKUP, ISOPEN | FOLLOW | LOCKSHARED | LOCKLEAF,
829 UIO_SYSSPACE, file);
830 if ((error = namei(nd)) != 0) {
831 nd->ni_vp = NULL;
832 goto fail;
833 }
834 NDFREE_PNBUF(nd);
835 imgp->vp = nd->ni_vp;
836
837 /*
838 * Check permissions, modes, uid, etc on the file, and "open" it.
839 */
840 error = exec_check_permissions(imgp);
841 if (error)
842 goto fail;
843
844 error = exec_map_first_page(imgp);
845 if (error)
846 goto fail;
847
848 imgp->object = nd->ni_vp->v_object;
849
850 hdr = (const Elf_Ehdr *)imgp->image_header;
851 if ((error = __elfN(check_header)(hdr)) != 0)
852 goto fail;
853 if (hdr->e_type == ET_DYN)
854 rbase = *addr;
855 else if (hdr->e_type == ET_EXEC)
856 rbase = 0;
857 else {
858 error = ENOEXEC;
859 goto fail;
860 }
861
862 if (hdr->e_phnum > __elfN(phnums)) {
863 error = ENOEXEC;
864 goto fail;
865 }
866 if (__elfN(phdr_in_zero_page)(hdr) &&
867 aligned(imgp->image_header + hdr->e_phoff, Elf_Addr)) {
868 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
869 } else {
870 VOP_UNLOCK(imgp->vp);
871 phdr = m_phdrs = malloc(hdr->e_phnum * sizeof(Elf_Phdr),
872 M_TEMP, M_WAITOK | M_ZERO);
873 vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
874 error = vn_rdwr(UIO_READ, imgp->vp, m_phdrs,
875 hdr->e_phnum * sizeof(Elf_Phdr), hdr->e_phoff,
876 UIO_SYSSPACE, IO_NODELOCKED, imgp->td->td_ucred,
877 NOCRED, NULL, imgp->td);
878 if (error != 0)
879 goto fail;
880 }
881
882 error = __elfN(load_sections)(imgp, hdr, phdr, rbase, &base_addr);
883 if (error != 0)
884 goto fail;
885
886 if (imgp->proc->p_sysent->sv_protect != NULL)
887 imgp->proc->p_sysent->sv_protect(imgp, SVP_INTERP);
888
889 *addr = base_addr;
890 *entry = (unsigned long)hdr->e_entry + rbase;
891
892 fail:
893 if (imgp->firstpage)
894 exec_unmap_first_page(imgp);
895
896 if (nd->ni_vp) {
897 if (imgp->textset)
898 VOP_UNSET_TEXT_CHECKED(nd->ni_vp);
899 vput(nd->ni_vp);
900 }
901 free(m_phdrs, M_TEMP);
902 free(tempdata, M_TEMP);
903
904 return (error);
905 }
906
907 /*
908 * Select randomized valid address in the map map, between minv and
909 * maxv, with specified alignment. The [minv, maxv) range must belong
910 * to the map. Note that function only allocates the address, it is
911 * up to caller to clamp maxv in a way that the final allocation
912 * length fit into the map.
913 *
914 * Result is returned in *resp, error code indicates that arguments
915 * did not pass sanity checks for overflow and range correctness.
916 */
917 static int
__CONCAT(rnd_,__elfN (base))918 __CONCAT(rnd_, __elfN(base))(vm_map_t map, u_long minv, u_long maxv,
919 u_int align, u_long *resp)
920 {
921 u_long rbase, res;
922
923 MPASS(vm_map_min(map) <= minv);
924
925 if (minv >= maxv || minv + align >= maxv || maxv > vm_map_max(map)) {
926 uprintf("Invalid ELF segments layout\n");
927 return (ENOEXEC);
928 }
929
930 arc4rand(&rbase, sizeof(rbase), 0);
931 res = roundup(minv, (u_long)align) + rbase % (maxv - minv);
932 res &= ~((u_long)align - 1);
933 if (res >= maxv)
934 res -= align;
935
936 KASSERT(res >= minv,
937 ("res %#lx < minv %#lx, maxv %#lx rbase %#lx",
938 res, minv, maxv, rbase));
939 KASSERT(res < maxv,
940 ("res %#lx > maxv %#lx, minv %#lx rbase %#lx",
941 res, maxv, minv, rbase));
942
943 *resp = res;
944 return (0);
945 }
946
947 static int
__elfN(enforce_limits)948 __elfN(enforce_limits)(struct image_params *imgp, const Elf_Ehdr *hdr,
949 const Elf_Phdr *phdr)
950 {
951 struct vmspace *vmspace;
952 const char *err_str;
953 u_long text_size, data_size, total_size, text_addr, data_addr;
954 u_long seg_size, seg_addr;
955 int i;
956
957 err_str = NULL;
958 text_size = data_size = total_size = text_addr = data_addr = 0;
959
960 for (i = 0; i < hdr->e_phnum; i++) {
961 if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
962 continue;
963
964 seg_addr = trunc_page(phdr[i].p_vaddr + imgp->et_dyn_addr);
965 seg_size = round_page(phdr[i].p_memsz +
966 phdr[i].p_vaddr + imgp->et_dyn_addr - seg_addr);
967
968 /*
969 * Make the largest executable segment the official
970 * text segment and all others data.
971 *
972 * Note that obreak() assumes that data_addr + data_size == end
973 * of data load area, and the ELF file format expects segments
974 * to be sorted by address. If multiple data segments exist,
975 * the last one will be used.
976 */
977
978 if ((phdr[i].p_flags & PF_X) != 0 && text_size < seg_size) {
979 text_size = seg_size;
980 text_addr = seg_addr;
981 } else {
982 data_size = seg_size;
983 data_addr = seg_addr;
984 }
985 total_size += seg_size;
986 }
987
988 if (data_addr == 0 && data_size == 0) {
989 data_addr = text_addr;
990 data_size = text_size;
991 }
992
993 /*
994 * Check limits. It should be safe to check the
995 * limits after loading the segments since we do
996 * not actually fault in all the segments pages.
997 */
998 PROC_LOCK(imgp->proc);
999 if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA))
1000 err_str = "Data segment size exceeds process limit";
1001 else if (text_size > maxtsiz)
1002 err_str = "Text segment size exceeds system limit";
1003 else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM))
1004 err_str = "Total segment size exceeds process limit";
1005 else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
1006 err_str = "Data segment size exceeds resource limit";
1007 else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
1008 err_str = "Total segment size exceeds resource limit";
1009 PROC_UNLOCK(imgp->proc);
1010 if (err_str != NULL) {
1011 uprintf("%s\n", err_str);
1012 return (ENOMEM);
1013 }
1014
1015 vmspace = imgp->proc->p_vmspace;
1016 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
1017 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
1018 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
1019 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
1020
1021 return (0);
1022 }
1023
1024 static int
__elfN(get_interp)1025 __elfN(get_interp)(struct image_params *imgp, const Elf_Phdr *phdr,
1026 char **interpp, bool *free_interpp)
1027 {
1028 char *interp;
1029 int error, interp_name_len;
1030
1031 KASSERT(phdr->p_type == PT_INTERP,
1032 ("%s: p_type %u != PT_INTERP", __func__, phdr->p_type));
1033 ASSERT_VOP_LOCKED(imgp->vp, __func__);
1034
1035 /* Path to interpreter */
1036 if (phdr->p_filesz < 2 || phdr->p_filesz > MAXPATHLEN) {
1037 uprintf("Invalid PT_INTERP\n");
1038 return (ENOEXEC);
1039 }
1040
1041 interp_name_len = phdr->p_filesz;
1042 if (phdr->p_offset > PAGE_SIZE ||
1043 interp_name_len > PAGE_SIZE - phdr->p_offset) {
1044 /*
1045 * The vnode lock might be needed by the pagedaemon to
1046 * clean pages owned by the vnode. Do not allow sleep
1047 * waiting for memory with the vnode locked, instead
1048 * try non-sleepable allocation first, and if it
1049 * fails, go to the slow path were we drop the lock
1050 * and do M_WAITOK. A text reference prevents
1051 * modifications to the vnode content.
1052 */
1053 interp = malloc(interp_name_len + 1, M_TEMP, M_NOWAIT);
1054 if (interp == NULL) {
1055 VOP_UNLOCK(imgp->vp);
1056 interp = malloc(interp_name_len + 1, M_TEMP, M_WAITOK);
1057 vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
1058 }
1059
1060 error = vn_rdwr(UIO_READ, imgp->vp, interp,
1061 interp_name_len, phdr->p_offset,
1062 UIO_SYSSPACE, IO_NODELOCKED, imgp->td->td_ucred,
1063 NOCRED, NULL, imgp->td);
1064 if (error != 0) {
1065 free(interp, M_TEMP);
1066 uprintf("i/o error PT_INTERP %d\n", error);
1067 return (error);
1068 }
1069 interp[interp_name_len] = '\0';
1070
1071 *interpp = interp;
1072 *free_interpp = true;
1073 return (0);
1074 }
1075
1076 interp = __DECONST(char *, imgp->image_header) + phdr->p_offset;
1077 if (interp[interp_name_len - 1] != '\0') {
1078 uprintf("Invalid PT_INTERP\n");
1079 return (ENOEXEC);
1080 }
1081
1082 *interpp = interp;
1083 *free_interpp = false;
1084 return (0);
1085 }
1086
1087 static int
__elfN(load_interp)1088 __elfN(load_interp)(struct image_params *imgp, const Elf_Brandinfo *brand_info,
1089 const char *interp, u_long *addr, u_long *entry)
1090 {
1091 int error;
1092
1093 if (brand_info->interp_newpath != NULL &&
1094 (brand_info->interp_path == NULL ||
1095 strcmp(interp, brand_info->interp_path) == 0)) {
1096 error = __elfN(load_interp_file)(imgp->td,
1097 brand_info->interp_newpath, addr, entry);
1098 if (error == 0)
1099 return (0);
1100 }
1101
1102 error = __elfN(load_interp_file)(imgp->td, interp, addr, entry);
1103 if (error == 0)
1104 return (0);
1105
1106 uprintf("ELF interpreter %s not found, error %d\n", interp, error);
1107 return (error);
1108 }
1109
1110 /*
1111 * Impossible et_dyn_addr initial value indicating that the real base
1112 * must be calculated later with some randomization applied.
1113 */
1114 #define ET_DYN_ADDR_RAND 1
1115
1116 static int
__CONCAT(exec_,__elfN (imgact))1117 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
1118 {
1119 const Elf_Ehdr *hdr;
1120 const Elf_Phdr *phdr;
1121 Elf_Auxargs *elf_auxargs;
1122 struct vmspace *vmspace;
1123 vm_map_t map;
1124 char *interp;
1125 const Elf_Brandinfo *brand_info;
1126 struct sysentvec *sv;
1127 void *m_phdrs;
1128 u_long addr, baddr, entry, proghdr;
1129 u_long maxalign, maxsalign, mapsz, maxv, maxv1, anon_loc;
1130 uint32_t fctl0;
1131 int32_t osrel;
1132 bool free_interp;
1133 int error, i, n;
1134
1135 hdr = (const Elf_Ehdr *)imgp->image_header;
1136
1137 /*
1138 * Do we have a valid ELF header ?
1139 *
1140 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
1141 * if particular brand doesn't support it.
1142 */
1143 if (__elfN(check_header)(hdr) != 0 ||
1144 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
1145 return (-1);
1146
1147 /*
1148 * From here on down, we return an errno, not -1, as we've
1149 * detected an ELF file.
1150 */
1151
1152 n = error = 0;
1153 baddr = 0;
1154 osrel = 0;
1155 fctl0 = 0;
1156 entry = proghdr = 0;
1157 interp = NULL;
1158 free_interp = false;
1159 m_phdrs = NULL;
1160
1161 if (hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize < hdr->e_phoff) {
1162 uprintf("PHDRS wrap\n");
1163 return (ENOEXEC);
1164 }
1165 if (hdr->e_phnum > __elfN(phnums)) {
1166 uprintf("Too many program headers (%u, %u max)\n",
1167 hdr->e_phnum, __elfN(phnums));
1168 return (ENOEXEC);
1169 }
1170 if (__elfN(phdr_in_zero_page)(hdr) &&
1171 aligned(imgp->image_header + hdr->e_phoff, Elf_Addr)) {
1172 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
1173 } else {
1174 VOP_UNLOCK(imgp->vp);
1175 phdr = m_phdrs = malloc(hdr->e_phnum * sizeof(Elf_Phdr),
1176 M_TEMP, M_WAITOK | M_ZERO);
1177 vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
1178 error = vn_rdwr(UIO_READ, imgp->vp, m_phdrs,
1179 hdr->e_phnum * sizeof(Elf_Phdr), hdr->e_phoff,
1180 UIO_SYSSPACE, IO_NODELOCKED, imgp->td->td_ucred,
1181 NOCRED, NULL, imgp->td);
1182 if (error != 0)
1183 goto ret;
1184 }
1185
1186 /*
1187 * Somewhat arbitrary, limit accepted max alignment for the
1188 * loadable segment to the max supported superpage size. Too
1189 * large alignment requests are not useful and are indicators
1190 * of corrupted or outright malicious binary.
1191 */
1192 maxalign = PAGE_SIZE;
1193 maxsalign = PAGE_SIZE * 1024;
1194 for (i = MAXPAGESIZES - 1; i > 0; i--) {
1195 if (pagesizes[i] > maxsalign) {
1196 maxsalign = pagesizes[i];
1197 break;
1198 }
1199 }
1200
1201 mapsz = 0;
1202
1203 for (i = 0; i < hdr->e_phnum; i++) {
1204 switch (phdr[i].p_type) {
1205 case PT_LOAD:
1206 if (n == 0)
1207 baddr = phdr[i].p_vaddr;
1208 if (!powerof2(phdr[i].p_align) ||
1209 phdr[i].p_align > maxsalign) {
1210 uprintf("Invalid segment alignment\n");
1211 error = ENOEXEC;
1212 goto ret;
1213 }
1214 if (phdr[i].p_align > maxalign)
1215 maxalign = phdr[i].p_align;
1216 if (mapsz + phdr[i].p_memsz < mapsz) {
1217 uprintf("Mapsize overflow\n");
1218 error = ENOEXEC;
1219 goto ret;
1220 }
1221 mapsz += phdr[i].p_memsz;
1222 n++;
1223
1224 /*
1225 * If this segment contains the program headers,
1226 * remember their virtual address for the AT_PHDR
1227 * aux entry. Static binaries don't usually include
1228 * a PT_PHDR entry.
1229 */
1230 if (phdr[i].p_offset == 0 &&
1231 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize <=
1232 phdr[i].p_filesz)
1233 proghdr = phdr[i].p_vaddr + hdr->e_phoff;
1234 break;
1235 case PT_INTERP:
1236 /* Path to interpreter */
1237 if (interp != NULL) {
1238 uprintf("Multiple PT_INTERP headers\n");
1239 error = ENOEXEC;
1240 goto ret;
1241 }
1242 error = __elfN(get_interp)(imgp, &phdr[i], &interp,
1243 &free_interp);
1244 if (error != 0)
1245 goto ret;
1246 break;
1247 case PT_GNU_STACK:
1248 if (__elfN(nxstack)) {
1249 imgp->stack_prot =
1250 __elfN(trans_prot)(phdr[i].p_flags);
1251 if ((imgp->stack_prot & VM_PROT_RW) !=
1252 VM_PROT_RW) {
1253 uprintf("Invalid PT_GNU_STACK\n");
1254 error = ENOEXEC;
1255 goto ret;
1256 }
1257 }
1258 imgp->stack_sz = phdr[i].p_memsz;
1259 break;
1260 case PT_PHDR: /* Program header table info */
1261 proghdr = phdr[i].p_vaddr;
1262 break;
1263 }
1264 }
1265
1266 brand_info = __elfN(get_brandinfo)(imgp, phdr, interp, &osrel, &fctl0);
1267 if (brand_info == NULL) {
1268 uprintf("ELF binary type \"%u\" not known.\n",
1269 hdr->e_ident[EI_OSABI]);
1270 error = ENOEXEC;
1271 goto ret;
1272 }
1273
1274 /*
1275 * Avoid a possible deadlock if the current address space is destroyed
1276 * and that address space maps the locked vnode. In the common case,
1277 * the locked vnode's v_usecount is decremented but remains greater
1278 * than zero. Consequently, the vnode lock is not needed by vrele().
1279 * However, in cases where the vnode lock is external, such as nullfs,
1280 * v_usecount may become zero.
1281 *
1282 * The VV_TEXT flag prevents modifications to the executable while
1283 * the vnode is unlocked.
1284 */
1285 VOP_UNLOCK(imgp->vp);
1286
1287 /*
1288 * Decide whether to enable randomization of user mappings. First,
1289 * reset user preferences for the setid binaries. Then, account for the
1290 * support of randomization by the ABI, by user preferences, and make
1291 * special treatment for PIE binaries.
1292 */
1293 if (imgp->credential_setid) {
1294 PROC_LOCK(imgp->proc);
1295 imgp->proc->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE |
1296 P2_WXORX_DISABLE | P2_WXORX_ENABLE_EXEC);
1297 PROC_UNLOCK(imgp->proc);
1298 }
1299
1300 sv = brand_info->sysvec;
1301 if (hdr->e_type == ET_DYN) {
1302 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
1303 uprintf("Cannot execute shared object\n");
1304 error = ENOEXEC;
1305 (void)vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
1306 goto ret;
1307 }
1308 /*
1309 * Honour the base load address from the dso if it is
1310 * non-zero for some reason.
1311 */
1312 if (baddr == 0) {
1313 if ((sv->sv_flags & SV_ASLR) == 0 ||
1314 (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0)
1315 imgp->et_dyn_addr = __elfN(pie_base);
1316 else if ((__elfN(pie_aslr_enabled) &&
1317 (imgp->proc->p_flag2 & P2_ASLR_DISABLE) == 0) ||
1318 (imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0)
1319 imgp->et_dyn_addr = ET_DYN_ADDR_RAND;
1320 else
1321 imgp->et_dyn_addr = __elfN(pie_base);
1322 }
1323 }
1324 if ((sv->sv_flags & SV_ASLR) == 0 ||
1325 (imgp->proc->p_flag2 & P2_ASLR_DISABLE) != 0 ||
1326 (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0) {
1327 KASSERT(imgp->et_dyn_addr != ET_DYN_ADDR_RAND,
1328 ("imgp->et_dyn_addr == RAND and !ASLR"));
1329 } else if ((imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0 ||
1330 (__elfN(aslr_enabled) && hdr->e_type == ET_EXEC) ||
1331 imgp->et_dyn_addr == ET_DYN_ADDR_RAND) {
1332 imgp->map_flags |= MAP_ASLR;
1333 /*
1334 * If user does not care about sbrk, utilize the bss
1335 * grow region for mappings as well. We can select
1336 * the base for the image anywere and still not suffer
1337 * from the fragmentation.
1338 */
1339 if (!__elfN(aslr_honor_sbrk) ||
1340 (imgp->proc->p_flag2 & P2_ASLR_IGNSTART) != 0)
1341 imgp->map_flags |= MAP_ASLR_IGNSTART;
1342 if (__elfN(aslr_stack))
1343 imgp->map_flags |= MAP_ASLR_STACK;
1344 if (__elfN(aslr_shared_page))
1345 imgp->imgp_flags |= IMGP_ASLR_SHARED_PAGE;
1346 }
1347
1348 if ((!__elfN(allow_wx) && (fctl0 & NT_FREEBSD_FCTL_WXNEEDED) == 0 &&
1349 (imgp->proc->p_flag2 & P2_WXORX_DISABLE) == 0) ||
1350 (imgp->proc->p_flag2 & P2_WXORX_ENABLE_EXEC) != 0)
1351 imgp->map_flags |= MAP_WXORX;
1352
1353 error = exec_new_vmspace(imgp, sv);
1354
1355 imgp->proc->p_sysent = sv;
1356 imgp->proc->p_elf_brandinfo = brand_info;
1357
1358 vmspace = imgp->proc->p_vmspace;
1359 map = &vmspace->vm_map;
1360 maxv = sv->sv_usrstack;
1361 if ((imgp->map_flags & MAP_ASLR_STACK) == 0)
1362 maxv -= lim_max(imgp->td, RLIMIT_STACK);
1363 if (error == 0 && mapsz >= maxv - vm_map_min(map)) {
1364 uprintf("Excessive mapping size\n");
1365 error = ENOEXEC;
1366 }
1367
1368 if (error == 0 && imgp->et_dyn_addr == ET_DYN_ADDR_RAND) {
1369 KASSERT((map->flags & MAP_ASLR) != 0,
1370 ("ET_DYN_ADDR_RAND but !MAP_ASLR"));
1371 error = __CONCAT(rnd_, __elfN(base))(map,
1372 vm_map_min(map) + mapsz + lim_max(imgp->td, RLIMIT_DATA),
1373 /* reserve half of the address space to interpreter */
1374 maxv / 2, maxalign, &imgp->et_dyn_addr);
1375 }
1376
1377 vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
1378 if (error != 0)
1379 goto ret;
1380
1381 error = __elfN(load_sections)(imgp, hdr, phdr, imgp->et_dyn_addr, NULL);
1382 if (error != 0)
1383 goto ret;
1384
1385 error = __elfN(enforce_limits)(imgp, hdr, phdr);
1386 if (error != 0)
1387 goto ret;
1388
1389 /*
1390 * We load the dynamic linker where a userland call
1391 * to mmap(0, ...) would put it. The rationale behind this
1392 * calculation is that it leaves room for the heap to grow to
1393 * its maximum allowed size.
1394 */
1395 addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(imgp->td,
1396 RLIMIT_DATA));
1397 if ((map->flags & MAP_ASLR) != 0) {
1398 maxv1 = maxv / 2 + addr / 2;
1399 error = __CONCAT(rnd_, __elfN(base))(map, addr, maxv1,
1400 #if VM_NRESERVLEVEL > 0
1401 pagesizes[VM_NRESERVLEVEL] != 0 ?
1402 /* Align anon_loc to the largest superpage size. */
1403 pagesizes[VM_NRESERVLEVEL] :
1404 #endif
1405 pagesizes[0], &anon_loc);
1406 if (error != 0)
1407 goto ret;
1408 map->anon_loc = anon_loc;
1409 } else {
1410 map->anon_loc = addr;
1411 }
1412
1413 entry = (u_long)hdr->e_entry + imgp->et_dyn_addr;
1414 imgp->entry_addr = entry;
1415
1416 if (sv->sv_protect != NULL)
1417 sv->sv_protect(imgp, SVP_IMAGE);
1418
1419 if (interp != NULL) {
1420 VOP_UNLOCK(imgp->vp);
1421 if ((map->flags & MAP_ASLR) != 0) {
1422 /* Assume that interpreter fits into 1/4 of AS */
1423 maxv1 = maxv / 2 + addr / 2;
1424 error = __CONCAT(rnd_, __elfN(base))(map, addr,
1425 maxv1, PAGE_SIZE, &addr);
1426 }
1427 if (error == 0) {
1428 error = __elfN(load_interp)(imgp, brand_info, interp,
1429 &addr, &imgp->entry_addr);
1430 }
1431 vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
1432 if (error != 0)
1433 goto ret;
1434 } else
1435 addr = imgp->et_dyn_addr;
1436
1437 error = exec_map_stack(imgp);
1438 if (error != 0)
1439 goto ret;
1440
1441 /*
1442 * Construct auxargs table (used by the copyout_auxargs routine)
1443 */
1444 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_NOWAIT);
1445 if (elf_auxargs == NULL) {
1446 VOP_UNLOCK(imgp->vp);
1447 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
1448 vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
1449 }
1450 elf_auxargs->execfd = -1;
1451 elf_auxargs->phdr = proghdr + imgp->et_dyn_addr;
1452 elf_auxargs->phent = hdr->e_phentsize;
1453 elf_auxargs->phnum = hdr->e_phnum;
1454 elf_auxargs->pagesz = PAGE_SIZE;
1455 elf_auxargs->base = addr;
1456 elf_auxargs->flags = 0;
1457 elf_auxargs->entry = entry;
1458 elf_auxargs->hdr_eflags = hdr->e_flags;
1459
1460 imgp->auxargs = elf_auxargs;
1461 imgp->interpreted = 0;
1462 imgp->reloc_base = addr;
1463 imgp->proc->p_osrel = osrel;
1464 imgp->proc->p_fctl0 = fctl0;
1465 imgp->proc->p_elf_flags = hdr->e_flags;
1466
1467 ret:
1468 ASSERT_VOP_LOCKED(imgp->vp, "skipped relock");
1469 if (free_interp)
1470 free(interp, M_TEMP);
1471 free(m_phdrs, M_TEMP);
1472 return (error);
1473 }
1474
1475 #define elf_suword __CONCAT(suword, __ELF_WORD_SIZE)
1476
1477 int
__elfN(freebsd_copyout_auxargs)1478 __elfN(freebsd_copyout_auxargs)(struct image_params *imgp, uintptr_t base)
1479 {
1480 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
1481 Elf_Auxinfo *argarray, *pos;
1482 struct vmspace *vmspace;
1483 rlim_t stacksz;
1484 int error, oc;
1485 uint32_t bsdflags;
1486
1487 argarray = pos = malloc(AT_COUNT * sizeof(*pos), M_TEMP,
1488 M_WAITOK | M_ZERO);
1489
1490 vmspace = imgp->proc->p_vmspace;
1491
1492 if (args->execfd != -1)
1493 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1494 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1495 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1496 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1497 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1498 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1499 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1500 AUXARGS_ENTRY(pos, AT_BASE, args->base);
1501 AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags);
1502 if (imgp->execpathp != 0)
1503 AUXARGS_ENTRY_PTR(pos, AT_EXECPATH, imgp->execpathp);
1504 AUXARGS_ENTRY(pos, AT_OSRELDATE,
1505 imgp->proc->p_ucred->cr_prison->pr_osreldate);
1506 if (imgp->canary != 0) {
1507 AUXARGS_ENTRY_PTR(pos, AT_CANARY, imgp->canary);
1508 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1509 }
1510 AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1511 if (imgp->pagesizes != 0) {
1512 AUXARGS_ENTRY_PTR(pos, AT_PAGESIZES, imgp->pagesizes);
1513 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1514 }
1515 if ((imgp->sysent->sv_flags & SV_TIMEKEEP) != 0) {
1516 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1517 vmspace->vm_shp_base + imgp->sysent->sv_timekeep_offset);
1518 }
1519 AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
1520 != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
1521 imgp->sysent->sv_stackprot);
1522 if (imgp->sysent->sv_hwcap != NULL)
1523 AUXARGS_ENTRY(pos, AT_HWCAP, *imgp->sysent->sv_hwcap);
1524 if (imgp->sysent->sv_hwcap2 != NULL)
1525 AUXARGS_ENTRY(pos, AT_HWCAP2, *imgp->sysent->sv_hwcap2);
1526 if (imgp->sysent->sv_hwcap3 != NULL)
1527 AUXARGS_ENTRY(pos, AT_HWCAP3, *imgp->sysent->sv_hwcap3);
1528 if (imgp->sysent->sv_hwcap4 != NULL)
1529 AUXARGS_ENTRY(pos, AT_HWCAP4, *imgp->sysent->sv_hwcap4);
1530 bsdflags = 0;
1531 bsdflags |= __elfN(sigfastblock) ? ELF_BSDF_SIGFASTBLK : 0;
1532 oc = atomic_load_int(&vm_overcommit);
1533 bsdflags |= (oc & (SWAP_RESERVE_FORCE_ON | SWAP_RESERVE_RLIMIT_ON)) !=
1534 0 ? ELF_BSDF_VMNOOVERCOMMIT : 0;
1535 AUXARGS_ENTRY(pos, AT_BSDFLAGS, bsdflags);
1536 AUXARGS_ENTRY(pos, AT_ARGC, imgp->args->argc);
1537 AUXARGS_ENTRY_PTR(pos, AT_ARGV, imgp->argv);
1538 AUXARGS_ENTRY(pos, AT_ENVC, imgp->args->envc);
1539 AUXARGS_ENTRY_PTR(pos, AT_ENVV, imgp->envv);
1540 AUXARGS_ENTRY_PTR(pos, AT_PS_STRINGS, imgp->ps_strings);
1541 #ifdef RANDOM_FENESTRASX
1542 if ((imgp->sysent->sv_flags & SV_RNG_SEED_VER) != 0) {
1543 AUXARGS_ENTRY(pos, AT_FXRNG,
1544 vmspace->vm_shp_base + imgp->sysent->sv_fxrng_gen_offset);
1545 }
1546 #endif
1547 if ((imgp->sysent->sv_flags & SV_DSO_SIG) != 0 && __elfN(vdso) != 0) {
1548 AUXARGS_ENTRY(pos, AT_KPRELOAD,
1549 vmspace->vm_shp_base + imgp->sysent->sv_vdso_offset);
1550 }
1551 AUXARGS_ENTRY(pos, AT_USRSTACKBASE, round_page(vmspace->vm_stacktop));
1552 stacksz = imgp->proc->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
1553 AUXARGS_ENTRY(pos, AT_USRSTACKLIM, stacksz);
1554 AUXARGS_ENTRY(pos, AT_NULL, 0);
1555
1556 free(imgp->auxargs, M_TEMP);
1557 imgp->auxargs = NULL;
1558 KASSERT(pos - argarray <= AT_COUNT, ("Too many auxargs"));
1559
1560 error = copyout(argarray, (void *)base, sizeof(*argarray) * AT_COUNT);
1561 free(argarray, M_TEMP);
1562 return (error);
1563 }
1564
1565 int
__elfN(freebsd_fixup)1566 __elfN(freebsd_fixup)(uintptr_t *stack_base, struct image_params *imgp)
1567 {
1568 Elf_Addr *base;
1569
1570 base = (Elf_Addr *)*stack_base;
1571 base--;
1572 if (elf_suword(base, imgp->args->argc) == -1)
1573 return (EFAULT);
1574 *stack_base = (uintptr_t)base;
1575 return (0);
1576 }
1577
1578 /*
1579 * Code for generating ELF core dumps.
1580 */
1581
1582 typedef void (*segment_callback)(vm_map_entry_t, void *);
1583
1584 /* Closure for cb_put_phdr(). */
1585 struct phdr_closure {
1586 Elf_Phdr *phdr; /* Program header to fill in */
1587 Elf_Off offset; /* Offset of segment in core file */
1588 int numsegs; /* Maximum number of segments */
1589 int nextseg; /* Next segment to fill in */
1590 };
1591
1592 struct note_info {
1593 int type; /* Note type. */
1594 struct regset *regset; /* Register set. */
1595 outfunc_t outfunc; /* Output function. */
1596 void *outarg; /* Argument for the output function. */
1597 size_t outsize; /* Output size. */
1598 TAILQ_ENTRY(note_info) link; /* Link to the next note info. */
1599 };
1600
1601 TAILQ_HEAD(note_info_list, note_info);
1602
1603 static void cb_put_phdr(vm_map_entry_t, void *);
1604 static void cb_size_segment(vm_map_entry_t, void *);
1605 static void each_dumpable_segment(struct thread *, segment_callback, void *,
1606 int);
1607 static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
1608 struct note_info_list *, size_t, int);
1609 static void __elfN(putnote)(struct thread *td, struct note_info *, struct sbuf *);
1610
1611 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1612 static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1613 static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1614 static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1615 static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1616 static void __elfN(note_procstat_kqueues)(void *, struct sbuf *, size_t *);
1617 static void note_procstat_files(void *, struct sbuf *, size_t *);
1618 static void note_procstat_groups(void *, struct sbuf *, size_t *);
1619 static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1620 static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1621 static void note_procstat_umask(void *, struct sbuf *, size_t *);
1622 static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
1623
1624 static int
core_compressed_write(void * base,size_t len,off_t offset,void * arg)1625 core_compressed_write(void *base, size_t len, off_t offset, void *arg)
1626 {
1627
1628 return (core_write((struct coredump_params *)arg, base, len, offset,
1629 UIO_SYSSPACE, NULL));
1630 }
1631
1632 int
__elfN(coredump)1633 __elfN(coredump)(struct thread *td, struct coredump_writer *cdw, off_t limit, int flags)
1634 {
1635 struct ucred *cred = td->td_ucred;
1636 int compm, error = 0;
1637 struct sseg_closure seginfo;
1638 struct note_info_list notelst;
1639 struct coredump_params params;
1640 struct note_info *ninfo;
1641 void *hdr, *tmpbuf;
1642 size_t hdrsize, notesz, coresize;
1643
1644 hdr = NULL;
1645 tmpbuf = NULL;
1646 TAILQ_INIT(¬elst);
1647
1648 /* Size the program segments. */
1649 __elfN(size_segments)(td, &seginfo, flags);
1650
1651 /*
1652 * Collect info about the core file header area.
1653 */
1654 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1655 if (seginfo.count + 1 >= PN_XNUM)
1656 hdrsize += sizeof(Elf_Shdr);
1657 td->td_proc->p_sysent->sv_elf_core_prepare_notes(td, ¬elst, ¬esz);
1658 coresize = round_page(hdrsize + notesz) + seginfo.size;
1659
1660 /* Set up core dump parameters. */
1661 params.offset = 0;
1662 params.active_cred = cred;
1663 params.td = td;
1664 params.cdw = cdw;
1665 params.comp = NULL;
1666
1667 #ifdef RACCT
1668 if (racct_enable) {
1669 PROC_LOCK(td->td_proc);
1670 error = racct_add(td->td_proc, RACCT_CORE, coresize);
1671 PROC_UNLOCK(td->td_proc);
1672 if (error != 0) {
1673 error = EFAULT;
1674 goto done;
1675 }
1676 }
1677 #endif
1678 if (coresize >= limit) {
1679 error = EFAULT;
1680 goto done;
1681 }
1682
1683 /* Create a compression stream if necessary. */
1684 compm = compress_user_cores;
1685 if ((flags & (SVC_PT_COREDUMP | SVC_NOCOMPRESS)) == SVC_PT_COREDUMP &&
1686 compm == 0)
1687 compm = COMPRESS_GZIP;
1688 if (compm != 0) {
1689 params.comp = compressor_init(core_compressed_write,
1690 compm, CORE_BUF_SIZE,
1691 compress_user_cores_level, ¶ms);
1692 if (params.comp == NULL) {
1693 error = EFAULT;
1694 goto done;
1695 }
1696 tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1697 }
1698
1699 if (cdw->init_fn != NULL) {
1700 error = (*cdw->init_fn)(cdw, ¶ms);
1701 if (error != 0)
1702 goto done;
1703 }
1704
1705 /*
1706 * Allocate memory for building the header, fill it up, and write it out
1707 * following the notes.
1708 *
1709 * Note that a process sharing our vmspace might be concurrently
1710 * mutating the map, in which case we could populate fewer than
1711 * seginfo.count headers. Zero the buffer to ensure that unpopulated
1712 * headers are still initialized.
1713 */
1714 hdr = malloc(hdrsize, M_TEMP, M_WAITOK | M_ZERO);
1715 error = __elfN(corehdr)(¶ms, seginfo.count, hdr, hdrsize, ¬elst,
1716 notesz, flags);
1717
1718 /* Write the contents of all of the writable segments. */
1719 if (error == 0) {
1720 Elf_Phdr *php;
1721 off_t offset;
1722 int i;
1723
1724 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1725 offset = round_page(hdrsize + notesz);
1726 for (i = 0; i < seginfo.count; i++) {
1727 error = core_output((char *)(uintptr_t)php->p_vaddr,
1728 php->p_filesz, offset, ¶ms, tmpbuf);
1729 if (error != 0)
1730 break;
1731 offset += php->p_filesz;
1732 php++;
1733 }
1734 if (error == 0 && params.comp != NULL)
1735 error = compressor_flush(params.comp);
1736 }
1737 if (error) {
1738 log(LOG_WARNING,
1739 "Failed to write core file for process %s (error %d)\n",
1740 curproc->p_comm, error);
1741 }
1742
1743 done:
1744 free(tmpbuf, M_TEMP);
1745 if (params.comp != NULL)
1746 compressor_fini(params.comp);
1747 while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) {
1748 TAILQ_REMOVE(¬elst, ninfo, link);
1749 free(ninfo, M_TEMP);
1750 }
1751 if (hdr != NULL)
1752 free(hdr, M_TEMP);
1753
1754 return (error);
1755 }
1756
1757 /*
1758 * A callback for each_dumpable_segment() to write out the segment's
1759 * program header entry.
1760 */
1761 static void
cb_put_phdr(vm_map_entry_t entry,void * closure)1762 cb_put_phdr(vm_map_entry_t entry, void *closure)
1763 {
1764 struct phdr_closure *phc = (struct phdr_closure *)closure;
1765 Elf_Phdr *phdr = phc->phdr;
1766
1767 if (phc->nextseg >= phc->numsegs) {
1768 /* Only write as many headers as we have space for. */
1769 return;
1770 }
1771
1772 phc->offset = round_page(phc->offset);
1773
1774 phdr->p_type = PT_LOAD;
1775 phdr->p_offset = phc->offset;
1776 phdr->p_vaddr = entry->start;
1777 phdr->p_paddr = 0;
1778 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1779 phdr->p_align = PAGE_SIZE;
1780 phdr->p_flags = __elfN(untrans_prot)(entry->protection);
1781
1782 phc->offset += phdr->p_filesz;
1783 phc->phdr++;
1784
1785 phc->nextseg++;
1786 }
1787
1788 /*
1789 * A callback for each_dumpable_segment() to gather information about
1790 * the number of segments and their total size.
1791 */
1792 static void
cb_size_segment(vm_map_entry_t entry,void * closure)1793 cb_size_segment(vm_map_entry_t entry, void *closure)
1794 {
1795 struct sseg_closure *ssc = (struct sseg_closure *)closure;
1796
1797 ssc->count++;
1798 ssc->size += entry->end - entry->start;
1799 }
1800
1801 void
__elfN(size_segments)1802 __elfN(size_segments)(struct thread *td, struct sseg_closure *seginfo,
1803 int flags)
1804 {
1805 seginfo->count = 0;
1806 seginfo->size = 0;
1807
1808 each_dumpable_segment(td, cb_size_segment, seginfo, flags);
1809 }
1810
1811 /*
1812 * For each writable segment in the process's memory map, call the given
1813 * function with a pointer to the map entry and some arbitrary
1814 * caller-supplied data.
1815 */
1816 static void
each_dumpable_segment(struct thread * td,segment_callback func,void * closure,int flags)1817 each_dumpable_segment(struct thread *td, segment_callback func, void *closure,
1818 int flags)
1819 {
1820 struct proc *p = td->td_proc;
1821 vm_map_t map = &p->p_vmspace->vm_map;
1822 vm_map_entry_t entry;
1823 vm_object_t backing_object, object;
1824 bool ignore_entry;
1825
1826 vm_map_lock_read(map);
1827 VM_MAP_ENTRY_FOREACH(entry, map) {
1828 /*
1829 * Don't dump inaccessible mappings, deal with legacy
1830 * coredump mode.
1831 *
1832 * Note that read-only segments related to the elf binary
1833 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1834 * need to arbitrarily ignore such segments.
1835 */
1836 if ((flags & SVC_ALL) == 0) {
1837 if (elf_legacy_coredump) {
1838 if ((entry->protection & VM_PROT_RW) !=
1839 VM_PROT_RW)
1840 continue;
1841 } else {
1842 if ((entry->protection & VM_PROT_ALL) == 0)
1843 continue;
1844 }
1845 }
1846
1847 /*
1848 * Dont include memory segment in the coredump if
1849 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1850 * madvise(2). Do not dump submaps (i.e. parts of the
1851 * kernel map).
1852 */
1853 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
1854 continue;
1855 if ((entry->eflags & MAP_ENTRY_NOCOREDUMP) != 0 &&
1856 (flags & SVC_ALL) == 0)
1857 continue;
1858 if ((object = entry->object.vm_object) == NULL)
1859 continue;
1860
1861 /* Ignore memory-mapped devices and such things. */
1862 VM_OBJECT_RLOCK(object);
1863 while ((backing_object = object->backing_object) != NULL) {
1864 VM_OBJECT_RLOCK(backing_object);
1865 VM_OBJECT_RUNLOCK(object);
1866 object = backing_object;
1867 }
1868 ignore_entry = (object->flags & OBJ_FICTITIOUS) != 0;
1869 VM_OBJECT_RUNLOCK(object);
1870 if (ignore_entry)
1871 continue;
1872
1873 (*func)(entry, closure);
1874 }
1875 vm_map_unlock_read(map);
1876 }
1877
1878 /*
1879 * Write the core file header to the file, including padding up to
1880 * the page boundary.
1881 */
1882 static int
__elfN(corehdr)1883 __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr,
1884 size_t hdrsize, struct note_info_list *notelst, size_t notesz,
1885 int flags)
1886 {
1887 struct note_info *ninfo;
1888 struct sbuf *sb;
1889 int error;
1890
1891 /* Fill in the header. */
1892 bzero(hdr, hdrsize);
1893 __elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz, flags);
1894
1895 sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1896 sbuf_set_drain(sb, sbuf_drain_core_output, p);
1897 sbuf_start_section(sb, NULL);
1898 sbuf_bcat(sb, hdr, hdrsize);
1899 TAILQ_FOREACH(ninfo, notelst, link)
1900 __elfN(putnote)(p->td, ninfo, sb);
1901 /* Align up to a page boundary for the program segments. */
1902 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1903 error = sbuf_finish(sb);
1904 sbuf_delete(sb);
1905
1906 return (error);
1907 }
1908
1909 void
__elfN(prepare_notes)1910 __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1911 size_t *sizep)
1912 {
1913 struct proc *p;
1914 struct thread *thr;
1915 size_t size;
1916
1917 p = td->td_proc;
1918 size = 0;
1919
1920 size += __elfN(register_note)(td, list, NT_PRPSINFO,
1921 __elfN(note_prpsinfo), p);
1922
1923 /*
1924 * To have the debugger select the right thread (LWP) as the initial
1925 * thread, we dump the state of the thread passed to us in td first.
1926 * This is the thread that causes the core dump and thus likely to
1927 * be the right thread one wants to have selected in the debugger.
1928 */
1929 thr = td;
1930 while (thr != NULL) {
1931 size += __elfN(prepare_register_notes)(td, list, thr);
1932 size += __elfN(register_note)(td, list, -1,
1933 __elfN(note_threadmd), thr);
1934
1935 thr = thr == td ? TAILQ_FIRST(&p->p_threads) :
1936 TAILQ_NEXT(thr, td_plist);
1937 if (thr == td)
1938 thr = TAILQ_NEXT(thr, td_plist);
1939 }
1940
1941 size += __elfN(register_note)(td, list, NT_PROCSTAT_PROC,
1942 __elfN(note_procstat_proc), p);
1943 size += __elfN(register_note)(td, list, NT_PROCSTAT_FILES,
1944 note_procstat_files, p);
1945 size += __elfN(register_note)(td, list, NT_PROCSTAT_VMMAP,
1946 note_procstat_vmmap, p);
1947 size += __elfN(register_note)(td, list, NT_PROCSTAT_GROUPS,
1948 note_procstat_groups, p);
1949 size += __elfN(register_note)(td, list, NT_PROCSTAT_UMASK,
1950 note_procstat_umask, p);
1951 size += __elfN(register_note)(td, list, NT_PROCSTAT_RLIMIT,
1952 note_procstat_rlimit, p);
1953 size += __elfN(register_note)(td, list, NT_PROCSTAT_OSREL,
1954 note_procstat_osrel, p);
1955 size += __elfN(register_note)(td, list, NT_PROCSTAT_PSSTRINGS,
1956 __elfN(note_procstat_psstrings), p);
1957 size += __elfN(register_note)(td, list, NT_PROCSTAT_AUXV,
1958 __elfN(note_procstat_auxv), p);
1959 size += __elfN(register_note)(td, list, NT_PROCSTAT_KQUEUES,
1960 __elfN(note_procstat_kqueues), p);
1961
1962 *sizep = size;
1963 }
1964
1965 void
__elfN(puthdr)1966 __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1967 size_t notesz, int flags)
1968 {
1969 Elf_Ehdr *ehdr;
1970 Elf_Phdr *phdr;
1971 Elf_Shdr *shdr;
1972 struct phdr_closure phc;
1973 const Elf_Brandinfo *bi;
1974
1975 ehdr = (Elf_Ehdr *)hdr;
1976 bi = td->td_proc->p_elf_brandinfo;
1977
1978 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1979 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1980 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1981 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1982 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1983 ehdr->e_ident[EI_DATA] = ELF_DATA;
1984 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1985 ehdr->e_ident[EI_OSABI] = td->td_proc->p_sysent->sv_elf_core_osabi;
1986 ehdr->e_ident[EI_ABIVERSION] = 0;
1987 ehdr->e_ident[EI_PAD] = 0;
1988 ehdr->e_type = ET_CORE;
1989 ehdr->e_machine = bi->machine;
1990 ehdr->e_version = EV_CURRENT;
1991 ehdr->e_entry = 0;
1992 ehdr->e_phoff = sizeof(Elf_Ehdr);
1993 ehdr->e_flags = td->td_proc->p_elf_flags;
1994 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1995 ehdr->e_phentsize = sizeof(Elf_Phdr);
1996 ehdr->e_shentsize = sizeof(Elf_Shdr);
1997 ehdr->e_shstrndx = SHN_UNDEF;
1998 if (numsegs + 1 < PN_XNUM) {
1999 ehdr->e_phnum = numsegs + 1;
2000 ehdr->e_shnum = 0;
2001 } else {
2002 ehdr->e_phnum = PN_XNUM;
2003 ehdr->e_shnum = 1;
2004
2005 ehdr->e_shoff = ehdr->e_phoff +
2006 (numsegs + 1) * ehdr->e_phentsize;
2007 KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr),
2008 ("e_shoff: %zu, hdrsize - shdr: %zu",
2009 (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr)));
2010
2011 shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
2012 memset(shdr, 0, sizeof(*shdr));
2013 /*
2014 * A special first section is used to hold large segment and
2015 * section counts. This was proposed by Sun Microsystems in
2016 * Solaris and has been adopted by Linux; the standard ELF
2017 * tools are already familiar with the technique.
2018 *
2019 * See table 7-7 of the Solaris "Linker and Libraries Guide"
2020 * (or 12-7 depending on the version of the document) for more
2021 * details.
2022 */
2023 shdr->sh_type = SHT_NULL;
2024 shdr->sh_size = ehdr->e_shnum;
2025 shdr->sh_link = ehdr->e_shstrndx;
2026 shdr->sh_info = numsegs + 1;
2027 }
2028
2029 /*
2030 * Fill in the program header entries.
2031 */
2032 phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
2033
2034 /* The note segment. */
2035 phdr->p_type = PT_NOTE;
2036 phdr->p_offset = hdrsize;
2037 phdr->p_vaddr = 0;
2038 phdr->p_paddr = 0;
2039 phdr->p_filesz = notesz;
2040 phdr->p_memsz = 0;
2041 phdr->p_flags = PF_R;
2042 phdr->p_align = ELF_NOTE_ROUNDSIZE;
2043 phdr++;
2044
2045 /* All the writable segments from the program. */
2046 phc.phdr = phdr;
2047 phc.offset = round_page(hdrsize + notesz);
2048 phc.numsegs = numsegs;
2049 phc.nextseg = 0;
2050 each_dumpable_segment(td, cb_put_phdr, &phc, flags);
2051 }
2052
2053 static size_t
__elfN(register_regset_note)2054 __elfN(register_regset_note)(struct thread *td, struct note_info_list *list,
2055 struct regset *regset, struct thread *target_td)
2056 {
2057 const struct sysentvec *sv;
2058 struct note_info *ninfo;
2059 size_t size, notesize;
2060
2061 size = 0;
2062 if (!regset->get(regset, target_td, NULL, &size) || size == 0)
2063 return (0);
2064
2065 ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
2066 ninfo->type = regset->note;
2067 ninfo->regset = regset;
2068 ninfo->outarg = target_td;
2069 ninfo->outsize = size;
2070 TAILQ_INSERT_TAIL(list, ninfo, link);
2071
2072 sv = td->td_proc->p_sysent;
2073 notesize = sizeof(Elf_Note) + /* note header */
2074 roundup2(strlen(sv->sv_elf_core_abi_vendor) + 1, ELF_NOTE_ROUNDSIZE) +
2075 /* note name */
2076 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
2077
2078 return (notesize);
2079 }
2080
2081 size_t
__elfN(register_note)2082 __elfN(register_note)(struct thread *td, struct note_info_list *list,
2083 int type, outfunc_t out, void *arg)
2084 {
2085 const struct sysentvec *sv;
2086 struct note_info *ninfo;
2087 size_t size, notesize;
2088
2089 sv = td->td_proc->p_sysent;
2090 size = 0;
2091 out(arg, NULL, &size);
2092 ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
2093 ninfo->type = type;
2094 ninfo->outfunc = out;
2095 ninfo->outarg = arg;
2096 ninfo->outsize = size;
2097 TAILQ_INSERT_TAIL(list, ninfo, link);
2098
2099 if (type == -1)
2100 return (size);
2101
2102 notesize = sizeof(Elf_Note) + /* note header */
2103 roundup2(strlen(sv->sv_elf_core_abi_vendor) + 1, ELF_NOTE_ROUNDSIZE) +
2104 /* note name */
2105 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
2106
2107 return (notesize);
2108 }
2109
2110 static size_t
append_note_data(const void * src,void * dst,size_t len)2111 append_note_data(const void *src, void *dst, size_t len)
2112 {
2113 size_t padded_len;
2114
2115 padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
2116 if (dst != NULL) {
2117 bcopy(src, dst, len);
2118 bzero((char *)dst + len, padded_len - len);
2119 }
2120 return (padded_len);
2121 }
2122
2123 size_t
__elfN(populate_note)2124 __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
2125 {
2126 Elf_Note *note;
2127 char *buf;
2128 size_t notesize;
2129
2130 buf = dst;
2131 if (buf != NULL) {
2132 note = (Elf_Note *)buf;
2133 note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
2134 note->n_descsz = size;
2135 note->n_type = type;
2136 buf += sizeof(*note);
2137 buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
2138 sizeof(FREEBSD_ABI_VENDOR));
2139 append_note_data(src, buf, size);
2140 if (descp != NULL)
2141 *descp = buf;
2142 }
2143
2144 notesize = sizeof(Elf_Note) + /* note header */
2145 roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
2146 /* note name */
2147 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
2148
2149 return (notesize);
2150 }
2151
2152 static void
__elfN(putnote)2153 __elfN(putnote)(struct thread *td, struct note_info *ninfo, struct sbuf *sb)
2154 {
2155 Elf_Note note;
2156 const struct sysentvec *sv;
2157 ssize_t old_len, sect_len;
2158 size_t new_len, descsz, i;
2159
2160 if (ninfo->type == -1) {
2161 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
2162 return;
2163 }
2164
2165 sv = td->td_proc->p_sysent;
2166
2167 note.n_namesz = strlen(sv->sv_elf_core_abi_vendor) + 1;
2168 note.n_descsz = ninfo->outsize;
2169 note.n_type = ninfo->type;
2170
2171 sbuf_bcat(sb, ¬e, sizeof(note));
2172 sbuf_start_section(sb, &old_len);
2173 sbuf_bcat(sb, sv->sv_elf_core_abi_vendor,
2174 strlen(sv->sv_elf_core_abi_vendor) + 1);
2175 sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
2176 if (note.n_descsz == 0)
2177 return;
2178 sbuf_start_section(sb, &old_len);
2179 if (ninfo->regset != NULL) {
2180 struct regset *regset = ninfo->regset;
2181 void *buf;
2182
2183 buf = malloc(ninfo->outsize, M_TEMP, M_ZERO | M_WAITOK);
2184 (void)regset->get(regset, ninfo->outarg, buf, &ninfo->outsize);
2185 sbuf_bcat(sb, buf, ninfo->outsize);
2186 free(buf, M_TEMP);
2187 } else
2188 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
2189 sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
2190 if (sect_len < 0)
2191 return;
2192
2193 new_len = (size_t)sect_len;
2194 descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
2195 if (new_len < descsz) {
2196 /*
2197 * It is expected that individual note emitters will correctly
2198 * predict their expected output size and fill up to that size
2199 * themselves, padding in a format-specific way if needed.
2200 * However, in case they don't, just do it here with zeros.
2201 */
2202 for (i = 0; i < descsz - new_len; i++)
2203 sbuf_putc(sb, 0);
2204 } else if (new_len > descsz) {
2205 /*
2206 * We can't always truncate sb -- we may have drained some
2207 * of it already.
2208 */
2209 KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
2210 "read it (%zu > %zu). Since it is longer than "
2211 "expected, this coredump's notes are corrupt. THIS "
2212 "IS A BUG in the note_procstat routine for type %u.\n",
2213 __func__, (unsigned)note.n_type, new_len, descsz,
2214 (unsigned)note.n_type));
2215 }
2216 }
2217
2218 /*
2219 * Miscellaneous note out functions.
2220 */
2221
2222 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2223 #include <compat/freebsd32/freebsd32.h>
2224 #include <compat/freebsd32/freebsd32_signal.h>
2225
2226 typedef struct prstatus32 elf_prstatus_t;
2227 typedef struct prpsinfo32 elf_prpsinfo_t;
2228 typedef struct fpreg32 elf_prfpregset_t;
2229 typedef struct fpreg32 elf_fpregset_t;
2230 typedef struct reg32 elf_gregset_t;
2231 typedef struct thrmisc32 elf_thrmisc_t;
2232 typedef struct ptrace_lwpinfo32 elf_lwpinfo_t;
2233 #define ELF_KERN_PROC_MASK KERN_PROC_MASK32
2234 typedef struct kinfo_proc32 elf_kinfo_proc_t;
2235 typedef uint32_t elf_ps_strings_t;
2236 #else
2237 typedef prstatus_t elf_prstatus_t;
2238 typedef prpsinfo_t elf_prpsinfo_t;
2239 typedef prfpregset_t elf_prfpregset_t;
2240 typedef prfpregset_t elf_fpregset_t;
2241 typedef gregset_t elf_gregset_t;
2242 typedef thrmisc_t elf_thrmisc_t;
2243 typedef struct ptrace_lwpinfo elf_lwpinfo_t;
2244 #define ELF_KERN_PROC_MASK 0
2245 typedef struct kinfo_proc elf_kinfo_proc_t;
2246 typedef vm_offset_t elf_ps_strings_t;
2247 #endif
2248
2249 static void
__elfN(note_prpsinfo)2250 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
2251 {
2252 struct sbuf sbarg;
2253 size_t len;
2254 char *cp, *end;
2255 struct proc *p;
2256 elf_prpsinfo_t *psinfo;
2257 int error;
2258
2259 p = arg;
2260 if (sb != NULL) {
2261 KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
2262 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
2263 psinfo->pr_version = PRPSINFO_VERSION;
2264 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
2265 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
2266 PROC_LOCK(p);
2267 if (p->p_args != NULL) {
2268 len = sizeof(psinfo->pr_psargs) - 1;
2269 if (len > p->p_args->ar_length)
2270 len = p->p_args->ar_length;
2271 memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
2272 PROC_UNLOCK(p);
2273 error = 0;
2274 } else {
2275 _PHOLD(p);
2276 PROC_UNLOCK(p);
2277 sbuf_new(&sbarg, psinfo->pr_psargs,
2278 sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
2279 error = proc_getargv(curthread, p, &sbarg);
2280 PRELE(p);
2281 if (sbuf_finish(&sbarg) == 0) {
2282 len = sbuf_len(&sbarg);
2283 if (len > 0)
2284 len--;
2285 } else {
2286 len = sizeof(psinfo->pr_psargs) - 1;
2287 }
2288 sbuf_delete(&sbarg);
2289 }
2290 if (error != 0 || len == 0 || (ssize_t)len == -1)
2291 strlcpy(psinfo->pr_psargs, p->p_comm,
2292 sizeof(psinfo->pr_psargs));
2293 else {
2294 KASSERT(len < sizeof(psinfo->pr_psargs),
2295 ("len is too long: %zu vs %zu", len,
2296 sizeof(psinfo->pr_psargs)));
2297 cp = psinfo->pr_psargs;
2298 end = cp + len - 1;
2299 for (;;) {
2300 cp = memchr(cp, '\0', end - cp);
2301 if (cp == NULL)
2302 break;
2303 *cp = ' ';
2304 }
2305 }
2306 psinfo->pr_pid = p->p_pid;
2307 sbuf_bcat(sb, psinfo, sizeof(*psinfo));
2308 free(psinfo, M_TEMP);
2309 }
2310 *sizep = sizeof(*psinfo);
2311 }
2312
2313 static bool
__elfN(get_prstatus)2314 __elfN(get_prstatus)(struct regset *rs, struct thread *td, void *buf,
2315 size_t *sizep)
2316 {
2317 elf_prstatus_t *status;
2318
2319 if (buf != NULL) {
2320 KASSERT(*sizep == sizeof(*status), ("%s: invalid size",
2321 __func__));
2322 status = buf;
2323 memset(status, 0, *sizep);
2324 status->pr_version = PRSTATUS_VERSION;
2325 status->pr_statussz = sizeof(elf_prstatus_t);
2326 status->pr_gregsetsz = sizeof(elf_gregset_t);
2327 status->pr_fpregsetsz = sizeof(elf_fpregset_t);
2328 status->pr_osreldate = osreldate;
2329 status->pr_cursig = td->td_proc->p_sig;
2330 status->pr_pid = td->td_tid;
2331 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2332 fill_regs32(td, &status->pr_reg);
2333 #else
2334 fill_regs(td, &status->pr_reg);
2335 #endif
2336 }
2337 *sizep = sizeof(*status);
2338 return (true);
2339 }
2340
2341 static bool
__elfN(set_prstatus)2342 __elfN(set_prstatus)(struct regset *rs, struct thread *td, void *buf,
2343 size_t size)
2344 {
2345 elf_prstatus_t *status;
2346
2347 KASSERT(size == sizeof(*status), ("%s: invalid size", __func__));
2348 status = buf;
2349 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2350 set_regs32(td, &status->pr_reg);
2351 #else
2352 set_regs(td, &status->pr_reg);
2353 #endif
2354 return (true);
2355 }
2356
2357 static struct regset __elfN(regset_prstatus) = {
2358 .note = NT_PRSTATUS,
2359 .size = sizeof(elf_prstatus_t),
2360 .get = __elfN(get_prstatus),
2361 .set = __elfN(set_prstatus),
2362 };
2363 ELF_REGSET(__elfN(regset_prstatus));
2364
2365 static bool
__elfN(get_fpregset)2366 __elfN(get_fpregset)(struct regset *rs, struct thread *td, void *buf,
2367 size_t *sizep)
2368 {
2369 elf_prfpregset_t *fpregset;
2370
2371 if (buf != NULL) {
2372 KASSERT(*sizep == sizeof(*fpregset), ("%s: invalid size",
2373 __func__));
2374 fpregset = buf;
2375 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2376 fill_fpregs32(td, fpregset);
2377 #else
2378 fill_fpregs(td, fpregset);
2379 #endif
2380 }
2381 *sizep = sizeof(*fpregset);
2382 return (true);
2383 }
2384
2385 static bool
__elfN(set_fpregset)2386 __elfN(set_fpregset)(struct regset *rs, struct thread *td, void *buf,
2387 size_t size)
2388 {
2389 elf_prfpregset_t *fpregset;
2390
2391 KASSERT(size == sizeof(*fpregset), ("%s: invalid size", __func__));
2392
2393 fpregset = buf;
2394 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2395 return (set_fpregs32(td, fpregset) == 0);
2396 #else
2397 return (set_fpregs(td, fpregset) == 0);
2398 #endif
2399 }
2400
2401 static struct regset __elfN(regset_fpregset) = {
2402 .note = NT_FPREGSET,
2403 .size = sizeof(elf_prfpregset_t),
2404 .get = __elfN(get_fpregset),
2405 .set = __elfN(set_fpregset),
2406 };
2407 ELF_REGSET(__elfN(regset_fpregset));
2408
2409 static bool
__elfN(get_thrmisc)2410 __elfN(get_thrmisc)(struct regset *rs, struct thread *td, void *buf,
2411 size_t *sizep)
2412 {
2413 elf_thrmisc_t *thrmisc;
2414
2415 if (buf != NULL) {
2416 KASSERT(*sizep == sizeof(*thrmisc),
2417 ("%s: invalid size", __func__));
2418 thrmisc = buf;
2419 bzero(thrmisc, sizeof(*thrmisc));
2420 strcpy(thrmisc->pr_tname, td->td_name);
2421 }
2422 *sizep = sizeof(*thrmisc);
2423 return (true);
2424 }
2425
2426 static struct regset __elfN(regset_thrmisc) = {
2427 .note = NT_THRMISC,
2428 .size = sizeof(elf_thrmisc_t),
2429 .get = __elfN(get_thrmisc),
2430 };
2431 ELF_REGSET(__elfN(regset_thrmisc));
2432
2433 static bool
__elfN(get_lwpinfo)2434 __elfN(get_lwpinfo)(struct regset *rs, struct thread *td, void *buf,
2435 size_t *sizep)
2436 {
2437 elf_lwpinfo_t pl;
2438 size_t size;
2439 int structsize;
2440
2441 size = sizeof(structsize) + sizeof(pl);
2442 if (buf != NULL) {
2443 KASSERT(*sizep == size, ("%s: invalid size", __func__));
2444 structsize = sizeof(pl);
2445 memcpy(buf, &structsize, sizeof(structsize));
2446 bzero(&pl, sizeof(pl));
2447 pl.pl_lwpid = td->td_tid;
2448 pl.pl_event = PL_EVENT_NONE;
2449 pl.pl_sigmask = td->td_sigmask;
2450 pl.pl_siglist = td->td_siglist;
2451 if (td->td_si.si_signo != 0) {
2452 pl.pl_event = PL_EVENT_SIGNAL;
2453 pl.pl_flags |= PL_FLAG_SI;
2454 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2455 siginfo_to_siginfo32(&td->td_si, &pl.pl_siginfo);
2456 #else
2457 pl.pl_siginfo = td->td_si;
2458 #endif
2459 }
2460 strcpy(pl.pl_tdname, td->td_name);
2461 /* XXX TODO: supply more information in struct ptrace_lwpinfo*/
2462 memcpy((int *)buf + 1, &pl, sizeof(pl));
2463 }
2464 *sizep = size;
2465 return (true);
2466 }
2467
2468 static struct regset __elfN(regset_lwpinfo) = {
2469 .note = NT_PTLWPINFO,
2470 .size = sizeof(int) + sizeof(elf_lwpinfo_t),
2471 .get = __elfN(get_lwpinfo),
2472 };
2473 ELF_REGSET(__elfN(regset_lwpinfo));
2474
2475 static size_t
__elfN(prepare_register_notes)2476 __elfN(prepare_register_notes)(struct thread *td, struct note_info_list *list,
2477 struct thread *target_td)
2478 {
2479 struct sysentvec *sv = td->td_proc->p_sysent;
2480 struct regset **regsetp, **regset_end, *regset;
2481 size_t size;
2482
2483 size = 0;
2484
2485 if (target_td == td)
2486 cpu_update_pcb(target_td);
2487
2488 /* NT_PRSTATUS must be the first register set note. */
2489 size += __elfN(register_regset_note)(td, list, &__elfN(regset_prstatus),
2490 target_td);
2491
2492 regsetp = sv->sv_regset_begin;
2493 if (regsetp == NULL) {
2494 /* XXX: This shouldn't be true for any FreeBSD ABIs. */
2495 size += __elfN(register_regset_note)(td, list,
2496 &__elfN(regset_fpregset), target_td);
2497 return (size);
2498 }
2499 regset_end = sv->sv_regset_end;
2500 MPASS(regset_end != NULL);
2501 for (; regsetp < regset_end; regsetp++) {
2502 regset = *regsetp;
2503 if (regset->note == NT_PRSTATUS)
2504 continue;
2505 size += __elfN(register_regset_note)(td, list, regset,
2506 target_td);
2507 }
2508 return (size);
2509 }
2510
2511 /*
2512 * Allow for MD specific notes, as well as any MD
2513 * specific preparations for writing MI notes.
2514 */
2515 static void
__elfN(note_threadmd)2516 __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
2517 {
2518 struct thread *td;
2519 void *buf;
2520 size_t size;
2521
2522 td = (struct thread *)arg;
2523 size = *sizep;
2524 if (size != 0 && sb != NULL)
2525 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
2526 else
2527 buf = NULL;
2528 size = 0;
2529 __elfN(dump_thread)(td, buf, &size);
2530 KASSERT(sb == NULL || *sizep == size, ("invalid size"));
2531 if (size != 0 && sb != NULL)
2532 sbuf_bcat(sb, buf, size);
2533 free(buf, M_TEMP);
2534 *sizep = size;
2535 }
2536
2537 #ifdef KINFO_PROC_SIZE
2538 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
2539 #endif
2540
2541 static void
__elfN(note_procstat_proc)2542 __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
2543 {
2544 struct proc *p;
2545 size_t size;
2546 int structsize;
2547
2548 p = arg;
2549 size = sizeof(structsize) + p->p_numthreads *
2550 sizeof(elf_kinfo_proc_t);
2551
2552 if (sb != NULL) {
2553 KASSERT(*sizep == size, ("invalid size"));
2554 structsize = sizeof(elf_kinfo_proc_t);
2555 sbuf_bcat(sb, &structsize, sizeof(structsize));
2556 sx_slock(&proctree_lock);
2557 PROC_LOCK(p);
2558 kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
2559 sx_sunlock(&proctree_lock);
2560 }
2561 *sizep = size;
2562 }
2563
2564 #ifdef KINFO_FILE_SIZE
2565 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
2566 #endif
2567
2568 static void
note_procstat_files(void * arg,struct sbuf * sb,size_t * sizep)2569 note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
2570 {
2571 struct proc *p;
2572 size_t size, sect_sz, i;
2573 ssize_t start_len, sect_len;
2574 int structsize, filedesc_flags;
2575
2576 if (coredump_pack_fileinfo)
2577 filedesc_flags = KERN_FILEDESC_PACK_KINFO;
2578 else
2579 filedesc_flags = 0;
2580
2581 p = arg;
2582 structsize = sizeof(struct kinfo_file);
2583 if (sb == NULL) {
2584 size = 0;
2585 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2586 sbuf_set_drain(sb, sbuf_count_drain, &size);
2587 sbuf_bcat(sb, &structsize, sizeof(structsize));
2588 PROC_LOCK(p);
2589 kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
2590 sbuf_finish(sb);
2591 sbuf_delete(sb);
2592 *sizep = size;
2593 } else {
2594 sbuf_start_section(sb, &start_len);
2595
2596 sbuf_bcat(sb, &structsize, sizeof(structsize));
2597 PROC_LOCK(p);
2598 kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
2599 filedesc_flags);
2600
2601 sect_len = sbuf_end_section(sb, start_len, 0, 0);
2602 if (sect_len < 0)
2603 return;
2604 sect_sz = sect_len;
2605
2606 KASSERT(sect_sz <= *sizep,
2607 ("kern_proc_filedesc_out did not respect maxlen; "
2608 "requested %zu, got %zu", *sizep - sizeof(structsize),
2609 sect_sz - sizeof(structsize)));
2610
2611 for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
2612 sbuf_putc(sb, 0);
2613 }
2614 }
2615
2616 #ifdef KINFO_VMENTRY_SIZE
2617 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2618 #endif
2619
2620 static void
note_procstat_vmmap(void * arg,struct sbuf * sb,size_t * sizep)2621 note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
2622 {
2623 struct proc *p;
2624 size_t size;
2625 int structsize, vmmap_flags;
2626
2627 if (coredump_pack_vmmapinfo)
2628 vmmap_flags = KERN_VMMAP_PACK_KINFO;
2629 else
2630 vmmap_flags = 0;
2631
2632 p = arg;
2633 structsize = sizeof(struct kinfo_vmentry);
2634 if (sb == NULL) {
2635 size = 0;
2636 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2637 sbuf_set_drain(sb, sbuf_count_drain, &size);
2638 sbuf_bcat(sb, &structsize, sizeof(structsize));
2639 PROC_LOCK(p);
2640 kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
2641 sbuf_finish(sb);
2642 sbuf_delete(sb);
2643 *sizep = size;
2644 } else {
2645 sbuf_bcat(sb, &structsize, sizeof(structsize));
2646 PROC_LOCK(p);
2647 kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize),
2648 vmmap_flags);
2649 }
2650 }
2651
2652 static void
note_procstat_groups(void * arg,struct sbuf * sb,size_t * sizep)2653 note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
2654 {
2655 struct proc *p;
2656 size_t size;
2657 int structsize;
2658
2659 p = arg;
2660 size = sizeof(structsize) +
2661 (1 + p->p_ucred->cr_ngroups) * sizeof(gid_t);
2662 if (sb != NULL) {
2663 KASSERT(*sizep == size, ("invalid size"));
2664 structsize = sizeof(gid_t);
2665 sbuf_bcat(sb, &structsize, sizeof(structsize));
2666 sbuf_bcat(sb, &p->p_ucred->cr_gid, sizeof(gid_t));
2667 sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2668 sizeof(gid_t));
2669 }
2670 *sizep = size;
2671 }
2672
2673 static void
note_procstat_umask(void * arg,struct sbuf * sb,size_t * sizep)2674 note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2675 {
2676 struct proc *p;
2677 size_t size;
2678 int structsize;
2679
2680 p = arg;
2681 size = sizeof(structsize) + sizeof(p->p_pd->pd_cmask);
2682 if (sb != NULL) {
2683 KASSERT(*sizep == size, ("invalid size"));
2684 structsize = sizeof(p->p_pd->pd_cmask);
2685 sbuf_bcat(sb, &structsize, sizeof(structsize));
2686 sbuf_bcat(sb, &p->p_pd->pd_cmask, sizeof(p->p_pd->pd_cmask));
2687 }
2688 *sizep = size;
2689 }
2690
2691 static void
note_procstat_rlimit(void * arg,struct sbuf * sb,size_t * sizep)2692 note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2693 {
2694 struct proc *p;
2695 struct rlimit rlim[RLIM_NLIMITS];
2696 size_t size;
2697 int structsize, i;
2698
2699 p = arg;
2700 size = sizeof(structsize) + sizeof(rlim);
2701 if (sb != NULL) {
2702 KASSERT(*sizep == size, ("invalid size"));
2703 structsize = sizeof(rlim);
2704 sbuf_bcat(sb, &structsize, sizeof(structsize));
2705 PROC_LOCK(p);
2706 for (i = 0; i < RLIM_NLIMITS; i++)
2707 lim_rlimit_proc(p, i, &rlim[i]);
2708 PROC_UNLOCK(p);
2709 sbuf_bcat(sb, rlim, sizeof(rlim));
2710 }
2711 *sizep = size;
2712 }
2713
2714 static void
note_procstat_osrel(void * arg,struct sbuf * sb,size_t * sizep)2715 note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2716 {
2717 struct proc *p;
2718 size_t size;
2719 int structsize;
2720
2721 p = arg;
2722 size = sizeof(structsize) + sizeof(p->p_osrel);
2723 if (sb != NULL) {
2724 KASSERT(*sizep == size, ("invalid size"));
2725 structsize = sizeof(p->p_osrel);
2726 sbuf_bcat(sb, &structsize, sizeof(structsize));
2727 sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2728 }
2729 *sizep = size;
2730 }
2731
2732 static void
__elfN(note_procstat_psstrings)2733 __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2734 {
2735 struct proc *p;
2736 elf_ps_strings_t ps_strings;
2737 size_t size;
2738 int structsize;
2739
2740 p = arg;
2741 size = sizeof(structsize) + sizeof(ps_strings);
2742 if (sb != NULL) {
2743 KASSERT(*sizep == size, ("invalid size"));
2744 structsize = sizeof(ps_strings);
2745 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2746 ps_strings = PTROUT(PROC_PS_STRINGS(p));
2747 #else
2748 ps_strings = PROC_PS_STRINGS(p);
2749 #endif
2750 sbuf_bcat(sb, &structsize, sizeof(structsize));
2751 sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2752 }
2753 *sizep = size;
2754 }
2755
2756 static void
__elfN(note_procstat_auxv)2757 __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2758 {
2759 struct proc *p;
2760 size_t size;
2761 int structsize;
2762
2763 p = arg;
2764 structsize = sizeof(Elf_Auxinfo);
2765 if (sb == NULL) {
2766 size = 0;
2767 sb = sbuf_new(NULL, NULL, AT_COUNT * sizeof(Elf_Auxinfo),
2768 SBUF_FIXEDLEN);
2769 sbuf_set_drain(sb, sbuf_count_drain, &size);
2770 sbuf_bcat(sb, &structsize, sizeof(structsize));
2771 PHOLD(p);
2772 proc_getauxv(curthread, p, sb);
2773 PRELE(p);
2774 sbuf_finish(sb);
2775 sbuf_delete(sb);
2776 *sizep = size;
2777 } else {
2778 sbuf_bcat(sb, &structsize, sizeof(structsize));
2779 PHOLD(p);
2780 proc_getauxv(curthread, p, sb);
2781 PRELE(p);
2782 }
2783 }
2784
2785 static void
__elfN(note_procstat_kqueues)2786 __elfN(note_procstat_kqueues)(void *arg, struct sbuf *sb, size_t *sizep)
2787 {
2788 struct proc *p;
2789 size_t size, sect_sz, i;
2790 ssize_t start_len, sect_len;
2791 int structsize;
2792 bool compat32;
2793
2794 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2795 compat32 = true;
2796 structsize = sizeof(struct kinfo_knote32);
2797 #else
2798 compat32 = false;
2799 structsize = sizeof(struct kinfo_knote);
2800 #endif
2801 p = arg;
2802 if (sb == NULL) {
2803 size = 0;
2804 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2805 sbuf_set_drain(sb, sbuf_count_drain, &size);
2806 sbuf_bcat(sb, &structsize, sizeof(structsize));
2807 kern_proc_kqueues_out(p, sb, -1, compat32);
2808 sbuf_finish(sb);
2809 sbuf_delete(sb);
2810 *sizep = size;
2811 } else {
2812 sbuf_start_section(sb, &start_len);
2813
2814 sbuf_bcat(sb, &structsize, sizeof(structsize));
2815 kern_proc_kqueues_out(p, sb, *sizep - sizeof(structsize),
2816 compat32);
2817
2818 sect_len = sbuf_end_section(sb, start_len, 0, 0);
2819 if (sect_len < 0)
2820 return;
2821 sect_sz = sect_len;
2822
2823 KASSERT(sect_sz <= *sizep,
2824 ("kern_proc_kqueue_out did not respect maxlen; "
2825 "requested %zu, got %zu", *sizep - sizeof(structsize),
2826 sect_sz - sizeof(structsize)));
2827
2828 for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
2829 sbuf_putc(sb, 0);
2830 }
2831 }
2832
2833 #define MAX_NOTES_LOOP 4096
2834 bool
__elfN(parse_notes)2835 __elfN(parse_notes)(const struct image_params *imgp, const Elf_Note *checknote,
2836 const char *note_vendor, const Elf_Phdr *pnote,
2837 bool (*cb)(const Elf_Note *, void *, bool *), void *cb_arg)
2838 {
2839 const Elf_Note *note, *note0, *note_end;
2840 const char *note_name;
2841 char *buf;
2842 int i, error;
2843 bool res;
2844
2845 /* We need some limit, might as well use PAGE_SIZE. */
2846 if (pnote == NULL || pnote->p_filesz > PAGE_SIZE)
2847 return (false);
2848 ASSERT_VOP_LOCKED(imgp->vp, "parse_notes");
2849 if (pnote->p_offset > PAGE_SIZE ||
2850 pnote->p_filesz > PAGE_SIZE - pnote->p_offset) {
2851 buf = malloc(pnote->p_filesz, M_TEMP, M_NOWAIT);
2852 if (buf == NULL) {
2853 VOP_UNLOCK(imgp->vp);
2854 buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK);
2855 vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
2856 }
2857 error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz,
2858 pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED,
2859 imgp->td->td_ucred, NOCRED, NULL, imgp->td);
2860 if (error != 0) {
2861 uprintf("i/o error PT_NOTE\n");
2862 goto retf;
2863 }
2864 note = note0 = (const Elf_Note *)buf;
2865 note_end = (const Elf_Note *)(buf + pnote->p_filesz);
2866 } else {
2867 note = note0 = (const Elf_Note *)(imgp->image_header +
2868 pnote->p_offset);
2869 note_end = (const Elf_Note *)(imgp->image_header +
2870 pnote->p_offset + pnote->p_filesz);
2871 buf = NULL;
2872 }
2873 for (i = 0; i < MAX_NOTES_LOOP && note >= note0 && note < note_end;
2874 i++) {
2875 if (!aligned(note, Elf32_Addr)) {
2876 uprintf("Unaligned ELF note\n");
2877 goto retf;
2878 }
2879 if ((const char *)note_end - (const char *)note <
2880 sizeof(Elf_Note)) {
2881 uprintf("ELF note too short\n");
2882 goto retf;
2883 }
2884 if (note->n_namesz != checknote->n_namesz ||
2885 note->n_descsz != checknote->n_descsz ||
2886 note->n_type != checknote->n_type)
2887 goto nextnote;
2888 note_name = (const char *)(note + 1);
2889 if (note_name + roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
2890 note->n_descsz > (const char *)note_end ||
2891 strncmp(note_vendor, note_name, checknote->n_namesz) != 0)
2892 goto nextnote;
2893
2894 if (cb(note, cb_arg, &res))
2895 goto ret;
2896 nextnote:
2897 note = (const Elf_Note *)((const char *)(note + 1) +
2898 roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
2899 roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
2900 }
2901 if (i >= MAX_NOTES_LOOP)
2902 uprintf("ELF note parser reached %d notes\n", i);
2903 retf:
2904 res = false;
2905 ret:
2906 free(buf, M_TEMP);
2907 return (res);
2908 }
2909
2910 struct brandnote_cb_arg {
2911 const Elf_Brandnote *brandnote;
2912 int32_t *osrel;
2913 };
2914
2915 static bool
brandnote_cb(const Elf_Note * note,void * arg0,bool * res)2916 brandnote_cb(const Elf_Note *note, void *arg0, bool *res)
2917 {
2918 struct brandnote_cb_arg *arg;
2919
2920 arg = arg0;
2921
2922 /*
2923 * Fetch the osreldate for binary from the ELF OSABI-note if
2924 * necessary.
2925 */
2926 *res = (arg->brandnote->flags & BN_TRANSLATE_OSREL) != 0 &&
2927 arg->brandnote->trans_osrel != NULL ?
2928 arg->brandnote->trans_osrel(note, arg->osrel) : true;
2929
2930 return (true);
2931 }
2932
2933 static const Elf_Note fctl_note = {
2934 .n_namesz = sizeof(FREEBSD_ABI_VENDOR),
2935 .n_descsz = sizeof(uint32_t),
2936 .n_type = NT_FREEBSD_FEATURE_CTL,
2937 };
2938
2939 struct fctl_cb_arg {
2940 bool *has_fctl0;
2941 uint32_t *fctl0;
2942 };
2943
2944 static bool
note_fctl_cb(const Elf_Note * note,void * arg0,bool * res)2945 note_fctl_cb(const Elf_Note *note, void *arg0, bool *res)
2946 {
2947 struct fctl_cb_arg *arg;
2948 const Elf32_Word *desc;
2949 uintptr_t p;
2950
2951 arg = arg0;
2952 p = (uintptr_t)(note + 1);
2953 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
2954 desc = (const Elf32_Word *)p;
2955 *arg->has_fctl0 = true;
2956 *arg->fctl0 = desc[0];
2957 *res = true;
2958 return (true);
2959 }
2960
2961 /*
2962 * Try to find the appropriate ABI-note section for checknote, fetch
2963 * the osreldate and feature control flags for binary from the ELF
2964 * OSABI-note. Only the first page of the image is searched, the same
2965 * as for headers.
2966 */
2967 static bool
__elfN(check_note)2968 __elfN(check_note)(struct image_params *imgp, const Elf_Phdr *phdr,
2969 const Elf_Brandnote *brandnote, int32_t *osrel, bool *has_fctl0,
2970 uint32_t *fctl0)
2971 {
2972 const Elf_Ehdr *hdr;
2973 struct brandnote_cb_arg b_arg;
2974 struct fctl_cb_arg f_arg;
2975 int i, j;
2976
2977 hdr = (const Elf_Ehdr *)imgp->image_header;
2978 b_arg.brandnote = brandnote;
2979 b_arg.osrel = osrel;
2980 f_arg.has_fctl0 = has_fctl0;
2981 f_arg.fctl0 = fctl0;
2982
2983 for (i = 0; i < hdr->e_phnum; i++) {
2984 if (phdr[i].p_type == PT_NOTE && __elfN(parse_notes)(imgp,
2985 &brandnote->hdr, brandnote->vendor, &phdr[i], brandnote_cb,
2986 &b_arg)) {
2987 for (j = 0; j < hdr->e_phnum; j++) {
2988 if (phdr[j].p_type == PT_NOTE &&
2989 __elfN(parse_notes)(imgp, &fctl_note,
2990 FREEBSD_ABI_VENDOR, &phdr[j],
2991 note_fctl_cb, &f_arg))
2992 break;
2993 }
2994 return (true);
2995 }
2996 }
2997 return (false);
2998
2999 }
3000
3001 /*
3002 * Tell kern_execve.c about it, with a little help from the linker.
3003 */
3004 static struct execsw __elfN(execsw) = {
3005 .ex_imgact = __CONCAT(exec_, __elfN(imgact)),
3006 .ex_name = ELF_ABI_NAME
3007 };
3008 EXEC_SET(ELF_ABI_ID, __elfN(execsw));
3009
3010 static vm_prot_t
__elfN(trans_prot)3011 __elfN(trans_prot)(Elf_Word flags)
3012 {
3013 vm_prot_t prot;
3014
3015 prot = 0;
3016 if (flags & PF_X)
3017 prot |= VM_PROT_EXECUTE;
3018 if (flags & PF_W)
3019 prot |= VM_PROT_WRITE;
3020 if (flags & PF_R)
3021 prot |= VM_PROT_READ;
3022 #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__))
3023 if (i386_read_exec && (flags & PF_R))
3024 prot |= VM_PROT_EXECUTE;
3025 #endif
3026 return (prot);
3027 }
3028
3029 static Elf_Word
__elfN(untrans_prot)3030 __elfN(untrans_prot)(vm_prot_t prot)
3031 {
3032 Elf_Word flags;
3033
3034 flags = 0;
3035 if (prot & VM_PROT_EXECUTE)
3036 flags |= PF_X;
3037 if (prot & VM_PROT_READ)
3038 flags |= PF_R;
3039 if (prot & VM_PROT_WRITE)
3040 flags |= PF_W;
3041 return (flags);
3042 }
3043