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(decl): potential nil ptr deref and shell script SIGUSR handling #243

Merged
merged 3 commits into from
Nov 26, 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
4 changes: 4 additions & 0 deletions pkg/test/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func (t *Test) validateNameUniqueness() error {

// validateContext validates that names used for test resources and steps are unique.
func (t *Test) validateContext() error {
if t.Context == nil {
return nil
}

processes := t.Context.Processes
processesLen := len(processes)
if processesLen <= 1 {
Expand Down
54 changes: 44 additions & 10 deletions pkg/test/script/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,81 @@ const (
// defines three placeholders: a placeholder for the "before" script, one for the signalingToken, and one for the
// "after" script.
scriptTemplate = `
# __SLEEP_PID is the PID of the sleep process used to block the current script execution.
__SLEEP_PID=

# __cleanup is used as handler for SIGTERM and SIGINT signals, as well as handler for script EXIT pseudo-signal.
__cleanup() {
[ -n "$__SLEEP_PID" ] && kill -9 $__SLEEP_PID
exit
}

# __usr1 is used as handler for SIGUSR1.
__usr1() {
kill -9 $__SLEEP_PID && __SLEEP_PID=
}

# Register signal handler to delete resources and exit upon SIGTERM and SIGINT or upon script EXIT.
trap __cleanup TERM INT EXIT;
%s;

# Launch "before" script.
%s

# Launch sleep process and register SIGUSR1 signal handler. The signal handler is responsible to kill the sleep process
# to unblock the current process waiting for it to exit (see "wait $__SLEEP_PID"" below).
sleep infinity & __SLEEP_PID=$!
trap __usr1 USR1

# Send token to parent process to signal "before" script execution completion.
echo %s
wait $PID;
%s;
`

# Wait for sleep process to exit.
wait $__SLEEP_PID

# The SIGUSR1 signal triggers __usr1 handler invocation and sets the exit code to 138. Existing from wait, we check if
# the exit code value is as expected and reset it to 0; otherwise, we return the obtained exit code.
if [ $? != 138 ]; then
echo "Wait on sleep unblocked due to a reason different from USR1 signal" >&2
exit $?
else
true # Reset exit code
fi

# Launch "after" script.
%s`
)

// New creates a new shell script by merging beforeScript and afterScript. Since the "before" and "after" are part of
// the same shell script, it is possible to reuse the declarations/definitions of the "before" part in the "after" part.
func New(logger logr.Logger, beforeScript, afterScript *string) test.Script {
var before, after string
if beforeScript != nil {
before = strings.TrimSpace(*beforeScript)
if beforeScript != nil && *beforeScript != "" {
before = strings.TrimSpace(*beforeScript) + ";"
}
if afterScript != nil && *afterScript != "" {
after = strings.TrimSpace(*afterScript) + ";"
}
if afterScript != nil {
after = strings.TrimSpace(*afterScript)
var script string
if before != "" || after != "" {
script = fmt.Sprintf(scriptTemplate, before, signalingToken, after)
}
script := fmt.Sprintf(scriptTemplate, before, signalingToken, after)
s := &shellScript{
logger: logger,
script: script,
}
return s
}

var noopAfterFunc = func(context.Context) error { return nil }

func (s *shellScript) RunBefore(ctx context.Context) (func(context.Context) error, error) {
script := s.script
if s.script == "" {
return noopAfterFunc, nil
}

processCtx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(processCtx, "sh", "-c", s.script) //nolint:gosec // Disable G204
cmd := exec.CommandContext(processCtx, "sh", "-c", script) //nolint:gosec // Disable G204
cmd.Cancel = func() error {
return cmd.Process.Signal(unix.SIGTERM)
}
Expand Down
Loading