1
/* IVLE - Informatics Virtual Learning Environment
2
* Copyright (C) 2007-2008 The University of Melbourne
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
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 General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* Author: Tom Conway, Matt Giuca
22
* This program runs a given program in a given working dir.
23
* First, it chroots to a jail path and setuids to a given user ID.
24
* This is intented to provide a safe execution environment for arbitrary
25
* programs and scripts.
27
* Scripts (such as Python programs) should be executed by supplying
28
* "/usr/bin/python" as the program, and the script as the first argument.
30
* Usage: trampoline uid jail-path working-path program [args...]
31
* Must run as root. Will safely setuid to the supplied uid, checking that it
32
* is not root. Recommended that the file is set up as follows:
33
* sudo chown root:root trampoline; sudo chroot +s trampoline
41
/* conf.h is admin-configured by the setup process.
42
* It defines jail_base.
48
#define ARG_JAILPATH 2
56
/* Returns TRUE if the given uid is allowed to execute trampoline.
57
* Only root or the web server should be allowed to execute.
58
* This is determined by the whitelist allowed_uids in conf.h.
60
int uid_allowed(int uid)
63
/* root is always allowed to execute trampoline */
66
/* loop over all allowed_uids */
67
for (i=(sizeof(allowed_uids)/sizeof(*allowed_uids))-1; i>=0; i--)
69
if (allowed_uids[i] == uid)
72
/* default to disallowing */
76
int main(int argc, char* const argv[])
81
/* Disallow execution from all users but the whitelisted ones, and root */
82
if (!uid_allowed(getuid()))
84
fprintf(stderr, "only the web server may execute trampoline\n");
88
/* Args check and usage */
91
fprintf(stderr, "usage: %s <uid> <jail> <cwd> <program> [args...]\n",
96
/* Disallow suiding to the root user */
97
uid = atoi(argv[ARG_UID]);
100
fprintf(stderr, "cannot set up a jail as root\n");
108
* 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)))
115
fprintf(stderr, "bad jail path: %s\n", jailpath);
119
/* chroot into the jail.
120
* Henceforth this process, and its children, cannot see anything above
122
if (chroot(jailpath))
124
perror("could not chroot");
128
/* chdir into the specified working directory */
129
if (chdir(argv[ARG_CWD]))
131
perror("could not chdir");
135
/* setuid to the given user ID.
136
* Henceforth we will be running as this user instead of root.
140
perror("could not setuid");
144
/* exec (replace this process with the a new instance of the target
145
* program). Pass along all the arguments.
146
* Note that for script execution, the "program" will be the interpreter,
147
* and the first argument will be the script. */
148
execv(argv[ARG_PROG], argv + ARG_PROG);
150
/* nb exec won't return unless there was an error */
151
perror("could not exec");