Skip to content

Commit

Permalink
sbctl: setup debug logging
Browse files Browse the repository at this point in the history
Signed-off-by: Morten Linderud <[email protected]>
  • Loading branch information
Foxboron committed Jul 29, 2024
1 parent a1d56bf commit b969902
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
13 changes: 13 additions & 0 deletions cmd/sbctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log"
"log/slog"
"os"
"strings"

Expand All @@ -23,6 +24,7 @@ type CmdOptions struct {
QuietOutput bool
Config string
DisableLandlock bool
Debug bool
}

type cliCommand struct {
Expand Down Expand Up @@ -59,6 +61,7 @@ func baseFlags(cmd *cobra.Command) {
flags.BoolVar(&cmdOptions.JsonOutput, "json", false, "Output as json")
flags.BoolVar(&cmdOptions.QuietOutput, "quiet", false, "Mute info from logging")
flags.BoolVar(&cmdOptions.DisableLandlock, "disable-landlock", false, "disable landlock")
flags.BoolVar(&cmdOptions.Debug, "debug", false, "debug logging")
flags.StringVarP(&cmdOptions.Config, "config", "", "", "Path to configuration file")

cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -124,6 +127,16 @@ func main() {
if cmdOptions.DisableLandlock {
state.Config.Landlock = false
}

// Setup debug logging
opts := &slog.HandlerOptions{
Level: slog.LevelInfo,
}
if cmdOptions.Debug {
opts.Level = slog.LevelDebug
}
logger := slog.New(slog.NewTextHandler(os.Stdout, opts))
slog.SetDefault(logger)
}

ctx := context.WithValue(context.Background(), stateDataKey{}, state)
Expand Down
38 changes: 38 additions & 0 deletions cmd/sbctl/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package main

import (
"fmt"
"log/slog"
"os"
"strings"

"github.com/foxboron/go-uefi/efi/signature"
"github.com/foxboron/sbctl"
"github.com/foxboron/sbctl/backend"
"github.com/foxboron/sbctl/certs"
"github.com/foxboron/sbctl/config"
"github.com/foxboron/sbctl/logging"
Expand Down Expand Up @@ -80,6 +83,37 @@ func PrintStatus(s *Status) {
}
}

func RunDebug(state *config.State) error {
kh, err := backend.GetKeyHierarchy(state.Fs, state.Config)
if err != nil {
return err
}

efistate, err := sbctl.SystemEFIVariables(state.Efivarfs)
if err != nil {
return err
}

guid, err := state.Config.GetGUID(state.Fs)
if err != nil {
return err
}

if efistate.PK.SigDataExists(signature.CERT_X509_GUID, &signature.SignatureData{Owner: *guid, Data: kh.PK.Certificate().Raw}) {
slog.Debug("PK is fine")
}

if efistate.KEK.SigDataExists(signature.CERT_X509_GUID, &signature.SignatureData{Owner: *guid, Data: kh.KEK.Certificate().Raw}) {
slog.Debug("KEK is fine")
}

if efistate.Db.SigDataExists(signature.CERT_X509_GUID, &signature.SignatureData{Owner: *guid, Data: kh.Db.Certificate().Raw}) {
slog.Debug("db is fine")
}

return nil
}

func RunStatus(cmd *cobra.Command, args []string) error {
state := cmd.Context().Value(stateDataKey{}).(*config.State)

Expand All @@ -89,6 +123,10 @@ func RunStatus(cmd *cobra.Command, args []string) error {
}
}

if cmdOptions.Debug {
RunDebug(state)
}

stat := NewStatus()
if _, err := state.Fs.Stat("/sys/firmware/efi/efivars"); os.IsNotExist(err) {
return fmt.Errorf("system is not booted with UEFI")
Expand Down
3 changes: 3 additions & 0 deletions docs/sbctl.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ Options
+
See linkman:landlock[7].

**--debug**::
Enable verbose debug logging. This will break the pretty printed text.


Bundles
-------
Expand Down
8 changes: 4 additions & 4 deletions lsm/lsm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lsm

import (
"log/slog"
"path/filepath"

"github.com/foxboron/sbctl/config"
Expand Down Expand Up @@ -36,10 +37,9 @@ func RestrictAdditionalPaths(r ...landlock.Rule) {
}

func Restrict() error {
// TODO: For debug logging
// for _, r := range rules {
// fmt.Println(r)
// }
for _, r := range rules {
slog.Debug("landlock", slog.Any("rule", r))
}
landlock.V5.BestEffort().RestrictNet()
return landlock.V5.BestEffort().RestrictPaths(rules...)
}

0 comments on commit b969902

Please sign in to comment.