75
/* Turn the process into a daemon using the standard
82
/* already a daemon */
83
if ( getppid() == 1 ) return;
85
/* Fork off the parent process */
90
/* If we got a good PID, then we can exit the parent process. */
95
/* At this point we are executing as the child process */
97
/* Change the file mode mask */
100
/* Create a new SID for the child process */
106
/* Change the current working directory. This prevents the current
107
directory from being locked; hence not being able to remove it. */
108
if ((chdir("/")) < 0) {
112
/* Redirect standard files to /dev/null */
113
freopen( "/dev/null", "r", stdin);
114
freopen( "/dev/null", "w", stdout);
115
freopen( "/dev/null", "w", stderr);
118
static void usage(const char* nm)
121
"usage: %s [-d] [-u] <uid> <jail> <cwd> <program> [args...]\n", nm);
125
#ifdef IVLE_AUFS_JAILS
126
/* Die more pleasantly if mallocs fail. */
127
void *die_if_null(void *ptr)
131
perror("not enough memory");
137
/* Find the path of the user components of a jail, given a mountpoint. */
138
char *jail_src(const char* jailpath)
144
srclen = strlen(jail_src_base);
145
dstlen = strlen(jail_base);
147
src = die_if_null(malloc(strlen(jailpath) + (srclen - dstlen) + 1));
148
strcpy(src, jail_src_base);
149
strcat(src, jailpath+dstlen);
154
/* Check for the validity of a jail in the given path, mounting it if it looks
156
* TODO: Updating /etc/mtab would be nice. */
157
void mount_if_needed(const char* jailpath)
163
/* Check if there is something useful in the jail. If not, it's probably
165
jaillib = die_if_null(malloc(strlen(jailpath) + 5));
166
sprintf(jaillib, "%s/lib", jailpath);
168
if (access(jaillib, F_OK))
170
/* No /lib? Mustn't be mounted. Mount it, creating the dir if needed. */
171
if (access(jailpath, F_OK))
173
if(mkdir(jailpath, 0755))
175
syslog(LOG_ERR, "could not create mountpoint %s\n", jailpath);
176
perror("could not create jail mountpoint");
179
syslog(LOG_NOTICE, "created mountpoint %s\n", jailpath);
182
jailsrc = jail_src(jailpath);
183
mountdata = die_if_null(malloc(3 + strlen(jailsrc) + 4 + strlen(jail_system) + 3 + 1));
184
sprintf(mountdata, "br:%s=rw:%s=ro", jailsrc, jail_system);
185
if (mount("none", jailpath, "aufs", 0, mountdata))
187
syslog(LOG_ERR, "could not mount %s\n", jailpath);
188
perror("could not mount");
192
syslog(LOG_INFO, "mounted %s\n", jailpath);
200
#endif /* IVLE_AUFS_JAILS */
202
76
int main(int argc, char* const argv[])
212
char canonical_jailpath[PATH_MAX];
214
81
/* Disallow execution from all users but the whitelisted ones, and root */
215
82
if (!uid_allowed(getuid()))
221
88
/* Args check and usage */
227
if (strcmp(argv[arg_num], "-d") == 0)
237
if (strcmp(argv[arg_num], "-u") == 0)
246
uid = atoi(argv[arg_num++]);
247
jailpath = argv[arg_num++];
248
work_dir = argv[arg_num++];
249
prog = argv[arg_num];
250
args = argv + arg_num;
91
fprintf(stderr, "usage: %s <uid> <jail> <cwd> <program> [args...]\n",
252
96
/* Disallow suiding to the root user */
97
uid = atoi(argv[ARG_UID]);
255
100
fprintf(stderr, "cannot set up a jail as root\n");
259
/* Jail path must be an absolute path,
260
* and it must begin with jail_base.
108
* Begin with jail_base
262
if (norm(canonical_jailpath, PATH_MAX, jailpath) != 0)
264
fprintf(stderr, "bad jail path: %s\n", jailpath);
267
if (strncmp(canonical_jailpath, jail_base, strlen(jail_base)))
269
fprintf(stderr, "bad jail path: %s\n", jailpath);
273
openlog("trampoline", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
275
#ifdef IVLE_AUFS_JAILS
276
mount_if_needed(canonical_jailpath);
277
#endif /* IVLE_AUFS_JAILS */
110
jailpath = argv[ARG_JAILPATH];
111
if (strlen(jailpath) < 1 || jailpath[0] != '/'
112
|| strstr(jailpath, "/..")
113
|| strncmp(jailpath, jail_base, strlen(jail_base)))
115
fprintf(stderr, "bad jail path: %s\n", jailpath);
279
119
/* chroot into the jail.
280
120
* Henceforth this process, and its children, cannot see anything above
281
* canoncial_jailpath. */
282
if (chroot(canonical_jailpath))
122
if (chroot(jailpath))
284
syslog(LOG_ERR, "chroot to %s failed\n", canonical_jailpath);
286
124
perror("could not chroot");
290
128
/* chdir into the specified working directory */
129
if (chdir(argv[ARG_CWD]))
293
131
perror("could not chdir");
297
135
/* setuid to the given user ID.
298
136
* Henceforth we will be running as this user instead of root.
302
perror("could not setgid");
308
140
perror("could not setuid");
317
/* set user resource limits */
321
/* Process size in virtual memory */
322
l.rlim_cur = 64 * 1024 * 1024; /* 64Mb */
323
l.rlim_max = 72 * 1024 * 1024; /* 64Mb */
324
if (setrlimit(RLIMIT_AS, &l))
326
perror("could not setrlimit/RLIMIT_AS");
333
if (setrlimit(RLIMIT_CORE, &l))
335
perror("could not setrlimit/RLIMIT_CORE");
342
if (setrlimit(RLIMIT_CPU, &l))
344
perror("could not setrlimit/RLIMIT_CPU");
349
l.rlim_cur = 64 * 1024 * 1024; /* 64Mb */
350
l.rlim_max = 72 * 1024 * 1024; /* 72Mb */
351
if (setrlimit(RLIMIT_FSIZE, &l))
353
perror("could not setrlimit/RLIMIT_FSIZE");
358
144
/* exec (replace this process with the a new instance of the target
359
145
* program). Pass along all the arguments.
360
146
* Note that for script execution, the "program" will be the interpreter,
361
147
* and the first argument will be the script. */
148
execv(argv[ARG_PROG], argv + ARG_PROG);
364
150
/* nb exec won't return unless there was an error */
365
syslog(LOG_ERR, "exec of %s in %s failed", prog, canonical_jailpath);
367
151
perror("could not exec");