Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve validation performance #13

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion core/suidcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func ValidateSUID(file string, isSUID bool) {
} else if fileStat.Mode()&os.ModeSetuid == 0 && isSUID {
fmt.Printf("[FAIL] File %s has incorrect suid permission\n", file)
correctSUID(file, isSUID)
} else {
}
}

Expand Down
133 changes: 82 additions & 51 deletions core/validate.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,119 @@
package core

import (
"bufio"
"bytes"
"crypto/sha1"
"fmt"
"io"
"os"
"strconv"
"runtime"
"strings"
"sync"

"github.com/linux-immutability-tools/FsGuard/config"
)

func ValidatePath(recipePath string) error {
data, err := os.ReadFile(recipePath)
if err != nil {
return err
}
func validatePathThread(dataCh chan string, errCh chan error, wg *sync.WaitGroup) {
defer wg.Done()

var wg sync.WaitGroup
errCh := make(chan error, 1)
var file *os.File = nil
var err error = nil
for data := range dataCh {
if len(data) == 0 {
return
}

for _, file := range strings.Split(string(data), "\n") {
if strings.TrimSpace(file) == "" {
idx := strings.Index(data, " #FSG# ")
if idx == -1 {
errCh <- fmt.Errorf("[FAIL] %s - Malformed line", data)
continue
}
properties := strings.Split(file, " #FSG# ")

wg.Add(1)
go func(prop []string) {
defer wg.Done()
if _, err := os.Stat(prop[0]); os.IsNotExist(err) {
errCh <- fmt.Errorf("[FAIL] %s - File not found", prop[0])
return
}
name := data[:idx]
sig := data[idx+7 : idx+47]
var isSUID bool
switch data[idx+54:] {
case "true":
isSUID = true
case "false":
isSUID = false
default:
errCh <- fmt.Errorf("[FAIL] %s - Cannot find suid value", data[idx+54:])
continue
}

file, err := os.Open(prop[0])
if err != nil {
errCh <- err
return
if file, err = os.Open(name); err != nil {
if os.IsNotExist(err) {
errCh <- fmt.Errorf("[FAIL] %s - File not found", name)
continue
}
defer file.Close()
errCh <- err
continue
}

sha1sum, err := calculateHash(file)
if err != nil {
file.Close()
errCh <- err
continue
}
file.Close()

sha1sum, err := calculateHash(file)
if err != nil {
failed := false
if err = validateChecksum(name, sha1sum, sig); err != nil {
if config.QuitOnFail {
errCh <- err
return
continue
} else {
failed = true
}
}

prop[1] = strings.TrimSpace(prop[1])

failed := false
errOut := ""
if err := validateChecksum(prop[0], sha1sum, prop[1]); err != nil {
if config.QuitOnFail {
errCh <- err
return
} else {
failed = true
errOut = err.Error()
}
}
ValidateSUID(name, isSUID)
if !failed {
fmt.Printf("[OK] %s - %s\n", name, sha1sum)
} else {
fmt.Printf("%s\n", err)
}
}
}

isSUID, err := strconv.ParseBool(prop[2])
if err != nil {
errCh <- fmt.Errorf("[FAIL] %s - Cannot find suid value", prop[0])
return
}
ValidateSUID(prop[0], isSUID)
if config.QuitOnFail || !failed {
fmt.Printf("[OK] %s - %s\n", prop[0], sha1sum)
} else {
fmt.Printf("%s\n", errOut)
}
}(properties)
func ValidatePath(recipePath string) error {
data, err := os.ReadFile(recipePath)
if err != nil {
return err
}

var wg sync.WaitGroup
threads := runtime.NumCPU()
errCh := make(chan error)
dataCh := make(chan string, threads)

for i := 0; i < threads; i++ {
wg.Add(1)
go validatePathThread(dataCh, errCh, &wg)
}

scanner := bufio.NewScanner(bytes.NewReader(data))
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
dataCh <- scanner.Text()
}

for i := 0; i < threads; i++ {
dataCh <- ""
}

go func() {
wg.Wait()
close(errCh)
close(dataCh)
}()

for err := range errCh {
if err != nil {
fmt.Println(err)
return err
}
}
Expand Down
Loading