mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
PrepareExecPrivilege chmodded jwt.key, secret.key and the config file to 0600 but left their owner as it was: a file owned by the executor uid (created by an agent process, or on the host under the uid the executor has in the container) stayed readable by the agent. It now makes root their owner first. The startup self-test also checks the directories: the probe, as the executor, must not be able to create an entry in the data directory, the key directory or the config file's directory (it could plant a key, a config or a data tree for the next start), and none of their parents may let the executor replace them (owned by it, or writable by it without the sticky bit). For the database, a test against PostgreSQL shows the server's role refused without its password (SQLSTATE 28P01); the probe already fails the start when the database lets that role in. No Unix socket is introduced: in the compose deployment PostgreSQL runs in its own container and is reached over TCP with a password that the executor cannot read (sealed environment, root-only config).
198 lines
7.2 KiB
Go
198 lines
7.2 KiB
Go
//go:build linux
|
|
|
|
package server
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
// isolationRootFixture is newExecRootFixture with the Bash shim installed, a key
|
|
// directory like /app/keys (0700, jwt.key and secret.key 0600) and a 0600 config.
|
|
type isolationRootFixture struct {
|
|
*execRootFixture
|
|
keyDir, config string
|
|
}
|
|
|
|
func newIsolationRootFixture(t *testing.T) *isolationRootFixture {
|
|
t.Helper()
|
|
f := newExecRootFixture(t) // skips without root
|
|
if err := InstallBashShim(f.priv); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.RemoveAll(filepath.SplitList(os.Getenv("PATH"))[0]) })
|
|
keyDir := filepath.Join(f.data, "..", filepath.Base(f.data)+"-keys")
|
|
if err := os.Mkdir(keyDir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.RemoveAll(keyDir) })
|
|
for _, name := range []string{"jwt.key", "secret.key"} {
|
|
if err := os.WriteFile(filepath.Join(keyDir, name), []byte("key-"+name), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// The config file lives in a root-only directory too (/app in the image).
|
|
config := filepath.Join(keyDir, "config.json")
|
|
if err := os.WriteFile(config, []byte(`{"database":{"dsn":"postgres://u:pw@h/db"}}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &isolationRootFixture{execRootFixture: f, keyDir: keyDir, config: config}
|
|
}
|
|
|
|
func (f *isolationRootFixture) targets() IsolationTargets {
|
|
return IsolationTargets{DataDir: f.data, KeyDir: f.keyDir, ConfigPath: f.config, DSN: os.Getenv("ARTEX_PG_DSN")}
|
|
}
|
|
|
|
// The deployment the self-test is for: root server, executor drop, shim, root-only
|
|
// keys and config. It passes, through the real Bash shim, and with ARTEX_PG_DSN set
|
|
// it also finds that the executor cannot open a session on that database without
|
|
// the password (PostgreSQL asks for one on TCP).
|
|
func TestIsolationSelfTestPassesAsRoot(t *testing.T) {
|
|
f := newIsolationRootFixture(t)
|
|
if os.Getenv("ARTEX_PG_DSN") == "" {
|
|
t.Log("ARTEX_PG_DSN unset: the database check is not exercised")
|
|
}
|
|
if err := IsolationSelfTest(f.priv, f.targets()); err != nil {
|
|
t.Fatalf("self-test on an isolated setup: %v", err)
|
|
}
|
|
}
|
|
|
|
// A secret the executor can read fails the self-test, for the Bash path and the
|
|
// file tool path both.
|
|
func TestIsolationSelfTestDetectsReadableSecretAsRoot(t *testing.T) {
|
|
f := newIsolationRootFixture(t)
|
|
key := filepath.Join(f.keyDir, "jwt.key")
|
|
if err := os.Chmod(f.keyDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(key, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err := IsolationSelfTest(f.priv, f.targets())
|
|
if err == nil || !strings.Contains(err.Error(), "Bash can read "+key) || !strings.Contains(err.Error(), "the file tools can read "+key) {
|
|
t.Fatalf("self-test with a world-readable key: %v", err)
|
|
}
|
|
}
|
|
|
|
// Without the shim on PATH, "bash" is the real bash and runs as root: the self-test
|
|
// sees it.
|
|
func TestIsolationSelfTestDetectsMissingShimAsRoot(t *testing.T) {
|
|
f := newExecRootFixture(t)
|
|
err := IsolationSelfTest(f.priv, IsolationTargets{DataDir: f.data})
|
|
if err == nil || !strings.Contains(err.Error(), "Bash runs as uid 0") {
|
|
t.Fatalf("self-test without the Bash shim: %v", err)
|
|
}
|
|
}
|
|
|
|
// A key file and a config file owned by the executor uid stay readable by it after a
|
|
// chmod 0600: PrepareExecPrivilege makes root their owner, and the executor can then
|
|
// read neither (the self-test passes, and a direct read as the executor is denied).
|
|
func TestPrepareExecPrivilegeOwnsKeysAndConfigAsRoot(t *testing.T) {
|
|
f := newExecRootFixture(t)
|
|
keyDir := filepath.Join(f.tmp, "keys")
|
|
if err := os.Mkdir(keyDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key, config := filepath.Join(keyDir, "jwt.key"), filepath.Join(keyDir, "config.json")
|
|
for _, p := range []string{key, config} {
|
|
if err := os.WriteFile(p, []byte("secret-"+filepath.Base(p)), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chown(p, DefaultExecUID, DefaultExecGID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := PrepareExecPrivilege(&f.priv, f.data, keyDir, config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range []string{key, config} {
|
|
fi, err := os.Stat(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if st := fi.Sys().(*syscall.Stat_t); st.Uid != 0 || fi.Mode().Perm() != 0o600 {
|
|
t.Errorf("%s: uid %d mode %v, want root 0600", p, st.Uid, fi.Mode().Perm())
|
|
}
|
|
}
|
|
c := exec.Command("sh", "-c", `for f in "$@"; do if cat "$f" >/dev/null 2>&1; then echo "read $f"; fi; done; echo done`, "sh", key, config)
|
|
applyExecCredential(c, f.priv)
|
|
out, err := c.CombinedOutput()
|
|
if err != nil || string(out) != "done\n" {
|
|
t.Fatalf("executor read of the key and config: %v\n%s", err, out)
|
|
}
|
|
if err := InstallBashShim(f.priv); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.RemoveAll(filepath.SplitList(os.Getenv("PATH"))[0]) })
|
|
if err := IsolationSelfTest(f.priv, IsolationTargets{DataDir: f.data, KeyDir: keyDir, ConfigPath: config}); err != nil {
|
|
t.Fatalf("self-test after PrepareExecPrivilege: %v", err)
|
|
}
|
|
}
|
|
|
|
// A data, key or config directory the executor can write fails the self-test: an
|
|
// agent process could plant a key, a config or a data tree for the next start.
|
|
func TestIsolationSelfTestDetectsWritableDirsAsRoot(t *testing.T) {
|
|
f := newIsolationRootFixture(t)
|
|
if err := os.Chmod(f.data, 0o777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err := IsolationSelfTest(f.priv, f.targets())
|
|
if err == nil || !strings.Contains(err.Error(), "Bash can create files in "+f.data) {
|
|
t.Fatalf("self-test with a world-writable data directory: %v", err)
|
|
}
|
|
if err := os.Chmod(f.data, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chown(f.keyDir, DefaultExecUID, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = IsolationSelfTest(f.priv, f.targets())
|
|
if err == nil || !strings.Contains(err.Error(), "Bash can create files in "+f.keyDir) {
|
|
t.Fatalf("self-test with a key directory owned by the executor: %v", err)
|
|
}
|
|
if entries, _ := os.ReadDir(f.keyDir); len(entries) != 3 {
|
|
t.Fatalf("the probe left an entry in the key directory: %v", entries)
|
|
}
|
|
}
|
|
|
|
// A data directory whose parent the executor can write (not sticky) can be renamed
|
|
// and replaced: the self-test reports it.
|
|
func TestIsolationSelfTestDetectsReplaceableParentAsRoot(t *testing.T) {
|
|
f := newIsolationRootFixture(t)
|
|
open := filepath.Join(f.tmp, "open-parent")
|
|
if err := os.Mkdir(open, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(open, 0o777); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data := filepath.Join(open, "data")
|
|
if err := os.Mkdir(data, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tg := f.targets()
|
|
tg.DataDir = data
|
|
err := IsolationSelfTest(f.priv, tg)
|
|
if err == nil || !strings.Contains(err.Error(), "an agent process can replace "+data) {
|
|
t.Fatalf("self-test with a replaceable data directory: %v", err)
|
|
}
|
|
}
|
|
|
|
// A database that lets the server's user in without a password fails the self-test:
|
|
// the probe, as the executor, opens a session on it.
|
|
func TestIsolationSelfTestDetectsPasswordlessDatabaseAsRoot(t *testing.T) {
|
|
f := newIsolationRootFixture(t)
|
|
host, port := fakeTrustPG(t)
|
|
tg := f.targets()
|
|
tg.DSN = "postgres://artex:server-password@" + host + ":" + strconv.Itoa(int(port)) + "/artex"
|
|
err := IsolationSelfTest(f.priv, tg)
|
|
if err == nil || !strings.Contains(err.Error(), `database session as "artex"`) {
|
|
t.Fatalf("self-test against a passwordless database: %v", err)
|
|
}
|
|
}
|