56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
//go:build linux
|
|
|
|
package isolation
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
func identityPIDsForRuntimeProbe(identity string) ([]int, error) { return identityPIDs(identity) }
|
|
func processUsesExecutable(ctx context.Context, pid int, identity, executable, working string, checkCWD bool) bool {
|
|
uid, err := identityUID(identity)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
proc := filepath.Join("/proc", strconv.Itoa(pid))
|
|
info, err := os.Stat(proc)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
stat, ok := info.Sys().(*syscall.Stat_t)
|
|
if !ok || stat.Uid != uid {
|
|
return false
|
|
}
|
|
expected, err := os.Stat(executable)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
expectedStat, ok := expected.Sys().(*syscall.Stat_t)
|
|
if !ok {
|
|
return false
|
|
}
|
|
// A restricted root agent intentionally lacks CAP_SYS_PTRACE. Inspect the
|
|
// process as its own UID, without adding broad process-reading privileges.
|
|
actual, err := tenantMaintenanceCommand(ctx, identity, "/usr/bin/stat", "-Lc", "%d:%i", filepath.Join(proc, "exe")).Output()
|
|
if err != nil || strings.TrimSpace(string(actual)) != fmt.Sprintf("%d:%d", expectedStat.Dev, expectedStat.Ino) {
|
|
return false
|
|
}
|
|
if checkCWD {
|
|
output, err := tenantMaintenanceCommand(ctx, identity, "/usr/bin/readlink", filepath.Join(proc, "cwd")).Output()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
cwd := strings.TrimSpace(string(output))
|
|
if filepath.Clean(cwd) != filepath.Clean(working) && !strings.HasPrefix(filepath.Clean(cwd)+"/", filepath.Clean(working)+"/") {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|