37
37
#include <stdlib.h>
38
38
#include <string.h>
39
39
#include <unistd.h>
40
#include <sys/types.h>
43
#include <sys/resource.h>
41
46
/* conf.h is admin-configured by the setup process.
42
47
* It defines jail_base.
48
#define ARG_JAILPATH 2
56
51
/* Returns TRUE if the given uid is allowed to execute trampoline.
57
52
* Only root or the web server should be allowed to execute.
58
53
* This is determined by the whitelist allowed_uids in conf.h.
71
/* Turn the process into a daemon using the standard
78
/* already a daemon */
79
if ( getppid() == 1 ) return;
81
/* Fork off the parent process */
86
/* If we got a good PID, then we can exit the parent process. */
91
/* At this point we are executing as the child process */
93
/* Change the file mode mask */
96
/* Create a new SID for the child process */
102
/* Change the current working directory. This prevents the current
103
directory from being locked; hence not being able to remove it. */
104
if ((chdir("/")) < 0) {
108
/* Redirect standard files to /dev/null */
109
freopen( "/dev/null", "r", stdin);
110
freopen( "/dev/null", "w", stdout);
111
freopen( "/dev/null", "w", stderr);
114
static void usage(const char* nm)
117
"usage: %s [-d] [-u] <uid> <jail> <cwd> <program> [args...]\n", nm);
76
121
int main(int argc, char* const argv[])
131
char canonical_jailpath[PATH_MAX];
81
133
/* Disallow execution from all users but the whitelisted ones, and root */
82
134
if (!uid_allowed(getuid()))
88
140
/* Args check and usage */
91
fprintf(stderr, "usage: %s <uid> <jail> <cwd> <program> [args...]\n",
146
if (strcmp(argv[arg_num], "-d") == 0)
156
if (strcmp(argv[arg_num], "-u") == 0)
165
uid = atoi(argv[arg_num++]);
166
jailpath = argv[arg_num++];
167
work_dir = argv[arg_num++];
168
prog = argv[arg_num];
169
args = argv + arg_num;
96
171
/* Disallow suiding to the root user */
97
uid = atoi(argv[ARG_UID]);
100
174
fprintf(stderr, "cannot set up a jail as root\n");
108
* Begin with jail_base
178
/* Jail path must be an absolute path,
179
* and it must begin with jail_base.
110
jailpath = argv[ARG_JAILPATH];
111
if (strlen(jailpath) < 1 || jailpath[0] != '/'
112
|| strstr(jailpath, "/..")
113
|| strncmp(jailpath, jail_base, strlen(jail_base)))
181
if (norm(canonical_jailpath, PATH_MAX, jailpath) != 0)
183
fprintf(stderr, "bad jail path: %s\n", jailpath);
186
if (strncmp(canonical_jailpath, jail_base, strlen(jail_base)))
115
188
fprintf(stderr, "bad jail path: %s\n", jailpath);
119
192
/* chroot into the jail.
120
193
* Henceforth this process, and its children, cannot see anything above
122
if (chroot(jailpath))
194
* canoncial_jailpath. */
195
if (chroot(canonical_jailpath))
124
197
perror("could not chroot");
128
201
/* chdir into the specified working directory */
129
if (chdir(argv[ARG_CWD]))
131
204
perror("could not chdir");
135
208
/* setuid to the given user ID.
136
209
* Henceforth we will be running as this user instead of root.
213
perror("could not setgid");
140
219
perror("could not setuid");
228
/* set user resource limits */
232
/* Process size in virtual memory */
233
l.rlim_cur = 64 * 1024 * 1024; /* 64Mb */
234
l.rlim_max = 72 * 1024 * 1024; /* 64Mb */
235
if (setrlimit(RLIMIT_AS, &l))
237
perror("could not setrlimit/RLIMIT_AS");
244
if (setrlimit(RLIMIT_CORE, &l))
246
perror("could not setrlimit/RLIMIT_CORE");
253
if (setrlimit(RLIMIT_CPU, &l))
255
perror("could not setrlimit/RLIMIT_CPU");
260
l.rlim_cur = 64 * 1024 * 1024; /* 64Mb */
261
l.rlim_max = 72 * 1024 * 1024; /* 72Mb */
262
if (setrlimit(RLIMIT_FSIZE, &l))
264
perror("could not setrlimit/RLIMIT_FSIZE");
144
269
/* exec (replace this process with the a new instance of the target
145
270
* program). Pass along all the arguments.
146
271
* Note that for script execution, the "program" will be the interpreter,
147
272
* and the first argument will be the script. */
148
execv(argv[ARG_PROG], argv + ARG_PROG);
275
/* XXX if (daemon_mode) use syslog? */
150
276
/* nb exec won't return unless there was an error */
151
277
perror("could not exec");