2024-11-02 03:03:44 +09:00
|
|
|
package main
|
2024-10-11 01:55:33 +09:00
|
|
|
|
|
|
|
import (
|
2024-12-18 19:39:25 +09:00
|
|
|
"errors"
|
2024-10-11 01:55:33 +09:00
|
|
|
"os"
|
2024-10-13 16:56:10 +09:00
|
|
|
"path"
|
2024-10-11 01:55:33 +09:00
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
|
2024-12-20 00:20:02 +09:00
|
|
|
init0 "git.gensokyo.uk/security/fortify/cmd/finit/ipc"
|
|
|
|
shim "git.gensokyo.uk/security/fortify/cmd/fshim/ipc"
|
|
|
|
"git.gensokyo.uk/security/fortify/helper"
|
|
|
|
"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-11-02 03:03:44 +09:00
|
|
|
// everything beyond this point runs as unconstrained target user
|
2024-10-11 01:55:33 +09:00
|
|
|
// proceed with caution!
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
func main() {
|
|
|
|
// sharing stdout with fortify
|
|
|
|
// USE WITH CAUTION
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.SetPrefix("shim")
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
// setting this prevents ptrace
|
|
|
|
if err := internal.PR_SET_DUMPABLE__SUID_DUMP_DISABLE(); err != nil {
|
|
|
|
fmsg.Fatalf("cannot set SUID_DUMP_DISABLE: %s", err)
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2024-10-13 16:56:10 +09:00
|
|
|
// re-exec
|
2024-11-02 03:03:44 +09:00
|
|
|
if len(os.Args) > 0 && (os.Args[0] != "fshim" || len(os.Args) != 1) && path.IsAbs(os.Args[0]) {
|
|
|
|
if err := syscall.Exec(os.Args[0], []string{"fshim"}, os.Environ()); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("cannot re-exec self:", err)
|
2024-10-13 16:56:10 +09:00
|
|
|
// continue anyway
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
// check path to finit
|
|
|
|
var finitPath string
|
|
|
|
if p, ok := internal.Path(internal.Finit); !ok {
|
|
|
|
fmsg.Fatal("invalid finit path, this copy of fshim is not compiled correctly")
|
|
|
|
} else {
|
|
|
|
finitPath = p
|
|
|
|
}
|
|
|
|
|
2024-12-18 19:39:25 +09:00
|
|
|
// receive setup payload
|
|
|
|
var (
|
|
|
|
payload shim.Payload
|
|
|
|
closeSetup func() error
|
|
|
|
)
|
|
|
|
if f, err := proc.Receive(shim.Env, &payload); err != nil {
|
|
|
|
if errors.Is(err, proc.ErrInvalid) {
|
|
|
|
fmsg.Fatal("invalid config descriptor")
|
|
|
|
}
|
|
|
|
if errors.Is(err, proc.ErrNotSet) {
|
|
|
|
fmsg.Fatal("FORTIFY_SHIM not set")
|
|
|
|
}
|
2024-10-11 01:55:33 +09:00
|
|
|
|
2024-12-18 19:39:25 +09:00
|
|
|
fmsg.Fatalf("cannot decode shim setup payload: %v", err)
|
|
|
|
panic("unreachable")
|
2024-10-11 01:55:33 +09:00
|
|
|
} else {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.SetVerbose(payload.Verbose)
|
2024-12-18 19:39:25 +09:00
|
|
|
closeSetup = f
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
|
2024-10-11 04:13:56 +09:00
|
|
|
if payload.Bwrap == nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Fatal("bwrap config not supplied")
|
2024-10-11 04:13:56 +09:00
|
|
|
}
|
|
|
|
|
2024-12-06 04:25:33 +09:00
|
|
|
// restore bwrap sync fd
|
|
|
|
if payload.Sync != nil {
|
|
|
|
payload.Bwrap.SetSync(os.NewFile(*payload.Sync, "sync"))
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// close setup socket
|
2024-12-18 19:39:25 +09:00
|
|
|
if err := closeSetup(); err != nil {
|
|
|
|
fmsg.Println("cannot close setup pipe:", err)
|
2024-10-11 01:55:33 +09:00
|
|
|
// not fatal
|
|
|
|
}
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
var ic init0.Payload
|
|
|
|
|
2024-10-11 01:55:33 +09:00
|
|
|
// resolve argv0
|
2024-10-14 02:27:02 +09:00
|
|
|
ic.Argv = payload.Argv
|
|
|
|
if len(ic.Argv) > 0 {
|
2024-10-11 01:55:33 +09:00
|
|
|
// looked up from $PATH by parent
|
2024-11-02 03:03:44 +09:00
|
|
|
ic.Argv0 = payload.Exec[1]
|
2024-10-11 01:55:33 +09:00
|
|
|
} else {
|
|
|
|
// no argv, look up shell instead
|
|
|
|
var ok bool
|
2024-11-16 21:19:45 +09:00
|
|
|
if payload.Bwrap.SetEnv == nil {
|
|
|
|
fmsg.Fatal("no command was specified and environment is unset")
|
|
|
|
}
|
|
|
|
if ic.Argv0, ok = payload.Bwrap.SetEnv["SHELL"]; !ok {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Fatal("no command was specified and $SHELL was unset")
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
ic.Argv = []string{ic.Argv0}
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
conf := payload.Bwrap
|
|
|
|
|
|
|
|
var extraFiles []*os.File
|
|
|
|
|
2024-12-18 17:20:31 +09:00
|
|
|
// serve setup payload
|
|
|
|
if fd, encoder, err := proc.Setup(&extraFiles); err != nil {
|
2024-11-16 21:19:45 +09:00
|
|
|
fmsg.Fatalf("cannot pipe: %v", err)
|
2024-10-14 02:27:02 +09:00
|
|
|
} else {
|
2024-12-18 17:20:31 +09:00
|
|
|
conf.SetEnv[init0.Env] = strconv.Itoa(fd)
|
2024-10-14 02:27:02 +09:00
|
|
|
go func() {
|
2024-12-18 17:20:31 +09:00
|
|
|
fmsg.VPrintln("transmitting config to init")
|
|
|
|
if err = encoder.Encode(&ic); err != nil {
|
2024-11-16 21:19:45 +09:00
|
|
|
fmsg.Fatalf("cannot transmit init config: %v", err)
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
|
|
|
}()
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
helper.BubblewrapName = payload.Exec[0] // resolved bwrap path by parent
|
|
|
|
if b, err := helper.NewBwrap(conf, nil, finitPath,
|
|
|
|
func(int, int) []string { return make([]string, 0) }); err != nil {
|
2024-11-16 21:19:45 +09:00
|
|
|
fmsg.Fatalf("malformed sandbox config: %v", err)
|
2024-10-11 01:55:33 +09:00
|
|
|
} else {
|
|
|
|
cmd := b.Unwrap()
|
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
|
|
|
cmd.ExtraFiles = extraFiles
|
|
|
|
|
2024-10-21 20:47:02 +09:00
|
|
|
if fmsg.Verbose() {
|
|
|
|
fmsg.VPrintln("bwrap args:", conf.Args())
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// run and pass through exit code
|
|
|
|
if err = b.Start(); err != nil {
|
2024-11-16 21:19:45 +09:00
|
|
|
fmsg.Fatalf("cannot start target process: %v", err)
|
2024-10-11 01:55:33 +09:00
|
|
|
} else if err = b.Wait(); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("wait:", err)
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
if b.Unwrap().ProcessState != nil {
|
2024-10-26 23:09:32 +09:00
|
|
|
fmsg.Exit(b.Unwrap().ProcessState.ExitCode())
|
2024-10-11 01:55:33 +09:00
|
|
|
} else {
|
2024-10-26 23:09:32 +09:00
|
|
|
fmsg.Exit(127)
|
2024-10-11 01:55:33 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|