173 lines
3.9 KiB
Go
173 lines
3.9 KiB
Go
//go:build linux
|
|
|
|
package isolation
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
func identityUID(identity string) (uint32, error) {
|
|
account, err := user.Lookup(identity)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
uid, err := strconv.ParseUint(account.Uid, 10, 32)
|
|
if err != nil || uid == 0 {
|
|
return 0, errors.New("invalid identity uid")
|
|
}
|
|
return uint32(uid), nil
|
|
}
|
|
|
|
func identityPIDs(identity string) ([]int, error) {
|
|
uid, err := identityUID(identity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
entries, err := os.ReadDir("/proc")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pids := make([]int, 0)
|
|
for _, entry := range entries {
|
|
pid, parseErr := strconv.Atoi(entry.Name())
|
|
if parseErr != nil || pid <= 1 {
|
|
continue
|
|
}
|
|
data, readErr := os.ReadFile("/proc/" + entry.Name() + "/status")
|
|
if readErr != nil {
|
|
continue
|
|
}
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
if !strings.HasPrefix(line, "Uid:") {
|
|
continue
|
|
}
|
|
fields := strings.Fields(line)
|
|
if len(fields) >= 2 {
|
|
value, _ := strconv.ParseUint(fields[1], 10, 32)
|
|
if uint32(value) == uid {
|
|
pids = append(pids, pid)
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
return pids, nil
|
|
}
|
|
|
|
func countIdentityProcesses(identity string) (uint32, error) {
|
|
pids, err := identityPIDs(identity)
|
|
return uint32(len(pids)), err
|
|
}
|
|
|
|
func terminateIdentityProcesses(ctx context.Context, identity string) error {
|
|
pids, err := identityPIDs(identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, pid := range pids {
|
|
_ = syscall.Kill(pid, syscall.SIGTERM)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-time.After(750 * time.Millisecond):
|
|
}
|
|
pids, err = identityPIDs(identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, pid := range pids {
|
|
if killErr := syscall.Kill(pid, syscall.SIGKILL); killErr != nil && !errors.Is(killErr, syscall.ESRCH) {
|
|
return killErr
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func runtimeDirectoryPIDs(ctx context.Context, identity, runtimeRoot string) ([]int, error) {
|
|
uid, err := identityUID(identity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
runtimeRoot = filepath.Clean(runtimeRoot)
|
|
entries, err := os.ReadDir("/proc")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pids := make([]int, 0)
|
|
for _, entry := range entries {
|
|
pid, parseErr := strconv.Atoi(entry.Name())
|
|
if parseErr != nil || pid <= 1 || pid == os.Getpid() {
|
|
continue
|
|
}
|
|
status, readErr := os.ReadFile("/proc/" + entry.Name() + "/status")
|
|
if readErr != nil || !statusHasUID(status, uid) {
|
|
continue
|
|
}
|
|
output, readErr := tenantMaintenanceCommand(ctx, identity, "/usr/bin/readlink", "/proc/"+entry.Name()+"/cwd").Output()
|
|
if readErr != nil {
|
|
continue
|
|
}
|
|
cwd := filepath.Clean(strings.TrimSpace(string(output)))
|
|
if cwd == runtimeRoot || strings.HasPrefix(cwd, runtimeRoot+string(os.PathSeparator)) {
|
|
pids = append(pids, pid)
|
|
}
|
|
}
|
|
return pids, nil
|
|
}
|
|
|
|
func statusHasUID(status []byte, uid uint32) bool {
|
|
for _, line := range strings.Split(string(status), "\n") {
|
|
if !strings.HasPrefix(line, "Uid:") {
|
|
continue
|
|
}
|
|
fields := strings.Fields(line)
|
|
if len(fields) < 2 {
|
|
return false
|
|
}
|
|
value, err := strconv.ParseUint(fields[1], 10, 32)
|
|
return err == nil && uint32(value) == uid
|
|
}
|
|
return false
|
|
}
|
|
|
|
func terminateRuntimeDirectoryProcesses(ctx context.Context, identity, runtimeRoot string) error {
|
|
pids, err := runtimeDirectoryPIDs(ctx, identity, runtimeRoot)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, pid := range pids {
|
|
_ = syscall.Kill(pid, syscall.SIGTERM)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-time.After(750 * time.Millisecond):
|
|
}
|
|
pids, err = runtimeDirectoryPIDs(ctx, identity, runtimeRoot)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, pid := range pids {
|
|
if killErr := syscall.Kill(pid, syscall.SIGKILL); killErr != nil && !errors.Is(killErr, syscall.ESRCH) {
|
|
return killErr
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fileIdentity(info os.FileInfo) uint64 {
|
|
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
|
|
return stat.Ino
|
|
}
|
|
return uint64(info.ModTime().UnixNano())
|
|
}
|