Skip to content

Commit

Permalink
fix(filters): int conversion without check
Browse files Browse the repository at this point in the history
This silences some CodeQL "Incorrect conversion between integer types"
warnings.
  • Loading branch information
geyslan committed Jan 17, 2025
1 parent c98f801 commit ca5ebd4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
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 @@ -177,22 +178,31 @@ func (f *DataFilter) Parse(id events.ID, fieldName string, operatorAndValues str
events.StackPivot:
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

0 comments on commit ca5ebd4

Please sign in to comment.