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
|
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 1994-1996 Søren Schmidt
* Copyright (c) 2006 Roman Divacky
* All rights reserved.
* Copyright (c) 2013 Dmitry Chagin <dchagin@FreeBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/imgact.h>
#include <sys/ktr.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/resourcevar.h>
#include <sys/sx.h>
#include <sys/syscallsubr.h>
#include <sys/sysent.h>
#include <compat/linux/linux_emul.h>
#include <compat/linux/linux_mib.h>
#include <compat/linux/linux_misc.h>
#include <compat/linux/linux_persona.h>
#include <compat/linux/linux_util.h>
#if BYTE_ORDER == LITTLE_ENDIAN
#define SHELLMAGIC 0x2123 /* #! */
#else
#define SHELLMAGIC 0x2321
#endif
/*
* This returns reference to the thread emuldata entry (if found)
*
* Hold PROC_LOCK when referencing emuldata from other threads.
*/
struct linux_emuldata *
em_find(struct thread *td)
{
struct linux_emuldata *em;
em = td->td_emuldata;
return (em);
}
/*
* This returns reference to the proc pemuldata entry (if found)
*
* Hold PROC_LOCK when referencing proc pemuldata from other threads.
* Hold LINUX_PEM_LOCK wher referencing pemuldata members.
*/
struct linux_pemuldata *
pem_find(struct proc *p)
{
struct linux_pemuldata *pem;
pem = p->p_emuldata;
return (pem);
}
/*
* Linux apps generally expect the soft open file limit to be set
* to 1024, often iterating over all the file descriptors up to that
* limit instead of using closefrom(2). Give them what they want,
* unless there already is a resource limit in place.
*/
static void
linux_set_default_openfiles(struct thread *td, struct proc *p)
{
struct rlimit rlim;
int error __diagused;
if (linux_default_openfiles < 0)
return;
PROC_LOCK(p);
lim_rlimit_proc(p, RLIMIT_NOFILE, &rlim);
PROC_UNLOCK(p);
if (rlim.rlim_cur != rlim.rlim_max ||
rlim.rlim_cur <= linux_default_openfiles)
return;
rlim.rlim_cur = linux_default_openfiles;
error = kern_proc_setrlimit(td, p, RLIMIT_NOFILE, &rlim);
KASSERT(error == 0, ("kern_proc_setrlimit failed"));
}
/*
* The default stack size limit in Linux is 8MB.
*/
static void
linux_set_default_stacksize(struct thread *td, struct proc *p)
{
struct rlimit rlim;
int error __diagused;
if (linux_default_stacksize < 0)
return;
PROC_LOCK(p);
lim_rlimit_proc(p, RLIMIT_STACK, &rlim);
PROC_UNLOCK(p);
if (rlim.rlim_cur != rlim.rlim_max ||
rlim.rlim_cur <= linux_default_stacksize)
return;
rlim.rlim_cur = linux_default_stacksize;
error = kern_proc_setrlimit(td, p, RLIMIT_STACK, &rlim);
KASSERT(error == 0, ("kern_proc_setrlimit failed"));
}
void
linux_proc_init(struct thread *td, struct thread *newtd, bool init_thread)
{
struct linux_emuldata *em;
struct linux_pemuldata *pem;
struct proc *p;
if (newtd != NULL) {
p = newtd->td_proc;
/* non-exec call */
em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
if (init_thread) {
LINUX_CTR1(proc_init, "thread newtd(%d)",
newtd->td_tid);
em->em_tid = newtd->td_tid;
} else {
LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
em->em_tid = p->p_pid;
pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
sx_init(&pem->pem_sx, "lpemlk");
p->p_emuldata = pem;
}
newtd->td_emuldata = em;
linux_set_default_openfiles(td, p);
linux_set_default_stacksize(td, p);
} else {
p = td->td_proc;
/* exec */
LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
/* lookup the old one */
em = em_find(td);
KASSERT(em != NULL, ("proc_init: thread emuldata not found.\n"));
em->em_tid = p->p_pid;
em->flags = 0;
em->robust_futexes = NULL;
em->child_clear_tid = NULL;
em->child_set_tid = NULL;
pem = pem_find(p);
KASSERT(pem != NULL, ("proc_init: proc emuldata not found.\n"));
pem->persona = 0;
pem->oom_score_adj = 0;
}
}
void
linux_on_exit(struct proc *p)
{
struct linux_pemuldata *pem;
struct thread *td = curthread;
MPASS(SV_CURPROC_ABI() == SV_ABI_LINUX);
LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
td->td_tid, p->p_pid, p);
pem = pem_find(p);
if (pem == NULL)
return;
(p->p_sysent->sv_thread_detach)(td);
p->p_emuldata = NULL;
sx_destroy(&pem->pem_sx);
free(pem, M_LINUX);
}
/*
* If a Linux binary is exec'ing something, try this image activator
* first. We override standard shell script execution in order to
* be able to modify the interpreter path. We only do this if a Linux
* binary is doing the exec, so we do not create an EXEC module for it.
*/
int
linux_exec_imgact_try(struct image_params *imgp)
{
const char *head = (const char *)imgp->image_header;
char *rpath;
int error = -1;
/*
* The interpreter for shell scripts run from a Linux binary needs
* to be located in /compat/linux if possible in order to recursively
* maintain Linux path emulation.
*/
if (((const short *)head)[0] == SHELLMAGIC) {
/*
* Run our normal shell image activator. If it succeeds attempt
* to use the alternate path for the interpreter. If an
* alternate path is found, use our stringspace to store it.
*/
if ((error = exec_shell_imgact(imgp)) == 0) {
linux_emul_convpath(imgp->interpreter_name, UIO_SYSSPACE,
&rpath, 0, AT_FDCWD);
if (rpath != NULL)
imgp->args->fname_buf =
imgp->interpreter_name = rpath;
}
}
return (error);
}
int
linux_common_execve(struct thread *td, struct image_args *eargs)
{
struct linux_pemuldata *pem;
struct vmspace *oldvmspace;
struct linux_emuldata *em;
struct proc *p;
int error;
p = td->td_proc;
error = pre_execve(td, &oldvmspace);
if (error != 0)
return (error);
error = kern_execve(td, eargs, NULL, oldvmspace);
post_execve(td, error, oldvmspace);
if (error != EJUSTRETURN)
return (error);
/*
* In a case of transition from Linux binary execing to
* FreeBSD binary we destroy Linux emuldata thread & proc entries.
*/
if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
PROC_LOCK(p);
em = em_find(td);
KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
td->td_emuldata = NULL;
pem = pem_find(p);
KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
p->p_emuldata = NULL;
PROC_UNLOCK(p);
free(em, M_TEMP);
free(pem, M_LINUX);
}
return (EJUSTRETURN);
}
void
linux_on_exec(struct proc *p, struct image_params *imgp)
{
struct thread *td;
struct thread *othertd;
#if defined(__amd64__)
struct linux_pemuldata *pem;
#endif
td = curthread;
MPASS((imgp->sysent->sv_flags & SV_ABI_MASK) == SV_ABI_LINUX);
/*
* When execing to Linux binary, we create Linux emuldata
* thread entry.
*/
if (SV_PROC_ABI(p) == SV_ABI_LINUX) {
/*
* Process already was under Linuxolator
* before exec. Update emuldata to reflect
* single-threaded cleaned state after exec.
*/
linux_proc_init(td, NULL, false);
} else {
/*
* We are switching the process to Linux emulator.
*/
linux_proc_init(td, td, false);
/*
* Create a transient td_emuldata for all suspended
* threads, so that p->p_sysent->sv_thread_detach() ==
* linux_thread_detach() can find expected but unused
* emuldata.
*/
FOREACH_THREAD_IN_PROC(td->td_proc, othertd) {
if (othertd == td)
continue;
linux_proc_init(td, othertd, true);
}
}
#if defined(__amd64__)
/*
* An IA32 executable which has executable stack will have the
* READ_IMPLIES_EXEC personality flag set automatically.
*/
if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
imgp->stack_prot & VM_PROT_EXECUTE) {
pem = pem_find(p);
pem->persona |= LINUX_READ_IMPLIES_EXEC;
}
#endif
}
void
linux_thread_dtor(struct thread *td)
{
struct linux_emuldata *em;
em = em_find(td);
if (em == NULL)
return;
td->td_emuldata = NULL;
LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
free(em, M_TEMP);
}
void
linux_schedtail(struct thread *td)
{
struct linux_emuldata *em;
#ifdef KTR
int error;
#else
int error __unused;
#endif
int *child_set_tid;
em = em_find(td);
KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
child_set_tid = em->child_set_tid;
if (child_set_tid != NULL) {
error = copyout(&em->em_tid, child_set_tid,
sizeof(em->em_tid));
LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
td->td_tid, child_set_tid, em->em_tid, error);
} else
LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);
}
|