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