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

Prevent pprof from causing BPF verifier livelocks #805

Merged
merged 5 commits into from Sep 28, 2022

Conversation

ti-mo
Copy link
Collaborator

@ti-mo ti-mo commented Sep 27, 2022

Read https://dxuuu.xyz/bpf-go-pprof.html for more context.

If a thread receives a signal while blocked in BPF_PROG_LOAD, the verifier can cooperatively interrupt itself by checking pending signals for the thread and return -EAGAIN from the syscall to request userspace to retry.

When a Go program is built and run with pprof enabled, threads are routinely sent a SIGPROF to make them dump profiling information, which can lead to a runaway reaction if the program takes longer to verify than the interrupt frequency. To prevent this, mask SIGPROF before calling BPF_PROG_LOAD and remove it when done.

@danobi

Signed-off-by: Timo Beckers <timo@isovalent.com>
@ti-mo ti-mo requested a review from lmb September 27, 2022 13:28
Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

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

Very nice, one of those PRs where the change is tiny but the impact is big!

internal/sys/types.go Outdated Show resolved Hide resolved
@@ -0,0 +1,41 @@
package unix
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you move our own new types from internal/unix to internal/sys? The former should only ever contain wrappers to make macOS users happy.

  • Makes it easier to get rid of internal/unix once I figure out how to get gopls to pass linux build tags when editing.
  • Avoids signals_other.go
  • Allows unexporting API

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

once I figure out how to get gopls to pass linux build tags when editing.

In VSCode, I use:

    "gopls": {
        "build.buildFlags": [
            "-tags=linux,amd64,integration"
        ],
    },

Copy link
Collaborator Author

@ti-mo ti-mo Sep 28, 2022

Choose a reason for hiding this comment

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

Avoids signals_other.go

How, though? Package sys also needs to build on macOS, so we'd still need a signals_other.go there.

Edit: you mean to fold signals_other.go into types_other.go instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I had forgotten about SIG_BLOCK and friends, sorry. I'll try to submit another CL that add them to x/sys, but let's keep SIG_BLOCK in internal/unix for now.

internal/sys/signals.go Outdated Show resolved Hide resolved
internal/sys/signals.go Outdated Show resolved Hide resolved
internal/sys/signals.go Show resolved Hide resolved
internal/unix/signals_mips.go Show resolved Hide resolved
internal/cmd/gentypes/main.go Outdated Show resolved Hide resolved
@ti-mo ti-mo force-pushed the tb/block-sigprof branch 2 times, most recently from 0bd797e to 1caaad1 Compare September 28, 2022 07:41
@ti-mo ti-mo marked this pull request as ready for review September 28, 2022 07:41
@ti-mo ti-mo requested a review from lmb September 28, 2022 07:42
Signed-off-by: Timo Beckers <timo@isovalent.com>
This patch implements the sigsetAdd function to add a signal to a signal set.
Care was taken to make this build and run correctly on different GOOS and
GOARCH combinations.

Signed-off-by: Timo Beckers <timo@isovalent.com>
sys.maskProfilerSignal() locks the calling goroutine to its underlying OS
thread and blocks the SIGPROF signal from interrupting the thread.

sys.unmaskProfilerSignal() reverses the operation and should be used in
tandem to properly restore OS thread state afterwards.

Add a wrapper around unix.PthreadSigmask added in a recent version of x/sys.

Signed-off-by: Timo Beckers <timo@isovalent.com>
If a thread receives a signal while blocked in BPF_PROG_LOAD, the verifier
can cooperatively interrupt itself by checking pending signals for the thread
and return -EAGAIN from the syscall to request userspace to retry.

When a Go program is built and run with pprof enabled, threads are routinely
sent a SIGPROF to make them dump profiling information, which can lead to a
runaway reaction if the program takes longer to verify than the interrupt
frequency. To prevent this, mask SIGPROF before calling BPF_PROG_LOAD and
remove it when done.

Signed-off-by: Timo Beckers <timo@isovalent.com>
@ti-mo ti-mo changed the title Prevent pprof from causing BPF verifier deadlocks Prevent pprof from causing BPF verifier livelocks Sep 28, 2022
unmaskProfilerSignal()

var old unix.Sigset_t
if err := unix.PthreadSigmask(0, nil, &old); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Neat!

Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

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

🚢 it!

@lmb lmb merged commit e78a613 into cilium:master Sep 28, 2022
@ti-mo ti-mo deleted the tb/block-sigprof branch September 28, 2022 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants