From 19068533823f22e9fd398d7bf6acf6dd09ee7b64 Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Sun, 8 Sep 2024 02:24:01 +0900 Subject: clean up setup/launcher code and enable better control over shares In the past Wayland, X and PulseAudio are shared unconditionally. This can unnecessarily increase attack surface as some of these resources might not be needed at all. This commit moves all environment preparation code to the internal app package and selectively call them based on flags. An "enablements" bitfield is introduced tracking all enabled shares. This value is registered after successful child process launch and stored in launcher states. Code responsible for running the child process is isolated to its own app/run file and cleaned up. Launch method selection is also extensively cleaned up. The internal state/track readLaunchers function now takes uid as an argument. Launcher state is now printed using text/tabwriter and argv is only emitted when verbose. Signed-off-by: Ophestra Umiker --- internal/app/setup.go | 77 ++++++--------------------------------------------- 1 file changed, 8 insertions(+), 69 deletions(-) (limited to 'internal/app/setup.go') diff --git a/internal/app/setup.go b/internal/app/setup.go index 7b5cd2ec..82765dfb 100644 --- a/internal/app/setup.go +++ b/internal/app/setup.go @@ -4,13 +4,11 @@ import ( "errors" "fmt" "os" - "os/exec" "os/user" "strconv" "git.ophivana.moe/cat/fortify/internal/state" "git.ophivana.moe/cat/fortify/internal/system" - "git.ophivana.moe/cat/fortify/internal/util" ) type App struct { @@ -18,78 +16,19 @@ type App struct { env []string command []string + enablements state.Enablements *user.User -} - -func (a *App) Run() { - f := a.launchBySudo - m, b := false, false - switch { - case system.MethodFlags[0]: // sudo - case system.MethodFlags[1]: // bare - m, b = true, true - default: // machinectl - m, b = true, false - } - - var toolPath string - - // dependency checks - const sudoFallback = "Falling back to 'sudo', some desktop integration features may not work" - if m { - if !util.SdBooted() { - fmt.Println("This system was not booted through systemd") - fmt.Println(sudoFallback) - } else if tp, ok := util.Which("machinectl"); !ok { - fmt.Println("Did not find 'machinectl' in PATH") - fmt.Println(sudoFallback) - } else { - toolPath = tp - f = func() []string { return a.launchByMachineCtl(b) } - } - } else if tp, ok := util.Which("sudo"); !ok { - state.Fatal("Did not find 'sudo' in PATH") - } else { - toolPath = tp - } - - if system.V.Verbose { - fmt.Printf("Selected launcher '%s' bare=%t\n", toolPath, b) - } - cmd := exec.Command(toolPath, f()...) - cmd.Env = a.env - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - cmd.Dir = system.V.RunDir - - if system.V.Verbose { - fmt.Println("Executing:", cmd) - } - - if err := cmd.Start(); err != nil { - state.Fatal("Error starting process:", err) - } - - if err := state.SaveProcess(a.Uid, cmd); err != nil { - // process already started, shouldn't be fatal - fmt.Println("Error registering process:", err) - } + // absolutely *no* method of this type is thread-safe + // so don't treat it as if it is +} - var r int - if err := cmd.Wait(); err != nil { - var exitError *exec.ExitError - if !errors.As(err, &exitError) { - state.Fatal("Error running process:", err) - } +func (a *App) setEnablement(e state.Enablement) { + if a.enablements.Has(e) { + panic("enablement " + e.String() + " set twice") } - if system.V.Verbose { - fmt.Println("Process exited with exit code", r) - } - state.BeforeExit() - os.Exit(r) + a.enablements |= e.Mask() } func New(userName string, args []string) *App { -- cgit v1.3.1