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

fix(filters): int conversion without check #4482

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions pkg/ebpf/event_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ebpf

import (
"fmt"
"math"
"strconv"

"github.com/aquasecurity/tracee/pkg/ebpf/probes"
Expand Down Expand Up @@ -70,11 +71,16 @@ func attachSuspiciousSyscallSourceProbes(t *Tracee, eventParams []map[string]fil
if err != nil {
return err
}
if !events.Core.IsDefined(events.ID(syscallID)) {
if syscallID < 0 || syscallID > math.MaxInt32 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this kind of check be resolved inside the IsDefined method? Is this the particular code which fixes the CodeQL issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Shouldn't this kind of check be resolved inside the IsDefined method?

IsDefined only checks for the existence of a events.ID value, it's agnostic to the origin of that value, so I believe it's sane to check it right after the Atoi conversion, since we're truncating the output (int - 64) to events.ID (int32).

Is this the particular code which fixes the CodeQL issue?

Yep. It's an attempt. Let's see if the issue is closed automagically after this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

BTW, I would like to ask you if these conversions https://github.com/aquasecurity/tracee/security/code-scanning/2 are ok. I mean, if they are the full size of those values, why the API providing them uses int (or other larger types) instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Related:

https://github.com/aquasecurity/tracee/pull/4484/files/21ff8fd5251120a52a961d6d9eb466f0f22135e9..ce22b0f64fe6a85a09760becd432a90dbced4709

image

If there's difference of size between cgroup HID versions, the API should be explicit and provide appropriate getters.

return fmt.Errorf("invalid syscall id: %s", entry)
}

id := events.ID(syscallID)
if !events.Core.IsDefined(id) {
return fmt.Errorf("syscall id %d is not defined", syscallID)
}

syscallName := events.Core.GetDefinitionByID(events.ID(syscallID)).GetName()
syscallName := events.Core.GetDefinitionByID(id).GetName()
syscalls[syscallName] = struct{}{}
}
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/filters/binary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filters

import (
"math"
"strconv"
"strings"

Expand Down Expand Up @@ -33,6 +34,9 @@ func getHostMntNS() (uint32, error) {
if err != nil {
return 0, errfmt.WrapError(err)
}
if ns < 0 || ns > math.MaxUint32 {
return 0, errfmt.Errorf("invalid mnt namespace %d", ns)
}

return uint32(ns), nil
}
Expand Down Expand Up @@ -84,6 +88,9 @@ func (f *BinaryFilter) Parse(operatorAndValues string) error {
if err != nil {
return InvalidValue(val)
}
if mntNS < 0 || mntNS > math.MaxUint32 {
return InvalidValue(val)
}
bin.MntNS = uint32(mntNS)
}
} else {
Expand Down
30 changes: 20 additions & 10 deletions pkg/filters/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filters

import (
"fmt"
"math"
"strconv"
"strings"

Expand Down Expand Up @@ -176,22 +177,31 @@ func (f *DataFilter) Parse(id events.ID, fieldName string, operatorAndValues str
events.SuspiciousSyscallSource:
if fieldName == "syscall" { // handle either syscall name or syscall id
_, err := strconv.Atoi(val)
if err != nil {
// if val is a syscall name, then we need to convert it to a syscall id
syscallID, ok := events.Core.GetDefinitionIDByName(val)
if !ok {
return val, errfmt.Errorf("invalid syscall name: %s", val)
}
val = strconv.Itoa(int(syscallID))
if err == nil {
return val, nil // val might already be a syscall id
}

// val might be a syscall name, then we need to convert it to a syscall id
syscallID, ok := events.Core.GetDefinitionIDByName(val)
if !ok {
return val, errfmt.Errorf("invalid syscall name: %s", val)
}
val = strconv.Itoa(int(syscallID))
}

case events.HookedSyscall:
if fieldName == "syscall" { // handle either syscall name or syscall id
dataEventID, err := strconv.Atoi(val)
if err == nil {
// if val is a syscall id, then we need to convert it to a syscall name
val = events.Core.GetDefinitionByID(events.ID(dataEventID)).GetName()
// check if dataEventID is a syscall id
if err != nil {
return val, nil // val might already be a syscall name
}
if dataEventID < 0 || dataEventID > math.MaxInt32 {
return val, errfmt.Errorf("invalid syscall id: %s", val)
}

// val might be a syscall id, then we need to convert it to a syscall name
val = events.Core.GetDefinitionByID(events.ID(dataEventID)).GetName()
}
}

Expand Down
Loading