Skip to content

Commit 361ebf4

Browse files
committed
IH-533: Remove usage of forkexecd daemon to execute processes
Forkexecd was written to avoid some issues with Ocaml and multi-threading. Instead use C code to launch processes and avoid these issues. Interface remains unchanged from Ocaml side but implementation rely entirely on C code. vfork() is used to avoid performance memory issue. Reap of the processes are done directly. Code automatically reap child processes to avoid zombies. One small helper is used to better separate Ocaml and C code and handling syslog redirection. This allows to better debug in case of issues. Syslog handling is done in a separate process allowing to restart the toolstack and keep launched programs running; note that even with forkexecd daemon one process was used for this purpose. Code tries to keep compatibility with forkexecd, in particular: - SIGPIPE is ignored in the parent; - /dev/null is open with O_WRONLY even for stdin; - file descriptors are limited to 1024. We use close_range (if available) to reduce system calls to close file descriptors. Cgroup is set to avoid systemd closing processes on toolstack restart. There's a fuzzer program to check file remapping algorithm; for this reason the algorithm is in a separate file. To turn internal debug on you need to set FORKEXECD_DEBUG_LOGS C preprocessor macro to 1. Signed-off-by: Frediano Ziglio <[email protected]>
1 parent 0f5c281 commit 361ebf4

File tree

19 files changed

+2040
-33
lines changed

19 files changed

+2040
-33
lines changed

ocaml/forkexecd/helper/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Set some macro but not override environment ones
2+
CFLAGS ?= -O2 -g -Wall -Werror
3+
LDFLAGS ?=
4+
5+
all:: vfork_helper
6+
7+
clean::
8+
rm -f vfork_helper *.o
9+
10+
%.o: %.c
11+
gcc $(CFLAGS) -c -o $@ $<
12+
13+
vfork_helper: vfork_helper.o close_from.o syslog.o
14+
gcc $(CFLAGS) $(LDFLAGS) -o $@ $^ -pthread
15+
16+
close_from.o: close_from.h Makefile
17+
syslog.o: syslog.h Makefile
18+
vfork_helper.o: redirect_algo.h Makefile
19+
20+
## Fuzzer uses AFL (American Fuzzy Lop).
21+
##
22+
## Use "make fuzz" to build and launch the fuzzer
23+
##
24+
## Use "make show" to look at the first failures (if found).
25+
26+
fuzz::
27+
afl-gcc $(CFLAGS) -Wall -Werror -o algo_fuzzer algo_fuzzer.c
28+
rm -rf testcase_dir
29+
mkdir testcase_dir
30+
echo maomaoamaoaoao > testcase_dir/test1
31+
rm -rf findings_dir/
32+
afl-fuzz -i testcase_dir -o findings_dir -D -- ./algo_fuzzer
33+
34+
show::
35+
cat "$$(ls -1 findings_dir/default/crashes/id* | head -1)" | ./algo_fuzzer

ocaml/forkexecd/helper/algo_fuzzer.c

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
2+
/*
3+
* Copyright (C) Citrix Systems Inc.
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published
7+
* by the Free Software Foundation; version 2.1 only. with the special
8+
* exception on linking described in file LICENSE.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*/
15+
16+
#undef NDEBUG
17+
#define DEBUG 1
18+
19+
#if DEBUG
20+
#define log(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
21+
#else
22+
#define log(fmt, ...) do {} while(0)
23+
#endif
24+
25+
// include as first file to make sure header is self contained
26+
#include "redirect_algo.h"
27+
28+
#include <stdio.h>
29+
#include <stdlib.h>
30+
#include <errno.h>
31+
#include <string.h>
32+
#include <stdint.h>
33+
#include <stdbool.h>
34+
#include <assert.h>
35+
36+
static int fake_close(int fd);
37+
38+
typedef struct {
39+
bool open;
40+
bool cloexec;
41+
char *name;
42+
} fd;
43+
44+
#define NUM_FDS 4096
45+
static fd fds[NUM_FDS];
46+
47+
static bool
48+
fake_close_fds_from(int fd_from)
49+
{
50+
for (int fd = fd_from; fd < NUM_FDS; ++fd)
51+
fake_close(fd);
52+
53+
return true;
54+
}
55+
56+
#define O_WRONLY 1
57+
static int
58+
fake_open(const char *fn, int dummy)
59+
{
60+
for (int i = 0; i < NUM_FDS; ++i)
61+
if (!fds[i].open) {
62+
assert(fds[i].name == NULL);
63+
fds[i].name = strdup(fn);
64+
fds[i].open = true;
65+
fds[i].cloexec = false;
66+
return i;
67+
}
68+
assert(0);
69+
return -1;
70+
}
71+
72+
static int
73+
fake_close(int fd)
74+
{
75+
assert(fd >= 0);
76+
assert(fd < NUM_FDS);
77+
if (!fds[fd].open) {
78+
errno = EBADF;
79+
return -1;
80+
}
81+
fds[fd].open = false;
82+
free(fds[fd].name);
83+
fds[fd].name = NULL;
84+
return 0;
85+
}
86+
87+
static int
88+
fake_dup2(int from, int to)
89+
{
90+
assert(from >= 0 && from < NUM_FDS);
91+
assert(to >= 0 && to < NUM_FDS);
92+
assert(fds[from].open);
93+
assert(from != to);
94+
free(fds[to].name);
95+
fds[to].open = true;
96+
fds[to].name = strdup(fds[from].name);
97+
fds[to].cloexec = false;
98+
return 0;
99+
}
100+
101+
static int
102+
fake_fcntl(int fd)
103+
{
104+
assert(fd >= 0 && fd < NUM_FDS);
105+
assert(fds[fd].open);
106+
fds[fd].cloexec = false;
107+
return 0;
108+
}
109+
110+
int main(int argc, char **argv)
111+
{
112+
// Input where a given FD goes??
113+
// No, not enough, can be duplicated.
114+
// Numbers >4096 in 2 bytes not file descriptor,
115+
// (-1 for standard, skip for normal).
116+
// We should add some random fds.
117+
enum { MAX_FILE_BUF = 2048 };
118+
uint16_t file_buf[MAX_FILE_BUF];
119+
size_t read = fread(file_buf, 2, MAX_FILE_BUF, stdin);
120+
if (read < 3)
121+
return 0;
122+
123+
static const char standard_names[][8] = {
124+
"stdin", "stdout", "stderr"
125+
};
126+
int num_mappings = 0;
127+
uint16_t *num = file_buf;
128+
mapping mappings[MAX_FILE_BUF];
129+
int i = 0;
130+
for (i = 0; i < 3; ++i) {
131+
mapping *m = &mappings[num_mappings++];
132+
m->uuid = standard_names[i];
133+
uint16_t n = *num++;
134+
m->current_fd = n < NUM_FDS ? n : -1;
135+
m->wanted_fd = i;
136+
}
137+
for (; i < read; ++i) {
138+
uint16_t n = *num++;
139+
if (n >= NUM_FDS)
140+
continue;
141+
142+
mapping *m = &mappings[num_mappings++];
143+
m->current_fd = n;
144+
m->wanted_fd = -1;
145+
char buf[64];
146+
sprintf(buf, "file%d", i);
147+
m->uuid = strdup(buf);
148+
}
149+
if (num_mappings > MAX_TOTAL_MAPPINGS)
150+
return 0;
151+
152+
for (unsigned n = 0; n < num_mappings; ++n) {
153+
mapping *m = &mappings[n];
154+
int fd = m->current_fd;
155+
if (fd < 0)
156+
continue;
157+
fake_close(fd);
158+
fds[fd].open = true;
159+
fds[fd].name = strdup(m->uuid);
160+
fds[fd].cloexec = true;
161+
}
162+
163+
// Check in the final file mapping all valid mappings
164+
// have an open file descriptor.
165+
// There should be no duplicate numbers in current_fd.
166+
// current_fd must be in a range.
167+
// Only if wanted_fd >= 0 current_fd can be -1.
168+
// There should be a correspondance between input and output names.
169+
// If current_fd was -1 it will still be -1.
170+
// If wanted_fd >= 0 current_fd should be the same.
171+
172+
fd_operation operations[MAX_OPERATIONS];
173+
int num_operations =
174+
redirect_mappings(mappings, num_mappings, operations);
175+
assert(num_operations > 0);
176+
assert(num_operations <= MAX_OPERATIONS);
177+
178+
for (int i = 0; i < num_operations; ++i) {
179+
const fd_operation* op = &operations[i];
180+
log("op %d %d %d", op->fd_from, op->fd_to, op->operation);
181+
switch (op->operation) {
182+
case FD_OP_DUP:
183+
if (op->fd_from == op->fd_to)
184+
fake_fcntl(op->fd_from);
185+
else
186+
fake_dup2(op->fd_from, op->fd_to);
187+
break;
188+
case FD_OP_MOVE:
189+
assert(op->fd_from != op->fd_to);
190+
fake_dup2(op->fd_from, op->fd_to);
191+
fake_close(op->fd_from);
192+
break;
193+
case FD_OP_DEVNULL:
194+
// first close old, then create new one
195+
fake_close(op->fd_to);
196+
// TODO ideally we want read only for input for Ocaml did the same...
197+
assert(fake_open("/dev/null", O_WRONLY) == op->fd_to);
198+
break;
199+
case FD_OP_CLOSE_FROM:
200+
fake_close_fds_from(op->fd_from);
201+
break;
202+
default:
203+
assert(0);
204+
}
205+
}
206+
207+
// check files opened
208+
for (int fd = 0; fd < NUM_FDS; ++fd)
209+
assert(fds[fd].open == (fd < num_mappings));
210+
211+
for (int fd = 0; fd < num_mappings; ++fd) {
212+
assert(fds[fd].cloexec == false);
213+
log("file %d %s", fd, fds[fd].name);
214+
}
215+
216+
// Check in the final file mapping all valid mappings
217+
// has an open file descriptor.
218+
bool already_found[NUM_FDS] = { false, };
219+
for (unsigned n = 0; n < num_mappings; ++n) {
220+
const int fd = mappings[n].current_fd;
221+
const int wanted = mappings[n].wanted_fd;
222+
if (fd >= 0) {
223+
assert(fd < NUM_FDS);
224+
assert(fds[fd].open);
225+
226+
// There should be no duplicate numbers in current_fd.
227+
assert(!already_found[fd]);
228+
already_found[fd] = true;
229+
} else {
230+
// Only if wanted_fd >= 0 current_fd can be -1.
231+
assert(mappings[n].wanted_fd >= 0);
232+
assert(fd == -1);
233+
}
234+
235+
// If wanted_fd >= 0 current_fd should be the same.
236+
if (wanted >= 0)
237+
assert(wanted == fd || fd == -1);
238+
239+
// current_fd must be in a range.
240+
assert(fd >= -1);
241+
assert(fd < num_mappings);
242+
}
243+
244+
// There should be a correspondance between input and output names.
245+
// If current_fd was -1 it will still be -1.
246+
}

ocaml/forkexecd/helper/close_from.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (C) Citrix Systems Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*/
14+
15+
#include "close_from.h"
16+
17+
#include <stdlib.h>
18+
#include <errno.h>
19+
#include <unistd.h>
20+
#include <dirent.h>
21+
#include <sys/types.h>
22+
#include <sys/resource.h>
23+
24+
#ifdef __linux__
25+
#include <sys/syscall.h>
26+
#endif
27+
28+
// try to use close_range on Linux even if not defined by headers
29+
#if defined(__linux__) && !defined(SYS_close_range)
30+
# if defined(__alpha__)
31+
# define SYS_close_range 546
32+
# elif defined(__amd64__) || defined(__x86_64__) || defined(__arm__) || \
33+
defined(__aarch64__) || defined(__hppa__) || defined(__i386__) || \
34+
defined(__ia64__) || defined(__m68k__) || defined(__mips__) || \
35+
defined(__powerpc__) || defined(__powerpc64__) || defined(__sparc__) || \
36+
defined(__s390x__)
37+
# define SYS_close_range 436
38+
# endif
39+
#endif
40+
41+
bool
42+
close_fds_from(int fd_from)
43+
{
44+
// first method, use close_range
45+
#if (defined(__linux__) && defined(SYS_close_range)) \
46+
|| (defined(__FreeBSD__) && defined(CLOSE_RANGE_CLOEXEC))
47+
static bool close_range_supported = true;
48+
if (close_range_supported) {
49+
#if defined(__linux__)
50+
if (syscall(SYS_close_range, fd_from, ~0U, 0) == 0)
51+
#else
52+
if (close_range(fd_from, ~0U, 0) == 0)
53+
#endif
54+
return true;
55+
56+
if (errno == ENOSYS)
57+
close_range_supported = false;
58+
}
59+
#endif
60+
61+
// second method, read fds list from /proc
62+
DIR *dir = opendir("/proc/self/fd");
63+
if (dir) {
64+
const int dir_fd = dirfd(dir);
65+
struct dirent *ent;
66+
while ((ent = readdir(dir)) != NULL) {
67+
char *end = NULL;
68+
unsigned long fd = strtoul(ent->d_name, &end, 10);
69+
if (end == NULL || *end)
70+
continue;
71+
if (fd >= fd_from && fd != dir_fd)
72+
close(fd);
73+
}
74+
closedir(dir);
75+
return true;
76+
}
77+
78+
// third method, use just a loop
79+
struct rlimit limit;
80+
if (getrlimit(RLIMIT_NOFILE, &limit) < 0)
81+
return false;
82+
for (int fd = fd_from; fd < limit.rlim_cur; ++ fd)
83+
close(fd);
84+
85+
return true;
86+
}

ocaml/forkexecd/helper/close_from.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (C) Citrix Systems Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*/
14+
15+
#pragma once
16+
17+
#include <stdbool.h>
18+
19+
bool close_fds_from(int fd);

0 commit comments

Comments
 (0)