-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcjail-init.c
123 lines (105 loc) · 2.38 KB
/
cjail-init.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
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/mount.h>
#include <libmount/libmount.h>
static int
revcmp (const void *_a, const void *_b)
{
const char *const *a = _a, *const *b = _b;
return strcmp (*b, *a);
}
char **
getmounts (const char *dir)
{
int dirlen = strlen (dir);
char **out = NULL;
int outlen = 0;
struct libmnt_table *tb;
struct libmnt_iter *itr;
struct libmnt_fs *fs = NULL;
while (dirlen && dir[dirlen-1] == '/')
dirlen--;
tb = mnt_new_table ();
if (mnt_table_parse_file (tb, "/proc/self/mountinfo")) {
fprintf (stderr, "can't parse mountinfo\n");
mnt_free_table (tb);
return NULL;
}
itr = mnt_new_iter (MNT_ITER_BACKWARD);
if (!itr) {
fprintf (stderr, "mnt_new_iter failed\n");
mnt_free_table (tb);
return NULL;
}
while(mnt_table_next_fs (tb, itr, &fs) == 0) {
const char *target = mnt_fs_get_target (fs);
if (strncmp (target, dir, dirlen)
|| (target[dirlen] != '/' && target[dirlen] != '\0'))
continue;
if (!(out = realloc (out, (outlen + 1) * sizeof (out[0])))
|| !(out[outlen] = strdup (target))) {
fprintf (stderr, "malloc failed\n");
exit (1);
}
outlen++;
}
if (!(out = realloc (out, (outlen + 1) * sizeof (out[0])))) {
fprintf (stderr, "malloc failed\n");
exit (1);
}
out[outlen] = NULL;
qsort (out, outlen, sizeof (out[0]), revcmp);
mnt_free_iter (itr);
mnt_free_table (tb);
return out;
}
int
unmountold (void)
{
char **mps, **mp;
int err = 0;
mps = getmounts ("/root/oldroot");
if (!mps)
return -1;
for (mp = mps; *mp; mp++) {
if (!err && (err = umount (*mp)))
perror (*mp);
free (*mp);
}
free (mps);
return err;
}
int
main (int argc, char **argv)
{
uid_t uid;
char **av;
int i;
if (getpid () != 1) {
fprintf (stderr, "%s: I should have PID 1\n", argv[0]);
exit (1);
}
if (argc < 3) {
fprintf (stderr, "usage: %s uid prog [arg ...]\n", argv[0]);
exit (1);
}
uid = atoi (argv[1]);
av = malloc ((argc - 1) * sizeof (av[0]));
for (i = 2; i < argc; i++)
av[i-2] = argv[i];
av[i-2] = NULL;
mount ("proc", "/proc", "proc", 0, NULL);
mount ("tmp", "/tmp", "tmpfs", 0, NULL);
unmountold ();
if (setuid (uid)) {
perror ("setuid");
exit (1);
}
execvp (av[0], av);
perror (av[0]);
exit (1);
}