-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrootperl.c
242 lines (199 loc) · 6.08 KB
/
chrootperl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/************************************************************************/
/**
\file chrootperl.c
\version V1.0
\date 20.10.15
\brief Run Perl in a chroot jail
\copyright (c) UCL / Dr. Andrew C. R. Martin 2015
\author Dr. Andrew C. R. Martin
\par
Institute of Structural & Molecular Biology,
University College London,
Gower Street,
London.
WC1E 6BT.
\par
andrew.martin@ucl.ac.uk
andrew@bioinf.org.uk
**************************************************************************
This program is released under the GNU Public Licence V2.0 or above
**************************************************************************
Description:
============
This program must have SANDBOX defined with -D when compiled.
The executable must be owned by root and have the setuid bit set -
this is all done by the Makefile
**************************************************************************
Usage:
======
This program runs perl in a chroot sandbox. makesandbox.pl must be
run first to create the sandbox and to install required binaries and
libraries.
**************************************************************************
Revision History:
=================
- V1.0 20.10.15 Original
*************************************************************************/
/* Includes
*/
#define _BSD_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <errno.h>
#include <sys/types.h>
/************************************************************************/
/* Defines and macros
*/
#define MAXBUFF 1024
#ifndef SANDBOX
#error "SANDBOX must be defined with -D during compilation!"
#endif
/************************************************************************/
/* Globals
*/
/************************************************************************/
/* Prototypes
*/
int main(int argc, char **argv, char **env);
int CheckCommandLine(int argc, char **argv);
void Usage(void);
void CopyPerlScript(char *script, char *sandbox);
int RunPerlScript(char *sandbox, int argc, char **argv, char **env);
/************************************************************************/
/*>int main(int argc, char **argv, char **env)
--------------------------------------------
*//**
Main program to be run Perl in a chroot sandbox environment
- 20.10.15 Original By: ACRM
*/
int main(int argc, char **argv, char **env)
{
/* Check the command line */
if(!CheckCommandLine(argc, argv))
{
Usage();
return(0);
}
#ifdef DEBUG
fprintf(stderr,"SANDBOX: %s\n", SANDBOX);
#endif
/* Copy the Perl script into the sandbox */
CopyPerlScript(argv[1], SANDBOX);
RunPerlScript(SANDBOX, argc, argv, env);
return(0);
}
/************************************************************************/
/*>void CopyPerlScript(char *script, char *sandbox)
------------------------------------------------
*//**
\param[in] script The perl script to be run
\param[in] sandbox The sandbox directory
Copies the perl script to the sandbox/run directory
- 20.10.15 Original By: ACRM
*/
void CopyPerlScript(char *script, char *sandbox)
{
char cmd[MAXBUFF];
sprintf(cmd, "cp %s %s/run", script, sandbox);
system(cmd);
}
/************************************************************************/
/*>void Usage(void)
----------------
*//**
Prints a usage message
- 20.10.15 Original By: ACRM
*/
void Usage(void)
{
fprintf(stderr,"\nchrootperl V1.0 (c) 2015 UCL, Dr. Andrew \
C.R. Martin\n");
fprintf(stderr,"\nUsage: chrootperl program.pl [arguments...]\n");
fprintf(stderr,"\nRuns Perl in a chroot sandbox\n\n");
}
/************************************************************************/
/*>int CheckCommandLine(int argc, char **argv)
-------------------------------------------
*//**
\param[in] argc The argument count
\param[in] argv The arguments
\return 0: Error; 1: Success
- 20.10.15 Original By: ACRM
*/
int CheckCommandLine(int argc, char **argv)
{
if(argc < 2)
{
return(0);
}
else
{
int i;
for(i=0; i<argc; i++)
{
if(!strcmp(argv[i], "-h"))
{
return(0);
}
}
}
return(1);
}
/************************************************************************/
/*>int RunPerlScript(char *sandbox, int argc, char **argv, char **env)
-------------------------------------------------------------------
*//**
\param[in] sandbox The sandbox directory
\param[in] argc Argument count
\param[in] argv Arguments
\param[in] env Environment variables
\return Error code (0: OK)
Runs a perl script in a chroot environment
argv[0] (the name of this program) is replaced by 'perl'
argv[1] (the name of the perl script) has '/run/' prepended to it
(and any existing path stripped)
- 20.10.15 Original By: ACRM
*/
int RunPerlScript(char *sandbox, int argc, char **argv, char **env)
{
int retval = 0;
const char *m;
char cmd[MAXBUFF],
*chp;
/* Replace the chrootperl name with 'perl' */
argv[0] = "perl";
/* Strip the path from the perl script name if present */
if((chp=strrchr(argv[1], '/'))==NULL)
chp = argv[1];
else
chp++;
/* Replace the name of the perl script with the one in sandbox/run */
sprintf(cmd, "/run/%s", chp);
argv[1] = cmd;
#ifdef DEBUG
{
int i;
printf("ARGUMENTS: ");
for(i=0; i<argc; i++)
{
printf("%s ", argv[i]);
}
printf("\n");
}
#endif
if((m="chdir" ,retval=chdir(sandbox)) == 0)
{
if((m="chroot",retval=chroot(sandbox)) == 0)
{
if((m="setuid",retval=setuid(getuid())) == 0)
{
m="execve", execve("/bin/perl", argv, NULL);
}
}
}
perror(m);
return(retval);
}