-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnicos-sandbox-helper.c
173 lines (142 loc) · 5.48 KB
/
nicos-sandbox-helper.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
/******************************************************************************
* NICOS, the Networked Instrument Control System of the FRM-II
* Copyright (c) 2009-2017 by the NICOS contributors (see AUTHORS)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Module authors:
* Georg Brandl <[email protected]>
*
*****************************************************************************/
/*
A filesystem/network sandbox for NICOS simulation processes, using Linux
unshare() namespaces.
Needs at least 4 arguments:
- temporary chroot directory
- numeric uid to change to
- numeric gid to change to
- name of binary to exec() in the sandboxed environment
- all other arguments are passed as-is to exec()
The helper does the following before exec()ing the new binary:
- "unshare" the network namespace, which means that the new process cannot
use existing network interfaces
- "unshare" the mount namespace, so that we can remount readonly without
influencing the whole world
- bind-mount the whole filesystem hierarchy to a temporary directory
- set all new mounts to readonly, except for temporaries
- chroot into the new root
- set user/group to desired values
- exec the sandboxed binary
A Linux kernel (and headers) version of 2.6.32 is required.
*/
/* to get access to unshare() */
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <unistd.h>
#include <sched.h>
#include <mntent.h>
#include <sys/mount.h>
/* compatibility for glibc < 2.12 */
#ifndef MS_PRIVATE
#include <linux/fs.h>
#endif
static void
make_mounts_readonly(const char *prefix)
{
struct mntent *ent;
/* Open list of mountpoints. */
FILE *mounts = setmntent("/proc/self/mounts", "r");
if (mounts == NULL)
err(1, "could not get list of mountpoints");
while ((ent = getmntent(mounts)) != NULL) {
int flags;
/* Skip mounts not in the new root dir. */
if (strstr(ent->mnt_dir, prefix) != ent->mnt_dir)
continue;
/* Skip tmpfs. */
if (!strcmp(ent->mnt_type, "tmpfs"))
continue;
flags = MS_BIND | MS_REMOUNT | MS_RDONLY;
/* Restore existing options. */
if (hasmntopt(ent, "nodev"))
flags |= MS_NODEV;
if (hasmntopt(ent, "noexec"))
flags |= MS_NOEXEC;
if (hasmntopt(ent, "nosuid"))
flags |= MS_NOSUID;
if (hasmntopt(ent, "noatime"))
flags |= MS_NOATIME;
if (hasmntopt(ent, "nodiratime"))
flags |= MS_NODIRATIME;
if (hasmntopt(ent, "relatime"))
flags |= MS_RELATIME;
if (mount(NULL, ent->mnt_dir, NULL, flags, NULL) < 0) {
/* Certain errors are ok here. */
if (errno != EACCES && errno != EINVAL && errno != ESTALE && errno != EPERM)
err(1, "could not set mountpoint %s read-only", ent->mnt_dir);
}
}
endmntent(mounts);
}
int
main(int argc, char **argv)
{
char *ptr;
long id;
if (argc < 5)
errx(1, "usage: %s rootdir uid gid binary [args...]", argv[0]);
/* Set up the new mount and network namespaces. */
if (unshare(CLONE_NEWNS | CLONE_NEWNET | CLONE_NEWIPC) < 0)
err(1, "could not create namespaces (is the binary setuid root?)");
/* Make our copy of the rootfs mount (and all others) private, so
our changes will not affect the parent namespace. */
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) < 0)
err(1, "could not make mounts private");
/* Mount the root filesystem (recursively) at the chroot target. */
if (mount("/", argv[1], NULL, MS_BIND | MS_REC, NULL) < 0)
err(1, "could not create bind mount at %s", argv[1]);
/* Make all filesystems readonly, with exceptions. */
make_mounts_readonly(argv[1]);
/* Change to the chroot directory. */
if (chdir(argv[1]) < 0)
err(1, "could not chdir into new root");
/* Change the root directory. */
if (chroot(argv[1]) < 0)
err(1, "could not create chroot jail");
/* Set desired user and group IDs. */
id = strtol(argv[3], &ptr, 10);
if (*argv[3] == '\0' || *ptr != '\0')
errx(1, "invalid numeric group id '%s' given", argv[3]);
if (id == 0)
errx(1, "cannot use group id 0");
if (setgid(id) < 0)
err(1, "could not set new group id %ld", id);
id = strtol(argv[2], &ptr, 10);
if (*argv[2] == '\0' || *ptr != '\0')
errx(1, "invalid numeric user id '%s' given", argv[3]);
if (id == 0)
errx(1, "cannot use user id 0");
if (setuid(id) < 0)
err(1, "could not set new user id %ld", id);
/* Execute desired process in the new environment. */
if (execvp(argv[4], &argv[4]) < 0)
err(1, "could not exec new process");
/* unreachable */
return 1;
}