fortify: root check before command handling
test / test (push) Successful in 22s Details

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra Umiker 2024-11-05 12:57:03 +09:00
parent 861bb1274f
commit 89bafd0c22
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
1 changed files with 6 additions and 7 deletions

13
main.go
View File

@ -35,6 +35,11 @@ func main() {
// not fatal: this program runs as the privileged user // not fatal: this program runs as the privileged user
} }
if os.Geteuid() == 0 {
fmsg.Fatal("this program must not run as root")
panic("unreachable")
}
flag.CommandLine.Usage = func() { flag.CommandLine.Usage = func() {
fmt.Println() fmt.Println()
fmt.Println("Usage:\tfortify [-v] COMMAND [OPTIONS]") fmt.Println("Usage:\tfortify [-v] COMMAND [OPTIONS]")
@ -54,19 +59,13 @@ func main() {
_, _ = fmt.Fprintf(w, "\t%s\t%s\n", c[0], c[1]) _, _ = fmt.Fprintf(w, "\t%s\t%s\n", c[0], c[1])
} }
if err := w.Flush(); err != nil { if err := w.Flush(); err != nil {
fmsg.Fatalf("cannot print help: %v", err) fmt.Printf("fortify: cannot write command list: %v\n", err)
} }
fmt.Println() fmt.Println()
} }
flag.Parse() flag.Parse()
fmsg.SetVerbose(flagVerbose) fmsg.SetVerbose(flagVerbose)
// root check
if os.Geteuid() == 0 {
fmsg.Fatal("this program must not run as root")
panic("unreachable")
}
args := flag.Args() args := flag.Args()
if len(args) == 0 { if len(args) == 0 {
flag.CommandLine.Usage() flag.CommandLine.Usage()