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