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

fix data race in race requests #3275

Merged
merged 1 commit into from
Feb 6, 2023
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
5 changes: 3 additions & 2 deletions integration_tests/debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ if [ $1 = "-h" ]; then
exit 0
fi

# Stop execution if race condition is found
export GORACE="halt_on_error=1"

echo "::group::Build nuclei"
rm nuclei 2>/dev/null
cd ../v2/cmd/nuclei
go build .
go build -race .
mv nuclei ../../../integration_tests/nuclei
echo -e "::endgroup::\n"
cd ../../../integration_tests

cmdstring=""

if [ -n "$1" ]; then
Expand Down
15 changes: 8 additions & 7 deletions v2/pkg/protocols/common/executer/executer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package executer
import (
"fmt"
"strings"
"sync/atomic"

"github.com/pkg/errors"

Expand Down Expand Up @@ -60,7 +61,7 @@ func (e *Executer) Requests() int {

// Execute executes the protocol group and returns true or false if results were found.
func (e *Executer) Execute(input *contextargs.Context) (bool, error) {
var results bool
results := &atomic.Bool{}

dynamicValues := make(map[string]interface{})
if input.HasArgs() {
Expand Down Expand Up @@ -98,7 +99,7 @@ func (e *Executer) Execute(input *contextargs.Context) (bool, error) {
}
} else {
if writer.WriteResult(event, e.options.Output, e.options.Progress, e.options.IssuesClient) {
results = true
results.CompareAndSwap(false, true)
} else {
if err := e.options.Output.WriteFailure(event.InternalEvent); err != nil {
gologger.Warning().Msgf("Could not write failure event to output: %s\n", err)
Expand All @@ -113,11 +114,11 @@ func (e *Executer) Execute(input *contextargs.Context) (bool, error) {
gologger.Warning().Msgf("[%s] Could not execute request for %s: %s\n", e.options.TemplateID, input.MetaInput.PrettyPrint(), err)
}
// If a match was found and stop at first match is set, break out of the loop and return
if results && (e.options.StopAtFirstMatch || e.options.Options.StopAtFirstMatch) {
if results.Load() && (e.options.StopAtFirstMatch || e.options.Options.StopAtFirstMatch) {
break
}
}
return results, nil
return results.Load(), nil
}

// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
Expand All @@ -129,7 +130,7 @@ func (e *Executer) ExecuteWithResults(input *contextargs.Context, callback proto
})
}
previous := make(map[string]interface{})
var results bool
results := &atomic.Bool{}

for _, req := range e.requests {
req := req
Expand All @@ -156,7 +157,7 @@ func (e *Executer) ExecuteWithResults(input *contextargs.Context, callback proto
if event.OperatorsResult == nil {
return
}
results = true
results.CompareAndSwap(false, true)
callback(event)
})
if err != nil {
Expand All @@ -166,7 +167,7 @@ func (e *Executer) ExecuteWithResults(input *contextargs.Context, callback proto
gologger.Warning().Msgf("[%s] Could not execute request for %s: %s\n", e.options.TemplateID, input.MetaInput.PrettyPrint(), err)
}
// If a match was found and stop at first match is set, break out of the loop and return
if results && (e.options.StopAtFirstMatch || e.options.Options.StopAtFirstMatch) {
if results.Load() && (e.options.StopAtFirstMatch || e.options.Options.StopAtFirstMatch) {
break
}
}
Expand Down