mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-26 01:41:04 +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).
37 lines
1.4 KiB
Go
37 lines
1.4 KiB
Go
package agent
|
|
|
|
import "sync/atomic"
|
|
|
|
// File tool identity. The confined file tools (filetools.go) and the ingest tools
|
|
// open the paths the agent names inside the server process, which is root in the
|
|
// Docker image. With the executor privilege drop active (server/execpriv.go) the
|
|
// server sets the executor identity here, and each of those calls then reaches the
|
|
// file system as that uid and gid, with that gid as its only group: the kernel
|
|
// checks every open, create and directory read against the executor's rights, the
|
|
// rights of the agent's shell, on top of the os.Root confinement. Linux only; see
|
|
// filetools_identity_linux.go.
|
|
|
|
// FileToolIdentity is the uid and gid the file tools access files as.
|
|
type FileToolIdentity struct{ UID, GID uint32 }
|
|
|
|
var fileToolIdentity atomic.Pointer[FileToolIdentity]
|
|
|
|
// SetFileToolIdentity sets the identity of the file tools; nil restores the
|
|
// server's own identity.
|
|
func SetFileToolIdentity(id *FileToolIdentity) { fileToolIdentity.Store(id) }
|
|
|
|
// FileToolIdentitySupported reports whether the file tools can run as another
|
|
// identity on this platform.
|
|
func FileToolIdentitySupported() bool { return fileIdentitySupported }
|
|
|
|
// RunAsFileToolIdentity runs fn with the file tool identity, or directly when none
|
|
// is set. When it returns an error, fn did not run.
|
|
func RunAsFileToolIdentity(fn func()) error {
|
|
id := fileToolIdentity.Load()
|
|
if id == nil {
|
|
fn()
|
|
return nil
|
|
}
|
|
return runWithFileIdentity(*id, fn)
|
|
}
|