cmd/fshim: switch to setup pipe
The socket-based approach is no longer necessary as fsu allows extra files and sudo compatibility is no longer relevant. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
parent
7be53a2438
commit
52f21a19f3
@ -1,11 +1,7 @@
|
|||||||
package shim0
|
package shim0
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"git.ophivana.moe/security/fortify/helper/bwrap"
|
"git.ophivana.moe/security/fortify/helper/bwrap"
|
||||||
"git.ophivana.moe/security/fortify/internal/fmsg"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const Env = "FORTIFY_SHIM"
|
const Env = "FORTIFY_SHIM"
|
||||||
@ -23,13 +19,3 @@ type Payload struct {
|
|||||||
// verbosity pass through
|
// verbosity pass through
|
||||||
Verbose bool
|
Verbose bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Payload) Serve(conn *net.UnixConn) error {
|
|
||||||
if err := gob.NewEncoder(conn).Encode(*p); err != nil {
|
|
||||||
return fmsg.WrapErrorSuffix(err,
|
|
||||||
"cannot stream shim payload:")
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmsg.WrapErrorSuffix(conn.Close(),
|
|
||||||
"cannot close setup connection:")
|
|
||||||
}
|
|
||||||
|
@ -1,18 +1,16 @@
|
|||||||
package shim
|
package shim
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/gob"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.ophivana.moe/security/fortify/acl"
|
|
||||||
shim0 "git.ophivana.moe/security/fortify/cmd/fshim/ipc"
|
shim0 "git.ophivana.moe/security/fortify/cmd/fshim/ipc"
|
||||||
"git.ophivana.moe/security/fortify/internal"
|
"git.ophivana.moe/security/fortify/internal"
|
||||||
"git.ophivana.moe/security/fortify/internal/fmsg"
|
"git.ophivana.moe/security/fortify/internal/fmsg"
|
||||||
@ -32,20 +30,14 @@ type Shim struct {
|
|||||||
aid string
|
aid string
|
||||||
// string representation of supplementary group ids
|
// string representation of supplementary group ids
|
||||||
supp []string
|
supp []string
|
||||||
// path to setup socket
|
|
||||||
socket string
|
|
||||||
// shim setup abort reason and completion
|
|
||||||
abort chan error
|
|
||||||
abortErr atomic.Pointer[error]
|
|
||||||
abortOnce sync.Once
|
|
||||||
// fallback exit notifier with error returned killing the process
|
// fallback exit notifier with error returned killing the process
|
||||||
killFallback chan error
|
killFallback chan error
|
||||||
// shim setup payload
|
// shim setup payload
|
||||||
payload *shim0.Payload
|
payload *shim0.Payload
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(uid uint32, aid string, supp []string, socket string, payload *shim0.Payload) *Shim {
|
func New(uid uint32, aid string, supp []string, payload *shim0.Payload) *Shim {
|
||||||
return &Shim{uid: uid, aid: aid, supp: supp, socket: socket, payload: payload}
|
return &Shim{uid: uid, aid: aid, supp: supp, payload: payload}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Shim) String() string {
|
func (s *Shim) String() string {
|
||||||
@ -59,39 +51,11 @@ func (s *Shim) Unwrap() *exec.Cmd {
|
|||||||
return s.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
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Shim) WaitFallback() chan error {
|
func (s *Shim) WaitFallback() chan error {
|
||||||
return s.killFallback
|
return s.killFallback
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Shim) Start() (*time.Time, error) {
|
func (s *Shim) Start() (*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:")
|
|
||||||
} else {
|
|
||||||
// accepts a connection after each call to accept
|
|
||||||
// connections are sent to the channel cf
|
|
||||||
cf, accept = c, a
|
|
||||||
}
|
|
||||||
|
|
||||||
// start user switcher process and save time
|
// start user switcher process and save time
|
||||||
var fsu string
|
var fsu string
|
||||||
if p, ok := internal.Check(internal.Fsu); !ok {
|
if p, ok := internal.Check(internal.Fsu); !ok {
|
||||||
@ -101,10 +65,19 @@ func (s *Shim) Start() (*time.Time, error) {
|
|||||||
fsu = p
|
fsu = p
|
||||||
}
|
}
|
||||||
s.cmd = exec.Command(fsu)
|
s.cmd = exec.Command(fsu)
|
||||||
|
|
||||||
|
var encoder *gob.Encoder
|
||||||
|
if fd, e, err := proc.Setup(&s.cmd.ExtraFiles); err != nil {
|
||||||
|
return nil, fmsg.WrapErrorSuffix(err,
|
||||||
|
"cannot create shim setup pipe:")
|
||||||
|
} else {
|
||||||
|
encoder = e
|
||||||
s.cmd.Env = []string{
|
s.cmd.Env = []string{
|
||||||
shim0.Env + "=" + s.socket,
|
shim0.Env + "=" + strconv.Itoa(fd),
|
||||||
"FORTIFY_APP_ID=" + s.aid,
|
"FORTIFY_APP_ID=" + s.aid,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(s.supp) > 0 {
|
if len(s.supp) > 0 {
|
||||||
fmsg.VPrintf("attaching supplementary group ids %s", s.supp)
|
fmsg.VPrintf("attaching supplementary group ids %s", s.supp)
|
||||||
s.cmd.Env = append(s.cmd.Env, "FORTIFY_GROUPS="+strings.Join(s.supp, " "))
|
s.cmd.Env = append(s.cmd.Env, "FORTIFY_GROUPS="+strings.Join(s.supp, " "))
|
||||||
@ -145,117 +118,20 @@ func (s *Shim) Start() (*time.Time, error) {
|
|||||||
signal.Ignore(syscall.SIGINT, syscall.SIGTERM)
|
signal.Ignore(syscall.SIGINT, syscall.SIGTERM)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
accept()
|
shimErr := make(chan error)
|
||||||
var conn *net.UnixConn
|
go func() { shimErr <- encoder.Encode(s.payload) }()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case c := <-cf:
|
case err := <-shimErr:
|
||||||
if c == nil {
|
|
||||||
return &startTime, fmsg.WrapErrorSuffix(*s.abortErr.Load(), "cannot accept call on setup socket:")
|
|
||||||
} else {
|
|
||||||
conn = c
|
|
||||||
}
|
|
||||||
case <-time.After(shimSetupTimeout):
|
|
||||||
err := fmsg.WrapError(errors.New("timed out waiting for shim"),
|
|
||||||
"timed out waiting for shim to connect")
|
|
||||||
s.AbortWait(err)
|
|
||||||
return &startTime, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 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
|
|
||||||
// this also closes the connection
|
|
||||||
err := s.payload.Serve(conn)
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
cfWg := new(sync.WaitGroup)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case err = <-s.abort:
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmsg.VPrintln("aborting shim setup, reason:", err)
|
return &startTime, fmsg.WrapErrorSuffix(err,
|
||||||
|
"cannot transmit shim config:")
|
||||||
}
|
}
|
||||||
if err = l.Close(); err != nil {
|
killShim = func() {}
|
||||||
fmsg.Println("cannot close setup socket:", err)
|
case <-time.After(shimSetupTimeout):
|
||||||
}
|
return &startTime, fmsg.WrapError(errors.New("timed out waiting for shim"),
|
||||||
close(s.abort)
|
"timed out waiting for shim")
|
||||||
go func() {
|
|
||||||
cfWg.Wait()
|
|
||||||
close(cf)
|
|
||||||
}()
|
|
||||||
return
|
|
||||||
case <-accept:
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cf, func() { accept <- struct{}{} }, nil
|
return &startTime, 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
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
"errors"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -38,15 +37,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// lookup socket path from environment
|
|
||||||
var socketPath string
|
|
||||||
if s, ok := os.LookupEnv(shim.Env); !ok {
|
|
||||||
fmsg.Fatal("FORTIFY_SHIM not set")
|
|
||||||
panic("unreachable")
|
|
||||||
} else {
|
|
||||||
socketPath = s
|
|
||||||
}
|
|
||||||
|
|
||||||
// check path to finit
|
// check path to finit
|
||||||
var finitPath string
|
var finitPath string
|
||||||
if p, ok := internal.Path(internal.Finit); !ok {
|
if p, ok := internal.Path(internal.Finit); !ok {
|
||||||
@ -55,21 +45,24 @@ func main() {
|
|||||||
finitPath = p
|
finitPath = p
|
||||||
}
|
}
|
||||||
|
|
||||||
// dial setup socket
|
// receive setup payload
|
||||||
var conn *net.UnixConn
|
var (
|
||||||
if c, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: socketPath, Net: "unix"}); err != nil {
|
payload shim.Payload
|
||||||
fmsg.Fatal(err.Error())
|
closeSetup func() error
|
||||||
panic("unreachable")
|
)
|
||||||
} else {
|
if f, err := proc.Receive(shim.Env, &payload); err != nil {
|
||||||
conn = c
|
if errors.Is(err, proc.ErrInvalid) {
|
||||||
|
fmsg.Fatal("invalid config descriptor")
|
||||||
|
}
|
||||||
|
if errors.Is(err, proc.ErrNotSet) {
|
||||||
|
fmsg.Fatal("FORTIFY_SHIM not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
// decode payload gob stream
|
fmsg.Fatalf("cannot decode shim setup payload: %v", err)
|
||||||
var payload shim.Payload
|
panic("unreachable")
|
||||||
if err := gob.NewDecoder(conn).Decode(&payload); err != nil {
|
|
||||||
fmsg.Fatalf("cannot decode shim payload: %v", err)
|
|
||||||
} else {
|
} else {
|
||||||
fmsg.SetVerbose(payload.Verbose)
|
fmsg.SetVerbose(payload.Verbose)
|
||||||
|
closeSetup = f
|
||||||
}
|
}
|
||||||
|
|
||||||
if payload.Bwrap == nil {
|
if payload.Bwrap == nil {
|
||||||
@ -82,8 +75,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// close setup socket
|
// close setup socket
|
||||||
if err := conn.Close(); err != nil {
|
if err := closeSetup(); err != nil {
|
||||||
fmsg.Println("cannot close setup socket:", err)
|
fmsg.Println("cannot close setup pipe:", err)
|
||||||
// not fatal
|
// not fatal
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,17 +83,17 @@ func main() {
|
|||||||
uid += aid
|
uid += aid
|
||||||
}
|
}
|
||||||
|
|
||||||
// pass through setup path to shim
|
// pass through setup fd to shim
|
||||||
var shimSetupPath string
|
var shimSetupFd string
|
||||||
if s, ok := os.LookupEnv(envShim); !ok {
|
if s, ok := os.LookupEnv(envShim); !ok {
|
||||||
// fortify requests target uid
|
// fortify requests target uid
|
||||||
// print resolved uid and exit
|
// print resolved uid and exit
|
||||||
fmt.Print(uid)
|
fmt.Print(uid)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
} else if !path.IsAbs(s) {
|
} else if len(s) != 1 || s[0] > '9' || s[0] < '3' {
|
||||||
log.Fatal("FORTIFY_SHIM is not absolute")
|
log.Fatal("FORTIFY_SHIM holds an invalid value")
|
||||||
} else {
|
} else {
|
||||||
shimSetupPath = s
|
shimSetupFd = s
|
||||||
}
|
}
|
||||||
|
|
||||||
// supplementary groups
|
// supplementary groups
|
||||||
@ -142,7 +142,7 @@ func main() {
|
|||||||
if _, _, errno := syscall.AllThreadsSyscall(syscall.SYS_PRCTL, PR_SET_NO_NEW_PRIVS, 1, 0); errno != 0 {
|
if _, _, errno := syscall.AllThreadsSyscall(syscall.SYS_PRCTL, PR_SET_NO_NEW_PRIVS, 1, 0); errno != 0 {
|
||||||
log.Fatalf("cannot set no_new_privs flag: %s", errno.Error())
|
log.Fatalf("cannot set no_new_privs flag: %s", errno.Error())
|
||||||
}
|
}
|
||||||
if err := syscall.Exec(fshim, []string{"fshim"}, []string{envShim + "=" + shimSetupPath}); err != nil {
|
if err := syscall.Exec(fshim, []string{"fshim"}, []string{envShim + "=" + shimSetupFd}); err != nil {
|
||||||
log.Fatalf("cannot start shim: %v", err)
|
log.Fatalf("cannot start shim: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -46,7 +45,6 @@ func (a *app) Start() error {
|
|||||||
uint32(a.seal.sys.UID()),
|
uint32(a.seal.sys.UID()),
|
||||||
a.seal.sys.user.as,
|
a.seal.sys.user.as,
|
||||||
a.seal.sys.user.supp,
|
a.seal.sys.user.supp,
|
||||||
path.Join(a.seal.share, "shim"),
|
|
||||||
&shim0.Payload{
|
&shim0.Payload{
|
||||||
Argv: a.seal.command,
|
Argv: a.seal.command,
|
||||||
Exec: shimExec,
|
Exec: shimExec,
|
||||||
@ -252,12 +250,6 @@ func (a *app) Wait() (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.shim.Unwrap() == nil {
|
|
||||||
fmsg.VPrintln("fault before shim start")
|
|
||||||
} else {
|
|
||||||
a.shim.AbortWait(errors.New("shim exited"))
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.seal.sys.needRevert {
|
if a.seal.sys.needRevert {
|
||||||
if err := a.seal.sys.Revert(ec); err != nil {
|
if err := a.seal.sys.Revert(ec); err != nil {
|
||||||
return err.(RevertCompoundError)
|
return err.(RevertCompoundError)
|
||||||
|
Loading…
Reference in New Issue
Block a user