2024-11-02 03:03:44 +09:00
|
|
|
package main
|
2024-10-14 02:27:02 +09:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2024-10-17 02:38:24 +09:00
|
|
|
"time"
|
2024-10-14 02:27:02 +09:00
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
init0 "git.ophivana.moe/security/fortify/cmd/finit/ipc"
|
|
|
|
"git.ophivana.moe/security/fortify/internal"
|
2024-10-21 20:47:02 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/fmsg"
|
2024-10-14 02:27:02 +09:00
|
|
|
)
|
|
|
|
|
2024-10-17 02:38:24 +09:00
|
|
|
const (
|
2024-11-02 03:03:44 +09:00
|
|
|
// time to wait for linger processes after death of initial process
|
2024-10-17 02:38:24 +09:00
|
|
|
residualProcessTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
// everything beyond this point runs within pid namespace
|
|
|
|
// proceed with caution!
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
func main() {
|
|
|
|
// sharing stdout with shim
|
|
|
|
// USE WITH CAUTION
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.SetPrefix("init")
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getpid() != 1 {
|
|
|
|
fmsg.Fatal("this process must run as pid 1")
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
// re-exec
|
2024-11-02 03:03:44 +09:00
|
|
|
if len(os.Args) > 0 && (os.Args[0] != "finit" || len(os.Args) != 1) && path.IsAbs(os.Args[0]) {
|
|
|
|
if err := syscall.Exec(os.Args[0], []string{"finit"}, os.Environ()); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("cannot re-exec self:", err)
|
2024-10-14 02:27:02 +09:00
|
|
|
// continue anyway
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
// setup pipe fd from environment
|
|
|
|
var setup *os.File
|
|
|
|
if s, ok := os.LookupEnv(init0.Env); !ok {
|
|
|
|
fmsg.Fatal("FORTIFY_INIT not set")
|
|
|
|
panic("unreachable")
|
|
|
|
} else {
|
|
|
|
if fd, err := strconv.Atoi(s); err != nil {
|
|
|
|
fmsg.Fatalf("cannot parse %q: %v", s, err)
|
|
|
|
panic("unreachable")
|
|
|
|
} else {
|
|
|
|
setup = os.NewFile(uintptr(fd), "setup")
|
|
|
|
if setup == nil {
|
|
|
|
fmsg.Fatal("invalid config descriptor")
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
2024-11-02 03:03:44 +09:00
|
|
|
|
|
|
|
var payload init0.Payload
|
|
|
|
if err := gob.NewDecoder(setup).Decode(&payload); err != nil {
|
|
|
|
fmsg.Fatal("cannot decode init setup payload:", err)
|
|
|
|
panic("unreachable")
|
2024-10-14 02:27:02 +09:00
|
|
|
} else {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.SetVerbose(payload.Verbose)
|
2024-10-14 02:27:02 +09:00
|
|
|
|
|
|
|
// child does not need to see this
|
2024-11-02 03:03:44 +09:00
|
|
|
if err = os.Unsetenv(init0.Env); err != nil {
|
|
|
|
fmsg.Printf("cannot unset %s: %v", init0.Env, err)
|
2024-10-14 02:27:02 +09:00
|
|
|
// not fatal
|
|
|
|
} else {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("received configuration")
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// die with parent
|
2024-11-02 03:03:44 +09:00
|
|
|
if err := internal.PR_SET_PDEATHSIG__SIGKILL(); err != nil {
|
|
|
|
fmsg.Fatalf("prctl(PR_SET_PDEATHSIG, SIGKILL): %v", err)
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(payload.Argv0)
|
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
|
|
|
cmd.Args = payload.Argv
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
|
|
|
|
// pass wayland fd
|
|
|
|
if payload.WL != -1 {
|
|
|
|
if f := os.NewFile(uintptr(payload.WL), "wayland"); f != nil {
|
|
|
|
cmd.Env = append(cmd.Env, "WAYLAND_SOCKET="+strconv.Itoa(3+len(cmd.ExtraFiles)))
|
|
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Fatalf("cannot start %q: %v", payload.Argv0, err)
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
2024-11-16 21:19:45 +09:00
|
|
|
fmsg.Suspend()
|
2024-11-02 03:03:44 +09:00
|
|
|
|
|
|
|
// close setup pipe as setup is now complete
|
|
|
|
if err := setup.Close(); err != nil {
|
|
|
|
fmsg.Println("cannot close setup pipe:", err)
|
|
|
|
// not fatal
|
|
|
|
}
|
2024-10-14 02:27:02 +09:00
|
|
|
|
|
|
|
sig := make(chan os.Signal, 2)
|
|
|
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
type winfo struct {
|
|
|
|
wpid int
|
|
|
|
wstatus syscall.WaitStatus
|
|
|
|
}
|
|
|
|
info := make(chan winfo, 1)
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
wpid = -2
|
|
|
|
wstatus syscall.WaitStatus
|
|
|
|
)
|
|
|
|
|
|
|
|
// keep going until no child process is left
|
|
|
|
for wpid != -1 {
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if wpid != -2 {
|
|
|
|
info <- winfo{wpid, wstatus}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = syscall.EINTR
|
|
|
|
for errors.Is(err, syscall.EINTR) {
|
|
|
|
wpid, err = syscall.Wait4(-1, &wstatus, 0, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !errors.Is(err, syscall.ECHILD) {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("unexpected wait4 response:", err)
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-11-02 03:03:44 +09:00
|
|
|
// closed after residualProcessTimeout has elapsed after initial process death
|
2024-10-17 02:38:24 +09:00
|
|
|
timeout := make(chan struct{})
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
r := 2
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case s := <-sig:
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintln("received", s.String())
|
2024-11-02 03:03:44 +09:00
|
|
|
fmsg.Resume() // output could still be withheld at this point, so resume is called
|
2024-10-26 23:09:32 +09:00
|
|
|
fmsg.Exit(0)
|
2024-10-14 02:27:02 +09:00
|
|
|
case w := <-info:
|
|
|
|
if w.wpid == cmd.Process.Pid {
|
2024-11-02 03:03:44 +09:00
|
|
|
// initial process exited, output is most likely available again
|
|
|
|
fmsg.Resume()
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
switch {
|
|
|
|
case w.wstatus.Exited():
|
|
|
|
r = w.wstatus.ExitStatus()
|
|
|
|
case w.wstatus.Signaled():
|
|
|
|
r = 128 + int(w.wstatus.Signal())
|
|
|
|
default:
|
|
|
|
r = 255
|
|
|
|
}
|
2024-10-17 20:46:25 +09:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(residualProcessTimeout)
|
|
|
|
close(timeout)
|
|
|
|
}()
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
|
|
|
case <-done:
|
2024-10-26 23:09:32 +09:00
|
|
|
fmsg.Exit(r)
|
2024-10-17 02:38:24 +09:00
|
|
|
case <-timeout:
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("timeout exceeded waiting for lingering processes")
|
2024-10-26 23:09:32 +09:00
|
|
|
fmsg.Exit(r)
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|