···
1
+
(* From Obuilder see License file at end of document *)
2
+
let ( / ) = Eio.Path.( / )
3
+
let ( // ) = Filename.concat
5
+
type config = { fast_sync : bool }
8
+
let ch = Unix.open_process_in "uname -m" in
9
+
let arch = input_line ch in
10
+
match Unix.close_process_in ch with
11
+
| Unix.WEXITED 0 -> String.trim arch
12
+
| _ -> failwith "Failed to get arch with 'uname -m'"
16
+
match get_machine () with
17
+
| "x86_64" -> [ "SCMP_ARCH_X86_64"; "SCMP_ARCH_X86"; "SCMP_ARCH_X32" ]
18
+
| "aarch64" -> [ "SCMP_ARCH_AARCH64"; "SCMP_ARCH_ARM" ]
22
+
let secret_file id = "secret-" ^ string_of_int id
24
+
module Json_config = struct
25
+
let mount ?(options = []) ~ty ~src dst =
28
+
("destination", `String dst);
29
+
("type", `String ty);
30
+
("source", `String src);
31
+
("options", `List (List.map (fun x -> `String x) options));
34
+
let strings xs = `List (List.map (fun x -> `String x) xs)
35
+
let namespace x = `Assoc [ ("type", `String x) ]
37
+
(* This is a subset of the capabilities that Docker uses by default.
38
+
These control what root can do in the container.
39
+
If the init process is non-root, permitted, effective and ambient sets are cleared.
40
+
See capabilities(7) for full details. *)
41
+
let default_linux_caps =
44
+
(* Make arbitrary changes to file UIDs and GIDs *)
46
+
(* Bypass file read, write, and execute permission checks. *)
48
+
(* Set SUID/SGID bits. *)
50
+
(* Bypass permission checks. *)
52
+
(* Create special files using mknod. *)
54
+
(* Make arbitrary manipulations of process GIDs. *)
56
+
(* Make arbitrary manipulations of process UIDs. *)
58
+
(* Set arbitrary capabilities on a file. *)
60
+
(* Add any capability from bounding set to inheritable set. *)
64
+
(* Bypass permission checks for sending signals. *)
66
+
(* Write records to kernel auditing log. *)
67
+
(* Allowed by Docker, but disabled here (because we use host networking):
68
+
"CAP_NET_RAW"; (* Use RAW and PACKET sockets / bind to any address *)
69
+
"CAP_NET_BIND_SERVICE"; (* Bind a socket to Internet domain privileged ports. *)
73
+
let seccomp_syscalls ~fast_sync =
78
+
(* Sync calls are pointless for the builder, because if the computer crashes then we'll
79
+
just throw the build dir away and start again. And btrfs sync is really slow.
80
+
Based on https://bblank.thinkmo.de/using-seccomp-to-filter-sync-operations.html
81
+
Note: requires runc >= v1.0.0-rc92. *)
92
+
("action", `String "SCMP_ACT_ERRNO");
93
+
("errnoRet", `Int 0);
94
+
(* Return error "success" *)
99
+
let seccomp_policy =
102
+
("defaultAction", `String "SCMP_ACT_ALLOW");
103
+
("syscalls", `List (seccomp_syscalls ~fast_sync:true));
110
+
argv : string list;
112
+
network : string list;
115
+
entrypoint : string option;
118
+
let make { cwd; argv; hostname; network; user; env; entrypoint } ~config_dir
119
+
~results_dir : Yojson.Safe.t =
120
+
assert (entrypoint = None);
122
+
let uid, gid = user in
123
+
`Assoc [ ("uid", `Int uid); ("gid", `Int gid) ]
128
+
| [] -> [ "network" ]
130
+
Fmt.failwith "Unsupported network configuration %a"
131
+
Fmt.Dump.(list string)
134
+
let namespaces = network_ns @ [ "pid"; "ipc"; "uts"; "mount" ] in
137
+
("ociVersion", `String "1.0.1-dev");
141
+
("terminal", `Bool false);
143
+
("args", strings argv);
144
+
("env", strings env);
145
+
("cwd", `String cwd);
149
+
("bounding", strings default_linux_caps);
150
+
(* Limits capabilities gained on execve. *)
151
+
("effective", strings default_linux_caps);
152
+
(* Checked by kernel to decide access *)
153
+
("inheritable", strings default_linux_caps);
154
+
(* Preserved across an execve (if root, or cap in ambient set) *)
155
+
("permitted", strings default_linux_caps);
156
+
(* Limiting superset for the effective capabilities *)
163
+
("type", `String "RLIMIT_NOFILE");
164
+
("hard", `Int 1024);
165
+
("soft", `Int 1024);
168
+
("noNewPrivileges", `Bool false);
173
+
("path", `String (results_dir // "rootfs"));
174
+
("readonly", `Bool false);
176
+
("hostname", `String hostname);
181
+
[ (* TODO: copy to others? *) "nosuid"; "noexec"; "nodev" ]
182
+
~ty:"proc" ~src:"proc"
183
+
:: mount "/dev" ~ty:"tmpfs" ~src:"tmpfs"
184
+
~options:[ "nosuid"; "strictatime"; "mode=755"; "size=65536k" ]
185
+
:: mount "/dev/pts" ~ty:"devpts" ~src:"devpts"
198
+
(* This is how Docker does it. runc's default is a bit different. *)
199
+
~ty:"sysfs" ~src:"sysfs"
200
+
~options:[ "nosuid"; "noexec"; "nodev"; "ro" ]
201
+
:: mount "/sys/fs/cgroup" ~ty:"cgroup" ~src:"cgroup"
202
+
~options:[ "ro"; "nosuid"; "noexec"; "nodev" ]
203
+
:: mount "/dev/shm" ~ty:"tmpfs" ~src:"shm"
205
+
[ "nosuid"; "noexec"; "nodev"; "mode=1777"; "size=65536k" ]
206
+
:: mount "/dev/mqueue" ~ty:"mqueue" ~src:"mqueue"
207
+
~options:[ "nosuid"; "noexec"; "nodev" ]
208
+
:: mount "/etc/hosts" ~ty:"bind" ~src:(config_dir // "hosts")
209
+
~options:[ "ro"; "rbind"; "rprivate" ]
211
+
(if network = [ "host" ] then
213
+
mount "/etc/resolv.conf" ~ty:"bind" ~src:"/etc/resolv.conf"
214
+
~options:[ "ro"; "rbind"; "rprivate" ];
220
+
("namespaces", `List (List.map namespace namespaces));
228
+
"/proc/latency_stats";
229
+
"/proc/timer_list";
230
+
"/proc/timer_stats";
231
+
"/proc/sched_debug";
242
+
"/proc/sysrq-trigger";
244
+
("seccomp", seccomp_policy);
249
+
let next_id = ref 0
251
+
let spawn ~sw fs proc config dir =
252
+
let tmp = Filename.temp_dir ~perms:0o700 "shelter-run-" "" in
253
+
let eio_tmp = Eio.Path.(fs / tmp) in
254
+
let json_config = Json_config.make config ~config_dir:tmp ~results_dir:dir in
255
+
Eio.Path.save ~create:(`If_missing 0o644) (eio_tmp / "config.json")
256
+
(Yojson.Safe.pretty_to_string json_config ^ "\n");
257
+
Eio.Path.save ~create:(`If_missing 0o644) (eio_tmp / "hosts")
258
+
"127.0.0.1 localhost builder";
259
+
let id = string_of_int !next_id in
261
+
let cmd = [ "runc"; "--root"; "runc"; "run"; id ] in
262
+
Eio.Process.spawn ~sw proc ~cwd:eio_tmp cmd
266
+
Version 2.0, January 2004
267
+
https://www.apache.org/licenses/
269
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
273
+
"License" shall mean the terms and conditions for use, reproduction,
274
+
and distribution as defined by Sections 1 through 9 of this document.
276
+
"Licensor" shall mean the copyright owner or entity authorized by
277
+
the copyright owner that is granting the License.
279
+
"Legal Entity" shall mean the union of the acting entity and all
280
+
other entities that control, are controlled by, or are under common
281
+
control with that entity. For the purposes of this definition,
282
+
"control" means (i) the power, direct or indirect, to cause the
283
+
direction or management of such entity, whether by contract or
284
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
285
+
outstanding shares, or (iii) beneficial ownership of such entity.
287
+
"You" (or "Your") shall mean an individual or Legal Entity
288
+
exercising permissions granted by this License.
290
+
"Source" form shall mean the preferred form for making modifications,
291
+
including but not limited to software source code, documentation
292
+
source, and configuration files.
294
+
"Object" form shall mean any form resulting from mechanical
295
+
transformation or translation of a Source form, including but
296
+
not limited to compiled object code, generated documentation,
297
+
and conversions to other media types.
299
+
"Work" shall mean the work of authorship, whether in Source or
300
+
Object form, made available under the License, as indicated by a
301
+
copyright notice that is included in or attached to the work
302
+
(an example is provided in the Appendix below).
304
+
"Derivative Works" shall mean any work, whether in Source or Object
305
+
form, that is based on (or derived from) the Work and for which the
306
+
editorial revisions, annotations, elaborations, or other modifications
307
+
represent, as a whole, an original work of authorship. For the purposes
308
+
of this License, Derivative Works shall not include works that remain
309
+
separable from, or merely link (or bind by name) to the interfaces of,
310
+
the Work and Derivative Works thereof.
312
+
"Contribution" shall mean any work of authorship, including
313
+
the original version of the Work and any modifications or additions
314
+
to that Work or Derivative Works thereof, that is intentionally
315
+
submitted to Licensor for inclusion in the Work by the copyright owner
316
+
or by an individual or Legal Entity authorized to submit on behalf of
317
+
the copyright owner. For the purposes of this definition, "submitted"
318
+
means any form of electronic, verbal, or written communication sent
319
+
to the Licensor or its representatives, including but not limited to
320
+
communication on electronic mailing lists, source code control systems,
321
+
and issue tracking systems that are managed by, or on behalf of, the
322
+
Licensor for the purpose of discussing and improving the Work, but
323
+
excluding communication that is conspicuously marked or otherwise
324
+
designated in writing by the copyright owner as "Not a Contribution."
326
+
"Contributor" shall mean Licensor and any individual or Legal Entity
327
+
on behalf of whom a Contribution has been received by Licensor and
328
+
subsequently incorporated within the Work.
330
+
2. Grant of Copyright License. Subject to the terms and conditions of
331
+
this License, each Contributor hereby grants to You a perpetual,
332
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
333
+
copyright license to reproduce, prepare Derivative Works of,
334
+
publicly display, publicly perform, sublicense, and distribute the
335
+
Work and such Derivative Works in Source or Object form.
337
+
3. Grant of Patent License. Subject to the terms and conditions of
338
+
this License, each Contributor hereby grants to You a perpetual,
339
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
340
+
(except as stated in this section) patent license to make, have made,
341
+
use, offer to sell, sell, import, and otherwise transfer the Work,
342
+
where such license applies only to those patent claims licensable
343
+
by such Contributor that are necessarily infringed by their
344
+
Contribution(s) alone or by combination of their Contribution(s)
345
+
with the Work to which such Contribution(s) was submitted. If You
346
+
institute patent litigation against any entity (including a
347
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
348
+
or a Contribution incorporated within the Work constitutes direct
349
+
or contributory patent infringement, then any patent licenses
350
+
granted to You under this License for that Work shall terminate
351
+
as of the date such litigation is filed.
353
+
4. Redistribution. You may reproduce and distribute copies of the
354
+
Work or Derivative Works thereof in any medium, with or without
355
+
modifications, and in Source or Object form, provided that You
356
+
meet the following conditions:
358
+
(a) You must give any other recipients of the Work or
359
+
Derivative Works a copy of this License; and
361
+
(b) You must cause any modified files to carry prominent notices
362
+
stating that You changed the files; and
364
+
(c) You must retain, in the Source form of any Derivative Works
365
+
that You distribute, all copyright, patent, trademark, and
366
+
attribution notices from the Source form of the Work,
367
+
excluding those notices that do not pertain to any part of
368
+
the Derivative Works; and
370
+
(d) If the Work includes a "NOTICE" text file as part of its
371
+
distribution, then any Derivative Works that You distribute must
372
+
include a readable copy of the attribution notices contained
373
+
within such NOTICE file, excluding those notices that do not
374
+
pertain to any part of the Derivative Works, in at least one
375
+
of the following places: within a NOTICE text file distributed
376
+
as part of the Derivative Works; within the Source form or
377
+
documentation, if provided along with the Derivative Works; or,
378
+
within a display generated by the Derivative Works, if and
379
+
wherever such third-party notices normally appear. The contents
380
+
of the NOTICE file are for informational purposes only and
381
+
do not modify the License. You may add Your own attribution
382
+
notices within Derivative Works that You distribute, alongside
383
+
or as an addendum to the NOTICE text from the Work, provided
384
+
that such additional attribution notices cannot be construed
385
+
as modifying the License.
387
+
You may add Your own copyright statement to Your modifications and
388
+
may provide additional or different license terms and conditions
389
+
for use, reproduction, or distribution of Your modifications, or
390
+
for any such Derivative Works as a whole, provided Your use,
391
+
reproduction, and distribution of the Work otherwise complies with
392
+
the conditions stated in this License.
394
+
5. Submission of Contributions. Unless You explicitly state otherwise,
395
+
any Contribution intentionally submitted for inclusion in the Work
396
+
by You to the Licensor shall be under the terms and conditions of
397
+
this License, without any additional terms or conditions.
398
+
Notwithstanding the above, nothing herein shall supersede or modify
399
+
the terms of any separate license agreement you may have executed
400
+
with Licensor regarding such Contributions.
402
+
6. Trademarks. This License does not grant permission to use the trade
403
+
names, trademarks, service marks, or product names of the Licensor,
404
+
except as required for reasonable and customary use in describing the
405
+
origin of the Work and reproducing the content of the NOTICE file.
407
+
7. Disclaimer of Warranty. Unless required by applicable law or
408
+
agreed to in writing, Licensor provides the Work (and each
409
+
Contributor provides its Contributions) on an "AS IS" BASIS,
410
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
411
+
implied, including, without limitation, any warranties or conditions
412
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
413
+
PARTICULAR PURPOSE. You are solely responsible for determining the
414
+
appropriateness of using or redistributing the Work and assume any
415
+
risks associated with Your exercise of permissions under this License.
417
+
8. Limitation of Liability. In no event and under no legal theory,
418
+
whether in tort (including negligence), contract, or otherwise,
419
+
unless required by applicable law (such as deliberate and grossly
420
+
negligent acts) or agreed to in writing, shall any Contributor be
421
+
liable to You for damages, including any direct, indirect, special,
422
+
incidental, or consequential damages of any character arising as a
423
+
result of this License or out of the use or inability to use the
424
+
Work (including but not limited to damages for loss of goodwill,
425
+
work stoppage, computer failure or malfunction, or any and all
426
+
other commercial damages or losses), even if such Contributor
427
+
has been advised of the possibility of such damages.
429
+
9. Accepting Warranty or Additional Liability. While redistributing
430
+
the Work or Derivative Works thereof, You may choose to offer,
431
+
and charge a fee for, acceptance of support, warranty, indemnity,
432
+
or other liability obligations and/or rights consistent with this
433
+
License. However, in accepting such obligations, You may act only
434
+
on Your own behalf and on Your sole responsibility, not on behalf
435
+
of any other Contributor, and only if You agree to indemnify,
436
+
defend, and hold each Contributor harmless for any liability
437
+
incurred by, or claims asserted against, such Contributor by reason
438
+
of your accepting any such warranty or additional liability.
440
+
END OF TERMS AND CONDITIONS
442
+
Copyright 2020 Thomas Leonard
444
+
Licensed under the Apache License, Version 2.0 (the "License");
445
+
you may not use this file except in compliance with the License.
446
+
You may obtain a copy of the License at
448
+
https://www.apache.org/licenses/LICENSE-2.0
450
+
Unless required by applicable law or agreed to in writing, software
451
+
distributed under the License is distributed on an "AS IS" BASIS,
452
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
453
+
See the License for the specific language governing permissions and
454
+
limitations under the License. *)