2024-10-11 01:55:33 +09:00
|
|
|
package shim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2024-10-27 00:46:15 +09:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2024-10-11 01:55:33 +09:00
|
|
|
"syscall"
|
2024-10-27 00:46:15 +09:00
|
|
|
"time"
|
2024-10-11 01:55:33 +09:00
|
|
|
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/acl"
|
2024-11-02 03:03:44 +09:00
|
|
|
shim0 "git.ophivana.moe/security/fortify/cmd/fshim/ipc"
|
2024-10-21 20:47:02 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/fmsg"
|
2024-10-11 01:55:33 +09:00
|
|
|
)
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
const shimSetupTimeout = 5 * time.Second
|
|
|
|
|
2024-10-27 00:46:15 +09:00
|
|
|
// used by the parent process
|
2024-10-11 01:55:33 +09:00
|
|
|
|
2024-10-27 00:46:15 +09:00
|
|
|
type Shim struct {
|
|
|
|
// user switcher process
|
|
|
|
cmd *exec.Cmd
|
|
|
|
// uid of shim target user
|
|
|
|
uid uint32
|
|
|
|
// whether to check shim pid
|
|
|
|
checkPid bool
|
|
|
|
// user switcher executable path
|
|
|
|
executable string
|
|
|
|
// path to setup socket
|
|
|
|
socket string
|
|
|
|
// shim setup abort reason and completion
|
|
|
|
abort chan error
|
|
|
|
abortErr atomic.Pointer[error]
|
|
|
|
abortOnce sync.Once
|
|
|
|
// wayland mediation, nil if disabled
|
2024-11-02 03:03:44 +09:00
|
|
|
wl *shim0.Wayland
|
2024-10-27 00:46:15 +09:00
|
|
|
// shim setup payload
|
2024-11-02 03:03:44 +09:00
|
|
|
payload *shim0.Payload
|
2024-10-27 00:46:15 +09:00
|
|
|
}
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
func New(executable string, uid uint32, socket string, wl *shim0.Wayland, payload *shim0.Payload, checkPid bool) *Shim {
|
2024-10-27 23:49:37 +09:00
|
|
|
return &Shim{uid: uid, executable: executable, socket: socket, wl: wl, payload: payload, checkPid: checkPid}
|
2024-10-27 00:46:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shim) String() string {
|
|
|
|
if s.cmd == nil {
|
|
|
|
return "(unused shim manager)"
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
2024-10-27 00:46:15 +09:00
|
|
|
return s.cmd.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shim) Unwrap() *exec.Cmd {
|
|
|
|
return s.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shim) Abort(err error) {
|
|
|
|
s.abortOnce.Do(func() {
|
|
|
|
s.abortErr.Store(&err)
|
|
|
|
// s.abort is buffered so this will never block
|
|
|
|
s.abort <- err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shim) AbortWait(err error) {
|
|
|
|
s.Abort(err)
|
|
|
|
<-s.abort
|
|
|
|
}
|
2024-10-11 01:55:33 +09:00
|
|
|
|
2024-10-27 00:46:15 +09:00
|
|
|
type CommandBuilder func(shimEnv string) (args []string)
|
2024-10-21 21:23:56 +09:00
|
|
|
|
2024-10-27 00:46:15 +09:00
|
|
|
func (s *Shim) Start(f CommandBuilder) (*time.Time, error) {
|
|
|
|
var (
|
|
|
|
cf chan *net.UnixConn
|
|
|
|
accept func()
|
|
|
|
)
|
|
|
|
|
|
|
|
// listen on setup socket
|
|
|
|
if c, a, err := s.serve(); err != nil {
|
|
|
|
return nil, fmsg.WrapErrorSuffix(err,
|
|
|
|
"cannot listen on shim setup socket:")
|
2024-10-11 01:55:33 +09:00
|
|
|
} else {
|
2024-10-27 00:46:15 +09:00
|
|
|
// accepts a connection after each call to accept
|
|
|
|
// connections are sent to the channel cf
|
|
|
|
cf, accept = c, a
|
|
|
|
}
|
2024-10-21 21:23:56 +09:00
|
|
|
|
2024-10-27 00:46:15 +09:00
|
|
|
// start user switcher process and save time
|
2024-11-02 03:03:44 +09:00
|
|
|
s.cmd = exec.Command(s.executable, f(shim0.Env+"="+s.socket)...)
|
2024-10-27 00:46:15 +09:00
|
|
|
s.cmd.Env = []string{}
|
|
|
|
s.cmd.Stdin, s.cmd.Stdout, s.cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
|
|
|
s.cmd.Dir = "/"
|
|
|
|
fmsg.VPrintln("starting shim via user switcher:", s.cmd)
|
|
|
|
fmsg.Withhold() // withhold messages to stderr
|
|
|
|
if err := s.cmd.Start(); err != nil {
|
|
|
|
return nil, fmsg.WrapErrorSuffix(err,
|
|
|
|
"cannot start user switcher:")
|
|
|
|
}
|
|
|
|
startTime := time.Now().UTC()
|
2024-10-21 21:23:56 +09:00
|
|
|
|
2024-10-27 00:46:15 +09:00
|
|
|
// kill shim if something goes wrong and an error is returned
|
|
|
|
killShim := func() {
|
|
|
|
if err := s.cmd.Process.Signal(os.Interrupt); err != nil {
|
|
|
|
fmsg.Println("cannot terminate shim on faulted setup:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer func() { killShim() }()
|
|
|
|
|
|
|
|
accept()
|
2024-11-02 03:03:44 +09:00
|
|
|
var conn *net.UnixConn
|
|
|
|
select {
|
|
|
|
case c := <-cf:
|
|
|
|
if c == nil {
|
|
|
|
return &startTime, fmsg.WrapErrorSuffix(*s.abortErr.Load(), "cannot accept call on setup socket:")
|
|
|
|
} else {
|
|
|
|
conn = c
|
|
|
|
}
|
|
|
|
case <-time.After(shimSetupTimeout):
|
2024-11-03 02:03:30 +09:00
|
|
|
err := fmsg.WrapError(errors.New("timed out waiting for shim"),
|
|
|
|
"timed out waiting for shim to connect")
|
2024-11-02 03:03:44 +09:00
|
|
|
s.AbortWait(err)
|
|
|
|
return &startTime, err
|
2024-10-27 00:46:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// authenticate against called provided uid and shim pid
|
|
|
|
if cred, err := peerCred(conn); err != nil {
|
|
|
|
return &startTime, fmsg.WrapErrorSuffix(*s.abortErr.Load(), "cannot retrieve shim credentials:")
|
|
|
|
} else if cred.Uid != s.uid {
|
|
|
|
fmsg.Printf("process %d owned by user %d tried to connect, expecting %d",
|
|
|
|
cred.Pid, cred.Uid, s.uid)
|
|
|
|
err = errors.New("compromised fortify build")
|
|
|
|
s.Abort(err)
|
|
|
|
return &startTime, err
|
|
|
|
} else if s.checkPid && cred.Pid != int32(s.cmd.Process.Pid) {
|
|
|
|
fmsg.Printf("process %d tried to connect to shim setup socket, expecting shim %d",
|
|
|
|
cred.Pid, s.cmd.Process.Pid)
|
|
|
|
err = errors.New("compromised target user")
|
|
|
|
s.Abort(err)
|
|
|
|
return &startTime, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// serve payload and wayland fd if enabled
|
|
|
|
// this also closes the connection
|
2024-11-02 03:03:44 +09:00
|
|
|
err := s.payload.Serve(conn, s.wl)
|
2024-10-27 00:46:15 +09:00
|
|
|
if err == nil {
|
|
|
|
killShim = func() {}
|
|
|
|
}
|
|
|
|
s.Abort(err) // aborting with nil indicates success
|
|
|
|
return &startTime, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shim) serve() (chan *net.UnixConn, func(), error) {
|
|
|
|
if s.abort != nil {
|
|
|
|
panic("attempted to serve shim setup twice")
|
|
|
|
}
|
|
|
|
s.abort = make(chan error, 1)
|
|
|
|
|
|
|
|
cf := make(chan *net.UnixConn)
|
|
|
|
accept := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
if l, err := net.ListenUnix("unix", &net.UnixAddr{Name: s.socket, Net: "unix"}); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
} else {
|
|
|
|
l.SetUnlinkOnClose(true)
|
|
|
|
|
|
|
|
fmsg.VPrintf("listening on shim setup socket %q", s.socket)
|
|
|
|
if err = acl.UpdatePerm(s.socket, int(s.uid), acl.Read, acl.Write, acl.Execute); err != nil {
|
|
|
|
fmsg.Println("cannot append ACL entry to shim setup socket:", err)
|
|
|
|
s.Abort(err) // ensures setup socket cleanup
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2024-11-02 03:03:44 +09:00
|
|
|
cfWg := new(sync.WaitGroup)
|
2024-10-27 00:46:15 +09:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err = <-s.abort:
|
|
|
|
if err != nil {
|
|
|
|
fmsg.VPrintln("aborting shim setup, reason:", err)
|
|
|
|
}
|
|
|
|
if err = l.Close(); err != nil {
|
|
|
|
fmsg.Println("cannot close setup socket:", err)
|
|
|
|
}
|
|
|
|
close(s.abort)
|
2024-11-02 03:03:44 +09:00
|
|
|
go func() {
|
|
|
|
cfWg.Wait()
|
|
|
|
close(cf)
|
|
|
|
}()
|
2024-10-11 01:55:33 +09:00
|
|
|
return
|
2024-10-27 00:46:15 +09:00
|
|
|
case <-accept:
|
2024-11-02 03:03:44 +09:00
|
|
|
cfWg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer cfWg.Done()
|
|
|
|
if conn, err0 := l.AcceptUnix(); err0 != nil {
|
|
|
|
// breaks loop
|
|
|
|
s.Abort(err0)
|
|
|
|
// receiver sees nil value and loads err0 stored during abort
|
|
|
|
cf <- nil
|
|
|
|
} else {
|
|
|
|
cf <- conn
|
|
|
|
}
|
|
|
|
}()
|
2024-10-21 21:23:56 +09:00
|
|
|
}
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2024-10-27 00:46:15 +09:00
|
|
|
|
|
|
|
return cf, func() { accept <- struct{}{} }, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// peerCred fetches peer credentials of conn
|
|
|
|
func peerCred(conn *net.UnixConn) (ucred *syscall.Ucred, err error) {
|
|
|
|
var raw syscall.RawConn
|
|
|
|
if raw, err = conn.SyscallConn(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err0 := raw.Control(func(fd uintptr) {
|
|
|
|
ucred, err = syscall.GetsockoptUcred(int(fd), syscall.SOL_SOCKET, syscall.SO_PEERCRED)
|
|
|
|
})
|
|
|
|
err = errors.Join(err, err0)
|
|
|
|
return
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|