mirror of
https://github.com/kemko/nomad.git
synced 2026-01-03 17:05:43 +03:00
The Nomad client renders templates in the same privileged process used for most other client operations. During internal testing, we discovered that a malicious task can create a symlink that can cause template rendering to read and write to arbitrary files outside the allocation sandbox. Because the Nomad agent can be restarted without restarting tasks, we can't simply check that the path is safe at the time we write without encountering a time-of-check/time-of-use race. To protect Nomad client hosts from this attack, we'll now read and write templates in a subprocess: * On Linux/Unix, this subprocess is sandboxed via chroot to the allocation directory. This requires that Nomad is running as a privileged process. A non-root Nomad agent will warn that it cannot sandbox the template renderer. * On Windows, this process is sandboxed via a Windows AppContainer which has been granted access to only to the allocation directory. This does not require special privileges on Windows. (Creating symlinks in the first place can be prevented by running workloads as non-Administrator or non-ContainerAdministrator users.) Both sandboxes cause encountered symlinks to be evaluated in the context of the sandbox, which will result in a "file not found" or "access denied" error, depending on the platform. This change will also require an update to Consul-Template to allow callers to inject a custom `ReaderFunc` and `RenderFunc`. This design is intended as a workaround to allow us to fix this bug without creating backwards compatibility issues for running tasks. A future version of Nomad may introduce a read-only mount specifically for templates and artifacts so that tasks cannot write into the same location that the Nomad agent is. Fixes: https://github.com/hashicorp/nomad/issues/19888 Fixes: CVE-2024-1329
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
//go:build !windows
|
|
|
|
package template
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"os/exec"
|
|
"time"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/template/renderer"
|
|
)
|
|
|
|
// createPlatformSandbox is a no-op outside of windows
|
|
func createPlatformSandbox(_ *TaskTemplateManagerConfig) error { return nil }
|
|
|
|
// destroyPlatformSandbox is a no-op outside of windows
|
|
func destroyPlatformSandbox(_ *TaskTemplateManagerConfig) error { return nil }
|
|
|
|
// renderTemplateInSandbox runs the template-render command in a subprocess that
|
|
// will chroot itself to prevent a task from swapping a directory between the
|
|
// sandbox path and the destination with a symlink pointing to somewhere outside
|
|
// the sandbox.
|
|
//
|
|
// See renderer/ subdirectory for implementation.
|
|
func renderTemplateInSandbox(cfg *sandboxConfig) (string, int, error) {
|
|
|
|
// Safe to inject user input as command arguments since Go's exec.Command
|
|
// does not invoke a shell.
|
|
args := []string{
|
|
"template-render",
|
|
"write",
|
|
"-sandbox-path", cfg.sandboxPath,
|
|
"-dest-path", cfg.destPath,
|
|
"-perms", cfg.perms,
|
|
}
|
|
if cfg.user != "" {
|
|
args = append(args, "-user")
|
|
args = append(args, cfg.user)
|
|
}
|
|
if cfg.group != "" {
|
|
args = append(args, "-group")
|
|
args = append(args, cfg.group)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
// note: we can't simply set cmd.SysProcAttr.Chroot here because the Nomad
|
|
// binary isn't in the chroot
|
|
cmd := exec.CommandContext(ctx, cfg.thisBin, args...)
|
|
stdin, err := cmd.StdinPipe()
|
|
if err != nil {
|
|
return "", 1, err
|
|
}
|
|
|
|
go func() {
|
|
defer stdin.Close()
|
|
io.Copy(stdin, bytes.NewReader(cfg.contents))
|
|
}()
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
code := cmd.ProcessState.ExitCode()
|
|
if code == renderer.ExitWouldRenderButDidnt {
|
|
err = nil // erase the "exit code 117" error
|
|
}
|
|
|
|
return string(out), code, err
|
|
}
|
|
|
|
// readTemplateFromSandbox runs the template-render command in a subprocess that
|
|
// will chroot itself to prevent a task from swapping a directory between the
|
|
// sandbox path and the source with a symlink pointing to somewhere outside
|
|
// the sandbox.
|
|
func readTemplateFromSandbox(cfg *sandboxConfig) ([]byte, []byte, int, error) {
|
|
|
|
// Safe to inject user input as command arguments since Go's exec.Command
|
|
// does not invoke a shell. Also, the only user-controlled argument here is
|
|
// the source path which we've already verified is at least a valid path in
|
|
// the caller.
|
|
args := []string{
|
|
"template-render",
|
|
"read",
|
|
"-sandbox-path", cfg.sandboxPath,
|
|
"-source-path", cfg.sourcePath,
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
// note: we can't simply set cmd.SysProcAttr.Chroot here because the Nomad
|
|
// binary isn't in the chroot
|
|
cmd := exec.CommandContext(ctx, cfg.thisBin, args...)
|
|
var outb, errb bytes.Buffer
|
|
cmd.Stdout = &outb
|
|
cmd.Stderr = &errb
|
|
|
|
err := cmd.Run()
|
|
stdout := outb.Bytes()
|
|
stderr := errb.Bytes()
|
|
return stdout, stderr, cmd.ProcessState.ExitCode(), err
|
|
}
|