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
|
/*
* FreeBSD setup_initial_stack() implementation.
*
* Copyright (c) 2013-14 Stacey D. Son
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TARGET_OS_STACK_H
#define TARGET_OS_STACK_H
#include <sys/param.h>
#include "target_arch_sigtramp.h"
#include "qemu/guest-random.h"
#include "user/tswap-target.h"
/*
* The initial FreeBSD stack is as follows:
* (see kern/kern_exec.c exec_copyout_strings() )
*
* Hi Address -> char **ps_argvstr (struct ps_strings for ps, w, etc.)
* unsigned ps_nargvstr
* char **ps_envstr
* PS_STRINGS -> unsigned ps_nenvstr
*
* machine dependent sigcode (sv_sigcode of size
* sv_szsigcode)
*
* execpath (absolute image path for rtld)
*
* SSP Canary (sizeof(long) * 8)
*
* page sizes array (usually sizeof(u_long) )
*
* "destp" -> argv, env strings (up to 262144 bytes)
*/
static inline int setup_initial_stack(struct bsd_binprm *bprm,
abi_ulong *ret_addr, abi_ulong *stringp)
{
int i;
abi_ulong stack_hi_addr;
size_t execpath_len, stringspace;
abi_ulong destp, argvp, envp, p;
struct target_ps_strings ps_strs;
char canary[sizeof(abi_long) * 8];
stack_hi_addr = p = target_stkbas + target_stksiz;
/* Save some space for ps_strings. */
p -= sizeof(struct target_ps_strings);
/* Add machine dependent sigcode. */
p -= TARGET_SZSIGCODE;
if (setup_sigtramp(p, (unsigned)offsetof(struct target_sigframe, sf_uc),
TARGET_FREEBSD_NR_sigreturn)) {
errno = EFAULT;
return -1;
}
if (bprm->fullpath) {
execpath_len = strlen(bprm->fullpath) + 1;
p -= roundup(execpath_len, sizeof(abi_ulong));
if (memcpy_to_target(p, bprm->fullpath, execpath_len)) {
errno = EFAULT;
return -1;
}
}
/* Add canary for SSP. */
qemu_guest_getrandom_nofail(canary, sizeof(canary));
p -= roundup(sizeof(canary), sizeof(abi_ulong));
if (memcpy_to_target(p, canary, sizeof(canary))) {
errno = EFAULT;
return -1;
}
/* Add page sizes array. */
p -= sizeof(abi_ulong);
if (put_user_ual(TARGET_PAGE_SIZE, p)) {
errno = EFAULT;
return -1;
}
/*
* Deviate from FreeBSD stack layout: force stack to new page here
* so that signal trampoline is not sharing the page with user stack
* frames. This is actively harmful in qemu as it marks pages with
* code it translated as read-only, which is somewhat problematic
* for user trying to use the stack as intended.
*/
p = rounddown(p, TARGET_PAGE_SIZE);
/* Calculate the string space needed */
stringspace = 0;
for (i = 0; i < bprm->argc; ++i) {
stringspace += strlen(bprm->argv[i]) + 1;
}
for (i = 0; i < bprm->envc; ++i) {
stringspace += strlen(bprm->envp[i]) + 1;
}
if (stringspace > TARGET_ARG_MAX) {
errno = ENOMEM;
return -1;
}
/* Make room for the argv and envp strings */
destp = rounddown(p - stringspace, sizeof(abi_ulong));
p = argvp = destp - (bprm->argc + bprm->envc + 2) * sizeof(abi_ulong);
/* Remember the strings pointer */
if (stringp) {
*stringp = destp;
}
/*
* Add argv strings. Note that the argv[] vectors are added by
* loader_build_argptr()
*/
/* XXX need to make room for auxargs */
ps_strs.ps_argvstr = tswapl(argvp);
ps_strs.ps_nargvstr = tswap32(bprm->argc);
for (i = 0; i < bprm->argc; ++i) {
size_t len = strlen(bprm->argv[i]) + 1;
if (memcpy_to_target(destp, bprm->argv[i], len)) {
errno = EFAULT;
return -1;
}
if (put_user_ual(destp, argvp)) {
errno = EFAULT;
return -1;
}
argvp += sizeof(abi_ulong);
destp += len;
}
if (put_user_ual(0, argvp)) {
errno = EFAULT;
return -1;
}
/*
* Add env strings. Note that the envp[] vectors are added by
* loader_build_argptr().
*/
envp = argvp + sizeof(abi_ulong);
ps_strs.ps_envstr = tswapl(envp);
ps_strs.ps_nenvstr = tswap32(bprm->envc);
for (i = 0; i < bprm->envc; ++i) {
size_t len = strlen(bprm->envp[i]) + 1;
if (memcpy_to_target(destp, bprm->envp[i], len)) {
errno = EFAULT;
return -1;
}
if (put_user_ual(destp, envp)) {
errno = EFAULT;
return -1;
}
envp += sizeof(abi_ulong);
destp += len;
}
if (put_user_ual(0, envp)) {
errno = EFAULT;
return -1;
}
if (memcpy_to_target(stack_hi_addr - sizeof(ps_strs), &ps_strs,
sizeof(ps_strs))) {
errno = EFAULT;
return -1;
}
if (ret_addr) {
*ret_addr = p;
}
return 0;
}
#endif /* TARGET_OS_STACK_H */
|