61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
//go:build linux
|
|
|
|
package isolation
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
func disconnectFTPSessions(ctx context.Context, identity string) error {
|
|
uid, err := identityUID(identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pids, err := identityPIDs(identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, pid := range pids {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
process, e := os.FindProcess(pid)
|
|
if e != nil {
|
|
continue
|
|
}
|
|
base := "/proc/" + strconv.Itoa(pid)
|
|
exe, exeErr := os.Readlink(base + "/exe")
|
|
status, readErr := os.ReadFile(base + "/status")
|
|
// CAP_SYS_PTRACE is intentionally absent from the agent. Linux can deny
|
|
// /proc/<pid>/exe across UIDs; status Name/Uid remain readable. Restrict the
|
|
// fallback to this host's UID and the exact FTP daemon name, with the pidfd
|
|
// acquired before either read so a recycled PID cannot target another task.
|
|
if readErr == nil && statusHasUID(status, uid) && managedFTPProcess(status, exe, exeErr != nil) {
|
|
e = process.Kill()
|
|
if e != nil && !errors.Is(e, os.ErrProcessDone) && !errors.Is(e, syscall.ESRCH) {
|
|
process.Release()
|
|
return e
|
|
}
|
|
}
|
|
process.Release()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func managedFTPProcess(status []byte, exe string, unreadable bool) bool {
|
|
if !unreadable {
|
|
return strings.HasPrefix(exe, "/www/server/pure-ftpd/") && strings.HasSuffix(exe, "/pure-ftpd")
|
|
}
|
|
for _, line := range strings.Split(string(status), "\n") {
|
|
if strings.HasPrefix(line, "Name:") {
|
|
return strings.TrimSpace(strings.TrimPrefix(line, "Name:")) == "pure-ftpd"
|
|
}
|
|
}
|
|
return false
|
|
}
|