mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
The confined file tools (Read, Write, Edit, MultiEdit, LS, Glob, Grep) and the opens of ingest_bbot and ingest_scan_output ran inside the server, as root, and relied on the os.Root path confinement alone. With the executor privilege drop active, PrepareExecPrivilege now gives them the executor uid and gid: each call runs on a thread of its own whose file system uid, file system gid and supplementary groups are the executor's (setfsuid, setfsgid and the per-thread setgroups of x/sys/unix). The thread is never unlocked, so the runtime ends it with the call and no other goroutine runs with those ids. The kernel then checks every open, create and directory read against the rights of the agent's shell, on top of the path confinement. Output over the cap is still spilled, after the call, with the server's identity. Linux only. Elsewhere the identity is not set and a log line says the file tools keep the server's identity. golang.org/x/sys becomes a direct dependency (already in the module graph at the same version).
35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// Without an identity fn runs directly.
|
|
func TestRunAsFileToolIdentityWithoutIdentity(t *testing.T) {
|
|
SetFileToolIdentity(nil)
|
|
ran := false
|
|
if err := RunAsFileToolIdentity(func() { ran = true }); err != nil || !ran {
|
|
t.Fatalf("err=%v ran=%v", err, ran)
|
|
}
|
|
}
|
|
|
|
// A process that cannot change its ids (not root, or not Linux) gets an error and fn
|
|
// does not run: the file tools fail closed instead of running as the server.
|
|
func TestRunAsFileToolIdentityFailsClosed(t *testing.T) {
|
|
if runtime.GOOS == "linux" && os.Geteuid() == 0 {
|
|
t.Skip("root can change its ids")
|
|
}
|
|
SetFileToolIdentity(&FileToolIdentity{UID: 10001, GID: 10001})
|
|
t.Cleanup(func() { SetFileToolIdentity(nil) })
|
|
ran := false
|
|
if err := RunAsFileToolIdentity(func() { ran = true }); err == nil || ran {
|
|
t.Fatalf("err=%v ran=%v, want an error and fn not run", err, ran)
|
|
}
|
|
f := newConfinedFixture(t)
|
|
if res := f.call(t, "Read", map[string]any{"file_path": "in.txt"}); !res.IsError {
|
|
t.Fatalf("Read ran without its identity: %s", res.Flatten())
|
|
}
|
|
}
|