Tahinli/forge
public0 watching
★ 0
| 1 | //! The sandbox helper: the body of `forge-runner __sandbox`. |
| 2 | //! |
| 3 | //! The process is single-threaded and has no tokio runtime, because libcontainer forks and |
| 4 | //! clones. It reads one [`SandboxRequest`] from stdin, prepares the rootfs, creates and starts a |
| 5 | //! container, and waits for it. Its stdout and stderr are the container's, so the runner just |
| 6 | //! forwards them. |
| 7 | //! |
| 8 | //! Setup happens in this order: |
| 9 | //! 1. Enter a user namespace with the request's [`IdMap`](super::idmap::IdMap): root is the |
| 10 | //! runner's uid, and in subuid mode container ids `1..=n` are the subordinate range. The |
| 11 | //! overlay mount needs CAP_SYS_ADMIN in a user namespace that owns the mount namespace. |
| 12 | //! 2. An overlay mount of the image rootfs. If that fails, the rootfs is copied instead. |
| 13 | //! 3. The OCI spec is written to `bundle/config.json` (0600) and libcontainer builds the |
| 14 | //! container from it. The file is deleted right after `build()`, since libcontainer has |
| 15 | //! read it by then. |
| 16 | //! 4. The container starts and the helper waits, polling so that SIGTERM is never missed. On |
| 17 | //! SIGTERM the container is killed and deleted. The exit code is the job's. |
| 18 | //! |
| 19 | //! Exit codes: the job's code, 128 + signal if the job was signalled, or 125 for a sandbox setup |
| 20 | //! failure. A setup failure prints `forge-sandbox: <reason>` on stderr. |
| 21 | |
| 22 | use ; |
| 23 | use ; |
| 24 | use AsFd; |
| 25 | use ; |
| 26 | use ; |
| 27 | use Command; |
| 28 | use ; |
| 29 | use Duration; |
| 30 | |
| 31 | use Container; |
| 32 | use ContainerBuilder; |
| 33 | use SyscallType; |
| 34 | use Errno; |
| 35 | use ; |
| 36 | use ; |
| 37 | use ; |
| 38 | use Pid; |
| 39 | use Spec; |
| 40 | |
| 41 | use FilteredExecutor; |
| 42 | use enter_mapped_userns; |
| 43 | use build_filter; |
| 44 | use ; |
| 45 | use ; |
| 46 | use cratevalidate_job_id; |
| 47 | |
| 48 | /// Exit code for a sandbox setup failure. The runner maps it to a sandbox error. |
| 49 | pub const EXIT_SETUP_FAILED: i32 = 125; |
| 50 | /// How often the wait loop checks for SIGTERM and polls the child. |
| 51 | const POLL: Duration = from_millis; |
| 52 | const HOSTS: &str = "127.0.0.1 localhost\n::1 localhost ip6-localhost ip6-loopback\n"; |
| 53 | |
| 54 | /// Set by the SIGTERM and SIGINT handler. The wait loop reads it. |
| 55 | static TERMINATE: AtomicBool = new; |
| 56 | |
| 57 | extern "C" |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | /// Entry point for `forge-runner __sandbox`. Returns the process exit code. |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | /// The directories under `job_dir` that the helper uses. |
| 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 | /// Sets every entry under `workspace` to uid and gid 0 of this namespace, which is the runner's |
| 132 | /// own id on the host. Symlinks are not followed. Entries already owned by 0:0 are left alone. |
| 133 | /// Failures are warnings only, because the job has already run. |
| 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 | /// Installs the SIGTERM and SIGINT handler. It does not set SA_RESTART. The wait loop polls, |
| 190 | /// so the handler only sets a flag. |
| 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 | /// The files bind mounted at `/etc/hosts` and `/etc/hostname`. |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | /// Mounts the overlay at `merged` and returns it as the rootfs. If the overlay fails, the image |
| 240 | /// rootfs is copied instead, and a warning is printed. |
| 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 | /// A full copy of the rootfs, reflinked where the filesystem supports it. |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | /// Runs the job with `[limits]` applied through its cgroup. If the container can't be built that |
| 302 | /// way, it is rebuilt in the same cgroup without the limit values, and a warning goes to the job |
| 303 | /// log. |
| 304 | /// |
| 305 | /// libcontainer 0.7 cannot run a container in a user namespace without a cgroup manager, and it |
| 306 | /// only avoids systemd when the cgroup path is absolute. So "run without cgroups" is not |
| 307 | /// available. If the job cgroup can't be created at all, the job fails with exit 125. |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | /// The job's cgroup path, absolute and relative to the cgroup2 mount. |
| 326 | /// |
| 327 | /// It must not be the runner's own cgroup. libcontainer kills every process in the job cgroup on |
| 328 | /// delete, so that would kill the runner. A cgroup that holds processes also can't enable |
| 329 | /// controllers for its children. The job cgroup goes in the parent slice instead, when the parent |
| 330 | /// is a plain domain cgroup. The runner's user owns both, because systemd delegates the user |
| 331 | /// slice. |
| 332 | /// |
| 333 | /// The name is `forge-job-<job id>-<helper pid>`, so the runner can compute it after it has |
| 334 | /// spawned the helper. It uses the runner's own cgroup, which is also the helper's, to find the |
| 335 | /// parent slice. The path is relative to the cgroup2 mount, so the directory is |
| 336 | /// `/sys/fs/cgroup` joined with it. |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | /// Whether `cgroup` (absolute, inside the cgroup2 mount) is a normal domain cgroup. A threaded |
| 354 | /// one can't hold domain children. |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | /// Removes the resource values. The cgroup path stays, so libcontainer still uses the fs driver. |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | /// Writes the bundle, builds, starts, and waits. Build and start errors are returned as |
| 376 | /// `Setup("container: …")` so the caller can tell them apart from setup errors. |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | /// Writes `bundle/config.json` (0600). It holds the job env and secrets, so it is removed right |
| 409 | /// after `build()`. |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | /// Creates the container. Its stdin is `/dev/null`. Its stdout and stderr are dups of the |
| 431 | /// helper's, so the runner receives them. |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | /// Starts the container and returns the init pid. The init is a child of this process. |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | /// SIGKILL, as libcontainer's own signal type. Its `Signal` is not nix's, so it is built from |
| 467 | /// the number. |
| 468 | const SIGKILL_NUMBER: i32 = 9; |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | /// Waits for the init process. On SIGTERM the container is killed. The child is then reaped, |
| 477 | /// and `delete` removes the container's state. Returns the job's exit code. |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | /// Removes the whole job dir: state and bundle (which hold the env), and the overlay upper dir |
| 515 | /// (which may hold files the job wrote). The container is gone by now. The overlay is still |
| 516 | /// mounted in this process's mount namespace, so detach it first. The helper is still root in its |
| 517 | /// user namespace, so it can remove the overlay work files that the runner's uid could not. |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 |