diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-10-27 12:08:17 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-10-27 12:09:34 +0900 |
| commit | 7df9d8d01dadcb05396d278aba11afc2ab4a2328 (patch) | |
| tree | 9114e61306153b50b2def1ab6f817af49e266427 | |
| parent | 6d8bcb63f28fb289aef8d4b702d7d938ac0af4eb (diff) | |
system: move sd_booted implementation to os abstraction
This implements lazy loading of the systemd marker (they are not accessed in init and shim) and ensures consistent behaviour when running with a stub.
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
| -rw-r--r-- | config.go | 3 | ||||
| -rw-r--r-- | internal/app/app_nixos_test.go | 4 | ||||
| -rw-r--r-- | internal/app/seal.go | 2 | ||||
| -rw-r--r-- | internal/early.go | 35 | ||||
| -rw-r--r-- | internal/system.go | 72 | ||||
| -rw-r--r-- | main.go | 2 |
6 files changed, 46 insertions, 72 deletions
@@ -6,7 +6,6 @@ import ( "fmt" "git.ophivana.moe/security/fortify/dbus" - "git.ophivana.moe/security/fortify/internal" "git.ophivana.moe/security/fortify/internal/app" "git.ophivana.moe/security/fortify/internal/fmsg" "git.ophivana.moe/security/fortify/internal/system" @@ -50,7 +49,7 @@ func init() { func init() { methodHelpString := "Method of launching the child process, can be one of \"sudo\"" - if internal.SdBootedV { + if os.SdBooted() { methodHelpString += ", \"systemd\"" } diff --git a/internal/app/app_nixos_test.go b/internal/app/app_nixos_test.go index 9488c6b0..e2ed585f 100644 --- a/internal/app/app_nixos_test.go +++ b/internal/app/app_nixos_test.go @@ -311,3 +311,7 @@ func (s *stubNixOS) Paths() internal.Paths { RunDirPath: "/run/user/1971/fortify", } } + +func (s *stubNixOS) SdBooted() bool { + return true +} diff --git a/internal/app/seal.go b/internal/app/seal.go index b433b82e..80c3ec29 100644 --- a/internal/app/seal.go +++ b/internal/app/seal.go @@ -108,7 +108,7 @@ func (a *app) Seal(config *Config) error { } case method[LaunchMethodMachineCtl]: seal.launchOption = LaunchMethodMachineCtl - if !internal.SdBootedV { + if !a.os.SdBooted() { return fmsg.WrapError(ErrSystemd, "system has not been booted with systemd as init system") } diff --git a/internal/early.go b/internal/early.go deleted file mode 100644 index 81ca49cd..00000000 --- a/internal/early.go +++ /dev/null @@ -1,35 +0,0 @@ -package internal - -import ( - "errors" - "io/fs" - "os" - - "git.ophivana.moe/security/fortify/internal/fmsg" -) - -const ( - systemdCheckPath = "/run/systemd/system" -) - -var SdBootedV = func() bool { - if v, err := SdBooted(); err != nil { - fmsg.Println("cannot read systemd marker:", err) - return false - } else { - return v - } -}() - -// SdBooted implements https://www.freedesktop.org/software/systemd/man/sd_booted.html -func SdBooted() (bool, error) { - _, err := os.Stat(systemdCheckPath) - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - err = nil - } - return false, err - } - - return true, nil -} diff --git a/internal/system.go b/internal/system.go index e98bb67e..3e973b56 100644 --- a/internal/system.go +++ b/internal/system.go @@ -1,6 +1,7 @@ package internal import ( + "errors" "io/fs" "os" "os/exec" @@ -37,6 +38,8 @@ type System interface { // Paths returns a populated [Paths] struct. Paths() Paths + // SdBooted implements https://www.freedesktop.org/software/systemd/man/sd_booted.html + SdBooted() bool } // Paths contains environment dependent paths used by fortify. @@ -71,50 +74,53 @@ func CopyPaths(os System, v *Paths) { type Std struct { paths Paths pathsOnce sync.Once -} - -func (s *Std) Geteuid() int { - return os.Geteuid() -} - -func (s *Std) LookupEnv(key string) (string, bool) { - return os.LookupEnv(key) -} -func (s *Std) TempDir() string { - return os.TempDir() + sdBooted bool + sdBootedOnce sync.Once } -func (s *Std) LookPath(file string) (string, error) { - return exec.LookPath(file) -} +func (s *Std) Geteuid() int { return os.Geteuid() } +func (s *Std) LookupEnv(key string) (string, bool) { return os.LookupEnv(key) } +func (s *Std) TempDir() string { return os.TempDir() } +func (s *Std) LookPath(file string) (string, error) { return exec.LookPath(file) } +func (s *Std) Executable() (string, error) { return os.Executable() } +func (s *Std) Lookup(username string) (*user.User, error) { return user.Lookup(username) } +func (s *Std) ReadDir(name string) ([]os.DirEntry, error) { return os.ReadDir(name) } +func (s *Std) Stat(name string) (fs.FileInfo, error) { return os.Stat(name) } +func (s *Std) Open(name string) (fs.File, error) { return os.Open(name) } +func (s *Std) Exit(code int) { fmsg.Exit(code) } -func (s *Std) Executable() (string, error) { - return os.Executable() -} +const xdgRuntimeDir = "XDG_RUNTIME_DIR" -func (s *Std) Lookup(username string) (*user.User, error) { - return user.Lookup(username) +func (s *Std) Paths() Paths { + s.pathsOnce.Do(func() { CopyPaths(s, &s.paths) }) + return s.paths } -func (s *Std) ReadDir(name string) ([]os.DirEntry, error) { - return os.ReadDir(name) +func (s *Std) SdBooted() bool { + s.sdBootedOnce.Do(func() { s.sdBooted = copySdBooted() }) + return s.sdBooted } -func (s *Std) Stat(name string) (fs.FileInfo, error) { - return os.Stat(name) -} +const systemdCheckPath = "/run/systemd/system" -func (s *Std) Open(name string) (fs.File, error) { - return os.Open(name) -} -func (s *Std) Exit(code int) { - fmsg.Exit(code) +func copySdBooted() bool { + if v, err := sdBooted(); err != nil { + fmsg.Println("cannot read systemd marker:", err) + return false + } else { + return v + } } -const xdgRuntimeDir = "XDG_RUNTIME_DIR" +func sdBooted() (bool, error) { + _, err := os.Stat(systemdCheckPath) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + err = nil + } + return false, err + } -func (s *Std) Paths() Paths { - s.pathsOnce.Do(func() { CopyPaths(s, &s.paths) }) - return s.paths + return true, nil } @@ -30,7 +30,7 @@ func main() { flag.Parse() fmsg.SetVerbose(flagVerbose) - if internal.SdBootedV { + if os.SdBooted() { fmsg.VPrintln("system booted with systemd as init system") } |
