8000 derive: fix cgroupv1 hid false derives by NDStrahilevitz · Pull Request #2453 · aquasecurity/tracee · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

derive: fix cgroupv1 hid false derives #2453

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

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
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/cgroup/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type Cgroup interface {
destroy() error
GetMountPoint() string
getDefaultHierarchyID() int
GetVersion() CgroupVersion
}

func NewCgroup(ver CgroupVersion) (Cgroup, error) {
Expand Down Expand Up @@ -227,6 +228,10 @@ func (c *CgroupV1) getDefaultHierarchyID() int {
return c.hid
}

func (c *CgroupV1) GetVersion() CgroupVersion {
return CgroupVersion1
}

// cgroupv2

type CgroupV2 struct {
Expand Down Expand Up @@ -274,6 +279,10 @@ func (c *CgroupV2) getDefaultHierarchyID() int {
return c.hid
}

func (c *CgroupV2) GetVersion() CgroupVersion {
return CgroupVersion2
}

//
// General
//
Expand Down
4 changes: 4 additions & 0 deletions pkg/containers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func (c *Containers) GetDefaultCgroupHierarchyID() int {
return c.cgroups.GetDefaultCgroupHierarchyID()
}

func (c *Containers) GetCgroupVersion() cgroup.CgroupVersion {
return c.cgroups.GetDefaultCgroup().GetVersion()
}

// Populate populates Containers struct by reading mounted proc and cgroups fs.
func (c *Containers) Populate() error {
return c.populate()
Expand Down
19 changes: 19 additions & 0 deletions pkg/events/derive/container_create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package derive

import (
"github.com/aquasecurity/tracee/pkg/cgroup"
"github.com/aquasecurity/tracee/pkg/containers"
"github.com/aquasecurity/tracee/pkg/events"
"github.com/aquasecurity/tracee/pkg/events/parse"
Expand All @@ -15,6 +16,10 @@ func ContainerCreate(containers *containers.Containers) deriveFunction {

func deriveContainerCreateArgs(containers *containers.Containers) func(event trace.Event) ([]interface{}, error) {
return func(event trace.Event) ([]interface{}, error) {
// if cgroup_id is from non default hid (v1 case), the cgroup info query will fail, so we skip
if check, err := isCgroupEventInHid(&event, containers); !check {
return nil, err
}
cgroupId, err := parse.ArgUint64Val(&event, "cgroup_id")
if err != nil {
return nil, err
Expand All @@ -35,3 +40,17 @@ func deriveContainerCreateArgs(containers *containers.Containers) func(event tra
return nil, nil
}
}

// isCgroupEventInHid checks if cgroup event is relevant for deriving container event in it's hierarchy id.
// in tracee we only care about containers inside the cpuset controller, as such other hierarchy ids will lead
// to a failed query.
func isCgroupEventInHid(event *trace.Event, containers *containers.Containers) (bool, error) {
if containers.GetCgroupVersion() == cgroup.CgroupVersion2 {
return true, nil
}
hierarchyID, err := parse.ArgUint32Val(event, "hierarchy_id")
if err != nil {
return false, err
}
return containers.GetDefaultCgroupHierarchyID() == int(hierarchyID), nil
}
4 changes: 4 additions & 0 deletions pkg/events/derive/container_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func ContainerRemove(containers *containers.Containers) deriveFunction {

func deriveContainerRemoveArgs(containers *containers.Containers) deriveArgsFunction {
return func(event trace.Event) ([]interface{}, error) {
// if cgroup_id is from non default hid (v1 case), the cgroup info query will fail, so we skip
if check, err := isCgroupEventInHid(&event, containers); !check {
return nil, err
}
cgroupId, err := parse.ArgUint64Val(&event, "cgroup_id")
if err != nil {
return nil, err
Expand Down
0