From 5f73d2ec93d37b8c39350708e0ba9acc329fa3bb Mon Sep 17 00:00:00 2001 From: Brian Davis Date: Tue, 17 Dec 2024 19:09:04 -0500 Subject: [PATCH] read kernel config from /boot read the kernel config from /boot if it exists and /proc/config.gz is not found --- cmd/kernel.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/cmd/kernel.go b/cmd/kernel.go index 162b560..2972116 100644 --- a/cmd/kernel.go +++ b/cmd/kernel.go @@ -1,8 +1,11 @@ package cmd import ( - "github.com/slimm609/checksec/pkg/utils" + "fmt" + "os" + "strings" + "github.com/slimm609/checksec/pkg/utils" "github.com/spf13/cobra" ) @@ -14,8 +17,27 @@ var kernelCmd = &cobra.Command{ var configFile string if len(args) > 0 { configFile = args[0] - } else { + } else if _, err := os.Stat("/proc/config.gz"); err == nil { configFile = "/proc/config.gz" + } else { + osReleaseFile := "/proc/sys/kernel/osrelease" + _, err := os.Stat(osReleaseFile) + if err != nil { + fmt.Println("Error: could not find kernel config") + os.Exit(1) + } + osReleaseVersion, err := os.ReadFile(osReleaseFile) + if err != nil { + fmt.Println("Error: could not find kernel config") + os.Exit(1) + } + content := strings.ReplaceAll(string(osReleaseVersion), "\n", "") + configFile = fmt.Sprintf("%s-%s", "/boot/config", content) + _, err = os.Stat(configFile) + if err != nil { + fmt.Println("Error: could not find kernel config") + os.Exit(1) + } } utils.CheckFileExists(configFile)