-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
devices.cpp
368 lines (297 loc) · 11.3 KB
/
devices.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdint.h>
#include <list>
#include <set>
#include <string>
#include <vector>
#include <process/defer.hpp>
#include <process/future.hpp>
#include <process/owned.hpp>
#include <process/shared.hpp>
#include <stout/error.hpp>
#include <stout/foreach.hpp>
#include <stout/hashset.hpp>
#include <stout/nothing.hpp>
#include <stout/option.hpp>
#include <stout/os.hpp>
#include <stout/try.hpp>
#include "linux/cgroups.hpp"
#include "slave/flags.hpp"
#include "slave/containerizer/containerizer.hpp"
#include "slave/containerizer/mesos/isolator.hpp"
#include "slave/containerizer/mesos/isolators/cgroups/devices.hpp"
using cgroups::devices::Entry;
using mesos::slave::ContainerConfig;
using mesos::slave::ContainerLaunchInfo;
using mesos::slave::ContainerLimitation;
using mesos::slave::ContainerState;
using mesos::slave::Isolator;
using mesos::internal::slave::Containerizer;
using process::Failure;
using process::Future;
using process::PID;
using process::Shared;
using std::list;
using std::set;
using std::string;
using std::vector;
namespace mesos {
namespace internal {
namespace slave {
// The default list of devices to whitelist when device isolation is
// turned on. The full list of devices can be found here:
// https://www.kernel.org/doc/Documentation/devices.txt
//
// Device whitelisting is described here:
// https://www.kernel.org/doc/Documentation/cgroup-v1/devices.txt
static const char* DEFAULT_WHITELIST_ENTRIES[] = {
"c *:* m", // Make new character devices.
"b *:* m", // Make new block devices.
"c 5:1 rwm", // /dev/console
"c 4:0 rwm", // /dev/tty0
"c 4:1 rwm", // /dev/tty1
"c 136:* rwm", // /dev/pts/*
"c 5:2 rwm", // /dev/ptmx
"c 10:200 rwm", // /dev/net/tun
"c 1:3 rwm", // /dev/null
"c 1:5 rwm", // /dev/zero
"c 1:7 rwm", // /dev/full
"c 5:0 rwm", // /dev/tty
"c 1:9 rwm", // /dev/urandom
"c 1:8 rwm", // /dev/random
};
CgroupsDevicesIsolatorProcess::CgroupsDevicesIsolatorProcess(
const Flags& _flags,
const string& _hierarchy)
: ProcessBase(process::ID::generate("cgroups-devices-isolator")),
flags(_flags),
hierarchy(_hierarchy) {}
Try<Isolator*> CgroupsDevicesIsolatorProcess::create(const Flags& flags)
{
// Prepare the cgroups device hierarchy.
Try<string> hierarchy = cgroups::prepare(
flags.cgroups_hierarchy,
"devices",
flags.cgroups_root);
if (hierarchy.isError()) {
return Error("Failed to prepare hierarchy for 'devices' subsystem: " +
hierarchy.error());
}
// Ensure that no other subsystem is attached to the hierarchy.
Try<set<string>> subsystems = cgroups::subsystems(hierarchy.get());
if (subsystems.isError()) {
return Error("Failed to get subsystems attached to hierarchy"
" '" + stringify(hierarchy.get()) + "': " +
subsystems.error());
} else if (subsystems->size() != 1) {
return Error("Unexpected subsystems attached to hierarchy"
" '" + stringify(hierarchy.get()) + "': " +
stringify(subsystems.get()));
}
process::Owned<MesosIsolatorProcess> process(
new CgroupsDevicesIsolatorProcess(
flags,
hierarchy.get()));
return new MesosIsolator(process);
}
Future<Nothing> CgroupsDevicesIsolatorProcess::recover(
const list<ContainerState>& states,
const hashset<ContainerID>& orphans)
{
foreach (const ContainerState& state, states) {
const ContainerID& containerId = state.container_id();
const string cgroup = path::join(flags.cgroups_root, containerId.value());
Try<bool> exists = cgroups::exists(hierarchy, cgroup);
if (exists.isError()) {
foreachvalue (Info* info, infos) {
delete info;
}
infos.clear();
return Failure("Failed to check cgroup for container '" +
stringify(containerId) + "'");
}
if (!exists.get()) {
// This may occur if the executor has exited and the isolator
// has destroyed the cgroup but the slave dies before noticing
// this. This will be detected when the containerizer tries to
// monitor the executor's pid.
VLOG(1) << "Couldn't find cgroup for container " << containerId;
continue;
}
infos[containerId] = new Info(containerId, cgroup);
}
// Remove orphan cgroups.
Try<vector<string>> cgroups = cgroups::get(hierarchy, flags.cgroups_root);
if (cgroups.isError()) {
foreachvalue (Info* info, infos) {
delete info;
}
infos.clear();
return Failure(cgroups.error());
}
foreach (const string& cgroup, cgroups.get()) {
// Ignore the slave cgroup (see the --agent_subsystems flag).
// TODO(idownes): Remove this when the cgroups layout is updated,
// see MESOS-1185.
if (cgroup == path::join(flags.cgroups_root, "slave")) {
continue;
}
ContainerID containerId;
containerId.set_value(Path(cgroup).basename());
if (infos.contains(containerId)) {
continue;
}
// Known orphan cgroups will be destroyed by the containerizer
// using the normal cleanup path. See MESOS-2367 for details.
if (orphans.contains(containerId)) {
infos[containerId] = new Info(containerId, cgroup);
continue;
}
LOG(INFO) << "Removing unknown orphaned cgroup '" << cgroup << "'";
// We don't wait on the destroy as we don't want to block recovery.
cgroups::destroy(hierarchy, cgroup, cgroups::DESTROY_TIMEOUT);
}
return Nothing();
}
Future<Option<ContainerLaunchInfo>> CgroupsDevicesIsolatorProcess::prepare(
const ContainerID& containerId,
const mesos::slave::ContainerConfig& containerConfig)
{
if (infos.contains(containerId)) {
return Failure("Container has already been prepared");
}
// TODO(bmahler): Don't insert into 'infos' unless we create the
// cgroup successfully. It's safe for now because 'cleanup' gets
// called if we return a Failure, but cleanup will fail because the
// cgroup does not exist when cgroups::destroy is called.
Info* info = new Info(
containerId, path::join(flags.cgroups_root, containerId.value()));
infos[containerId] = info;
Try<bool> exists = cgroups::exists(hierarchy, info->cgroup);
if (exists.isError()) {
return Failure("Failed to prepare isolator: " + exists.error());
} else if (exists.get()) {
return Failure("Failed to prepare isolator: cgroup already exists");
}
Try<Nothing> create = cgroups::create(hierarchy, info->cgroup);
if (create.isError()) {
return Failure("Failed to prepare isolator: " + create.error());
}
// Chown the cgroup so the executor can create nested cgroups. Do
// not recurse so the control files are still owned by the slave
// user and thus cannot be changed by the executor.
if (containerConfig.has_user()) {
Try<Nothing> chown = os::chown(
containerConfig.user(),
path::join(hierarchy, info->cgroup),
false);
if (chown.isError()) {
return Failure("Failed to prepare isolator: " + chown.error());
}
}
// When a devices cgroup is first created, its whitelist inherits
// all devices from its parent's whitelist (i.e., "a *:* rwm" by
// default). In theory, we should be able to add and remove devices
// from the whitelist by writing to the respective `devices.allow`
// and `devices.deny` files associated with the cgroup. However, the
// semantics of the whitelist are such that writing to the deny file
// will only remove entries in the whitelist that are explicitly
// listed in there (i.e., denying "b 1:3 rwm" when the whitelist
// only contains "a *:* rwm" will not modify the whitelist because
// "b 1:3 rwm" is not explicitly listed). Although the whitelist
// doesn't change, access to the device is still denied as expected
// (there is just no way of querying the system to detect it).
// Because of this, we first deny access to all devices and
// selectively add some back in so we can control the entries in the
// whitelist explicitly.
cgroups::devices::Entry all;
all.selector.type = Entry::Selector::Type::ALL;
all.selector.major = None();
all.selector.minor = None();
all.access.read = true;
all.access.write = true;
all.access.mknod = true;
Try<Nothing> deny = cgroups::devices::deny(hierarchy, info->cgroup, all);
if (deny.isError()) {
return Failure("Failed to deny all devices: " + deny.error());
}
foreach (const char* _entry, DEFAULT_WHITELIST_ENTRIES) {
Try<cgroups::devices::Entry> entry =
cgroups::devices::Entry::parse(_entry);
CHECK_SOME(entry);
Try<Nothing> allow =
cgroups::devices::allow(hierarchy, info->cgroup, entry.get());
if (allow.isError()) {
return Failure("Failed to whitelist default device"
" '" + stringify(entry.get()) + "': " + allow.error());
}
}
return None();
}
Future<Nothing> CgroupsDevicesIsolatorProcess::isolate(
const ContainerID& containerId,
pid_t pid)
{
if (!infos.contains(containerId)) {
return Failure("Unknown container");
}
Info* info = CHECK_NOTNULL(infos[containerId]);
Try<Nothing> assign = cgroups::assign(
hierarchy, info->cgroup, pid);
if (assign.isError()) {
LOG(ERROR) << "Failed to assign container '" << info->containerId << "'"
<< " to cgroup '" << path::join(hierarchy, info->cgroup) << "': "
<< assign.error();
return Failure("Failed to isolate container: " + assign.error());
}
return Nothing();
}
Future<Nothing> CgroupsDevicesIsolatorProcess::cleanup(
const ContainerID& containerId)
{
// Multiple calls may occur during test clean up.
if (!infos.contains(containerId)) {
VLOG(1) << "Ignoring cleanup request for unknown container " << containerId;
return Nothing();
}
Info* info = CHECK_NOTNULL(infos[containerId]);
return cgroups::destroy(hierarchy, info->cgroup, cgroups::DESTROY_TIMEOUT)
.onAny(defer(PID<CgroupsDevicesIsolatorProcess>(this),
&CgroupsDevicesIsolatorProcess::_cleanup,
containerId,
lambda::_1));
}
Future<Nothing> CgroupsDevicesIsolatorProcess::_cleanup(
const ContainerID& containerId,
const Future<Nothing>& future)
{
if (!infos.contains(containerId)) {
return Failure("Unknown container");
}
Info* info = CHECK_NOTNULL(infos[containerId]);
if (!future.isReady()) {
return Failure(
"Failed to clean up container " + stringify(containerId) + ": " +
(future.isFailed() ? future.failure() : "discarded"));
}
delete info;
infos.erase(containerId);
return Nothing();
}
} // namespace slave {
} // namespace internal {
} // namespace mesos {