Skip to content

Commit

Permalink
fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshvardhan Karn committed Jul 1, 2024
1 parent 4a33313 commit cf36d6d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
4 changes: 0 additions & 4 deletions pkg/scan/process_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ type fileMatches struct {
updatedSeverity string
}

var (
imageTarFileName = "save-output.tar"
)

func calculateSeverity(inputString []string, severity string, severityScore float64) (string, float64) {
updatedSeverity := "low"
lenMatch := len(inputString)
Expand Down
13 changes: 7 additions & 6 deletions pkg/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scan
import (
"context"
"fmt"
"sync"

"github.com/deepfence/YaraHunter/pkg/config"
"github.com/deepfence/YaraHunter/pkg/output"
Expand Down Expand Up @@ -67,6 +68,7 @@ func (s *Scanner) Scan(ctx *tasks.ScanContext, stype ScanType, namespace, id str
var (
extract extractor.FileExtractor
err error
wg sync.WaitGroup
)
switch stype {
case DirScan:
Expand All @@ -86,11 +88,12 @@ func (s *Scanner) Scan(ctx *tasks.ScanContext, stype ScanType, namespace, id str
// results has to be 1 element max
// to avoid overwriting the buffer entries
results := make(chan []output.IOCFound)
defer close(results)
m := [2][]output.IOCFound{}
i := 0

wg.Add(1)
go func() {
defer wg.Done()
for malwares := range results {
for _, malware := range malwares {
outputFn(malware, scanID)
Expand All @@ -106,18 +109,16 @@ func (s *Scanner) Scan(ctx *tasks.ScanContext, stype ScanType, namespace, id str
}
}

if isExecutable(f.Filename) || isSharedLibrary(f.Filename) {
return
}

err = ScanFile(s, f.Filename, f.Content, f.ContentSize, &m[i], "")
if err != nil {
logrus.Infof("file: %v, err: %v", f.Filename, err)
logrus.Warnf("file: %v, err: %v", f.Filename, err)
}

results <- m[i]
i += 1
i %= len(m)
})
close(results)
wg.Wait()
return nil
}

0 comments on commit cf36d6d

Please sign in to comment.