2024-10-14 02:27:02 +09:00
|
|
|
package init0
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"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-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 (
|
|
|
|
// time to wait for linger processes after death initial process
|
|
|
|
residualProcessTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
// everything beyond this point runs within pid namespace
|
|
|
|
// proceed with caution!
|
|
|
|
|
|
|
|
func doInit(fd uintptr) {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.SetPrefix("init")
|
|
|
|
|
2024-10-14 02:27:02 +09:00
|
|
|
// re-exec
|
|
|
|
if len(os.Args) > 0 && os.Args[0] != "fortify" && path.IsAbs(os.Args[0]) {
|
|
|
|
if err := syscall.Exec(os.Args[0], []string{"fortify", "init"}, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload Payload
|
|
|
|
p := os.NewFile(fd, "config-stream")
|
|
|
|
if p == nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Fatal("invalid config descriptor")
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
|
|
|
if err := gob.NewDecoder(p).Decode(&payload); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Fatal("cannot decode init payload:", err)
|
2024-10-14 02:27:02 +09:00
|
|
|
} else {
|
|
|
|
// sharing stdout with parent
|
|
|
|
// USE WITH CAUTION
|
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
|
|
|
|
if err = os.Unsetenv(EnvInit); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("cannot unset", EnvInit+":", 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close config fd
|
|
|
|
if err := p.Close(); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("cannot close config fd:", err)
|
2024-10-14 02:27:02 +09:00
|
|
|
// not fatal
|
|
|
|
}
|
|
|
|
|
|
|
|
// die with parent
|
|
|
|
if _, _, errno := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_PDEATHSIG, uintptr(syscall.SIGKILL), 0); errno != 0 {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Fatal("prctl(PR_SET_PDEATHSIG, SIGKILL):", errno.Error())
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-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-10-14 02:27:02 +09:00
|
|
|
os.Exit(0)
|
|
|
|
case w := <-info:
|
|
|
|
if w.wpid == cmd.Process.Pid {
|
|
|
|
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:
|
|
|
|
os.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-17 02:38:24 +09:00
|
|
|
os.Exit(r)
|
2024-10-14 02:27:02 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try runs init and stops execution if FORTIFY_INIT is set.
|
|
|
|
func Try() {
|
|
|
|
if os.Getpid() != 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if args := flag.Args(); len(args) == 1 && args[0] == "init" {
|
|
|
|
if s, ok := os.LookupEnv(EnvInit); ok {
|
|
|
|
if fd, err := strconv.Atoi(s); err != nil {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Fatalf("cannot parse %q: %v", s, err)
|
2024-10-14 02:27:02 +09:00
|
|
|
} else {
|
|
|
|
doInit(uintptr(fd))
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|