Skip to content

🏎️ access score impact safely #1526

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

Closed
wants to merge 3 commits into from
Closed

Conversation

afiune
Copy link
Contributor

@afiune afiune commented Dec 27, 2024

We have a number of errors like this one below, I am not 100% sure why we are accessing
an invalid memory address or nil pointer since we are indeed checking the pointers are not
nil but, the only thing I can think of is:

  1. We are trying to convert int32 to uint32 and we are overflowing (not likely but I added a check)
  2. The impact is check but when we try to access it, the graph already consumed the node and therefore the impact is nil (also very unlikely but added variable assignation and a check)

Note we did not get notified of this panic because of #1525

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x161e309]

goroutine 86 [running]:
go.mondoo.com/cnspec/v11/policy.(*bandedScoreCalculator).Add(0x22618c0?, 0xc00101f8c0?, 0xc000c83858?)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/score_calculator.go:505 +0x69
go.mondoo.com/cnspec/v11/policy.AddSpecdScore({0x228f728, 0xc001928600}, 0xc000c83858?, 0x58?, 0xc00115fc40)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/score_calculator.go:103 +0xb9
go.mondoo.com/cnspec/v11/policy/executor/internal.(*ReportingJobNodeData).score(0xc0015e8080)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/executor/internal/nodes.go:593 +0x279
go.mondoo.com/cnspec/v11/policy/executor/internal.(*ReportingJobNodeData).recalculate(0xc0015e8080)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/executor/internal/nodes.go:486 +0x3f
go.mondoo.com/cnspec/v11/policy/executor/internal.(*GraphExecutor).Execute(0xc000db6370)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/executor/internal/graph.go:121 +0x46d
go.mondoo.com/cnspec/v11/policy/executor.ExecuteResolvedPolicy({0x229a8c8, 0xc000343700}, {0x229dcc8, 0xc000e0bec0}, {0xc000e747e0, 0x56}, 0xc001215f00, {0xc000a76160, 0x1, 0x8}, ...)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/executor/graph.go:59 +0x453
go.mondoo.com/cnspec/v11/policy/scan.(*localAssetScanner).runPolicy(0xc0002efd38)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:979 +0x549
go.mondoo.com/cnspec/v11/policy/scan.(*localAssetScanner).run(0xc000c83d38)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:750 +0x33
go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).runMotorizedAsset.func1(0xc000d994c0?, 0xc000e0bec0?)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:601 +0x218
go.mondoo.com/cnspec/v11/internal/datalakes/inmemory.WithDb({0x229a8c8?, 0xc000590580?}, 0x3f?, 0xc000e37df8)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/internal/datalakes/inmemory/inmemory.go:52 +0x42
go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).runMotorizedAsset(0xc0003951f0?, 0x1fe1566?)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:577 +0x65
go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).RunAssetJob(0xc000169080, 0xc000e0c700)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:524 +0xab
go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).distributeJob.func3()
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:450 +0x4c5
created by go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).distributeJob in goroutine 1
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:416 +0x836

We have a number of errors like this one below, I am not 100% sure why
we are accessing an invalid memory address or nil pointer but, this is
a best-effort to access the score impact safely.

```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x161e309]

goroutine 86 [running]:
go.mondoo.com/cnspec/v11/policy.(*bandedScoreCalculator).Add(0x22618c0?, 0xc00101f8c0?, 0xc000c83858?)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/score_calculator.go:505 +0x69
go.mondoo.com/cnspec/v11/policy.AddSpecdScore({0x228f728, 0xc001928600}, 0xc000c83858?, 0x58?, 0xc00115fc40)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/score_calculator.go:103 +0xb9
go.mondoo.com/cnspec/v11/policy/executor/internal.(*ReportingJobNodeData).score(0xc0015e8080)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/executor/internal/nodes.go:593 +0x279
go.mondoo.com/cnspec/v11/policy/executor/internal.(*ReportingJobNodeData).recalculate(0xc0015e8080)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/executor/internal/nodes.go:486 +0x3f
go.mondoo.com/cnspec/v11/policy/executor/internal.(*GraphExecutor).Execute(0xc000db6370)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/executor/internal/graph.go:121 +0x46d
go.mondoo.com/cnspec/v11/policy/executor.ExecuteResolvedPolicy({0x229a8c8, 0xc000343700}, {0x229dcc8, 0xc000e0bec0}, {0xc000e747e0, 0x56}, 0xc001215f00, {0xc000a76160, 0x1, 0x8}, ...)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/executor/graph.go:59 +0x453
go.mondoo.com/cnspec/v11/policy/scan.(*localAssetScanner).runPolicy(0xc0002efd38)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:979 +0x549
go.mondoo.com/cnspec/v11/policy/scan.(*localAssetScanner).run(0xc000c83d38)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:750 +0x33
go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).runMotorizedAsset.func1(0xc000d994c0?, 0xc000e0bec0?)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:601 +0x218
go.mondoo.com/cnspec/v11/internal/datalakes/inmemory.WithDb({0x229a8c8?, 0xc000590580?}, 0x3f?, 0xc000e37df8)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/internal/datalakes/inmemory/inmemory.go:52 +0x42
go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).runMotorizedAsset(0xc0003951f0?, 0x1fe1566?)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:577 +0x65
go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).RunAssetJob(0xc000169080, 0xc000e0c700)
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:524 +0xab
go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).distributeJob.func3()
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:450 +0x4c5
created by go.mondoo.com/cnspec/v11/policy/scan.(*LocalScanner).distributeJob in goroutine 1
	/go/pkg/mod/go.mondoo.com/cnspec/v11@v11.34.0/policy/scan/local_scanner.go:416 +0x836
```

Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
Copy link
Contributor

github-actions bot commented Dec 27, 2024

Test Results

  1 files   27 suites   1m 6s ⏱️
509 tests 508 ✅ 1 💤 0 ❌
510 runs  509 ✅ 1 💤 0 ❌

Results for commit 7df4daf.

♻️ This comment has been updated with latest results.

@afiune afiune requested a review from czunker December 27, 2024 13:55
@chris-rock chris-rock requested a review from jaym December 27, 2024 13:56
afiune and others added 2 commits December 27, 2024 15:05
Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
Co-authored-by: Christian Zunker <827818+czunker@users.noreply.github.com>
@afiune afiune force-pushed the afiune/score-impact-safe branch from 201624e to 7df4daf Compare December 27, 2024 14:05
@@ -501,8 +501,13 @@ func (c *bandedScoreCalculator) Add(score *Score, impact *explorer.Impact) {

if score.ScoreCompletion != 0 && score.Weight != 0 {
category := uint32(0)
if impact != nil && impact.Value != nil {
category = 100 - uint32(impact.Value.Value)
if impact != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these 2 look the same to me. Am i missing somethng

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -590,7 +590,8 @@ func (nodeData *ReportingJobNodeData) score() (*policy.Score, error) {
if s == nil {
return nil, nil
}
policy.AddSpecdScore(calculator, s, rjRes.score != nil, rjRes.impact)
i := rjRes.impact
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't understand why this makes it safer to access

@afiune
Copy link
Contributor Author

afiune commented Jan 6, 2025

Closing in favor of #1514

@afiune afiune closed this Jan 6, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jan 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants