2024-10-11 01:55:33 +09:00
|
|
|
package shim
|
|
|
|
|
|
|
|
import (
|
2025-01-15 23:39:51 +09:00
|
|
|
"context"
|
2024-12-18 19:39:25 +09:00
|
|
|
"encoding/gob"
|
2024-10-11 01:55:33 +09:00
|
|
|
"errors"
|
2024-10-27 00:46:15 +09:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-12-18 19:39:25 +09:00
|
|
|
"strconv"
|
2024-11-16 21:19:45 +09:00
|
|
|
"strings"
|
2024-10-27 00:46:15 +09:00
|
|
|
"time"
|
2024-10-11 01:55:33 +09:00
|
|
|
|
2024-12-20 00:20:02 +09:00
|
|
|
"git.gensokyo.uk/security/fortify/internal"
|
|
|
|
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
|
|
|
"git.gensokyo.uk/security/fortify/internal/proc"
|
2024-10-11 01:55:33 +09:00
|
|
|
)
|
|
|
|
|
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
|
2024-11-16 21:19:45 +09:00
|
|
|
// fallback exit notifier with error returned killing the process
|
|
|
|
killFallback chan error
|
2025-01-15 23:39:51 +09:00
|
|
|
// monitor to shim encoder
|
|
|
|
encoder *gob.Encoder
|
2025-01-19 18:38:13 +09:00
|
|
|
// bwrap --sync-fd value
|
|
|
|
sync *uintptr
|
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
|
|
|
|
}
|
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
func (s *Shim) WaitFallback() chan error {
|
|
|
|
return s.killFallback
|
|
|
|
}
|
2024-10-21 21:23:56 +09:00
|
|
|
|
2025-01-19 18:05:53 +09:00
|
|
|
func (s *Shim) Start(
|
|
|
|
// string representation of application id
|
|
|
|
aid string,
|
|
|
|
// string representation of supplementary group ids
|
|
|
|
supp []string,
|
2025-01-19 18:38:13 +09:00
|
|
|
// bwrap --sync-fd
|
|
|
|
syncFd *os.File,
|
2025-01-19 18:05:53 +09:00
|
|
|
) (*time.Time, error) {
|
2025-01-15 23:39:51 +09:00
|
|
|
// prepare user switcher invocation
|
2024-11-16 21:19:45 +09:00
|
|
|
var fsu string
|
2025-01-18 11:59:33 +09:00
|
|
|
if p, ok := internal.Path(internal.Fsu); !ok {
|
2025-01-17 23:43:32 +09:00
|
|
|
fmsg.Fatal("invalid fsu path, this copy of fortify is not compiled correctly")
|
2024-11-16 21:19:45 +09:00
|
|
|
panic("unreachable")
|
|
|
|
} else {
|
|
|
|
fsu = p
|
|
|
|
}
|
|
|
|
s.cmd = exec.Command(fsu)
|
2024-12-18 19:39:25 +09:00
|
|
|
|
2025-01-15 23:39:51 +09:00
|
|
|
// pass shim setup pipe
|
2024-12-18 19:39:25 +09:00
|
|
|
if fd, e, err := proc.Setup(&s.cmd.ExtraFiles); err != nil {
|
|
|
|
return nil, fmsg.WrapErrorSuffix(err,
|
|
|
|
"cannot create shim setup pipe:")
|
|
|
|
} else {
|
2025-01-15 23:39:51 +09:00
|
|
|
s.encoder = e
|
2024-12-18 19:39:25 +09:00
|
|
|
s.cmd.Env = []string{
|
2025-01-17 23:43:32 +09:00
|
|
|
Env + "=" + strconv.Itoa(fd),
|
2025-01-19 18:05:53 +09:00
|
|
|
"FORTIFY_APP_ID=" + aid,
|
2024-12-18 19:39:25 +09:00
|
|
|
}
|
2024-11-16 21:19:45 +09:00
|
|
|
}
|
2024-12-18 19:39:25 +09:00
|
|
|
|
2025-01-15 23:39:51 +09:00
|
|
|
// format fsu supplementary groups
|
2025-01-19 18:05:53 +09:00
|
|
|
if len(supp) > 0 {
|
|
|
|
fmsg.VPrintf("attaching supplementary group ids %s", supp)
|
|
|
|
s.cmd.Env = append(s.cmd.Env, "FORTIFY_GROUPS="+strings.Join(supp, " "))
|
2024-11-16 21:19:45 +09:00
|
|
|
}
|
2024-10-27 00:46:15 +09:00
|
|
|
s.cmd.Stdin, s.cmd.Stdout, s.cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
|
|
|
s.cmd.Dir = "/"
|
2024-12-06 04:25:33 +09:00
|
|
|
|
|
|
|
// pass sync fd if set
|
2025-01-19 18:38:13 +09:00
|
|
|
if syncFd != nil {
|
|
|
|
fd := proc.ExtraFile(s.cmd, syncFd)
|
|
|
|
s.sync = &fd
|
2024-12-06 04:25:33 +09:00
|
|
|
}
|
|
|
|
|
2024-11-16 21:19:45 +09:00
|
|
|
fmsg.VPrintln("starting shim via fsu:", s.cmd)
|
2025-01-15 23:39:51 +09:00
|
|
|
// withhold messages to stderr
|
|
|
|
fmsg.Suspend()
|
2024-10-27 00:46:15 +09:00
|
|
|
if err := s.cmd.Start(); err != nil {
|
|
|
|
return nil, fmsg.WrapErrorSuffix(err,
|
2024-11-16 21:19:45 +09:00
|
|
|
"cannot start fsu:")
|
2024-10-27 00:46:15 +09:00
|
|
|
}
|
|
|
|
startTime := time.Now().UTC()
|
2025-01-15 23:39:51 +09:00
|
|
|
return &startTime, nil
|
|
|
|
}
|
2024-10-21 21:23:56 +09:00
|
|
|
|
2025-01-19 18:05:53 +09:00
|
|
|
func (s *Shim) Serve(ctx context.Context, payload *Payload) error {
|
2024-10-27 00:46:15 +09:00
|
|
|
// kill shim if something goes wrong and an error is returned
|
2024-11-16 21:19:45 +09:00
|
|
|
s.killFallback = make(chan error, 1)
|
2024-10-27 00:46:15 +09:00
|
|
|
killShim := func() {
|
|
|
|
if err := s.cmd.Process.Signal(os.Interrupt); err != nil {
|
2024-11-16 21:19:45 +09:00
|
|
|
s.killFallback <- err
|
2024-10-27 00:46:15 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
defer func() { killShim() }()
|
|
|
|
|
2025-01-19 18:38:13 +09:00
|
|
|
payload.Sync = s.sync
|
2025-01-15 23:39:51 +09:00
|
|
|
encodeErr := make(chan error)
|
2025-01-19 18:05:53 +09:00
|
|
|
go func() { encodeErr <- s.encoder.Encode(payload) }()
|
2024-12-18 19:39:25 +09:00
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
select {
|
2025-01-15 23:39:51 +09:00
|
|
|
// encode return indicates setup completion
|
|
|
|
case err := <-encodeErr:
|
2024-12-18 19:39:25 +09:00
|
|
|
if err != nil {
|
2025-01-15 23:39:51 +09:00
|
|
|
return fmsg.WrapErrorSuffix(err,
|
2024-12-18 19:39:25 +09:00
|
|
|
"cannot transmit shim config:")
|
2024-11-02 03:03:44 +09:00
|
|
|
}
|
2024-10-27 00:46:15 +09:00
|
|
|
killShim = func() {}
|
2025-01-15 23:39:51 +09:00
|
|
|
return nil
|
|
|
|
|
|
|
|
// setup canceled before payload was accepted
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := ctx.Err()
|
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
return fmsg.WrapError(errors.New("shim setup canceled"),
|
|
|
|
"shim setup canceled")
|
|
|
|
}
|
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return fmsg.WrapError(errors.New("deadline exceeded waiting for shim"),
|
|
|
|
"deadline exceeded waiting for shim")
|
|
|
|
}
|
|
|
|
// unreachable
|
|
|
|
return err
|
2024-10-27 00:46:15 +09:00
|
|
|
}
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|