1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <kvm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <kvm.h>
#include <strings.h>
#include <sys/types32.h>
#define _SYSCALL32
/*
* VERSION FOR MACHINES WITH STACKS GROWING DOWNWARD IN MEMORY
*
* On program entry, the top of the stack frame looks like this:
*
* hi: |-----------------------|
* | unspecified |
* |-----------------------|+
* | : | \
* | arg and env strings | > no more than NCARGS bytes
* | : | /
* |-----------------------|+
* | unspecified |
* |-----------------------|
* | null auxiliary vector |
* |-----------------------|
* | auxiliary vector |
* | (2-word entries) |
* | : |
* |-----------------------|
* | (char *)0 |
* |-----------------------|
* | ptrs to env strings |
* | : |
* |-----------------------|
* | (char *)0 |
* |-----------------------|
* | ptrs to arg strings |
* | (argc = # of ptrs) |
* | : |
* |-----------------------|
* | argc |
* low: |-----------------------|
*/
#define RoundUp(v, t) (((v) + sizeof (t) - 1) & ~(sizeof (t) - 1))
static int
kvm_getcmd32(kvm_t *kd,
struct proc *p, struct user *u, char ***arg, char ***env)
{
#if defined(_LP64) || defined(lint)
size_t size32;
void *stack32;
int i, argc, envc;
int auxc = 0;
size_t asize, esize;
char **argv = NULL;
char **envp = NULL;
size_t strpoolsz;
int aptrcount;
int eptrcount;
caddr_t stackp;
ptrdiff_t reloc;
char *str;
/*
* Bring the entire stack into memory first, size it
* as an LP64 user stack, then allocate and copy into
* the buffer(s) to be returned to the caller.
*/
size32 = (size_t)p->p_usrstack - (size_t)u->u_argv;
if ((stack32 = malloc(size32)) == NULL)
return (-1);
if (kvm_uread(kd, (uintptr_t)u->u_argv, stack32, size32) != size32) {
free(stack32);
return (-1);
}
/*
* Find the interesting sizes of a 32-bit stack.
*/
argc = u->u_argc;
stackp = (caddr_t)stack32 + ((1 + argc) * sizeof (caddr32_t));
for (envc = 0; *(caddr32_t *)stackp; envc++) {
stackp += sizeof (caddr32_t);
if ((stackp - (caddr_t)stack32) >= size32) {
free(stack32);
return (-1);
}
}
if (u->u_auxv[0].a_type != AT_NULL) {
stackp += sizeof (caddr32_t);
for (auxc = 0; *(int32_t *)stackp; auxc++) {
stackp += 2 * sizeof (caddr32_t);
if ((stackp - (caddr_t)stack32) >= size32) {
free(stack32);
return (-1);
}
}
auxc++; /* terminating AT_NULL record */
}
/*
* Compute the sizes of the stuff we're going to allocate or copy.
*/
eptrcount = (envc + 1) + 2 * auxc;
aptrcount = (argc + 1) + eptrcount;
strpoolsz = size32 - aptrcount * sizeof (caddr32_t);
asize = aptrcount * sizeof (uintptr_t) + RoundUp(strpoolsz, uintptr_t);
if (arg && (argv = calloc(1, asize + sizeof (uintptr_t))) == NULL) {
free(stack32);
return (-1);
}
esize = eptrcount * sizeof (uintptr_t) + RoundUp(strpoolsz, uintptr_t);
if (env && (envp = calloc(1, esize + sizeof (uintptr_t))) == NULL) {
if (argv)
free(argv);
free(stack32);
return (-1);
}
/*
* Walk up the 32-bit stack, filling in the 64-bit argv and envp
* as we go.
*/
stackp = (caddr_t)stack32;
/*
* argument vector
*/
if (argv) {
for (i = 0; i < argc; i++) {
argv[i] = (char *)(uintptr_t)(*(caddr32_t *)stackp);
stackp += sizeof (caddr32_t);
}
argv[argc] = 0;
stackp += sizeof (caddr32_t);
} else
stackp += (1 + argc) * sizeof (caddr32_t);
/*
* environment
*/
if (envp) {
for (i = 0; i < envc; i++) {
envp[i] = (char *)(uintptr_t)(*(caddr32_t *)stackp);
stackp += sizeof (caddr32_t);
}
envp[envc] = 0;
stackp += sizeof (caddr32_t);
} else
stackp += (1 + envc) * sizeof (caddr32_t);
/*
* auxiliary vector (skip it..)
*/
stackp += auxc * (sizeof (int32_t) + sizeof (uint32_t));
/*
* Copy the string pool, untranslated
*/
if (argv)
(void) memcpy(argv + aptrcount, (void *)stackp, strpoolsz);
if (envp)
(void) memcpy(envp + eptrcount, (void *)stackp, strpoolsz);
free(stack32);
/*
* Relocate the pointers to point at the newly allocated space.
* Use the same algorithms as kvm_getcmd to handle naughty
* changes to the argv and envp arrays.
*/
if (argv) {
char *argv_null = (char *)argv + asize;
reloc = (char *)(argv + aptrcount) - (char *)
((caddr_t)u->u_argv + aptrcount * sizeof (caddr32_t));
for (i = 0; i < argc; i++)
if (argv[i] != NULL) {
str = (argv[i] += reloc);
if (str < (char *)argv ||
str >= (char *)argv + asize)
argv[i] = argv_null;
}
*arg = argv;
}
if (envp) {
char *envp_null = (char *)envp + esize;
char *last_str;
reloc = (char *)(envp + eptrcount) - (char *)
((caddr_t)u->u_envp + eptrcount * sizeof (caddr32_t));
last_str = (char *)((size_t)u->u_argv +
(1 + argc) * sizeof (caddr32_t) + reloc);
if (last_str < (char *)envp ||
last_str >= (char *)envp + esize)
last_str = envp_null;
for (i = 0; i < envc; i++) {
str = (envp[i] += reloc);
if (str < (char *)envp ||
str >= (char *)envp + esize) {
if (last_str != envp_null)
envp[i] = (char *)((size_t)last_str +
strlen(last_str) + 1);
else
envp[i] = envp_null;
}
last_str = envp[i];
}
*env = envp;
}
#endif /* _LP64 || lint */
return (0);
}
/*
* reconstruct an argv-like argument list from the target process
*/
int
kvm_getcmd(kvm_t *kd,
struct proc *proc, struct user *u, char ***arg, char ***env)
{
size_t asize;
size_t esize;
size_t offset;
int i;
int argc;
char **argv = NULL;
char **envp = NULL;
char *str;
char *last_str;
char *argv_null; /* Known null in the returned argv */
char *envp_null; /* Known null in the returned envp */
if (proc->p_flag & SSYS) /* system process */
return (-1);
/*
* Protect against proc structs found by kvm_nextproc()
* while the kernel was doing a fork(). Such a proc struct
* may have p_usrstack set but a still zeroed uarea.
* We wouldn't want to unecessarily allocate 4GB memory ...
*/
if (u->u_argv == NULL || u->u_envp == NULL)
return (-1);
/*
* If this is a 32-bit process running on a 64-bit system,
* then the stack is laid out using ILP32 pointers, not LP64.
* To minimize potential confusion, we blow it up to "LP64
* shaped" right here.
*/
if (proc->p_model != DATAMODEL_NATIVE &&
proc->p_model == DATAMODEL_ILP32)
return (kvm_getcmd32(kd, proc, u, arg, env));
/*
* Space for the stack, from the argument vector. An additional
* word is added to guarantee a NULL word terminates the buffer.
*/
if (arg) {
asize = (size_t)proc->p_usrstack - (size_t)u->u_argv;
if ((argv = malloc(asize + sizeof (uintptr_t))) == NULL)
return (-1);
argv_null = (char *)argv + asize;
*(uintptr_t *)argv_null = 0;
}
/*
* Space for the stack, from the environment vector. An additional
* word is added to guarantee a NULL word terminates the buffer.
*/
if (env) {
esize = (size_t)proc->p_usrstack - (size_t)u->u_envp;
if ((envp = malloc(esize + sizeof (uintptr_t))) == NULL) {
if (argv)
free(argv);
return (-1);
}
envp_null = (char *)envp + esize;
*(uintptr_t *)envp_null = 0;
}
argc = u->u_argc;
if (argv) {
/* read the whole initial stack */
if (kvm_uread(kd,
(uintptr_t)u->u_argv, argv, asize) != asize) {
free(argv);
if (envp)
free(envp);
return (-1);
}
argv[argc] = 0;
if (envp) {
/*
* Copy it to the malloc()d space for the envp array
*/
(void) memcpy(envp, &argv[argc + 1], esize);
}
} else if (envp) {
/* read most of the initial stack (excluding argv) */
if (kvm_uread(kd,
(uintptr_t)u->u_envp, envp, esize) != esize) {
free(envp);
return (-1);
}
}
/*
* Relocate and sanity check the argv array. Entries which have
* been explicity nulled are left that way. Entries which have
* been replaced are pointed to a null string. Well behaved apps
* don't do any of this.
*/
if (argv) {
/* relocate the argv[] addresses */
offset = (char *)argv - (char *)u->u_argv;
for (i = 0; i < argc; i++) {
if (argv[i] != NULL) {
str = (argv[i] += offset);
if (str < (char *)argv ||
str >= (char *)argv + asize)
argv[i] = argv_null;
}
}
argv[i] = NULL;
*arg = argv;
}
/*
* Relocate and sanity check the envp array. A null entry indicates
* the end of the environment. Entries which point outside of the
* initial stack are replaced with what must have been the initial
* value based on the known ordering of the string table by the
* kernel. If stack corruption prevents the calculation of the
* location of an initial string value, a pointer to a null string
* is returned. To return a null pointer would prematurely terminate
* the list. Well behaved apps do set pointers outside of the
* initial stack via the putenv(3C) library routine.
*/
if (envp) {
/*
* Determine the start of the environment strings as one
* past the last argument string.
*/
offset = (char *)envp - (char *)u->u_envp;
if (kvm_uread(kd,
(uintptr_t)u->u_argv + (argc - 1) * sizeof (char **),
&last_str, sizeof (last_str)) != sizeof (last_str))
last_str = envp_null;
else {
last_str += offset;
if (last_str < (char *)envp ||
last_str >= (char *)envp + esize)
last_str = envp_null;
}
/*
* Relocate the envp[] addresses, while ensuring that we
* don't return bad addresses.
*/
for (i = 0; envp[i] != NULL; i++) {
str = (envp[i] += offset);
if (str < (char *)envp || str >= (char *)envp + esize) {
if (last_str != envp_null)
envp[i] = last_str +
strlen(last_str) + 1;
else
envp[i] = envp_null;
}
last_str = envp[i];
}
envp[i] = NULL;
*env = envp;
}
return (0);
}
|