121
109
static void usage(const char* nm)
124
"usage: %s [-d] [-u] <uid> <jail> <cwd> <program> [args...]\n", nm);
112
"usage: %s [-d] <uid> <jail> <cwd> <program> [args...]\n", nm);
128
#ifdef IVLE_AUFS_JAILS
129
/* Die more pleasantly if mallocs fail. */
130
void *die_if_null(void *ptr)
134
perror("not enough memory");
140
/* Find the path of the user components of a jail, given a mountpoint. */
141
char *jail_src(const char* jailpath)
147
srclen = strlen(jail_src_base);
148
dstlen = strlen(jail_base);
150
src = die_if_null(malloc(strlen(jailpath) + (srclen - dstlen) + 1));
151
strcpy(src, jail_src_base);
152
strcat(src, jailpath+dstlen);
157
/* Check for the validity of a jail in the given path, mounting it if it looks
159
* TODO: Updating /etc/mtab would be nice. */
160
void mount_if_needed(const char* jailpath)
166
/* Check if there is something useful in the jail. If not, it's probably
168
jaillib = die_if_null(malloc(strlen(jailpath) + 5));
169
sprintf(jaillib, "%s/lib", jailpath);
171
if (access(jaillib, F_OK))
173
/* No /lib? Mustn't be mounted. Mount it, creating the dir if needed. */
174
if (access(jailpath, F_OK))
176
if(mkdir(jailpath, 0755))
178
syslog(LOG_ERR, "could not create mountpoint %s\n", jailpath);
179
perror("could not create jail mountpoint");
182
syslog(LOG_NOTICE, "created mountpoint %s\n", jailpath);
185
jailsrc = jail_src(jailpath);
186
mountdata = die_if_null(malloc(3 + strlen(jailsrc) + 4 + strlen(jail_system) + 3 + 1));
187
sprintf(mountdata, "br:%s=rw:%s=ro", jailsrc, jail_system);
188
if (mount("none", jailpath, "aufs", 0, mountdata))
190
syslog(LOG_ERR, "could not mount %s\n", jailpath);
191
perror("could not mount");
195
syslog(LOG_INFO, "mounted %s\n", jailpath);
203
#endif /* IVLE_AUFS_JAILS */
205
/* Unsets any signal mask applied by the parent process */
206
int unmask_signals(void)
210
sigset = die_if_null(malloc(sizeof(sigset_t)));
212
result = sigprocmask(SIG_SETMASK, sigset, NULL);
214
printf("%d", result);
218
116
int main(int argc, char* const argv[])
275
/* Jail path must be an absolute path,
276
* and it must begin with jail_base.
165
* Begin with jail_base
278
if (norm(canonical_jailpath, PATH_MAX, jailpath) != 0)
280
fprintf(stderr, "bad jail path: %s\n", jailpath);
283
if (strncmp(canonical_jailpath, jail_base, strlen(jail_base)))
285
fprintf(stderr, "bad jail path: %s\n", jailpath);
289
openlog("trampoline", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
291
#ifdef IVLE_AUFS_JAILS
292
mount_if_needed(canonical_jailpath);
293
#endif /* IVLE_AUFS_JAILS */
167
if (strlen(jailpath) < 1 || jailpath[0] != '/'
168
|| strstr(jailpath, "/..")
169
|| strncmp(jailpath, jail_base, strlen(jail_base)))
171
fprintf(stderr, "bad jail path: %s\n", jailpath);
295
175
/* chroot into the jail.
296
176
* Henceforth this process, and its children, cannot see anything above
297
* canoncial_jailpath. */
298
if (chroot(canonical_jailpath))
178
if (chroot(jailpath))
300
syslog(LOG_ERR, "chroot to %s failed\n", canonical_jailpath);
302
180
perror("could not chroot");
313
191
/* setuid to the given user ID.
314
192
* Henceforth we will be running as this user instead of root.
318
perror("could not setgid");
324
196
perror("could not setuid");
328
/* set user resource limits */
332
/* Process adress space in memory */
333
l.rlim_cur = 192 * 1024 * 1024; /* 192MiB */
334
l.rlim_max = 256 * 1024 * 1024; /* 256MiB */
335
if (setrlimit(RLIMIT_AS, &l))
337
perror("could not setrlimit/RLIMIT_AS");
341
/* Process data segment in memory
342
* Note: This requires a kernel patch to work correctly otherwise it is
343
* ineffective (thus you are only limited by RLIMIT_AS)
345
l.rlim_cur = 192 * 1024 * 1024; /* 192MiB */
346
l.rlim_max = 256 * 1024 * 1024; /* 256MiB */
347
if (setrlimit(RLIMIT_DATA, &l))
349
perror("could not setrlimit/RLIMIT_DATA");
354
l.rlim_cur = 0; /* No core files */
355
l.rlim_max = 0; /* No core files */
356
if (setrlimit(RLIMIT_CORE, &l))
358
perror("could not setrlimit/RLIMIT_CORE");
363
l.rlim_cur = 25; /* 25 Seconds */
364
l.rlim_max = 30; /* 30 Seconds */
365
if (setrlimit(RLIMIT_CPU, &l))
367
perror("could not setrlimit/RLIMIT_CPU");
372
l.rlim_cur = 64 * 1024 * 1024; /* 64MiB */
373
l.rlim_max = 72 * 1024 * 1024; /* 72MiB */
374
if (setrlimit(RLIMIT_FSIZE, &l))
376
perror("could not setrlimit/RLIMIT_FSIZE");
381
/* Remove any signal handler masks so we can send signals to the child */
384
perror("could not unmask signals");
388
/* If everything was OK daemonize (if required) */