17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <kvm.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <kvm.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <sys/types32.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define	_SYSCALL32
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * VERSION FOR MACHINES WITH STACKS GROWING DOWNWARD IN MEMORY
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * On program entry, the top of the stack frame looks like this:
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * hi:	|-----------------------|
447c478bd9Sstevel@tonic-gate  *	|	unspecified	|
457c478bd9Sstevel@tonic-gate  *	|-----------------------|+
467c478bd9Sstevel@tonic-gate  *	|	   :		| \
477c478bd9Sstevel@tonic-gate  *	|  arg and env strings	|  > no more than NCARGS bytes
487c478bd9Sstevel@tonic-gate  *	|	   :		| /
497c478bd9Sstevel@tonic-gate  *	|-----------------------|+
507c478bd9Sstevel@tonic-gate  *	|	unspecified	|
517c478bd9Sstevel@tonic-gate  *	|-----------------------|
527c478bd9Sstevel@tonic-gate  *	| null auxiliary vector	|
537c478bd9Sstevel@tonic-gate  *	|-----------------------|
547c478bd9Sstevel@tonic-gate  *	|   auxiliary vector	|
557c478bd9Sstevel@tonic-gate  *	|   (2-word entries)	|
567c478bd9Sstevel@tonic-gate  *	|	   :		|
577c478bd9Sstevel@tonic-gate  *	|-----------------------|
587c478bd9Sstevel@tonic-gate  *	|	(char *)0	|
597c478bd9Sstevel@tonic-gate  *	|-----------------------|
607c478bd9Sstevel@tonic-gate  *	|  ptrs to env strings	|
617c478bd9Sstevel@tonic-gate  *	|	   :		|
627c478bd9Sstevel@tonic-gate  *	|-----------------------|
637c478bd9Sstevel@tonic-gate  *	|	(char *)0	|
647c478bd9Sstevel@tonic-gate  *	|-----------------------|
657c478bd9Sstevel@tonic-gate  *	|  ptrs to arg strings	|
667c478bd9Sstevel@tonic-gate  *	|   (argc = # of ptrs)	|
677c478bd9Sstevel@tonic-gate  *	|	   :		|
687c478bd9Sstevel@tonic-gate  *	|-----------------------|
697c478bd9Sstevel@tonic-gate  *	|	  argc		|
707c478bd9Sstevel@tonic-gate  * low:	|-----------------------|
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	RoundUp(v, t)	(((v) + sizeof (t) - 1) & ~(sizeof (t) - 1))
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static int
kvm_getcmd32(kvm_t * kd,struct proc * p,struct user * u,char *** arg,char *** env)767c478bd9Sstevel@tonic-gate kvm_getcmd32(kvm_t *kd,
777c478bd9Sstevel@tonic-gate     struct proc *p, struct user *u, char ***arg, char ***env)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate #if defined(_LP64) || defined(lint)
807c478bd9Sstevel@tonic-gate 	size_t size32;
817c478bd9Sstevel@tonic-gate 	void *stack32;
827c478bd9Sstevel@tonic-gate 	int i, argc, envc;
837c478bd9Sstevel@tonic-gate 	int auxc = 0;
847c478bd9Sstevel@tonic-gate 	size_t asize, esize;
857c478bd9Sstevel@tonic-gate 	char **argv = NULL;
867c478bd9Sstevel@tonic-gate 	char **envp = NULL;
877c478bd9Sstevel@tonic-gate 	size_t strpoolsz;
887c478bd9Sstevel@tonic-gate 	int aptrcount;
897c478bd9Sstevel@tonic-gate 	int eptrcount;
907c478bd9Sstevel@tonic-gate 	caddr_t stackp;
917c478bd9Sstevel@tonic-gate 	ptrdiff_t reloc;
927c478bd9Sstevel@tonic-gate 	char *str;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/*
957c478bd9Sstevel@tonic-gate 	 * Bring the entire stack into memory first, size it
967c478bd9Sstevel@tonic-gate 	 * as an LP64 user stack, then allocate and copy into
977c478bd9Sstevel@tonic-gate 	 * the buffer(s) to be returned to the caller.
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 	size32 = (size_t)p->p_usrstack - (size_t)u->u_argv;
1007c478bd9Sstevel@tonic-gate 	if ((stack32 = malloc(size32)) == NULL)
1017c478bd9Sstevel@tonic-gate 		return (-1);
1027c478bd9Sstevel@tonic-gate 	if (kvm_uread(kd, (uintptr_t)u->u_argv, stack32, size32) != size32) {
1037c478bd9Sstevel@tonic-gate 		free(stack32);
1047c478bd9Sstevel@tonic-gate 		return (-1);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * Find the interesting sizes of a 32-bit stack.
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	argc = u->u_argc;
1117c478bd9Sstevel@tonic-gate 	stackp = (caddr_t)stack32 + ((1 + argc) * sizeof (caddr32_t));
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	for (envc = 0; *(caddr32_t *)stackp; envc++) {
1147c478bd9Sstevel@tonic-gate 		stackp += sizeof (caddr32_t);
1157c478bd9Sstevel@tonic-gate 		if ((stackp - (caddr_t)stack32) >= size32) {
1167c478bd9Sstevel@tonic-gate 			free(stack32);
1177c478bd9Sstevel@tonic-gate 			return (-1);
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (u->u_auxv[0].a_type != AT_NULL) {
1227c478bd9Sstevel@tonic-gate 		stackp += sizeof (caddr32_t);
1237c478bd9Sstevel@tonic-gate 		for (auxc = 0; *(int32_t *)stackp; auxc++) {
1247c478bd9Sstevel@tonic-gate 			stackp += 2 * sizeof (caddr32_t);
1257c478bd9Sstevel@tonic-gate 			if ((stackp - (caddr_t)stack32) >= size32) {
1267c478bd9Sstevel@tonic-gate 				free(stack32);
1277c478bd9Sstevel@tonic-gate 				return (-1);
1287c478bd9Sstevel@tonic-gate 			}
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		auxc++;		/* terminating AT_NULL record */
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * Compute the sizes of the stuff we're going to allocate or copy.
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	eptrcount = (envc + 1) + 2 * auxc;
1377c478bd9Sstevel@tonic-gate 	aptrcount = (argc + 1) + eptrcount;
1387c478bd9Sstevel@tonic-gate 	strpoolsz = size32 - aptrcount * sizeof (caddr32_t);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	asize = aptrcount * sizeof (uintptr_t) + RoundUp(strpoolsz, uintptr_t);
1417c478bd9Sstevel@tonic-gate 	if (arg && (argv = calloc(1, asize + sizeof (uintptr_t))) == NULL) {
1427c478bd9Sstevel@tonic-gate 		free(stack32);
1437c478bd9Sstevel@tonic-gate 		return (-1);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	esize = eptrcount * sizeof (uintptr_t) + RoundUp(strpoolsz, uintptr_t);
1477c478bd9Sstevel@tonic-gate 	if (env && (envp = calloc(1, esize + sizeof (uintptr_t))) == NULL) {
1487c478bd9Sstevel@tonic-gate 		if (argv)
1497c478bd9Sstevel@tonic-gate 			free(argv);
1507c478bd9Sstevel@tonic-gate 		free(stack32);
1517c478bd9Sstevel@tonic-gate 		return (-1);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * Walk up the 32-bit stack, filling in the 64-bit argv and envp
1567c478bd9Sstevel@tonic-gate 	 * as we go.
1577c478bd9Sstevel@tonic-gate 	 */
1587c478bd9Sstevel@tonic-gate 	stackp = (caddr_t)stack32;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * argument vector
1627c478bd9Sstevel@tonic-gate 	 */
1637c478bd9Sstevel@tonic-gate 	if (argv) {
1647c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
1657c478bd9Sstevel@tonic-gate 			argv[i] = (char *)(uintptr_t)(*(caddr32_t *)stackp);
1667c478bd9Sstevel@tonic-gate 			stackp += sizeof (caddr32_t);
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 		argv[argc] = 0;
1697c478bd9Sstevel@tonic-gate 		stackp += sizeof (caddr32_t);
1707c478bd9Sstevel@tonic-gate 	} else
1717c478bd9Sstevel@tonic-gate 		stackp += (1 + argc) * sizeof (caddr32_t);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * environment
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	if (envp) {
1777c478bd9Sstevel@tonic-gate 		for (i = 0; i < envc; i++) {
1787c478bd9Sstevel@tonic-gate 			envp[i] = (char *)(uintptr_t)(*(caddr32_t *)stackp);
1797c478bd9Sstevel@tonic-gate 			stackp += sizeof (caddr32_t);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 		envp[envc] = 0;
1827c478bd9Sstevel@tonic-gate 		stackp += sizeof (caddr32_t);
1837c478bd9Sstevel@tonic-gate 	} else
1847c478bd9Sstevel@tonic-gate 		stackp += (1 + envc) * sizeof (caddr32_t);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * auxiliary vector (skip it..)
1887c478bd9Sstevel@tonic-gate 	 */
1897c478bd9Sstevel@tonic-gate 	stackp += auxc * (sizeof (int32_t) + sizeof (uint32_t));
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * Copy the string pool, untranslated
1937c478bd9Sstevel@tonic-gate 	 */
1947c478bd9Sstevel@tonic-gate 	if (argv)
1957c478bd9Sstevel@tonic-gate 		(void) memcpy(argv + aptrcount, (void *)stackp, strpoolsz);
1967c478bd9Sstevel@tonic-gate 	if (envp)
1977c478bd9Sstevel@tonic-gate 		(void) memcpy(envp + eptrcount, (void *)stackp, strpoolsz);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	free(stack32);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * Relocate the pointers to point at the newly allocated space.
2037c478bd9Sstevel@tonic-gate 	 * Use the same algorithms as kvm_getcmd to handle naughty
2047c478bd9Sstevel@tonic-gate 	 * changes to the argv and envp arrays.
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	if (argv) {
2077c478bd9Sstevel@tonic-gate 		char *argv_null = (char *)argv + asize;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		reloc = (char *)(argv + aptrcount) - (char *)
2107c478bd9Sstevel@tonic-gate 		    ((caddr_t)u->u_argv + aptrcount * sizeof (caddr32_t));
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++)
2137c478bd9Sstevel@tonic-gate 			if (argv[i] != NULL) {
2147c478bd9Sstevel@tonic-gate 				str = (argv[i] += reloc);
2157c478bd9Sstevel@tonic-gate 				if (str < (char *)argv ||
2167c478bd9Sstevel@tonic-gate 				    str >= (char *)argv + asize)
2177c478bd9Sstevel@tonic-gate 					argv[i] = argv_null;
2187c478bd9Sstevel@tonic-gate 			}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		*arg = argv;
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (envp) {
2247c478bd9Sstevel@tonic-gate 		char *envp_null = (char *)envp + esize;
2257c478bd9Sstevel@tonic-gate 		char *last_str;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		reloc = (char *)(envp + eptrcount) - (char *)
2287c478bd9Sstevel@tonic-gate 		    ((caddr_t)u->u_envp + eptrcount * sizeof (caddr32_t));
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		last_str = (char *)((size_t)u->u_argv +
2317c478bd9Sstevel@tonic-gate 		    (1 + argc) * sizeof (caddr32_t) + reloc);
2327c478bd9Sstevel@tonic-gate 		if (last_str < (char *)envp ||
2337c478bd9Sstevel@tonic-gate 		    last_str >= (char *)envp + esize)
2347c478bd9Sstevel@tonic-gate 			last_str = envp_null;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		for (i = 0; i < envc; i++) {
2377c478bd9Sstevel@tonic-gate 			str = (envp[i] += reloc);
2387c478bd9Sstevel@tonic-gate 			if (str < (char *)envp ||
2397c478bd9Sstevel@tonic-gate 			    str >= (char *)envp + esize) {
2407c478bd9Sstevel@tonic-gate 				if (last_str != envp_null)
2417c478bd9Sstevel@tonic-gate 					envp[i] = (char *)((size_t)last_str +
2427c478bd9Sstevel@tonic-gate 					    strlen(last_str) + 1);
2437c478bd9Sstevel@tonic-gate 				else
2447c478bd9Sstevel@tonic-gate 					envp[i] = envp_null;
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 			last_str = envp[i];
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 		*env = envp;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate #endif	/* _LP64 || lint */
2517c478bd9Sstevel@tonic-gate 	return (0);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * reconstruct an argv-like argument list from the target process
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate int
kvm_getcmd(kvm_t * kd,struct proc * proc,struct user * u,char *** arg,char *** env)2587c478bd9Sstevel@tonic-gate kvm_getcmd(kvm_t *kd,
2597c478bd9Sstevel@tonic-gate     struct proc *proc, struct user *u, char ***arg, char ***env)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	size_t asize;
2627c478bd9Sstevel@tonic-gate 	size_t esize;
2637c478bd9Sstevel@tonic-gate 	size_t offset;
2647c478bd9Sstevel@tonic-gate 	int i;
2657c478bd9Sstevel@tonic-gate 	int argc;
2667c478bd9Sstevel@tonic-gate 	char **argv = NULL;
2677c478bd9Sstevel@tonic-gate 	char **envp = NULL;
2687c478bd9Sstevel@tonic-gate 	char *str;
2697c478bd9Sstevel@tonic-gate 	char *last_str;
2707c478bd9Sstevel@tonic-gate 	char *argv_null;	/* Known null in the returned argv */
2717c478bd9Sstevel@tonic-gate 	char *envp_null;	/* Known null in the returned envp */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (proc->p_flag & SSYS)	/* system process */
2747c478bd9Sstevel@tonic-gate 		return (-1);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * Protect against proc structs found by kvm_nextproc()
2787c478bd9Sstevel@tonic-gate 	 * while the kernel was doing a fork(). Such a proc struct
2797c478bd9Sstevel@tonic-gate 	 * may have p_usrstack set but a still zeroed uarea.
2807c478bd9Sstevel@tonic-gate 	 * We wouldn't want to unecessarily allocate 4GB memory ...
2817c478bd9Sstevel@tonic-gate 	 */
282*d2807fddSToomas Soome 	if (u->u_argv == (uintptr_t)NULL || u->u_envp == (uintptr_t)NULL)
2837c478bd9Sstevel@tonic-gate 		return (-1);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * If this is a 32-bit process running on a 64-bit system,
2877c478bd9Sstevel@tonic-gate 	 * then the stack is laid out using ILP32 pointers, not LP64.
2887c478bd9Sstevel@tonic-gate 	 * To minimize potential confusion, we blow it up to "LP64
2897c478bd9Sstevel@tonic-gate 	 * shaped" right here.
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 	if (proc->p_model != DATAMODEL_NATIVE &&
2927c478bd9Sstevel@tonic-gate 	    proc->p_model == DATAMODEL_ILP32)
2937c478bd9Sstevel@tonic-gate 		return (kvm_getcmd32(kd, proc, u, arg, env));
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * Space for the stack, from the argument vector.  An additional
2977c478bd9Sstevel@tonic-gate 	 * word is added to guarantee a NULL word terminates the buffer.
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	if (arg) {
3007c478bd9Sstevel@tonic-gate 		asize = (size_t)proc->p_usrstack - (size_t)u->u_argv;
3017c478bd9Sstevel@tonic-gate 		if ((argv = malloc(asize + sizeof (uintptr_t))) == NULL)
3027c478bd9Sstevel@tonic-gate 			return (-1);
3037c478bd9Sstevel@tonic-gate 		argv_null = (char *)argv + asize;
3047c478bd9Sstevel@tonic-gate 		*(uintptr_t *)argv_null = 0;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * Space for the stack, from the environment vector.  An additional
3097c478bd9Sstevel@tonic-gate 	 * word is added to guarantee a NULL word terminates the buffer.
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (env) {
3127c478bd9Sstevel@tonic-gate 		esize = (size_t)proc->p_usrstack - (size_t)u->u_envp;
3137c478bd9Sstevel@tonic-gate 		if ((envp = malloc(esize + sizeof (uintptr_t))) == NULL) {
3147c478bd9Sstevel@tonic-gate 			if (argv)
3157c478bd9Sstevel@tonic-gate 				free(argv);
3167c478bd9Sstevel@tonic-gate 			return (-1);
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 		envp_null = (char *)envp + esize;
3197c478bd9Sstevel@tonic-gate 		*(uintptr_t *)envp_null = 0;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	argc = u->u_argc;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if (argv) {
3257c478bd9Sstevel@tonic-gate 		/* read the whole initial stack */
3267c478bd9Sstevel@tonic-gate 		if (kvm_uread(kd,
3277c478bd9Sstevel@tonic-gate 		    (uintptr_t)u->u_argv, argv, asize) != asize) {
3287c478bd9Sstevel@tonic-gate 			free(argv);
3297c478bd9Sstevel@tonic-gate 			if (envp)
3307c478bd9Sstevel@tonic-gate 				free(envp);
3317c478bd9Sstevel@tonic-gate 			return (-1);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		argv[argc] = 0;
3347c478bd9Sstevel@tonic-gate 		if (envp) {
3357c478bd9Sstevel@tonic-gate 			/*
3367c478bd9Sstevel@tonic-gate 			 * Copy it to the malloc()d space for the envp array
3377c478bd9Sstevel@tonic-gate 			 */
3387c478bd9Sstevel@tonic-gate 			(void) memcpy(envp, &argv[argc + 1], esize);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	} else if (envp) {
3417c478bd9Sstevel@tonic-gate 		/* read most of the initial stack (excluding argv) */
3427c478bd9Sstevel@tonic-gate 		if (kvm_uread(kd,
3437c478bd9Sstevel@tonic-gate 		    (uintptr_t)u->u_envp, envp, esize) != esize) {
3447c478bd9Sstevel@tonic-gate 			free(envp);
3457c478bd9Sstevel@tonic-gate 			return (-1);
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Relocate and sanity check the argv array.  Entries which have
3517c478bd9Sstevel@tonic-gate 	 * been explicity nulled are left that way.  Entries which have
3527c478bd9Sstevel@tonic-gate 	 * been replaced are pointed to a null string.  Well behaved apps
3537c478bd9Sstevel@tonic-gate 	 * don't do any of this.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	if (argv) {
3567c478bd9Sstevel@tonic-gate 		/* relocate the argv[] addresses */
3577c478bd9Sstevel@tonic-gate 		offset = (char *)argv - (char *)u->u_argv;
3587c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
3597c478bd9Sstevel@tonic-gate 			if (argv[i] != NULL) {
3607c478bd9Sstevel@tonic-gate 				str = (argv[i] += offset);
3617c478bd9Sstevel@tonic-gate 				if (str < (char *)argv ||
3627c478bd9Sstevel@tonic-gate 				    str >= (char *)argv + asize)
3637c478bd9Sstevel@tonic-gate 					argv[i] = argv_null;
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		argv[i] = NULL;
3677c478bd9Sstevel@tonic-gate 		*arg = argv;
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * Relocate and sanity check the envp array.  A null entry indicates
3727c478bd9Sstevel@tonic-gate 	 * the end of the environment.  Entries which point outside of the
3737c478bd9Sstevel@tonic-gate 	 * initial stack are replaced with what must have been the initial
3747c478bd9Sstevel@tonic-gate 	 * value based on the known ordering of the string table by the
3757c478bd9Sstevel@tonic-gate 	 * kernel.  If stack corruption prevents the calculation of the
3767c478bd9Sstevel@tonic-gate 	 * location of an initial string value, a pointer to a null string
3777c478bd9Sstevel@tonic-gate 	 * is returned.  To return a null pointer would prematurely terminate
3787c478bd9Sstevel@tonic-gate 	 * the list.  Well behaved apps do set pointers outside of the
3797c478bd9Sstevel@tonic-gate 	 * initial stack via the putenv(3C) library routine.
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	if (envp) {
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		/*
3847c478bd9Sstevel@tonic-gate 		 * Determine the start of the environment strings as one
3857c478bd9Sstevel@tonic-gate 		 * past the last argument string.
3867c478bd9Sstevel@tonic-gate 		 */
3877c478bd9Sstevel@tonic-gate 		offset = (char *)envp - (char *)u->u_envp;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		if (kvm_uread(kd,
3907c478bd9Sstevel@tonic-gate 		    (uintptr_t)u->u_argv + (argc - 1) * sizeof (char **),
3917c478bd9Sstevel@tonic-gate 		    &last_str, sizeof (last_str)) != sizeof (last_str))
3927c478bd9Sstevel@tonic-gate 			last_str = envp_null;
3937c478bd9Sstevel@tonic-gate 		else {
3947c478bd9Sstevel@tonic-gate 			last_str += offset;
3957c478bd9Sstevel@tonic-gate 			if (last_str < (char *)envp ||
3967c478bd9Sstevel@tonic-gate 			    last_str >= (char *)envp + esize)
3977c478bd9Sstevel@tonic-gate 				last_str = envp_null;
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		/*
4017c478bd9Sstevel@tonic-gate 		 * Relocate the envp[] addresses, while ensuring that we
4027c478bd9Sstevel@tonic-gate 		 * don't return bad addresses.
4037c478bd9Sstevel@tonic-gate 		 */
4047c478bd9Sstevel@tonic-gate 		for (i = 0; envp[i] != NULL; i++) {
4057c478bd9Sstevel@tonic-gate 			str = (envp[i] += offset);
4067c478bd9Sstevel@tonic-gate 			if (str < (char *)envp || str >= (char *)envp + esize) {
4077c478bd9Sstevel@tonic-gate 				if (last_str != envp_null)
4087c478bd9Sstevel@tonic-gate 					envp[i] = last_str +
4097c478bd9Sstevel@tonic-gate 					    strlen(last_str) + 1;
4107c478bd9Sstevel@tonic-gate 				else
4117c478bd9Sstevel@tonic-gate 					envp[i] = envp_null;
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 			last_str = envp[i];
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 		envp[i] = NULL;
4167c478bd9Sstevel@tonic-gate 		*env = envp;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	return (0);
4207c478bd9Sstevel@tonic-gate }
421