Skip to content

Commit 73fb7eb

Browse files
committed
capabilities: make new capabilities a singleton
This allows any package to raise their capabilities.
1 parent 02804d8 commit 73fb7eb

File tree

4 files changed

+29
-30
lines changed

4 files changed

+29
-30
lines changed

cmd/tracee-rules/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func main() {
5151

5252
// Capabilities command line flags
5353

54-
_, err := capabilities.NewCapabilities(c.Bool("allcaps"))
54+
err := capabilities.NewCapabilities(c.Bool("allcaps"))
5555
if err != nil {
5656
return err
5757
}

pkg/capabilities/capabilities.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"kernel.org/pub/linux/libs/security/libcap/cap"
1212
)
1313

14-
var initialized bool
14+
var Caps Capabilities // singleton for all packages
1515

1616
const pkgName = "capabilities"
1717

@@ -31,28 +31,27 @@ const (
3131
)
3232

3333
type Capabilities struct {
34-
have *cap.Set
35-
all map[cap.Value]map[ringType]bool
36-
bypass bool
37-
lock *sync.Mutex // big lock to guarantee all threads are on the same ring
34+
have *cap.Set
35+
all map[cap.Value]map[ringType]bool
36+
bypass bool
37+
initialized bool
38+
lock *sync.Mutex // big lock to guarantee all threads are on the same ring
3839
}
3940

40-
func NewCapabilities(bypass bool) (*Capabilities, error) {
41-
c := &Capabilities{}
42-
err := c.initialize(bypass)
43-
return c, err
41+
func NewCapabilities(bypass bool) error {
42+
Caps = Capabilities{}
43+
return Caps.initialize(bypass)
4444
}
4545

4646
func (c *Capabilities) initialize(bypass bool) error {
4747
if bypass {
4848
c.bypass = true
4949
return nil
5050
}
51-
if initialized {
52-
return noConcurrentCapablities()
51+
if c.initialized {
52+
return alreadyInitialized()
5353
}
5454

55-
initialized = true
5655
c.lock = new(sync.Mutex)
5756
c.all = make(map[cap.Value]map[ringType]bool)
5857

@@ -316,10 +315,6 @@ func couldNotReadPerfEventParanoid() error {
316315
return fmt.Errorf("could not read procfs perf_event_paranoid")
317316
}
318317

319-
func noConcurrentCapablities() error {
320-
return fmt.Errorf("can't have concurrent capabilities")
321-
}
322-
323318
func couldNotSetProc(e error) error {
324319
return fmt.Errorf("could not set capabilities: %v", e)
325320
}
@@ -328,6 +323,10 @@ func couldNotGetProc(e error) error {
328323
return fmt.Errorf("could not get capabilities: %v", e)
329324
}
330325

326+
func alreadyInitialized() error {
327+
return fmt.Errorf("capabilities were already initialized")
328+
}
329+
331330
//
332331
// Standalone Functions
333332
//

pkg/ebpf/events_processor.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77
"time"
88

9+
"github.com/aquasecurity/tracee/pkg/capabilities"
910
"github.com/aquasecurity/tracee/pkg/utils"
1011

1112
"github.com/aquasecurity/tracee/pkg/bufferdecoder"
@@ -148,7 +149,7 @@ func (t *Tracee) processEvent(event *trace.Event) error {
148149
if !ok || lastCtime != castedSourceFileCtime {
149150

150151
// capture (ring1)
151-
err = t.capabilities.Required(func() error {
152+
err = capabilities.Caps.Required(func() error {
152153
return utils.CopyRegularFileByRelativePath(
153154
sourceFilePath,
154155
t.outDir,
@@ -177,7 +178,7 @@ func (t *Tracee) processEvent(event *trace.Event) error {
177178
} else {
178179

179180
// ring1
180-
t.capabilities.Required(func() error {
181+
capabilities.Caps.Required(func() error {
181182
currentHash, err = computeFileHashAtPath(sourceFilePath)
182183
if err == nil {
183184
hashInfoObj = fileExecInfo{castedSourceFileCtime, currentHash}

pkg/ebpf/tracee.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ type Tracee struct {
197197
triggerContexts trigger.Context
198198
running bool
199199
outDir *os.File // All file operations to output dir should be through the utils package file operations (like utils.OpenAt) using this directory file.
200-
capabilities *capabilities.Capabilities
201200
}
202201

203202
func (t *Tracee) Stats() *metrics.Stats {
@@ -276,7 +275,7 @@ func New(cfg Config) (*Tracee, error) {
276275
}
277276

278277
// Start capabilities rings
279-
t.capabilities, err = capabilities.NewCapabilities(t.config.Capabilities.BypassCaps)
278+
err = capabilities.NewCapabilities(t.config.Capabilities.BypassCaps)
280279
if err != nil {
281280
return t, err
282281
}
@@ -304,7 +303,7 @@ func New(cfg Config) (*Tracee, error) {
304303
return t, fmt.Errorf("could not get event")
305304
}
306305
for _, capArray := range evt.Dependencies.Capabilities {
307-
t.capabilities.Require(capArray)
306+
capabilities.Caps.Require(capArray)
308307
}
309308
}
310309

@@ -314,7 +313,7 @@ func New(cfg Config) (*Tracee, error) {
314313
if err != nil {
315314
return t, err
316315
}
317-
err = t.capabilities.Require(capsToAdd...)
316+
err = capabilities.Caps.Require(capsToAdd...)
318317
if err != nil {
319318
return t, err
320319
}
@@ -323,7 +322,7 @@ func New(cfg Config) (*Tracee, error) {
323322
if err != nil {
324323
return t, err
325324
}
326-
err = t.capabilities.Unrequire(capsToDrop...)
325+
err = capabilities.Caps.Unrequire(capsToDrop...)
327326
if err != nil {
328327
return t, err
329328
}
@@ -377,7 +376,7 @@ func (t *Tracee) Init() error {
377376
// Init kernel symbols map
378377

379378
if initReq.kallsyms {
380-
err = t.capabilities.Requested(func() error { // ring2
379+
err = capabilities.Caps.Requested(func() error { // ring2
381380

382381
t.kernelSymbols, err = helpers.NewKernelSymbolsMap()
383382
if err != nil {
@@ -405,7 +404,7 @@ func (t *Tracee) Init() error {
405404

406405
// Initialize containers enrichment logic
407406

408-
t.capabilities.Requested(func() error { // TODO: workaround until PR: #2233 is in place
407+
capabilities.Caps.Requested(func() error { // TODO: workaround until PR: #2233 is in place
409408

410409
t.containers, err = containers.New(t.config.Sockets, "containers_map", t.config.Debug)
411410
if err != nil {
@@ -1159,7 +1158,7 @@ func (t *Tracee) initBPF() error {
11591158

11601159
// Execute code with higher privileges: ring1 (required)
11611160

1162-
err = t.capabilities.Required(func() error {
1161+
err = capabilities.Caps.Required(func() error {
11631162

11641163
// Load the eBPF object into kernel
11651164

@@ -1326,7 +1325,7 @@ func (t *Tracee) Run(ctx gocontext.Context) error {
13261325

13271326
// Close cleans up created resources
13281327
func (t *Tracee) Close() {
1329-
err := t.capabilities.Required(func() error { // ring1
1328+
err := capabilities.Caps.Required(func() error { // ring1
13301329

13311330
if t.probes != nil {
13321331
err := t.probes.DetachAll()
@@ -1399,7 +1398,7 @@ func (t *Tracee) updateFileSHA() {
13991398

14001399
func (t *Tracee) invokeInitEvents() {
14011400
if t.events[events.InitNamespaces].emit {
1402-
t.capabilities.Requested(func() error { // ring2
1401+
capabilities.Caps.Requested(func() error { // ring2
14031402
systemInfoEvent := events.InitNamespacesEvent()
14041403
t.config.ChanEvents <- systemInfoEvent
14051404
return nil
@@ -1483,7 +1482,7 @@ func (t *Tracee) triggerSeqOpsIntegrityCheckCall(
14831482
}
14841483

14851484
func (t *Tracee) updateKallsyms() error {
1486-
return t.capabilities.Requested(func() error { // ring2
1485+
return capabilities.Caps.Requested(func() error { // ring2
14871486

14881487
kernelSymbols, err := helpers.NewKernelSymbolsMap()
14891488
if err != nil {

0 commit comments

Comments
 (0)