Print this page
sync further changes from uts/aslr
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/exec/elf/elf.c
+++ new/usr/src/uts/common/exec/elf/elf.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 27 /* All Rights Reserved */
28 28 /*
29 29 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
30 30 */
31 31
32 32 #include <sys/types.h>
33 33 #include <sys/param.h>
34 34 #include <sys/thread.h>
35 35 #include <sys/sysmacros.h>
36 36 #include <sys/signal.h>
37 37 #include <sys/cred.h>
38 38 #include <sys/user.h>
39 39 #include <sys/errno.h>
40 40 #include <sys/vnode.h>
41 41 #include <sys/mman.h>
42 42 #include <sys/kmem.h>
43 43 #include <sys/proc.h>
44 44 #include <sys/pathname.h>
45 45 #include <sys/policy.h>
46 46 #include <sys/cmn_err.h>
47 47 #include <sys/systm.h>
48 48 #include <sys/elf.h>
49 49 #include <sys/vmsystm.h>
50 50 #include <sys/debug.h>
51 51 #include <sys/auxv.h>
52 52 #include <sys/exec.h>
53 53 #include <sys/prsystm.h>
54 54 #include <vm/as.h>
55 55 #include <vm/rm.h>
56 56 #include <vm/seg.h>
57 57 #include <vm/seg_vn.h>
58 58 #include <sys/modctl.h>
59 59 #include <sys/systeminfo.h>
60 60 #include <sys/vmparam.h>
61 61 #include <sys/machelf.h>
62 62 #include <sys/shm_impl.h>
63 63 #include <sys/archsystm.h>
64 64 #include <sys/fasttrap.h>
65 65 #include <sys/brand.h>
66 66 #include "elf_impl.h"
67 67 #include <sys/sdt.h>
68 68 #include <sys/siginfo.h>
69 69 #include <sys/random.h>
70 70
71 71 extern int at_flags;
72 72 extern volatile size_t aslr_max_brk_skew;
73 73
74 74 #define ORIGIN_STR "ORIGIN"
75 75 #define ORIGIN_STR_SIZE 6
76 76
77 77 static int getelfhead(vnode_t *, cred_t *, Ehdr *, int *, int *, int *);
78 78 static int getelfphdr(vnode_t *, cred_t *, const Ehdr *, int, caddr_t *,
79 79 ssize_t *);
80 80 static int getelfshdr(vnode_t *, cred_t *, const Ehdr *, int, int, caddr_t *,
81 81 ssize_t *, caddr_t *, ssize_t *);
82 82 static size_t elfsize(Ehdr *, int, caddr_t, uintptr_t *);
83 83 static int mapelfexec(vnode_t *, Ehdr *, int, caddr_t,
84 84 Phdr **, Phdr **, Phdr **, Phdr **, Phdr *,
85 85 caddr_t *, caddr_t *, intptr_t *, intptr_t *, size_t, long *, size_t *);
86 86
87 87 typedef enum {
88 88 STR_CTF,
89 89 STR_SYMTAB,
90 90 STR_DYNSYM,
91 91 STR_STRTAB,
92 92 STR_DYNSTR,
93 93 STR_SHSTRTAB,
94 94 STR_NUM
95 95 } shstrtype_t;
96 96
97 97 static const char *shstrtab_data[] = {
98 98 ".SUNW_ctf",
99 99 ".symtab",
100 100 ".dynsym",
101 101 ".strtab",
102 102 ".dynstr",
103 103 ".shstrtab"
104 104 };
105 105
106 106 typedef struct shstrtab {
107 107 int sst_ndx[STR_NUM];
108 108 int sst_cur;
109 109 } shstrtab_t;
110 110
111 111 static void
112 112 shstrtab_init(shstrtab_t *s)
113 113 {
114 114 bzero(&s->sst_ndx, sizeof (s->sst_ndx));
115 115 s->sst_cur = 1;
116 116 }
117 117
118 118 static int
119 119 shstrtab_ndx(shstrtab_t *s, shstrtype_t type)
120 120 {
121 121 int ret;
122 122
123 123 if ((ret = s->sst_ndx[type]) != 0)
124 124 return (ret);
125 125
126 126 ret = s->sst_ndx[type] = s->sst_cur;
127 127 s->sst_cur += strlen(shstrtab_data[type]) + 1;
128 128
129 129 return (ret);
130 130 }
131 131
132 132 static size_t
133 133 shstrtab_size(const shstrtab_t *s)
134 134 {
135 135 return (s->sst_cur);
136 136 }
137 137
138 138 static void
139 139 shstrtab_dump(const shstrtab_t *s, char *buf)
140 140 {
141 141 int i, ndx;
142 142
143 143 *buf = '\0';
144 144 for (i = 0; i < STR_NUM; i++) {
145 145 if ((ndx = s->sst_ndx[i]) != 0)
146 146 (void) strcpy(buf + ndx, shstrtab_data[i]);
147 147 }
148 148 }
149 149
150 150 static int
151 151 dtrace_safe_phdr(Phdr *phdrp, struct uarg *args, uintptr_t base)
152 152 {
153 153 ASSERT(phdrp->p_type == PT_SUNWDTRACE);
154 154
155 155 /*
156 156 * See the comment in fasttrap.h for information on how to safely
157 157 * update this program header.
158 158 */
159 159 if (phdrp->p_memsz < PT_SUNWDTRACE_SIZE ||
160 160 (phdrp->p_flags & (PF_R | PF_W | PF_X)) != (PF_R | PF_W | PF_X))
161 161 return (-1);
162 162
163 163 args->thrptr = phdrp->p_vaddr + base;
164 164
165 165 return (0);
166 166 }
167 167
168 168 static int
169 169 handle_secflag_dt(proc_t *p, uint_t dt, uint_t val)
170 170 {
171 171 uint_t flag;
172 172
173 173 switch (dt) {
174 174 case DT_SUNW_ASLR:
175 175 flag = PROC_SEC_ASLR;
176 176 break;
177 177 default:
↓ open down ↓ |
177 lines elided |
↑ open up ↑ |
178 178 return (EINVAL);
179 179 }
180 180
181 181 if (val == 0) {
182 182 if (secflag_isset(p->p_secflags.psf_lower, flag))
183 183 return (EPERM);
184 184 if ((secpolicy_psecflags(CRED(), p, p) != 0) &&
185 185 secflag_isset(p->p_secflags.psf_inherit, flag))
186 186 return (EPERM);
187 187
188 - secflag_clear(&p->p_secflags.psf_inherit, flag);
189 188 secflag_clear(&p->p_secflags.psf_effective, flag);
190 189 } else {
191 190 if (!secflag_isset(p->p_secflags.psf_upper, flag))
192 191 return (EPERM);
193 192
194 193 if ((secpolicy_psecflags(CRED(), p, p) != 0) &&
195 194 !secflag_isset(p->p_secflags.psf_inherit, flag))
196 195 return (EPERM);
197 196
198 - secflag_set(&p->p_secflags.psf_inherit, flag);
199 197 secflag_set(&p->p_secflags.psf_effective, flag);
200 198 }
201 199
202 200 return (0);
203 201 }
204 202
205 203 /*
206 204 * Map in the executable pointed to by vp. Returns 0 on success.
207 205 */
208 206 int
209 207 mapexec_brand(vnode_t *vp, uarg_t *args, Ehdr *ehdr, Addr *uphdr_vaddr,
210 208 intptr_t *voffset, caddr_t exec_file, int *interp, caddr_t *bssbase,
211 209 caddr_t *brkbase, size_t *brksize, uintptr_t *lddatap)
212 210 {
213 211 size_t len;
214 212 struct vattr vat;
215 213 caddr_t phdrbase = NULL;
216 214 ssize_t phdrsize;
217 215 int nshdrs, shstrndx, nphdrs;
218 216 int error = 0;
219 217 Phdr *uphdr = NULL;
220 218 Phdr *junk = NULL;
221 219 Phdr *dynphdr = NULL;
222 220 Phdr *dtrphdr = NULL;
223 221 uintptr_t lddata;
224 222 long execsz;
225 223 intptr_t minaddr;
226 224
227 225 if (lddatap != NULL)
228 226 *lddatap = NULL;
229 227
230 228 if (error = execpermissions(vp, &vat, args)) {
231 229 uprintf("%s: Cannot execute %s\n", exec_file, args->pathname);
232 230 return (error);
233 231 }
234 232
235 233 if ((error = getelfhead(vp, CRED(), ehdr, &nshdrs, &shstrndx,
236 234 &nphdrs)) != 0 ||
237 235 (error = getelfphdr(vp, CRED(), ehdr, nphdrs, &phdrbase,
238 236 &phdrsize)) != 0) {
239 237 uprintf("%s: Cannot read %s\n", exec_file, args->pathname);
240 238 return (error);
241 239 }
242 240
243 241 if ((len = elfsize(ehdr, nphdrs, phdrbase, &lddata)) == 0) {
244 242 uprintf("%s: Nothing to load in %s", exec_file, args->pathname);
245 243 kmem_free(phdrbase, phdrsize);
246 244 return (ENOEXEC);
247 245 }
248 246 if (lddatap != NULL)
249 247 *lddatap = lddata;
250 248
251 249 if (error = mapelfexec(vp, ehdr, nphdrs, phdrbase, &uphdr, &dynphdr,
252 250 &junk, &dtrphdr, NULL, bssbase, brkbase, voffset, &minaddr,
253 251 len, &execsz, brksize)) {
254 252 uprintf("%s: Cannot map %s\n", exec_file, args->pathname);
255 253 kmem_free(phdrbase, phdrsize);
256 254 return (error);
257 255 }
258 256
259 257 /*
260 258 * Inform our caller if the executable needs an interpreter.
261 259 */
262 260 *interp = (dynphdr == NULL) ? 0 : 1;
263 261
264 262 /*
265 263 * If this is a statically linked executable, voffset should indicate
266 264 * the address of the executable itself (it normally holds the address
267 265 * of the interpreter).
268 266 */
269 267 if (ehdr->e_type == ET_EXEC && *interp == 0)
270 268 *voffset = minaddr;
271 269
272 270 if (uphdr != NULL) {
273 271 *uphdr_vaddr = uphdr->p_vaddr;
274 272 } else {
275 273 *uphdr_vaddr = (Addr)-1;
276 274 }
277 275
278 276 kmem_free(phdrbase, phdrsize);
279 277 return (error);
280 278 }
281 279
282 280 /*ARGSUSED*/
283 281 int
284 282 elfexec(vnode_t *vp, execa_t *uap, uarg_t *args, intpdata_t *idatap,
285 283 int level, long *execsz, int setid, caddr_t exec_file, cred_t *cred,
286 284 int brand_action)
287 285 {
288 286 caddr_t phdrbase = NULL;
289 287 caddr_t bssbase = 0;
290 288 caddr_t brkbase = 0;
291 289 size_t brksize = 0;
292 290 ssize_t dlnsize;
293 291 aux_entry_t *aux;
294 292 int error;
295 293 ssize_t resid;
296 294 int fd = -1;
297 295 intptr_t voffset;
298 296 Phdr *intphdr = NULL;
299 297 Phdr *dynamicphdr = NULL;
300 298 Phdr *stphdr = NULL;
301 299 Phdr *uphdr = NULL;
302 300 Phdr *junk = NULL;
303 301 size_t len;
304 302 ssize_t phdrsize;
305 303 int postfixsize = 0;
306 304 int i, hsize;
307 305 Phdr *phdrp;
308 306 Phdr *dataphdrp = NULL;
309 307 Phdr *dtrphdr;
310 308 Phdr *capphdr = NULL;
311 309 Cap *cap = NULL;
312 310 ssize_t capsize;
313 311 Dyn *dyn = NULL;
314 312 int hasu = 0;
315 313 int hasauxv = 0;
316 314 int hasintp = 0;
317 315 int branded = 0;
318 316
319 317 struct proc *p = ttoproc(curthread);
320 318 struct user *up = PTOU(p);
321 319 struct bigwad {
322 320 Ehdr ehdr;
323 321 aux_entry_t elfargs[__KERN_NAUXV_IMPL];
324 322 char dl_name[MAXPATHLEN];
325 323 char pathbuf[MAXPATHLEN];
326 324 struct vattr vattr;
327 325 struct execenv exenv;
328 326 } *bigwad; /* kmem_alloc this behemoth so we don't blow stack */
329 327 Ehdr *ehdrp;
330 328 int nshdrs, shstrndx, nphdrs;
331 329 char *dlnp;
332 330 char *pathbufp;
333 331 rlim64_t limit;
334 332 rlim64_t roundlimit;
335 333
336 334 ASSERT(p->p_model == DATAMODEL_ILP32 || p->p_model == DATAMODEL_LP64);
337 335
338 336 bigwad = kmem_alloc(sizeof (struct bigwad), KM_SLEEP);
339 337 ehdrp = &bigwad->ehdr;
340 338 dlnp = bigwad->dl_name;
341 339 pathbufp = bigwad->pathbuf;
342 340
343 341 /*
344 342 * Obtain ELF and program header information.
345 343 */
346 344 if ((error = getelfhead(vp, CRED(), ehdrp, &nshdrs, &shstrndx,
347 345 &nphdrs)) != 0 ||
348 346 (error = getelfphdr(vp, CRED(), ehdrp, nphdrs, &phdrbase,
349 347 &phdrsize)) != 0)
350 348 goto out;
351 349
352 350 /*
353 351 * Prevent executing an ELF file that has no entry point.
354 352 */
355 353 if (ehdrp->e_entry == 0) {
356 354 uprintf("%s: Bad entry point\n", exec_file);
357 355 goto bad;
358 356 }
359 357
360 358 /*
361 359 * Put data model that we're exec-ing to into the args passed to
362 360 * exec_args(), so it will know what it is copying to on new stack.
363 361 * Now that we know whether we are exec-ing a 32-bit or 64-bit
364 362 * executable, we can set execsz with the appropriate NCARGS.
365 363 */
366 364 #ifdef _LP64
367 365 if (ehdrp->e_ident[EI_CLASS] == ELFCLASS32) {
368 366 args->to_model = DATAMODEL_ILP32;
369 367 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS32-1);
370 368 } else {
371 369 args->to_model = DATAMODEL_LP64;
372 370 args->stk_prot &= ~PROT_EXEC;
373 371 #if defined(__i386) || defined(__amd64)
374 372 args->dat_prot &= ~PROT_EXEC;
375 373 #endif
376 374 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS64-1);
377 375 }
378 376 #else /* _LP64 */
379 377 args->to_model = DATAMODEL_ILP32;
380 378 *execsz = btopr(SINCR) + btopr(SSIZE) + btopr(NCARGS-1);
381 379 #endif /* _LP64 */
382 380
383 381 /*
384 382 * We delay invoking the brand callback until we've figured out
385 383 * what kind of elf binary we're trying to run, 32-bit or 64-bit.
386 384 * We do this because now the brand library can just check
387 385 * args->to_model to see if the target is 32-bit or 64-bit without
388 386 * having do duplicate all the code above.
389 387 *
390 388 * The level checks associated with brand handling below are used to
391 389 * prevent a loop since the brand elfexec function typically comes back
392 390 * through this function. We must check <= here since the nested
393 391 * handling in the #! interpreter code will increment the level before
394 392 * calling gexec to run the final elfexec interpreter.
395 393 */
396 394 if ((level <= INTP_MAXDEPTH) &&
397 395 (brand_action != EBA_NATIVE) && (PROC_IS_BRANDED(p))) {
398 396 error = BROP(p)->b_elfexec(vp, uap, args,
399 397 idatap, level + 1, execsz, setid, exec_file, cred,
400 398 brand_action);
401 399 goto out;
402 400 }
403 401
404 402 /*
405 403 * Determine aux size now so that stack can be built
406 404 * in one shot (except actual copyout of aux image),
407 405 * determine any non-default stack protections,
408 406 * and still have this code be machine independent.
409 407 */
410 408 hsize = ehdrp->e_phentsize;
411 409 phdrp = (Phdr *)phdrbase;
412 410 for (i = nphdrs; i > 0; i--) {
413 411 switch (phdrp->p_type) {
414 412 case PT_INTERP:
415 413 hasauxv = hasintp = 1;
416 414 break;
417 415 case PT_PHDR:
418 416 hasu = 1;
419 417 break;
420 418 case PT_SUNWSTACK:
421 419 args->stk_prot = PROT_USER;
422 420 if (phdrp->p_flags & PF_R)
423 421 args->stk_prot |= PROT_READ;
424 422 if (phdrp->p_flags & PF_W)
425 423 args->stk_prot |= PROT_WRITE;
426 424 if (phdrp->p_flags & PF_X)
427 425 args->stk_prot |= PROT_EXEC;
428 426 break;
429 427 case PT_LOAD:
430 428 dataphdrp = phdrp;
431 429 break;
432 430 case PT_SUNWCAP:
433 431 capphdr = phdrp;
434 432 break;
435 433 case PT_DYNAMIC:
436 434 dynamicphdr = phdrp;
437 435 break;
438 436 }
439 437 phdrp = (Phdr *)((caddr_t)phdrp + hsize);
440 438 }
441 439
442 440 if (ehdrp->e_type != ET_EXEC) {
443 441 dataphdrp = NULL;
444 442 hasauxv = 1;
445 443 }
446 444
447 445 /* Copy BSS permissions to args->dat_prot */
448 446 if (dataphdrp != NULL) {
449 447 args->dat_prot = PROT_USER;
450 448 if (dataphdrp->p_flags & PF_R)
451 449 args->dat_prot |= PROT_READ;
452 450 if (dataphdrp->p_flags & PF_W)
453 451 args->dat_prot |= PROT_WRITE;
454 452 if (dataphdrp->p_flags & PF_X)
455 453 args->dat_prot |= PROT_EXEC;
456 454 }
457 455
458 456 /*
459 457 * If a auxvector will be required - reserve the space for
460 458 * it now. This may be increased by exec_args if there are
461 459 * ISA-specific types (included in __KERN_NAUXV_IMPL).
462 460 */
463 461 if (hasauxv) {
464 462 /*
465 463 * If a AUX vector is being built - the base AUX
466 464 * entries are:
467 465 *
468 466 * AT_BASE
469 467 * AT_FLAGS
470 468 * AT_PAGESZ
471 469 * AT_SUN_AUXFLAGS
472 470 * AT_SUN_HWCAP
473 471 * AT_SUN_HWCAP2
474 472 * AT_SUN_PLATFORM (added in stk_copyout)
475 473 * AT_SUN_EXECNAME (added in stk_copyout)
476 474 * AT_NULL
477 475 *
478 476 * total == 9
479 477 */
480 478 if (hasintp && hasu) {
481 479 /*
482 480 * Has PT_INTERP & PT_PHDR - the auxvectors that
483 481 * will be built are:
484 482 *
485 483 * AT_PHDR
486 484 * AT_PHENT
487 485 * AT_PHNUM
488 486 * AT_ENTRY
489 487 * AT_LDDATA
490 488 *
491 489 * total = 5
492 490 */
493 491 args->auxsize = (9 + 5) * sizeof (aux_entry_t);
494 492 } else if (hasintp) {
495 493 /*
496 494 * Has PT_INTERP but no PT_PHDR
497 495 *
498 496 * AT_EXECFD
499 497 * AT_LDDATA
500 498 *
501 499 * total = 2
502 500 */
503 501 args->auxsize = (9 + 2) * sizeof (aux_entry_t);
504 502 } else {
505 503 args->auxsize = 9 * sizeof (aux_entry_t);
506 504 }
507 505 } else {
508 506 args->auxsize = 0;
509 507 }
510 508
511 509 /*
512 510 * If this binary is using an emulator, we need to add an
513 511 * AT_SUN_EMULATOR aux entry.
514 512 */
515 513 if (args->emulator != NULL)
516 514 args->auxsize += sizeof (aux_entry_t);
517 515
518 516 if ((brand_action != EBA_NATIVE) && (PROC_IS_BRANDED(p))) {
519 517 branded = 1;
520 518 /*
521 519 * We will be adding 4 entries to the aux vectors. One for
522 520 * the the brandname and 3 for the brand specific aux vectors.
523 521 */
524 522 args->auxsize += 4 * sizeof (aux_entry_t);
525 523 }
526 524
527 525 /* If the binary has an explicit ASLR flag, it must be honoured */
528 526 if ((dynamicphdr != NULL) &&
529 527 (dynamicphdr->p_filesz > 0)) {
530 528 Dyn *dp;
531 529 off_t i = 0;
532 530
533 531 #define DYN_STRIDE 100
534 532 for (i = 0; i < dynamicphdr->p_filesz;
535 533 i += sizeof (*dyn) * DYN_STRIDE) {
536 534 int ndyns = (dynamicphdr->p_filesz - i) / sizeof (*dyn);
537 535 size_t dynsize;
538 536
539 537 ndyns = MIN(DYN_STRIDE, ndyns);
540 538 dynsize = ndyns * sizeof (*dyn);
541 539
542 540 dyn = kmem_alloc(dynsize, KM_SLEEP);
543 541
544 542 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)dyn,
545 543 dynsize, (offset_t)(dynamicphdr->p_offset + i),
546 544 UIO_SYSSPACE, 0, (rlim64_t)0,
547 545 CRED(), &resid)) != 0) {
548 546 uprintf("%s: cannot read .dynamic section\n",
549 547 exec_file);
550 548 goto out;
551 549 }
552 550
553 551 for (dp = dyn; dp < (dyn + ndyns); dp++) {
554 552 if (dp->d_tag == DT_SUNW_ASLR) {
555 553 if ((error = handle_secflag_dt(p,
556 554 DT_SUNW_ASLR,
557 555 dp->d_un.d_val)) != 0) {
558 556 uprintf("%s: error setting "
559 557 "security-flag from "
560 558 "DT_SUNW_ASLR: %d\n",
561 559 exec_file, error);
562 560 goto out;
563 561 }
564 562 }
565 563 }
566 564
567 565 kmem_free(dyn, dynsize);
568 566 }
569 567 }
570 568
571 569 /* Hardware/Software capabilities */
572 570 if (capphdr != NULL &&
573 571 (capsize = capphdr->p_filesz) > 0 &&
574 572 capsize <= 16 * sizeof (*cap)) {
575 573 int ncaps = capsize / sizeof (*cap);
576 574 Cap *cp;
577 575
578 576 cap = kmem_alloc(capsize, KM_SLEEP);
579 577 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)cap,
580 578 capsize, (offset_t)capphdr->p_offset,
581 579 UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid)) != 0) {
582 580 uprintf("%s: Cannot read capabilities section\n",
583 581 exec_file);
584 582 goto out;
585 583 }
586 584 for (cp = cap; cp < cap + ncaps; cp++) {
587 585 if (cp->c_tag == CA_SUNW_SF_1 &&
588 586 (cp->c_un.c_val & SF1_SUNW_ADDR32)) {
589 587 if (args->to_model == DATAMODEL_LP64)
590 588 args->addr32 = 1;
591 589 break;
592 590 }
593 591 }
594 592 }
595 593
596 594 aux = bigwad->elfargs;
597 595 /*
598 596 * Move args to the user's stack.
599 597 * This can fill in the AT_SUN_PLATFORM and AT_SUN_EXECNAME aux entries.
600 598 */
601 599 if ((error = exec_args(uap, args, idatap, (void **)&aux)) != 0) {
602 600 if (error == -1) {
603 601 error = ENOEXEC;
604 602 goto bad;
605 603 }
606 604 goto out;
607 605 }
608 606 /* we're single threaded after this point */
609 607
610 608 /*
611 609 * If this is an ET_DYN executable (shared object),
612 610 * determine its memory size so that mapelfexec() can load it.
613 611 */
614 612 if (ehdrp->e_type == ET_DYN)
615 613 len = elfsize(ehdrp, nphdrs, phdrbase, NULL);
616 614 else
617 615 len = 0;
618 616
619 617 dtrphdr = NULL;
620 618
621 619 if ((error = mapelfexec(vp, ehdrp, nphdrs, phdrbase, &uphdr, &intphdr,
622 620 &stphdr, &dtrphdr, dataphdrp, &bssbase, &brkbase, &voffset, NULL,
623 621 len, execsz, &brksize)) != 0)
624 622 goto bad;
625 623
626 624 if (uphdr != NULL && intphdr == NULL)
627 625 goto bad;
628 626
629 627 if (dtrphdr != NULL && dtrace_safe_phdr(dtrphdr, args, voffset) != 0) {
630 628 uprintf("%s: Bad DTrace phdr in %s\n", exec_file, exec_file);
631 629 goto bad;
632 630 }
633 631
634 632 if (intphdr != NULL) {
635 633 size_t len;
636 634 uintptr_t lddata;
637 635 char *p;
638 636 struct vnode *nvp;
639 637
640 638 dlnsize = intphdr->p_filesz;
641 639
642 640 if (dlnsize > MAXPATHLEN || dlnsize <= 0)
643 641 goto bad;
644 642
645 643 /*
646 644 * Read in "interpreter" pathname.
647 645 */
648 646 if ((error = vn_rdwr(UIO_READ, vp, dlnp, intphdr->p_filesz,
649 647 (offset_t)intphdr->p_offset, UIO_SYSSPACE, 0, (rlim64_t)0,
650 648 CRED(), &resid)) != 0) {
651 649 uprintf("%s: Cannot obtain interpreter pathname\n",
652 650 exec_file);
653 651 goto bad;
654 652 }
655 653
656 654 if (resid != 0 || dlnp[dlnsize - 1] != '\0')
657 655 goto bad;
658 656
659 657 /*
660 658 * Search for '$ORIGIN' token in interpreter path.
661 659 * If found, expand it.
662 660 */
663 661 for (p = dlnp; p = strchr(p, '$'); ) {
664 662 uint_t len, curlen;
665 663 char *_ptr;
666 664
667 665 if (strncmp(++p, ORIGIN_STR, ORIGIN_STR_SIZE))
668 666 continue;
669 667
670 668 /*
671 669 * We don't support $ORIGIN on setid programs to close
672 670 * a potential attack vector.
673 671 */
674 672 if ((setid & EXECSETID_SETID) != 0) {
675 673 error = ENOEXEC;
676 674 goto bad;
677 675 }
678 676
679 677 curlen = 0;
680 678 len = p - dlnp - 1;
681 679 if (len) {
682 680 bcopy(dlnp, pathbufp, len);
683 681 curlen += len;
684 682 }
685 683 if (_ptr = strrchr(args->pathname, '/')) {
686 684 len = _ptr - args->pathname;
687 685 if ((curlen + len) > MAXPATHLEN)
688 686 break;
689 687
690 688 bcopy(args->pathname, &pathbufp[curlen], len);
691 689 curlen += len;
692 690 } else {
693 691 /*
694 692 * executable is a basename found in the
695 693 * current directory. So - just substitue
696 694 * '.' for ORIGIN.
697 695 */
698 696 pathbufp[curlen] = '.';
699 697 curlen++;
700 698 }
701 699 p += ORIGIN_STR_SIZE;
702 700 len = strlen(p);
703 701
704 702 if ((curlen + len) > MAXPATHLEN)
705 703 break;
706 704 bcopy(p, &pathbufp[curlen], len);
707 705 curlen += len;
708 706 pathbufp[curlen++] = '\0';
709 707 bcopy(pathbufp, dlnp, curlen);
710 708 }
711 709
712 710 /*
713 711 * /usr/lib/ld.so.1 is known to be a symlink to /lib/ld.so.1
714 712 * (and /usr/lib/64/ld.so.1 is a symlink to /lib/64/ld.so.1).
715 713 * Just in case /usr is not mounted, change it now.
716 714 */
717 715 if (strcmp(dlnp, USR_LIB_RTLD) == 0)
718 716 dlnp += 4;
719 717 error = lookupname(dlnp, UIO_SYSSPACE, FOLLOW, NULLVPP, &nvp);
720 718 if (error && dlnp != bigwad->dl_name) {
721 719 /* new kernel, old user-level */
722 720 error = lookupname(dlnp -= 4, UIO_SYSSPACE, FOLLOW,
723 721 NULLVPP, &nvp);
724 722 }
725 723 if (error) {
726 724 uprintf("%s: Cannot find %s\n", exec_file, dlnp);
727 725 goto bad;
728 726 }
729 727
730 728 /*
731 729 * Setup the "aux" vector.
732 730 */
733 731 if (uphdr) {
734 732 if (ehdrp->e_type == ET_DYN) {
735 733 /* don't use the first page */
736 734 bigwad->exenv.ex_brkbase = (caddr_t)PAGESIZE;
737 735 bigwad->exenv.ex_bssbase = (caddr_t)PAGESIZE;
738 736 } else {
739 737 bigwad->exenv.ex_bssbase = bssbase;
740 738 bigwad->exenv.ex_brkbase = brkbase;
741 739 }
742 740 bigwad->exenv.ex_brksize = brksize;
743 741 bigwad->exenv.ex_magic = elfmagic;
744 742 bigwad->exenv.ex_vp = vp;
745 743 setexecenv(&bigwad->exenv);
746 744
747 745 ADDAUX(aux, AT_PHDR, uphdr->p_vaddr + voffset)
748 746 ADDAUX(aux, AT_PHENT, ehdrp->e_phentsize)
749 747 ADDAUX(aux, AT_PHNUM, nphdrs)
750 748 ADDAUX(aux, AT_ENTRY, ehdrp->e_entry + voffset)
751 749 } else {
752 750 if ((error = execopen(&vp, &fd)) != 0) {
753 751 VN_RELE(nvp);
754 752 goto bad;
755 753 }
756 754
757 755 ADDAUX(aux, AT_EXECFD, fd)
758 756 }
759 757
760 758 if ((error = execpermissions(nvp, &bigwad->vattr, args)) != 0) {
761 759 VN_RELE(nvp);
762 760 uprintf("%s: Cannot execute %s\n", exec_file, dlnp);
763 761 goto bad;
764 762 }
765 763
766 764 /*
767 765 * Now obtain the ELF header along with the entire program
768 766 * header contained in "nvp".
769 767 */
770 768 kmem_free(phdrbase, phdrsize);
771 769 phdrbase = NULL;
772 770 if ((error = getelfhead(nvp, CRED(), ehdrp, &nshdrs,
773 771 &shstrndx, &nphdrs)) != 0 ||
774 772 (error = getelfphdr(nvp, CRED(), ehdrp, nphdrs, &phdrbase,
775 773 &phdrsize)) != 0) {
776 774 VN_RELE(nvp);
777 775 uprintf("%s: Cannot read %s\n", exec_file, dlnp);
778 776 goto bad;
779 777 }
780 778
781 779 /*
782 780 * Determine memory size of the "interpreter's" loadable
783 781 * sections. This size is then used to obtain the virtual
784 782 * address of a hole, in the user's address space, large
785 783 * enough to map the "interpreter".
786 784 */
787 785 if ((len = elfsize(ehdrp, nphdrs, phdrbase, &lddata)) == 0) {
788 786 VN_RELE(nvp);
789 787 uprintf("%s: Nothing to load in %s\n", exec_file, dlnp);
790 788 goto bad;
791 789 }
792 790
793 791 dtrphdr = NULL;
794 792
795 793 error = mapelfexec(nvp, ehdrp, nphdrs, phdrbase, &junk, &junk,
796 794 &junk, &dtrphdr, NULL, NULL, NULL, &voffset, NULL, len,
797 795 execsz, NULL);
798 796 if (error || junk != NULL) {
799 797 VN_RELE(nvp);
800 798 uprintf("%s: Cannot map %s\n", exec_file, dlnp);
801 799 goto bad;
802 800 }
803 801
804 802 /*
805 803 * We use the DTrace program header to initialize the
806 804 * architecture-specific user per-LWP location. The dtrace
807 805 * fasttrap provider requires ready access to per-LWP scratch
808 806 * space. We assume that there is only one such program header
809 807 * in the interpreter.
810 808 */
811 809 if (dtrphdr != NULL &&
812 810 dtrace_safe_phdr(dtrphdr, args, voffset) != 0) {
813 811 VN_RELE(nvp);
814 812 uprintf("%s: Bad DTrace phdr in %s\n", exec_file, dlnp);
815 813 goto bad;
816 814 }
817 815
818 816 VN_RELE(nvp);
819 817 ADDAUX(aux, AT_SUN_LDDATA, voffset + lddata)
820 818 }
821 819
822 820 if (hasauxv) {
823 821 int auxf = AF_SUN_HWCAPVERIFY;
824 822 /*
825 823 * Note: AT_SUN_PLATFORM and AT_SUN_EXECNAME were filled in via
826 824 * exec_args()
827 825 */
828 826 ADDAUX(aux, AT_BASE, voffset)
829 827 ADDAUX(aux, AT_FLAGS, at_flags)
830 828 ADDAUX(aux, AT_PAGESZ, PAGESIZE)
831 829 /*
832 830 * Linker flags. (security)
833 831 * p_flag not yet set at this time.
834 832 * We rely on gexec() to provide us with the information.
835 833 * If the application is set-uid but this is not reflected
836 834 * in a mismatch between real/effective uids/gids, then
837 835 * don't treat this as a set-uid exec. So we care about
838 836 * the EXECSETID_UGIDS flag but not the ...SETID flag.
839 837 */
840 838 if ((setid &= ~EXECSETID_SETID) != 0)
841 839 auxf |= AF_SUN_SETUGID;
842 840
843 841 /*
844 842 * If we're running a native process from within a branded
845 843 * zone under pfexec then we clear the AF_SUN_SETUGID flag so
846 844 * that the native ld.so.1 is able to link with the native
847 845 * libraries instead of using the brand libraries that are
848 846 * installed in the zone. We only do this for processes
849 847 * which we trust because we see they are already running
850 848 * under pfexec (where uid != euid). This prevents a
851 849 * malicious user within the zone from crafting a wrapper to
852 850 * run native suid commands with unsecure libraries interposed.
853 851 */
854 852 if ((brand_action == EBA_NATIVE) && (PROC_IS_BRANDED(p) &&
855 853 (setid &= ~EXECSETID_SETID) != 0))
856 854 auxf &= ~AF_SUN_SETUGID;
857 855
858 856 /*
859 857 * Record the user addr of the auxflags aux vector entry
860 858 * since brands may optionally want to manipulate this field.
861 859 */
862 860 args->auxp_auxflags =
863 861 (char *)((char *)args->stackend +
864 862 ((char *)&aux->a_type -
865 863 (char *)bigwad->elfargs));
866 864 ADDAUX(aux, AT_SUN_AUXFLAGS, auxf);
867 865
868 866 /*
869 867 * Hardware capability flag word (performance hints)
870 868 * Used for choosing faster library routines.
871 869 * (Potentially different between 32-bit and 64-bit ABIs)
872 870 */
873 871 #if defined(_LP64)
874 872 if (args->to_model == DATAMODEL_NATIVE) {
875 873 ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap)
876 874 ADDAUX(aux, AT_SUN_HWCAP2, auxv_hwcap_2)
877 875 } else {
878 876 ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap32)
879 877 ADDAUX(aux, AT_SUN_HWCAP2, auxv_hwcap32_2)
880 878 }
881 879 #else
882 880 ADDAUX(aux, AT_SUN_HWCAP, auxv_hwcap)
883 881 ADDAUX(aux, AT_SUN_HWCAP2, auxv_hwcap_2)
884 882 #endif
885 883 if (branded) {
886 884 /*
887 885 * Reserve space for the brand-private aux vectors,
888 886 * and record the user addr of that space.
889 887 */
890 888 args->auxp_brand =
891 889 (char *)((char *)args->stackend +
892 890 ((char *)&aux->a_type -
893 891 (char *)bigwad->elfargs));
894 892 ADDAUX(aux, AT_SUN_BRAND_AUX1, 0)
895 893 ADDAUX(aux, AT_SUN_BRAND_AUX2, 0)
896 894 ADDAUX(aux, AT_SUN_BRAND_AUX3, 0)
897 895 }
898 896
899 897 ADDAUX(aux, AT_NULL, 0)
900 898 postfixsize = (char *)aux - (char *)bigwad->elfargs;
901 899
902 900 /*
903 901 * We make assumptions above when we determine how many aux
904 902 * vector entries we will be adding. However, if we have an
905 903 * invalid elf file, it is possible that mapelfexec might
906 904 * behave differently (but not return an error), in which case
907 905 * the number of aux entries we actually add will be different.
908 906 * We detect that now and error out.
909 907 */
910 908 if (postfixsize != args->auxsize) {
911 909 DTRACE_PROBE2(elfexec_badaux, int, postfixsize,
912 910 int, args->auxsize);
913 911 goto bad;
914 912 }
915 913 ASSERT(postfixsize <= __KERN_NAUXV_IMPL * sizeof (aux_entry_t));
916 914 }
917 915
918 916 /*
919 917 * For the 64-bit kernel, the limit is big enough that rounding it up
920 918 * to a page can overflow the 64-bit limit, so we check for btopr()
921 919 * overflowing here by comparing it with the unrounded limit in pages.
922 920 * If it hasn't overflowed, compare the exec size with the rounded up
923 921 * limit in pages. Otherwise, just compare with the unrounded limit.
924 922 */
925 923 limit = btop(p->p_vmem_ctl);
926 924 roundlimit = btopr(p->p_vmem_ctl);
927 925 if ((roundlimit > limit && *execsz > roundlimit) ||
928 926 (roundlimit < limit && *execsz > limit)) {
929 927 mutex_enter(&p->p_lock);
930 928 (void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p,
931 929 RCA_SAFE);
932 930 mutex_exit(&p->p_lock);
933 931 error = ENOMEM;
934 932 goto bad;
935 933 }
936 934
937 935 bzero(up->u_auxv, sizeof (up->u_auxv));
938 936 if (postfixsize) {
939 937 int num_auxv;
940 938
941 939 /*
942 940 * Copy the aux vector to the user stack.
943 941 */
944 942 error = execpoststack(args, bigwad->elfargs, postfixsize);
945 943 if (error)
946 944 goto bad;
947 945
948 946 /*
949 947 * Copy auxv to the process's user structure for use by /proc.
950 948 * If this is a branded process, the brand's exec routine will
951 949 * copy it's private entries to the user structure later. It
952 950 * relies on the fact that the blank entries are at the end.
953 951 */
954 952 num_auxv = postfixsize / sizeof (aux_entry_t);
955 953 ASSERT(num_auxv <= sizeof (up->u_auxv) / sizeof (auxv_t));
956 954 aux = bigwad->elfargs;
957 955 for (i = 0; i < num_auxv; i++) {
958 956 up->u_auxv[i].a_type = aux[i].a_type;
959 957 up->u_auxv[i].a_un.a_val = (aux_val_t)aux[i].a_un.a_val;
960 958 }
961 959 }
962 960
963 961 /*
964 962 * Pass back the starting address so we can set the program counter.
965 963 */
966 964 args->entry = (uintptr_t)(ehdrp->e_entry + voffset);
967 965
968 966 if (!uphdr) {
969 967 if (ehdrp->e_type == ET_DYN) {
970 968 /*
971 969 * If we are executing a shared library which doesn't
972 970 * have a interpreter (probably ld.so.1) then
973 971 * we don't set the brkbase now. Instead we
974 972 * delay it's setting until the first call
975 973 * via grow.c::brk(). This permits ld.so.1 to
976 974 * initialize brkbase to the tail of the executable it
977 975 * loads (which is where it needs to be).
978 976 */
979 977 bigwad->exenv.ex_brkbase = (caddr_t)0;
980 978 bigwad->exenv.ex_bssbase = (caddr_t)0;
981 979 bigwad->exenv.ex_brksize = 0;
982 980 } else {
983 981 bigwad->exenv.ex_brkbase = brkbase;
984 982 bigwad->exenv.ex_bssbase = bssbase;
985 983 bigwad->exenv.ex_brksize = brksize;
986 984 }
987 985 bigwad->exenv.ex_magic = elfmagic;
988 986 bigwad->exenv.ex_vp = vp;
989 987 setexecenv(&bigwad->exenv);
990 988 }
991 989
992 990 ASSERT(error == 0);
993 991 goto out;
994 992
995 993 bad:
996 994 if (fd != -1) /* did we open the a.out yet */
997 995 (void) execclose(fd);
998 996
999 997 psignal(p, SIGKILL);
1000 998
1001 999 if (error == 0)
1002 1000 error = ENOEXEC;
1003 1001 out:
1004 1002 if (phdrbase != NULL)
1005 1003 kmem_free(phdrbase, phdrsize);
1006 1004 if (cap != NULL)
1007 1005 kmem_free(cap, capsize);
1008 1006 kmem_free(bigwad, sizeof (struct bigwad));
1009 1007 return (error);
1010 1008 }
1011 1009
1012 1010 /*
1013 1011 * Compute the memory size requirement for the ELF file.
1014 1012 */
1015 1013 static size_t
1016 1014 elfsize(Ehdr *ehdrp, int nphdrs, caddr_t phdrbase, uintptr_t *lddata)
1017 1015 {
1018 1016 size_t len;
1019 1017 Phdr *phdrp = (Phdr *)phdrbase;
1020 1018 int hsize = ehdrp->e_phentsize;
1021 1019 int first = 1;
1022 1020 int dfirst = 1; /* first data segment */
1023 1021 uintptr_t loaddr = 0;
1024 1022 uintptr_t hiaddr = 0;
1025 1023 uintptr_t lo, hi;
1026 1024 int i;
1027 1025
1028 1026 for (i = nphdrs; i > 0; i--) {
1029 1027 if (phdrp->p_type == PT_LOAD) {
1030 1028 lo = phdrp->p_vaddr;
1031 1029 hi = lo + phdrp->p_memsz;
1032 1030 if (first) {
1033 1031 loaddr = lo;
1034 1032 hiaddr = hi;
1035 1033 first = 0;
1036 1034 } else {
1037 1035 if (loaddr > lo)
1038 1036 loaddr = lo;
1039 1037 if (hiaddr < hi)
1040 1038 hiaddr = hi;
1041 1039 }
1042 1040
1043 1041 /*
1044 1042 * save the address of the first data segment
1045 1043 * of a object - used for the AT_SUNW_LDDATA
1046 1044 * aux entry.
1047 1045 */
1048 1046 if ((lddata != NULL) && dfirst &&
1049 1047 (phdrp->p_flags & PF_W)) {
1050 1048 *lddata = lo;
1051 1049 dfirst = 0;
1052 1050 }
1053 1051 }
1054 1052 phdrp = (Phdr *)((caddr_t)phdrp + hsize);
1055 1053 }
1056 1054
1057 1055 len = hiaddr - (loaddr & PAGEMASK);
1058 1056 len = roundup(len, PAGESIZE);
1059 1057
1060 1058 return (len);
1061 1059 }
1062 1060
1063 1061 /*
1064 1062 * Read in the ELF header and program header table.
1065 1063 * SUSV3 requires:
1066 1064 * ENOEXEC File format is not recognized
1067 1065 * EINVAL Format recognized but execution not supported
1068 1066 */
1069 1067 static int
1070 1068 getelfhead(vnode_t *vp, cred_t *credp, Ehdr *ehdr, int *nshdrs, int *shstrndx,
1071 1069 int *nphdrs)
1072 1070 {
1073 1071 int error;
1074 1072 ssize_t resid;
1075 1073
1076 1074 /*
1077 1075 * We got here by the first two bytes in ident,
1078 1076 * now read the entire ELF header.
1079 1077 */
1080 1078 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)ehdr,
1081 1079 sizeof (Ehdr), (offset_t)0, UIO_SYSSPACE, 0,
1082 1080 (rlim64_t)0, credp, &resid)) != 0)
1083 1081 return (error);
1084 1082
1085 1083 /*
1086 1084 * Since a separate version is compiled for handling 32-bit and
1087 1085 * 64-bit ELF executables on a 64-bit kernel, the 64-bit version
1088 1086 * doesn't need to be able to deal with 32-bit ELF files.
1089 1087 */
1090 1088 if (resid != 0 ||
1091 1089 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1092 1090 ehdr->e_ident[EI_MAG3] != ELFMAG3)
1093 1091 return (ENOEXEC);
1094 1092
1095 1093 if ((ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) ||
1096 1094 #if defined(_ILP32) || defined(_ELF32_COMPAT)
1097 1095 ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
1098 1096 #else
1099 1097 ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
1100 1098 #endif
1101 1099 !elfheadcheck(ehdr->e_ident[EI_DATA], ehdr->e_machine,
1102 1100 ehdr->e_flags))
1103 1101 return (EINVAL);
1104 1102
1105 1103 *nshdrs = ehdr->e_shnum;
1106 1104 *shstrndx = ehdr->e_shstrndx;
1107 1105 *nphdrs = ehdr->e_phnum;
1108 1106
1109 1107 /*
1110 1108 * If e_shnum, e_shstrndx, or e_phnum is its sentinel value, we need
1111 1109 * to read in the section header at index zero to acces the true
1112 1110 * values for those fields.
1113 1111 */
1114 1112 if ((*nshdrs == 0 && ehdr->e_shoff != 0) ||
1115 1113 *shstrndx == SHN_XINDEX || *nphdrs == PN_XNUM) {
1116 1114 Shdr shdr;
1117 1115
1118 1116 if (ehdr->e_shoff == 0)
1119 1117 return (EINVAL);
1120 1118
1121 1119 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)&shdr,
1122 1120 sizeof (shdr), (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0,
1123 1121 (rlim64_t)0, credp, &resid)) != 0)
1124 1122 return (error);
1125 1123
1126 1124 if (*nshdrs == 0)
1127 1125 *nshdrs = shdr.sh_size;
1128 1126 if (*shstrndx == SHN_XINDEX)
1129 1127 *shstrndx = shdr.sh_link;
1130 1128 if (*nphdrs == PN_XNUM && shdr.sh_info != 0)
1131 1129 *nphdrs = shdr.sh_info;
1132 1130 }
1133 1131
1134 1132 return (0);
1135 1133 }
1136 1134
1137 1135 #ifdef _ELF32_COMPAT
1138 1136 extern size_t elf_nphdr_max;
1139 1137 #else
1140 1138 size_t elf_nphdr_max = 1000;
1141 1139 #endif
1142 1140
1143 1141 static int
1144 1142 getelfphdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr, int nphdrs,
1145 1143 caddr_t *phbasep, ssize_t *phsizep)
1146 1144 {
1147 1145 ssize_t resid, minsize;
1148 1146 int err;
1149 1147
1150 1148 /*
1151 1149 * Since we're going to be using e_phentsize to iterate down the
1152 1150 * array of program headers, it must be 8-byte aligned or else
1153 1151 * a we might cause a misaligned access. We use all members through
1154 1152 * p_flags on 32-bit ELF files and p_memsz on 64-bit ELF files so
1155 1153 * e_phentsize must be at least large enough to include those
1156 1154 * members.
1157 1155 */
1158 1156 #if !defined(_LP64) || defined(_ELF32_COMPAT)
1159 1157 minsize = offsetof(Phdr, p_flags) + sizeof (((Phdr *)NULL)->p_flags);
1160 1158 #else
1161 1159 minsize = offsetof(Phdr, p_memsz) + sizeof (((Phdr *)NULL)->p_memsz);
1162 1160 #endif
1163 1161 if (ehdr->e_phentsize < minsize || (ehdr->e_phentsize & 3))
1164 1162 return (EINVAL);
1165 1163
1166 1164 *phsizep = nphdrs * ehdr->e_phentsize;
1167 1165
1168 1166 if (*phsizep > sizeof (Phdr) * elf_nphdr_max) {
1169 1167 if ((*phbasep = kmem_alloc(*phsizep, KM_NOSLEEP)) == NULL)
1170 1168 return (ENOMEM);
1171 1169 } else {
1172 1170 *phbasep = kmem_alloc(*phsizep, KM_SLEEP);
1173 1171 }
1174 1172
1175 1173 if ((err = vn_rdwr(UIO_READ, vp, *phbasep, *phsizep,
1176 1174 (offset_t)ehdr->e_phoff, UIO_SYSSPACE, 0, (rlim64_t)0,
1177 1175 credp, &resid)) != 0) {
1178 1176 kmem_free(*phbasep, *phsizep);
1179 1177 *phbasep = NULL;
1180 1178 return (err);
1181 1179 }
1182 1180
1183 1181 return (0);
1184 1182 }
1185 1183
1186 1184 #ifdef _ELF32_COMPAT
1187 1185 extern size_t elf_nshdr_max;
1188 1186 extern size_t elf_shstrtab_max;
1189 1187 #else
1190 1188 size_t elf_nshdr_max = 10000;
1191 1189 size_t elf_shstrtab_max = 100 * 1024;
1192 1190 #endif
1193 1191
1194 1192
1195 1193 static int
1196 1194 getelfshdr(vnode_t *vp, cred_t *credp, const Ehdr *ehdr,
1197 1195 int nshdrs, int shstrndx, caddr_t *shbasep, ssize_t *shsizep,
1198 1196 char **shstrbasep, ssize_t *shstrsizep)
1199 1197 {
1200 1198 ssize_t resid, minsize;
1201 1199 int err;
1202 1200 Shdr *shdr;
1203 1201
1204 1202 /*
1205 1203 * Since we're going to be using e_shentsize to iterate down the
1206 1204 * array of section headers, it must be 8-byte aligned or else
1207 1205 * a we might cause a misaligned access. We use all members through
1208 1206 * sh_entsize (on both 32- and 64-bit ELF files) so e_shentsize
1209 1207 * must be at least large enough to include that member. The index
1210 1208 * of the string table section must also be valid.
1211 1209 */
1212 1210 minsize = offsetof(Shdr, sh_entsize) + sizeof (shdr->sh_entsize);
1213 1211 if (ehdr->e_shentsize < minsize || (ehdr->e_shentsize & 3) ||
1214 1212 shstrndx >= nshdrs)
1215 1213 return (EINVAL);
1216 1214
1217 1215 *shsizep = nshdrs * ehdr->e_shentsize;
1218 1216
1219 1217 if (*shsizep > sizeof (Shdr) * elf_nshdr_max) {
1220 1218 if ((*shbasep = kmem_alloc(*shsizep, KM_NOSLEEP)) == NULL)
1221 1219 return (ENOMEM);
1222 1220 } else {
1223 1221 *shbasep = kmem_alloc(*shsizep, KM_SLEEP);
1224 1222 }
1225 1223
1226 1224 if ((err = vn_rdwr(UIO_READ, vp, *shbasep, *shsizep,
1227 1225 (offset_t)ehdr->e_shoff, UIO_SYSSPACE, 0, (rlim64_t)0,
1228 1226 credp, &resid)) != 0) {
1229 1227 kmem_free(*shbasep, *shsizep);
1230 1228 return (err);
1231 1229 }
1232 1230
1233 1231 /*
1234 1232 * Pull the section string table out of the vnode; fail if the size
1235 1233 * is zero.
1236 1234 */
1237 1235 shdr = (Shdr *)(*shbasep + shstrndx * ehdr->e_shentsize);
1238 1236 if ((*shstrsizep = shdr->sh_size) == 0) {
1239 1237 kmem_free(*shbasep, *shsizep);
1240 1238 return (EINVAL);
1241 1239 }
1242 1240
1243 1241 if (*shstrsizep > elf_shstrtab_max) {
1244 1242 if ((*shstrbasep = kmem_alloc(*shstrsizep,
1245 1243 KM_NOSLEEP)) == NULL) {
1246 1244 kmem_free(*shbasep, *shsizep);
1247 1245 return (ENOMEM);
1248 1246 }
1249 1247 } else {
1250 1248 *shstrbasep = kmem_alloc(*shstrsizep, KM_SLEEP);
1251 1249 }
1252 1250
1253 1251 if ((err = vn_rdwr(UIO_READ, vp, *shstrbasep, *shstrsizep,
1254 1252 (offset_t)shdr->sh_offset, UIO_SYSSPACE, 0, (rlim64_t)0,
1255 1253 credp, &resid)) != 0) {
1256 1254 kmem_free(*shbasep, *shsizep);
1257 1255 kmem_free(*shstrbasep, *shstrsizep);
1258 1256 return (err);
1259 1257 }
1260 1258
1261 1259 /*
1262 1260 * Make sure the strtab is null-terminated to make sure we
1263 1261 * don't run off the end of the table.
1264 1262 */
1265 1263 (*shstrbasep)[*shstrsizep - 1] = '\0';
1266 1264
1267 1265 return (0);
1268 1266 }
1269 1267
1270 1268 static int
1271 1269 mapelfexec(
1272 1270 vnode_t *vp,
1273 1271 Ehdr *ehdr,
1274 1272 int nphdrs,
1275 1273 caddr_t phdrbase,
1276 1274 Phdr **uphdr,
1277 1275 Phdr **intphdr,
1278 1276 Phdr **stphdr,
1279 1277 Phdr **dtphdr,
1280 1278 Phdr *dataphdrp,
1281 1279 caddr_t *bssbase,
1282 1280 caddr_t *brkbase,
1283 1281 intptr_t *voffset,
1284 1282 intptr_t *minaddr,
1285 1283 size_t len,
1286 1284 long *execsz,
1287 1285 size_t *brksize)
1288 1286 {
1289 1287 Phdr *phdr;
1290 1288 int i, prot, error;
1291 1289 caddr_t addr = NULL;
1292 1290 size_t zfodsz;
1293 1291 int ptload = 0;
1294 1292 int page;
1295 1293 off_t offset;
1296 1294 int hsize = ehdr->e_phentsize;
1297 1295 caddr_t mintmp = (caddr_t)-1;
1298 1296 extern int use_brk_lpg;
1299 1297
1300 1298 if (ehdr->e_type == ET_DYN) {
1301 1299 secflagset_t flags = 0;
1302 1300 /*
1303 1301 * Obtain the virtual address of a hole in the
1304 1302 * address space to map the "interpreter".
1305 1303 */
1306 1304 if (secflag_enabled(curproc, PROC_SEC_ASLR))
1307 1305 flags |= _MAP_RANDOMIZE;
1308 1306
1309 1307 map_addr(&addr, len, (offset_t)0, 1, flags);
1310 1308 if (addr == NULL)
1311 1309 return (ENOMEM);
1312 1310 *voffset = (intptr_t)addr;
1313 1311
1314 1312 /*
1315 1313 * Calculate the minimum vaddr so it can be subtracted out.
1316 1314 * According to the ELF specification, since PT_LOAD sections
1317 1315 * must be sorted by increasing p_vaddr values, this is
1318 1316 * guaranteed to be the first PT_LOAD section.
1319 1317 */
1320 1318 phdr = (Phdr *)phdrbase;
1321 1319 for (i = nphdrs; i > 0; i--) {
1322 1320 if (phdr->p_type == PT_LOAD) {
1323 1321 *voffset -= (uintptr_t)phdr->p_vaddr;
1324 1322 break;
1325 1323 }
1326 1324 phdr = (Phdr *)((caddr_t)phdr + hsize);
1327 1325 }
1328 1326
1329 1327 } else {
1330 1328 *voffset = 0;
1331 1329 }
1332 1330 phdr = (Phdr *)phdrbase;
1333 1331 for (i = nphdrs; i > 0; i--) {
1334 1332 switch (phdr->p_type) {
1335 1333 case PT_LOAD:
1336 1334 if ((*intphdr != NULL) && (*uphdr == NULL))
1337 1335 return (0);
1338 1336
1339 1337 ptload = 1;
1340 1338 prot = PROT_USER;
1341 1339 if (phdr->p_flags & PF_R)
1342 1340 prot |= PROT_READ;
1343 1341 if (phdr->p_flags & PF_W)
1344 1342 prot |= PROT_WRITE;
1345 1343 if (phdr->p_flags & PF_X)
1346 1344 prot |= PROT_EXEC;
1347 1345
1348 1346 addr = (caddr_t)((uintptr_t)phdr->p_vaddr + *voffset);
1349 1347
1350 1348 /*
1351 1349 * Keep track of the segment with the lowest starting
1352 1350 * address.
1353 1351 */
1354 1352 if (addr < mintmp)
1355 1353 mintmp = addr;
1356 1354
1357 1355 zfodsz = (size_t)phdr->p_memsz - phdr->p_filesz;
1358 1356
1359 1357 offset = phdr->p_offset;
1360 1358 if (((uintptr_t)offset & PAGEOFFSET) ==
1361 1359 ((uintptr_t)addr & PAGEOFFSET) &&
1362 1360 (!(vp->v_flag & VNOMAP))) {
1363 1361 page = 1;
1364 1362 } else {
1365 1363 page = 0;
1366 1364 }
1367 1365
1368 1366 /*
1369 1367 * Set the heap pagesize for OOB when the bss size
1370 1368 * is known and use_brk_lpg is not 0.
1371 1369 */
1372 1370 if (brksize != NULL && use_brk_lpg &&
1373 1371 zfodsz != 0 && phdr == dataphdrp &&
1374 1372 (prot & PROT_WRITE)) {
1375 1373 size_t tlen = P2NPHASE((uintptr_t)addr +
1376 1374 phdr->p_filesz, PAGESIZE);
1377 1375
1378 1376 if (zfodsz > tlen) {
1379 1377 curproc->p_brkpageszc =
1380 1378 page_szc(map_pgsz(MAPPGSZ_HEAP,
1381 1379 curproc, addr + phdr->p_filesz +
1382 1380 tlen, zfodsz - tlen, 0));
1383 1381 }
1384 1382 }
1385 1383
1386 1384 if (curproc->p_brkpageszc != 0 && phdr == dataphdrp &&
1387 1385 (prot & PROT_WRITE)) {
1388 1386 uint_t szc = curproc->p_brkpageszc;
1389 1387 size_t pgsz = page_get_pagesize(szc);
1390 1388 caddr_t ebss = addr + phdr->p_memsz;
1391 1389 /*
1392 1390 * If we need extra space to keep the BSS an
1393 1391 * integral number of pages in size, some of
1394 1392 * that space may fall beyond p_brkbase, so we
1395 1393 * need to set p_brksize to account for it
1396 1394 * being (logically) part of the brk.
1397 1395 */
1398 1396 size_t extra_zfodsz;
1399 1397
1400 1398 ASSERT(pgsz > PAGESIZE);
1401 1399
1402 1400 extra_zfodsz = P2NPHASE((uintptr_t)ebss, pgsz);
1403 1401
1404 1402 if (error = execmap(vp, addr, phdr->p_filesz,
1405 1403 zfodsz + extra_zfodsz, phdr->p_offset,
1406 1404 prot, page, szc))
1407 1405 goto bad;
1408 1406 if (brksize != NULL)
1409 1407 *brksize = extra_zfodsz;
1410 1408 } else {
1411 1409 if (error = execmap(vp, addr, phdr->p_filesz,
1412 1410 zfodsz, phdr->p_offset, prot, page, 0))
1413 1411 goto bad;
1414 1412 }
1415 1413
1416 1414 if (bssbase != NULL && addr >= *bssbase &&
1417 1415 phdr == dataphdrp) {
1418 1416 *bssbase = addr + phdr->p_filesz;
1419 1417 }
1420 1418 if (brkbase != NULL && addr >= *brkbase) {
1421 1419 *brkbase = addr + phdr->p_memsz;
1422 1420 }
1423 1421
1424 1422 *execsz += btopr(phdr->p_memsz);
1425 1423 break;
1426 1424
1427 1425 case PT_INTERP:
1428 1426 if (ptload)
1429 1427 goto bad;
1430 1428 *intphdr = phdr;
1431 1429 break;
1432 1430
1433 1431 case PT_SHLIB:
1434 1432 *stphdr = phdr;
1435 1433 break;
1436 1434
1437 1435 case PT_PHDR:
1438 1436 if (ptload)
1439 1437 goto bad;
1440 1438 *uphdr = phdr;
1441 1439 break;
1442 1440
1443 1441 case PT_NULL:
1444 1442 case PT_DYNAMIC:
1445 1443 case PT_NOTE:
1446 1444 break;
1447 1445
1448 1446 case PT_SUNWDTRACE:
1449 1447 if (dtphdr != NULL)
1450 1448 *dtphdr = phdr;
1451 1449 break;
1452 1450
1453 1451 default:
1454 1452 break;
1455 1453 }
1456 1454 phdr = (Phdr *)((caddr_t)phdr + hsize);
1457 1455 }
1458 1456
1459 1457 if (minaddr != NULL) {
1460 1458 ASSERT(mintmp != (caddr_t)-1);
1461 1459 *minaddr = (intptr_t)mintmp;
1462 1460 }
1463 1461
1464 1462 if (brkbase != NULL && secflag_enabled(curproc, PROC_SEC_ASLR)) {
1465 1463 size_t off;
1466 1464 uintptr_t base = (uintptr_t)*brkbase;
1467 1465 uintptr_t oend = base + *brksize;
1468 1466
1469 1467 ASSERT(ISP2(aslr_max_brk_skew));
1470 1468
1471 1469 (void) random_get_pseudo_bytes((uint8_t *)&off, sizeof (off));
1472 1470 base += P2PHASE(off, aslr_max_brk_skew);
1473 1471 base = P2ROUNDUP(base, PAGESIZE);
1474 1472 *brkbase = (caddr_t)base;
1475 1473 /*
1476 1474 * Above, we set *brksize to account for the possibility we
1477 1475 * had to grow the 'brk' in padding out the BSS to a page
1478 1476 * boundary.
1479 1477 *
1480 1478 * We now need to adjust that based on where we now are
1481 1479 * actually putting the brk.
1482 1480 */
1483 1481 if (oend > base)
1484 1482 *brksize = oend - base;
1485 1483 else
1486 1484 *brksize = 0;
1487 1485 }
1488 1486
1489 1487 return (0);
1490 1488 bad:
1491 1489 if (error == 0)
1492 1490 error = EINVAL;
1493 1491 return (error);
1494 1492 }
1495 1493
1496 1494 int
1497 1495 elfnote(vnode_t *vp, offset_t *offsetp, int type, int descsz, void *desc,
1498 1496 rlim64_t rlimit, cred_t *credp)
1499 1497 {
1500 1498 Note note;
1501 1499 int error;
1502 1500
1503 1501 bzero(¬e, sizeof (note));
1504 1502 bcopy("CORE", note.name, 4);
1505 1503 note.nhdr.n_type = type;
1506 1504 /*
1507 1505 * The System V ABI states that n_namesz must be the length of the
1508 1506 * string that follows the Nhdr structure including the terminating
1509 1507 * null. The ABI also specifies that sufficient padding should be
1510 1508 * included so that the description that follows the name string
1511 1509 * begins on a 4- or 8-byte boundary for 32- and 64-bit binaries
1512 1510 * respectively. However, since this change was not made correctly
1513 1511 * at the time of the 64-bit port, both 32- and 64-bit binaries
1514 1512 * descriptions are only guaranteed to begin on a 4-byte boundary.
1515 1513 */
1516 1514 note.nhdr.n_namesz = 5;
1517 1515 note.nhdr.n_descsz = roundup(descsz, sizeof (Word));
1518 1516
1519 1517 if (error = core_write(vp, UIO_SYSSPACE, *offsetp, ¬e,
1520 1518 sizeof (note), rlimit, credp))
1521 1519 return (error);
1522 1520
1523 1521 *offsetp += sizeof (note);
1524 1522
1525 1523 if (error = core_write(vp, UIO_SYSSPACE, *offsetp, desc,
1526 1524 note.nhdr.n_descsz, rlimit, credp))
1527 1525 return (error);
1528 1526
1529 1527 *offsetp += note.nhdr.n_descsz;
1530 1528 return (0);
1531 1529 }
1532 1530
1533 1531 /*
1534 1532 * Copy the section data from one vnode to the section of another vnode.
1535 1533 */
1536 1534 static void
1537 1535 copy_scn(Shdr *src, vnode_t *src_vp, Shdr *dst, vnode_t *dst_vp, Off *doffset,
1538 1536 void *buf, size_t size, cred_t *credp, rlim64_t rlimit)
1539 1537 {
1540 1538 ssize_t resid;
1541 1539 size_t len, n = src->sh_size;
1542 1540 offset_t off = 0;
1543 1541
1544 1542 while (n != 0) {
1545 1543 len = MIN(size, n);
1546 1544 if (vn_rdwr(UIO_READ, src_vp, buf, len, src->sh_offset + off,
1547 1545 UIO_SYSSPACE, 0, (rlim64_t)0, credp, &resid) != 0 ||
1548 1546 resid >= len ||
1549 1547 core_write(dst_vp, UIO_SYSSPACE, *doffset + off,
1550 1548 buf, len - resid, rlimit, credp) != 0) {
1551 1549 dst->sh_size = 0;
1552 1550 dst->sh_offset = 0;
1553 1551 return;
1554 1552 }
1555 1553
1556 1554 ASSERT(n >= len - resid);
1557 1555
1558 1556 n -= len - resid;
1559 1557 off += len - resid;
1560 1558 }
1561 1559
1562 1560 *doffset += src->sh_size;
1563 1561 }
1564 1562
1565 1563 #ifdef _ELF32_COMPAT
1566 1564 extern size_t elf_datasz_max;
1567 1565 #else
1568 1566 size_t elf_datasz_max = 1 * 1024 * 1024;
1569 1567 #endif
1570 1568
1571 1569 /*
1572 1570 * This function processes mappings that correspond to load objects to
1573 1571 * examine their respective sections for elfcore(). It's called once with
1574 1572 * v set to NULL to count the number of sections that we're going to need
1575 1573 * and then again with v set to some allocated buffer that we fill in with
1576 1574 * all the section data.
1577 1575 */
1578 1576 static int
1579 1577 process_scns(core_content_t content, proc_t *p, cred_t *credp, vnode_t *vp,
1580 1578 Shdr *v, int nv, rlim64_t rlimit, Off *doffsetp, int *nshdrsp)
1581 1579 {
1582 1580 vnode_t *lastvp = NULL;
1583 1581 struct seg *seg;
1584 1582 int i, j;
1585 1583 void *data = NULL;
1586 1584 size_t datasz = 0;
1587 1585 shstrtab_t shstrtab;
1588 1586 struct as *as = p->p_as;
1589 1587 int error = 0;
1590 1588
1591 1589 if (v != NULL)
1592 1590 shstrtab_init(&shstrtab);
1593 1591
1594 1592 i = 1;
1595 1593 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
1596 1594 uint_t prot;
1597 1595 vnode_t *mvp;
1598 1596 void *tmp = NULL;
1599 1597 caddr_t saddr = seg->s_base;
1600 1598 caddr_t naddr;
1601 1599 caddr_t eaddr;
1602 1600 size_t segsize;
1603 1601
1604 1602 Ehdr ehdr;
1605 1603 int nshdrs, shstrndx, nphdrs;
1606 1604 caddr_t shbase;
1607 1605 ssize_t shsize;
1608 1606 char *shstrbase;
1609 1607 ssize_t shstrsize;
1610 1608
1611 1609 Shdr *shdr;
1612 1610 const char *name;
1613 1611 size_t sz;
1614 1612 uintptr_t off;
1615 1613
1616 1614 int ctf_ndx = 0;
1617 1615 int symtab_ndx = 0;
1618 1616
1619 1617 /*
1620 1618 * Since we're just looking for text segments of load
1621 1619 * objects, we only care about the protection bits; we don't
1622 1620 * care about the actual size of the segment so we use the
1623 1621 * reserved size. If the segment's size is zero, there's
1624 1622 * something fishy going on so we ignore this segment.
1625 1623 */
1626 1624 if (seg->s_ops != &segvn_ops ||
1627 1625 SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 ||
1628 1626 mvp == lastvp || mvp == NULL || mvp->v_type != VREG ||
1629 1627 (segsize = pr_getsegsize(seg, 1)) == 0)
1630 1628 continue;
1631 1629
1632 1630 eaddr = saddr + segsize;
1633 1631 prot = pr_getprot(seg, 1, &tmp, &saddr, &naddr, eaddr);
1634 1632 pr_getprot_done(&tmp);
1635 1633
1636 1634 /*
1637 1635 * Skip this segment unless the protection bits look like
1638 1636 * what we'd expect for a text segment.
1639 1637 */
1640 1638 if ((prot & (PROT_WRITE | PROT_EXEC)) != PROT_EXEC)
1641 1639 continue;
1642 1640
1643 1641 if (getelfhead(mvp, credp, &ehdr, &nshdrs, &shstrndx,
1644 1642 &nphdrs) != 0 ||
1645 1643 getelfshdr(mvp, credp, &ehdr, nshdrs, shstrndx,
1646 1644 &shbase, &shsize, &shstrbase, &shstrsize) != 0)
1647 1645 continue;
1648 1646
1649 1647 off = ehdr.e_shentsize;
1650 1648 for (j = 1; j < nshdrs; j++, off += ehdr.e_shentsize) {
1651 1649 Shdr *symtab = NULL, *strtab;
1652 1650
1653 1651 shdr = (Shdr *)(shbase + off);
1654 1652
1655 1653 if (shdr->sh_name >= shstrsize)
1656 1654 continue;
1657 1655
1658 1656 name = shstrbase + shdr->sh_name;
1659 1657
1660 1658 if (strcmp(name, shstrtab_data[STR_CTF]) == 0) {
1661 1659 if ((content & CC_CONTENT_CTF) == 0 ||
1662 1660 ctf_ndx != 0)
1663 1661 continue;
1664 1662
1665 1663 if (shdr->sh_link > 0 &&
1666 1664 shdr->sh_link < nshdrs) {
1667 1665 symtab = (Shdr *)(shbase +
1668 1666 shdr->sh_link * ehdr.e_shentsize);
1669 1667 }
1670 1668
1671 1669 if (v != NULL && i < nv - 1) {
1672 1670 if (shdr->sh_size > datasz &&
1673 1671 shdr->sh_size <= elf_datasz_max) {
1674 1672 if (data != NULL)
1675 1673 kmem_free(data, datasz);
1676 1674
1677 1675 datasz = shdr->sh_size;
1678 1676 data = kmem_alloc(datasz,
1679 1677 KM_SLEEP);
1680 1678 }
1681 1679
1682 1680 v[i].sh_name = shstrtab_ndx(&shstrtab,
1683 1681 STR_CTF);
1684 1682 v[i].sh_addr = (Addr)(uintptr_t)saddr;
1685 1683 v[i].sh_type = SHT_PROGBITS;
1686 1684 v[i].sh_addralign = 4;
1687 1685 *doffsetp = roundup(*doffsetp,
1688 1686 v[i].sh_addralign);
1689 1687 v[i].sh_offset = *doffsetp;
1690 1688 v[i].sh_size = shdr->sh_size;
1691 1689 if (symtab == NULL) {
1692 1690 v[i].sh_link = 0;
1693 1691 } else if (symtab->sh_type ==
1694 1692 SHT_SYMTAB &&
1695 1693 symtab_ndx != 0) {
1696 1694 v[i].sh_link =
1697 1695 symtab_ndx;
1698 1696 } else {
1699 1697 v[i].sh_link = i + 1;
1700 1698 }
1701 1699
1702 1700 copy_scn(shdr, mvp, &v[i], vp,
1703 1701 doffsetp, data, datasz, credp,
1704 1702 rlimit);
1705 1703 }
1706 1704
1707 1705 ctf_ndx = i++;
1708 1706
1709 1707 /*
1710 1708 * We've already dumped the symtab.
1711 1709 */
1712 1710 if (symtab != NULL &&
1713 1711 symtab->sh_type == SHT_SYMTAB &&
1714 1712 symtab_ndx != 0)
1715 1713 continue;
1716 1714
1717 1715 } else if (strcmp(name,
1718 1716 shstrtab_data[STR_SYMTAB]) == 0) {
1719 1717 if ((content & CC_CONTENT_SYMTAB) == 0 ||
1720 1718 symtab != 0)
1721 1719 continue;
1722 1720
1723 1721 symtab = shdr;
1724 1722 }
1725 1723
1726 1724 if (symtab != NULL) {
1727 1725 if ((symtab->sh_type != SHT_DYNSYM &&
1728 1726 symtab->sh_type != SHT_SYMTAB) ||
1729 1727 symtab->sh_link == 0 ||
1730 1728 symtab->sh_link >= nshdrs)
1731 1729 continue;
1732 1730
1733 1731 strtab = (Shdr *)(shbase +
1734 1732 symtab->sh_link * ehdr.e_shentsize);
1735 1733
1736 1734 if (strtab->sh_type != SHT_STRTAB)
1737 1735 continue;
1738 1736
1739 1737 if (v != NULL && i < nv - 2) {
1740 1738 sz = MAX(symtab->sh_size,
1741 1739 strtab->sh_size);
1742 1740 if (sz > datasz &&
1743 1741 sz <= elf_datasz_max) {
1744 1742 if (data != NULL)
1745 1743 kmem_free(data, datasz);
1746 1744
1747 1745 datasz = sz;
1748 1746 data = kmem_alloc(datasz,
1749 1747 KM_SLEEP);
1750 1748 }
1751 1749
1752 1750 if (symtab->sh_type == SHT_DYNSYM) {
1753 1751 v[i].sh_name = shstrtab_ndx(
1754 1752 &shstrtab, STR_DYNSYM);
1755 1753 v[i + 1].sh_name = shstrtab_ndx(
1756 1754 &shstrtab, STR_DYNSTR);
1757 1755 } else {
1758 1756 v[i].sh_name = shstrtab_ndx(
1759 1757 &shstrtab, STR_SYMTAB);
1760 1758 v[i + 1].sh_name = shstrtab_ndx(
1761 1759 &shstrtab, STR_STRTAB);
1762 1760 }
1763 1761
1764 1762 v[i].sh_type = symtab->sh_type;
1765 1763 v[i].sh_addr = symtab->sh_addr;
1766 1764 if (ehdr.e_type == ET_DYN ||
1767 1765 v[i].sh_addr == 0)
1768 1766 v[i].sh_addr +=
1769 1767 (Addr)(uintptr_t)saddr;
1770 1768 v[i].sh_addralign =
1771 1769 symtab->sh_addralign;
1772 1770 *doffsetp = roundup(*doffsetp,
1773 1771 v[i].sh_addralign);
1774 1772 v[i].sh_offset = *doffsetp;
1775 1773 v[i].sh_size = symtab->sh_size;
1776 1774 v[i].sh_link = i + 1;
1777 1775 v[i].sh_entsize = symtab->sh_entsize;
1778 1776 v[i].sh_info = symtab->sh_info;
1779 1777
1780 1778 copy_scn(symtab, mvp, &v[i], vp,
1781 1779 doffsetp, data, datasz, credp,
1782 1780 rlimit);
1783 1781
1784 1782 v[i + 1].sh_type = SHT_STRTAB;
1785 1783 v[i + 1].sh_flags = SHF_STRINGS;
1786 1784 v[i + 1].sh_addr = symtab->sh_addr;
1787 1785 if (ehdr.e_type == ET_DYN ||
1788 1786 v[i + 1].sh_addr == 0)
1789 1787 v[i + 1].sh_addr +=
1790 1788 (Addr)(uintptr_t)saddr;
1791 1789 v[i + 1].sh_addralign =
1792 1790 strtab->sh_addralign;
1793 1791 *doffsetp = roundup(*doffsetp,
1794 1792 v[i + 1].sh_addralign);
1795 1793 v[i + 1].sh_offset = *doffsetp;
1796 1794 v[i + 1].sh_size = strtab->sh_size;
1797 1795
1798 1796 copy_scn(strtab, mvp, &v[i + 1], vp,
1799 1797 doffsetp, data, datasz, credp,
1800 1798 rlimit);
1801 1799 }
1802 1800
1803 1801 if (symtab->sh_type == SHT_SYMTAB)
1804 1802 symtab_ndx = i;
1805 1803 i += 2;
1806 1804 }
1807 1805 }
1808 1806
1809 1807 kmem_free(shstrbase, shstrsize);
1810 1808 kmem_free(shbase, shsize);
1811 1809
1812 1810 lastvp = mvp;
1813 1811 }
1814 1812
1815 1813 if (v == NULL) {
1816 1814 if (i == 1)
1817 1815 *nshdrsp = 0;
1818 1816 else
1819 1817 *nshdrsp = i + 1;
1820 1818 goto done;
1821 1819 }
1822 1820
1823 1821 if (i != nv - 1) {
1824 1822 cmn_err(CE_WARN, "elfcore: core dump failed for "
1825 1823 "process %d; address space is changing", p->p_pid);
1826 1824 error = EIO;
1827 1825 goto done;
1828 1826 }
1829 1827
1830 1828 v[i].sh_name = shstrtab_ndx(&shstrtab, STR_SHSTRTAB);
1831 1829 v[i].sh_size = shstrtab_size(&shstrtab);
1832 1830 v[i].sh_addralign = 1;
1833 1831 *doffsetp = roundup(*doffsetp, v[i].sh_addralign);
1834 1832 v[i].sh_offset = *doffsetp;
1835 1833 v[i].sh_flags = SHF_STRINGS;
1836 1834 v[i].sh_type = SHT_STRTAB;
1837 1835
1838 1836 if (v[i].sh_size > datasz) {
1839 1837 if (data != NULL)
1840 1838 kmem_free(data, datasz);
1841 1839
1842 1840 datasz = v[i].sh_size;
1843 1841 data = kmem_alloc(datasz,
1844 1842 KM_SLEEP);
1845 1843 }
1846 1844
1847 1845 shstrtab_dump(&shstrtab, data);
1848 1846
1849 1847 if ((error = core_write(vp, UIO_SYSSPACE, *doffsetp,
1850 1848 data, v[i].sh_size, rlimit, credp)) != 0)
1851 1849 goto done;
1852 1850
1853 1851 *doffsetp += v[i].sh_size;
1854 1852
1855 1853 done:
1856 1854 if (data != NULL)
1857 1855 kmem_free(data, datasz);
1858 1856
1859 1857 return (error);
1860 1858 }
1861 1859
1862 1860 int
1863 1861 elfcore(vnode_t *vp, proc_t *p, cred_t *credp, rlim64_t rlimit, int sig,
1864 1862 core_content_t content)
1865 1863 {
1866 1864 offset_t poffset, soffset;
1867 1865 Off doffset;
1868 1866 int error, i, nphdrs, nshdrs;
1869 1867 int overflow = 0;
1870 1868 struct seg *seg;
1871 1869 struct as *as = p->p_as;
1872 1870 union {
1873 1871 Ehdr ehdr;
1874 1872 Phdr phdr[1];
1875 1873 Shdr shdr[1];
1876 1874 } *bigwad;
1877 1875 size_t bigsize;
1878 1876 size_t phdrsz, shdrsz;
1879 1877 Ehdr *ehdr;
1880 1878 Phdr *v;
1881 1879 caddr_t brkbase;
1882 1880 size_t brksize;
1883 1881 caddr_t stkbase;
1884 1882 size_t stksize;
1885 1883 int ntries = 0;
1886 1884 klwp_t *lwp = ttolwp(curthread);
1887 1885
1888 1886 top:
1889 1887 /*
1890 1888 * Make sure we have everything we need (registers, etc.).
1891 1889 * All other lwps have already stopped and are in an orderly state.
1892 1890 */
1893 1891 ASSERT(p == ttoproc(curthread));
1894 1892 prstop(0, 0);
1895 1893
1896 1894 AS_LOCK_ENTER(as, RW_WRITER);
1897 1895 nphdrs = prnsegs(as, 0) + 2; /* two CORE note sections */
1898 1896
1899 1897 /*
1900 1898 * Count the number of section headers we're going to need.
1901 1899 */
1902 1900 nshdrs = 0;
1903 1901 if (content & (CC_CONTENT_CTF | CC_CONTENT_SYMTAB)) {
1904 1902 (void) process_scns(content, p, credp, NULL, NULL, NULL, 0,
1905 1903 NULL, &nshdrs);
1906 1904 }
1907 1905 AS_LOCK_EXIT(as);
1908 1906
1909 1907 ASSERT(nshdrs == 0 || nshdrs > 1);
1910 1908
1911 1909 /*
1912 1910 * The core file contents may required zero section headers, but if
1913 1911 * we overflow the 16 bits allotted to the program header count in
1914 1912 * the ELF header, we'll need that program header at index zero.
1915 1913 */
1916 1914 if (nshdrs == 0 && nphdrs >= PN_XNUM)
1917 1915 nshdrs = 1;
1918 1916
1919 1917 phdrsz = nphdrs * sizeof (Phdr);
1920 1918 shdrsz = nshdrs * sizeof (Shdr);
1921 1919
1922 1920 bigsize = MAX(sizeof (*bigwad), MAX(phdrsz, shdrsz));
1923 1921 bigwad = kmem_alloc(bigsize, KM_SLEEP);
1924 1922
1925 1923 ehdr = &bigwad->ehdr;
1926 1924 bzero(ehdr, sizeof (*ehdr));
1927 1925
1928 1926 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1929 1927 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1930 1928 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1931 1929 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1932 1930 ehdr->e_ident[EI_CLASS] = ELFCLASS;
1933 1931 ehdr->e_type = ET_CORE;
1934 1932
1935 1933 #if !defined(_LP64) || defined(_ELF32_COMPAT)
1936 1934
1937 1935 #if defined(__sparc)
1938 1936 ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
1939 1937 ehdr->e_machine = EM_SPARC;
1940 1938 #elif defined(__i386) || defined(__i386_COMPAT)
1941 1939 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1942 1940 ehdr->e_machine = EM_386;
1943 1941 #else
1944 1942 #error "no recognized machine type is defined"
1945 1943 #endif
1946 1944
1947 1945 #else /* !defined(_LP64) || defined(_ELF32_COMPAT) */
1948 1946
1949 1947 #if defined(__sparc)
1950 1948 ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
1951 1949 ehdr->e_machine = EM_SPARCV9;
1952 1950 #elif defined(__amd64)
1953 1951 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1954 1952 ehdr->e_machine = EM_AMD64;
1955 1953 #else
1956 1954 #error "no recognized 64-bit machine type is defined"
1957 1955 #endif
1958 1956
1959 1957 #endif /* !defined(_LP64) || defined(_ELF32_COMPAT) */
1960 1958
1961 1959 /*
1962 1960 * If the count of program headers or section headers or the index
1963 1961 * of the section string table can't fit in the mere 16 bits
1964 1962 * shortsightedly allotted to them in the ELF header, we use the
1965 1963 * extended formats and put the real values in the section header
1966 1964 * as index 0.
1967 1965 */
1968 1966 ehdr->e_version = EV_CURRENT;
1969 1967 ehdr->e_ehsize = sizeof (Ehdr);
1970 1968
1971 1969 if (nphdrs >= PN_XNUM)
1972 1970 ehdr->e_phnum = PN_XNUM;
1973 1971 else
1974 1972 ehdr->e_phnum = (unsigned short)nphdrs;
1975 1973
1976 1974 ehdr->e_phoff = sizeof (Ehdr);
1977 1975 ehdr->e_phentsize = sizeof (Phdr);
1978 1976
1979 1977 if (nshdrs > 0) {
1980 1978 if (nshdrs >= SHN_LORESERVE)
1981 1979 ehdr->e_shnum = 0;
1982 1980 else
1983 1981 ehdr->e_shnum = (unsigned short)nshdrs;
1984 1982
1985 1983 if (nshdrs - 1 >= SHN_LORESERVE)
1986 1984 ehdr->e_shstrndx = SHN_XINDEX;
1987 1985 else
1988 1986 ehdr->e_shstrndx = (unsigned short)(nshdrs - 1);
1989 1987
1990 1988 ehdr->e_shoff = ehdr->e_phoff + ehdr->e_phentsize * nphdrs;
1991 1989 ehdr->e_shentsize = sizeof (Shdr);
1992 1990 }
1993 1991
1994 1992 if (error = core_write(vp, UIO_SYSSPACE, (offset_t)0, ehdr,
1995 1993 sizeof (Ehdr), rlimit, credp))
1996 1994 goto done;
1997 1995
1998 1996 poffset = sizeof (Ehdr);
1999 1997 soffset = sizeof (Ehdr) + phdrsz;
2000 1998 doffset = sizeof (Ehdr) + phdrsz + shdrsz;
2001 1999
2002 2000 v = &bigwad->phdr[0];
2003 2001 bzero(v, phdrsz);
2004 2002
2005 2003 setup_old_note_header(&v[0], p);
2006 2004 v[0].p_offset = doffset = roundup(doffset, sizeof (Word));
2007 2005 doffset += v[0].p_filesz;
2008 2006
2009 2007 setup_note_header(&v[1], p);
2010 2008 v[1].p_offset = doffset = roundup(doffset, sizeof (Word));
2011 2009 doffset += v[1].p_filesz;
2012 2010
2013 2011 mutex_enter(&p->p_lock);
2014 2012
2015 2013 brkbase = p->p_brkbase;
2016 2014 brksize = p->p_brksize;
2017 2015
2018 2016 stkbase = p->p_usrstack - p->p_stksize;
2019 2017 stksize = p->p_stksize;
2020 2018
2021 2019 mutex_exit(&p->p_lock);
2022 2020
2023 2021 AS_LOCK_ENTER(as, RW_WRITER);
2024 2022 i = 2;
2025 2023 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
2026 2024 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
2027 2025 caddr_t saddr, naddr;
2028 2026 void *tmp = NULL;
2029 2027 extern struct seg_ops segspt_shmops;
2030 2028
2031 2029 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
2032 2030 uint_t prot;
2033 2031 size_t size;
2034 2032 int type;
2035 2033 vnode_t *mvp;
2036 2034
2037 2035 prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
2038 2036 prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
2039 2037 if ((size = (size_t)(naddr - saddr)) == 0)
2040 2038 continue;
2041 2039 if (i == nphdrs) {
2042 2040 overflow++;
2043 2041 continue;
2044 2042 }
2045 2043 v[i].p_type = PT_LOAD;
2046 2044 v[i].p_vaddr = (Addr)(uintptr_t)saddr;
2047 2045 v[i].p_memsz = size;
2048 2046 if (prot & PROT_READ)
2049 2047 v[i].p_flags |= PF_R;
2050 2048 if (prot & PROT_WRITE)
2051 2049 v[i].p_flags |= PF_W;
2052 2050 if (prot & PROT_EXEC)
2053 2051 v[i].p_flags |= PF_X;
2054 2052
2055 2053 /*
2056 2054 * Figure out which mappings to include in the core.
2057 2055 */
2058 2056 type = SEGOP_GETTYPE(seg, saddr);
2059 2057
2060 2058 if (saddr == stkbase && size == stksize) {
2061 2059 if (!(content & CC_CONTENT_STACK))
2062 2060 goto exclude;
2063 2061
2064 2062 } else if (saddr == brkbase && size == brksize) {
2065 2063 if (!(content & CC_CONTENT_HEAP))
2066 2064 goto exclude;
2067 2065
2068 2066 } else if (seg->s_ops == &segspt_shmops) {
2069 2067 if (type & MAP_NORESERVE) {
2070 2068 if (!(content & CC_CONTENT_DISM))
2071 2069 goto exclude;
2072 2070 } else {
2073 2071 if (!(content & CC_CONTENT_ISM))
2074 2072 goto exclude;
2075 2073 }
2076 2074
2077 2075 } else if (seg->s_ops != &segvn_ops) {
2078 2076 goto exclude;
2079 2077
2080 2078 } else if (type & MAP_SHARED) {
2081 2079 if (shmgetid(p, saddr) != SHMID_NONE) {
2082 2080 if (!(content & CC_CONTENT_SHM))
2083 2081 goto exclude;
2084 2082
2085 2083 } else if (SEGOP_GETVP(seg, seg->s_base,
2086 2084 &mvp) != 0 || mvp == NULL ||
2087 2085 mvp->v_type != VREG) {
2088 2086 if (!(content & CC_CONTENT_SHANON))
2089 2087 goto exclude;
2090 2088
2091 2089 } else {
2092 2090 if (!(content & CC_CONTENT_SHFILE))
2093 2091 goto exclude;
2094 2092 }
2095 2093
2096 2094 } else if (SEGOP_GETVP(seg, seg->s_base, &mvp) != 0 ||
2097 2095 mvp == NULL || mvp->v_type != VREG) {
2098 2096 if (!(content & CC_CONTENT_ANON))
2099 2097 goto exclude;
2100 2098
2101 2099 } else if (prot == (PROT_READ | PROT_EXEC)) {
2102 2100 if (!(content & CC_CONTENT_TEXT))
2103 2101 goto exclude;
2104 2102
2105 2103 } else if (prot == PROT_READ) {
2106 2104 if (!(content & CC_CONTENT_RODATA))
2107 2105 goto exclude;
2108 2106
2109 2107 } else {
2110 2108 if (!(content & CC_CONTENT_DATA))
2111 2109 goto exclude;
2112 2110 }
2113 2111
2114 2112 doffset = roundup(doffset, sizeof (Word));
2115 2113 v[i].p_offset = doffset;
2116 2114 v[i].p_filesz = size;
2117 2115 doffset += size;
2118 2116 exclude:
2119 2117 i++;
2120 2118 }
2121 2119 ASSERT(tmp == NULL);
2122 2120 }
2123 2121 AS_LOCK_EXIT(as);
2124 2122
2125 2123 if (overflow || i != nphdrs) {
2126 2124 if (ntries++ == 0) {
2127 2125 kmem_free(bigwad, bigsize);
2128 2126 overflow = 0;
2129 2127 goto top;
2130 2128 }
2131 2129 cmn_err(CE_WARN, "elfcore: core dump failed for "
2132 2130 "process %d; address space is changing", p->p_pid);
2133 2131 error = EIO;
2134 2132 goto done;
2135 2133 }
2136 2134
2137 2135 if ((error = core_write(vp, UIO_SYSSPACE, poffset,
2138 2136 v, phdrsz, rlimit, credp)) != 0)
2139 2137 goto done;
2140 2138
2141 2139 if ((error = write_old_elfnotes(p, sig, vp, v[0].p_offset, rlimit,
2142 2140 credp)) != 0)
2143 2141 goto done;
2144 2142
2145 2143 if ((error = write_elfnotes(p, sig, vp, v[1].p_offset, rlimit,
2146 2144 credp, content)) != 0)
2147 2145 goto done;
2148 2146
2149 2147 for (i = 2; i < nphdrs; i++) {
2150 2148 prkillinfo_t killinfo;
2151 2149 sigqueue_t *sq;
2152 2150 int sig, j;
2153 2151
2154 2152 if (v[i].p_filesz == 0)
2155 2153 continue;
2156 2154
2157 2155 /*
2158 2156 * If dumping out this segment fails, rather than failing
2159 2157 * the core dump entirely, we reset the size of the mapping
2160 2158 * to zero to indicate that the data is absent from the core
2161 2159 * file and or in the PF_SUNW_FAILURE flag to differentiate
2162 2160 * this from mappings that were excluded due to the core file
2163 2161 * content settings.
2164 2162 */
2165 2163 if ((error = core_seg(p, vp, v[i].p_offset,
2166 2164 (caddr_t)(uintptr_t)v[i].p_vaddr, v[i].p_filesz,
2167 2165 rlimit, credp)) == 0) {
2168 2166 continue;
2169 2167 }
2170 2168
2171 2169 if ((sig = lwp->lwp_cursig) == 0) {
2172 2170 /*
2173 2171 * We failed due to something other than a signal.
2174 2172 * Since the space reserved for the segment is now
2175 2173 * unused, we stash the errno in the first four
2176 2174 * bytes. This undocumented interface will let us
2177 2175 * understand the nature of the failure.
2178 2176 */
2179 2177 (void) core_write(vp, UIO_SYSSPACE, v[i].p_offset,
2180 2178 &error, sizeof (error), rlimit, credp);
2181 2179
2182 2180 v[i].p_filesz = 0;
2183 2181 v[i].p_flags |= PF_SUNW_FAILURE;
2184 2182 if ((error = core_write(vp, UIO_SYSSPACE,
2185 2183 poffset + sizeof (v[i]) * i, &v[i], sizeof (v[i]),
2186 2184 rlimit, credp)) != 0)
2187 2185 goto done;
2188 2186
2189 2187 continue;
2190 2188 }
2191 2189
2192 2190 /*
2193 2191 * We took a signal. We want to abort the dump entirely, but
2194 2192 * we also want to indicate what failed and why. We therefore
2195 2193 * use the space reserved for the first failing segment to
2196 2194 * write our error (which, for purposes of compatability with
2197 2195 * older core dump readers, we set to EINTR) followed by any
2198 2196 * siginfo associated with the signal.
2199 2197 */
2200 2198 bzero(&killinfo, sizeof (killinfo));
2201 2199 killinfo.prk_error = EINTR;
2202 2200
2203 2201 sq = sig == SIGKILL ? curproc->p_killsqp : lwp->lwp_curinfo;
2204 2202
2205 2203 if (sq != NULL) {
2206 2204 bcopy(&sq->sq_info, &killinfo.prk_info,
2207 2205 sizeof (sq->sq_info));
2208 2206 } else {
2209 2207 killinfo.prk_info.si_signo = lwp->lwp_cursig;
2210 2208 killinfo.prk_info.si_code = SI_NOINFO;
2211 2209 }
2212 2210
2213 2211 #if (defined(_SYSCALL32_IMPL) || defined(_LP64))
2214 2212 /*
2215 2213 * If this is a 32-bit process, we need to translate from the
2216 2214 * native siginfo to the 32-bit variant. (Core readers must
2217 2215 * always have the same data model as their target or must
2218 2216 * be aware of -- and compensate for -- data model differences.)
2219 2217 */
2220 2218 if (curproc->p_model == DATAMODEL_ILP32) {
2221 2219 siginfo32_t si32;
2222 2220
2223 2221 siginfo_kto32((k_siginfo_t *)&killinfo.prk_info, &si32);
2224 2222 bcopy(&si32, &killinfo.prk_info, sizeof (si32));
2225 2223 }
2226 2224 #endif
2227 2225
2228 2226 (void) core_write(vp, UIO_SYSSPACE, v[i].p_offset,
2229 2227 &killinfo, sizeof (killinfo), rlimit, credp);
2230 2228
2231 2229 /*
2232 2230 * For the segment on which we took the signal, indicate that
2233 2231 * its data now refers to a siginfo.
2234 2232 */
2235 2233 v[i].p_filesz = 0;
2236 2234 v[i].p_flags |= PF_SUNW_FAILURE | PF_SUNW_KILLED |
2237 2235 PF_SUNW_SIGINFO;
2238 2236
2239 2237 /*
2240 2238 * And for every other segment, indicate that its absence
2241 2239 * is due to a signal.
2242 2240 */
2243 2241 for (j = i + 1; j < nphdrs; j++) {
2244 2242 v[j].p_filesz = 0;
2245 2243 v[j].p_flags |= PF_SUNW_FAILURE | PF_SUNW_KILLED;
2246 2244 }
2247 2245
2248 2246 /*
2249 2247 * Finally, write out our modified program headers.
2250 2248 */
2251 2249 if ((error = core_write(vp, UIO_SYSSPACE,
2252 2250 poffset + sizeof (v[i]) * i, &v[i],
2253 2251 sizeof (v[i]) * (nphdrs - i), rlimit, credp)) != 0)
2254 2252 goto done;
2255 2253
2256 2254 break;
2257 2255 }
2258 2256
2259 2257 if (nshdrs > 0) {
2260 2258 bzero(&bigwad->shdr[0], shdrsz);
2261 2259
2262 2260 if (nshdrs >= SHN_LORESERVE)
2263 2261 bigwad->shdr[0].sh_size = nshdrs;
2264 2262
2265 2263 if (nshdrs - 1 >= SHN_LORESERVE)
2266 2264 bigwad->shdr[0].sh_link = nshdrs - 1;
2267 2265
2268 2266 if (nphdrs >= PN_XNUM)
2269 2267 bigwad->shdr[0].sh_info = nphdrs;
2270 2268
2271 2269 if (nshdrs > 1) {
2272 2270 AS_LOCK_ENTER(as, RW_WRITER);
2273 2271 if ((error = process_scns(content, p, credp, vp,
2274 2272 &bigwad->shdr[0], nshdrs, rlimit, &doffset,
2275 2273 NULL)) != 0) {
2276 2274 AS_LOCK_EXIT(as);
2277 2275 goto done;
2278 2276 }
2279 2277 AS_LOCK_EXIT(as);
2280 2278 }
2281 2279
2282 2280 if ((error = core_write(vp, UIO_SYSSPACE, soffset,
2283 2281 &bigwad->shdr[0], shdrsz, rlimit, credp)) != 0)
2284 2282 goto done;
2285 2283 }
2286 2284
2287 2285 done:
2288 2286 kmem_free(bigwad, bigsize);
2289 2287 return (error);
2290 2288 }
2291 2289
2292 2290 #ifndef _ELF32_COMPAT
2293 2291
2294 2292 static struct execsw esw = {
2295 2293 #ifdef _LP64
2296 2294 elf64magicstr,
2297 2295 #else /* _LP64 */
2298 2296 elf32magicstr,
2299 2297 #endif /* _LP64 */
2300 2298 0,
2301 2299 5,
2302 2300 elfexec,
2303 2301 elfcore
2304 2302 };
2305 2303
2306 2304 static struct modlexec modlexec = {
2307 2305 &mod_execops, "exec module for elf", &esw
2308 2306 };
2309 2307
2310 2308 #ifdef _LP64
2311 2309 extern int elf32exec(vnode_t *vp, execa_t *uap, uarg_t *args,
2312 2310 intpdata_t *idatap, int level, long *execsz,
2313 2311 int setid, caddr_t exec_file, cred_t *cred,
2314 2312 int brand_action);
2315 2313 extern int elf32core(vnode_t *vp, proc_t *p, cred_t *credp,
2316 2314 rlim64_t rlimit, int sig, core_content_t content);
2317 2315
2318 2316 static struct execsw esw32 = {
2319 2317 elf32magicstr,
2320 2318 0,
2321 2319 5,
2322 2320 elf32exec,
2323 2321 elf32core
2324 2322 };
2325 2323
2326 2324 static struct modlexec modlexec32 = {
2327 2325 &mod_execops, "32-bit exec module for elf", &esw32
2328 2326 };
2329 2327 #endif /* _LP64 */
2330 2328
2331 2329 static struct modlinkage modlinkage = {
2332 2330 MODREV_1,
2333 2331 (void *)&modlexec,
2334 2332 #ifdef _LP64
2335 2333 (void *)&modlexec32,
2336 2334 #endif /* _LP64 */
2337 2335 NULL
2338 2336 };
2339 2337
2340 2338 int
2341 2339 _init(void)
2342 2340 {
2343 2341 return (mod_install(&modlinkage));
2344 2342 }
2345 2343
2346 2344 int
2347 2345 _fini(void)
2348 2346 {
2349 2347 return (mod_remove(&modlinkage));
2350 2348 }
2351 2349
2352 2350 int
2353 2351 _info(struct modinfo *modinfop)
2354 2352 {
2355 2353 return (mod_info(&modlinkage, modinfop));
2356 2354 }
2357 2355
2358 2356 #endif /* !_ELF32_COMPAT */
↓ open down ↓ |
2150 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX