Skip to content

Commit

Permalink
bpf: use exe to read the binary path in sys_execve
Browse files Browse the repository at this point in the history
Previously, we used the filename argument from the sys_execve tracepoint
to read the binary path, later potentially (if it was a relative path)
combined with cwd in userspace. With this change we replace that with
exe, already used and needed by matchBinaries, and remove the userspace
work (since exe is already absolute).

It has a user facing change: tetragon events will now contain the
canonical binary path while previously it could have been a symlink.

Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
  • Loading branch information
mtardy committed Jan 4, 2024
1 parent eeed60e commit 0bfffab
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 33 deletions.
6 changes: 4 additions & 2 deletions bpf/process/bpf_execve_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ __attribute__((section("tracepoint/sys_execve"), used)) int
event_execve(struct sched_execve_args *ctx)
{
struct task_struct *task = (struct task_struct *)get_current_task();
char *filename = (char *)ctx + (ctx->filename & 0xFFFF);
struct msg_execve_event *event;
struct execve_map_value *parent;
struct msg_process *p;
Expand Down Expand Up @@ -204,7 +203,10 @@ event_execve(struct sched_execve_args *ctx)
// a symlink) coming from the execve tracepoint.
read_exe(task, &event->exe);

p->size += read_path(ctx, event, filename);
// Previously, we used the filename for read_path, potentially later
// combined with cwd in user space. Now we reuse exe, that is absolute and
// not a potential symlink.
p->size += read_path(ctx, event, event->exe.off);
p->size += read_args(ctx, event);
p->size += read_cwd(ctx, p);

Expand Down
8 changes: 3 additions & 5 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cilium/tetragon/pkg/reader/exec"
"github.com/cilium/tetragon/pkg/reader/namespace"
"github.com/cilium/tetragon/pkg/reader/node"
"github.com/cilium/tetragon/pkg/reader/path"
"github.com/cilium/tetragon/pkg/reader/proc"
"github.com/cilium/tetragon/pkg/watcher"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -270,14 +269,13 @@ func initProcessInternalExec(
execID := GetExecID(&process)
protoPod := GetPodInfo(containerID, process.Filename, args, process.NSPID)
apiCaps := caps.GetMsgCapabilities(event.Capabilities)
binary := path.GetBinaryAbsolutePath(process.Filename, cwd)
apiNs, err := namespace.GetMsgNamespaces(event.Namespaces)
if err != nil {
logger.GetLogger().WithFields(logrus.Fields{
"event.name": "Execve",
"event.process.pid": process.PID,
"event.process.tid": process.TID,
"event.process.binary": binary,
"event.process.binary": process.Filename,
"event.process.exec_id": execID,
"event.parent.exec_id": parentExecID,
}).Warn("ExecveEvent: parsing namespaces failed")
Expand Down Expand Up @@ -319,7 +317,7 @@ func initProcessInternalExec(
"event.name": "Execve",
"event.process.pid": process.PID,
"event.process.tid": process.TID,
"event.process.binary": binary,
"event.process.binary": process.Filename,
"event.process.exec_id": execID,
"event.parent.exec_id": parentExecID,
}).Warn("ExecveEvent: process PID and TID mismatch")
Expand All @@ -333,7 +331,7 @@ func initProcessInternalExec(
Tid: &wrapperspb.UInt32Value{Value: process.TID},
Uid: &wrapperspb.UInt32Value{Value: process.UID},
Cwd: cwd,
Binary: binary,
Binary: process.Filename,
Arguments: args,
Flags: strings.Join(exec.DecodeCommonFlags(process.Flags), " "),
StartTime: ktime.ToProtoOpt(process.Ktime, (process.Flags&api.EventProcFS) == 0),
Expand Down
9 changes: 0 additions & 9 deletions pkg/reader/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@
package path

import (
"path/filepath"

"github.com/cilium/tetragon/pkg/api/processapi"
)

func GetBinaryAbsolutePath(binary string, cwd string) string {
if filepath.IsAbs(binary) {
return binary
}
return filepath.Join(cwd, binary)
}

func FilePathFlagsToStr(flags uint32) string {
if (flags & processapi.UnresolvedPathComponents) != 0 {
return "unresolvedPathComponents"
Expand Down
17 changes: 0 additions & 17 deletions pkg/reader/path/path_test.go

This file was deleted.

0 comments on commit 0bfffab

Please sign in to comment.