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

Unsafe int cast in kill command #783

Merged
merged 4 commits into from
May 4, 2023
Merged
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions cmd/cometbft/commands/debug/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package debug
import (
"errors"
"fmt"
"math"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -123,11 +124,15 @@ func killProc(pid uint64, dir string) error {
go func() {
// Killing the CometBFT process with the '-ABRT|-6' signal will result in
// a goroutine stacktrace.
p, err := os.FindProcess(int(pid))
if err != nil {
fmt.Fprintf(os.Stderr, "failed to find PID to kill CometBFT process: %s", err)
} else if err = p.Signal(syscall.SIGABRT); err != nil {
fmt.Fprintf(os.Stderr, "failed to kill CometBFT process: %s", err)
if pid <= math.MaxInt {
Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly, I'm scratching my head at this. I think the killProc function should already only accept int, if processes are stored as int. Accepting uint64 because that's a better validation seems counterintuitive, if it is then converted again.

OS processes should always be bigger than 0 for sure, but the high limit is dependent on the architecture. (32-bit vs 64-bit)

If FindProcess requires an int, I would make killProc require an int too. No need to make it more complex.

Copy link
Contributor Author

@sergio-mena sergio-mena May 3, 2023

Choose a reason for hiding this comment

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

I think the killProc function should already only accept int

TBH, I was hesitating whether to add the limit check, or go all the way up to the CLI param... and went for the former. Main reason: limiting code changes on what will likely be backported to all branches.

Now that I see you're leaning towards the other solution, let's go for it. Bear with me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Please take a look

p, err := os.FindProcess(int(pid))
if err != nil {
fmt.Fprintf(os.Stderr, "failed to find PID to kill CometBFT process: %s", err)
} else if err = p.Signal(syscall.SIGABRT); err != nil {
fmt.Fprintf(os.Stderr, "failed to kill CometBFT process: %s", err)
}
} else {
fmt.Fprintf(os.Stderr, "failed to convert PID to int (%v > %v)", pid, math.MaxInt)
}

// allow some time to allow the CometBFT process to be killed
Expand Down