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

Tweak capabilities detection #1588

Merged
merged 2 commits into from
Jan 30, 2025
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
11 changes: 7 additions & 4 deletions pkg/beyla/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,22 @@ func testAndSet(caps *helpers.OSCapabilities, capError *osCapabilitiesError, c h
func checkCapabilitiesForSetOptions(config *Config, caps *helpers.OSCapabilities, capError *osCapabilitiesError) {
if config.Enabled(FeatureAppO11y) {
testAndSet(caps, capError, unix.CAP_CHECKPOINT_RESTORE)
testAndSet(caps, capError, unix.CAP_DAC_READ_SEARCH)
testAndSet(caps, capError, unix.CAP_SYS_PTRACE)
testAndSet(caps, capError, unix.CAP_PERFMON)
testAndSet(caps, capError, unix.CAP_NET_RAW)

if config.EBPF.ContextPropagationEnabled || config.EBPF.UseTCForL7CP {
testAndSet(caps, capError, unix.CAP_NET_ADMIN)
}
}

if config.Enabled(FeatureNetO11y) {
// test for net raw only if we don't have net admin
if !caps.Has(unix.CAP_NET_ADMIN) {
if config.NetworkFlows.Source == EbpfSourceSock {
testAndSet(caps, capError, unix.CAP_NET_RAW)
} else if config.NetworkFlows.Source == EbpfSourceTC {
testAndSet(caps, capError, unix.CAP_PERFMON)
testAndSet(caps, capError, unix.CAP_NET_ADMIN)
}
}
}
Expand Down Expand Up @@ -126,8 +131,6 @@ func CheckOSCapabilities(config *Config) error {

// core capabilities
testAndSet(caps, &capError, unix.CAP_BPF)
testAndSet(caps, &capError, unix.CAP_PERFMON)
testAndSet(caps, &capError, unix.CAP_DAC_READ_SEARCH)

// CAP_SYS_RESOURCE is only required on kernels < 5.11
if (major == 5 && minor < 11) || (major < 5) {
Expand Down
36 changes: 27 additions & 9 deletions pkg/beyla/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"

"github.com/grafana/beyla/pkg/config"
"github.com/grafana/beyla/pkg/internal/helpers"
"github.com/grafana/beyla/pkg/services"
)
Expand Down Expand Up @@ -102,17 +103,25 @@ type capTestData struct {
class capClass
kernMaj int
kernMin int
useTC bool
}

var capTests = []capTestData{
{osCap: unix.CAP_BPF, class: capCore, kernMaj: 6, kernMin: 10},
{osCap: unix.CAP_PERFMON, class: capCore, kernMaj: 6, kernMin: 10},
{osCap: unix.CAP_DAC_READ_SEARCH, class: capCore, kernMaj: 6, kernMin: 10},
{osCap: unix.CAP_SYS_RESOURCE, class: capCore, kernMaj: 5, kernMin: 10},
{osCap: unix.CAP_SYS_ADMIN, class: capCore, kernMaj: 4, kernMin: 11},
{osCap: unix.CAP_CHECKPOINT_RESTORE, class: capApp, kernMaj: 6, kernMin: 10},
{osCap: unix.CAP_SYS_PTRACE, class: capApp, kernMaj: 6, kernMin: 10},
{osCap: unix.CAP_NET_RAW, class: capNet, kernMaj: 6, kernMin: 10},
// core
{osCap: unix.CAP_BPF, class: capCore, kernMaj: 6, kernMin: 10, useTC: false},

// app o11y
{osCap: unix.CAP_CHECKPOINT_RESTORE, class: capApp, kernMaj: 6, kernMin: 10, useTC: false},
{osCap: unix.CAP_DAC_READ_SEARCH, class: capApp, kernMaj: 6, kernMin: 10, useTC: false},
{osCap: unix.CAP_SYS_PTRACE, class: capApp, kernMaj: 6, kernMin: 10, useTC: false},
{osCap: unix.CAP_PERFMON, class: capApp, kernMaj: 6, kernMin: 10, useTC: false},
{osCap: unix.CAP_NET_RAW, class: capApp, kernMaj: 6, kernMin: 10, useTC: false},
{osCap: unix.CAP_NET_ADMIN, class: capApp, kernMaj: 6, kernMin: 10, useTC: true},

// net o11y
{osCap: unix.CAP_NET_RAW, class: capNet, kernMaj: 6, kernMin: 10, useTC: false},
{osCap: unix.CAP_PERFMON, class: capNet, kernMaj: 6, kernMin: 10, useTC: true},
{osCap: unix.CAP_NET_ADMIN, class: capNet, kernMaj: 6, kernMin: 10, useTC: true},
}

func TestCheckOSCapabilities(t *testing.T) {
Expand All @@ -129,9 +138,18 @@ func TestCheckOSCapabilities(t *testing.T) {
test := func(data *capTestData) {
overrideKernelVersion(testCase{data.kernMaj, data.kernMin})

netSource := func(useTC bool) string {
if useTC {
return EbpfSourceTC
}

return EbpfSourceSock
}

cfg := Config{
NetworkFlows: NetworkConfig{Enable: data.class == capNet},
NetworkFlows: NetworkConfig{Enable: data.class == capNet, Source: netSource(data.useTC)},
Discovery: services.DiscoveryConfig{SystemWide: data.class == capApp},
EBPF: config.EBPFTracer{ContextPropagationEnabled: data.useTC},
}

err := CheckOSCapabilities(&cfg)
Expand Down
Loading