Skip to content

Commit

Permalink
[WIP]: Username for process_exec events
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Fedotov <[email protected]>
  • Loading branch information
anfedotoff committed Apr 24, 2024
1 parent 2101ea1 commit 5d48ea0
Show file tree
Hide file tree
Showing 13 changed files with 1,363 additions and 1,286 deletions.
1 change: 1 addition & 0 deletions api/v1/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions api/v1/tetragon/codegen/eventchecker/eventchecker.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

866 changes: 438 additions & 428 deletions api/v1/tetragon/tetragon.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/v1/tetragon/tetragon.proto
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ message Process {
ProcessCredentials process_credentials = 17;
// Executed binary properties. This field is only available on ProcessExec events.
BinaryProperties binary_properties = 18;
// User name associated with process.
string username = 19;
}

message ProcessExec {
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/content/en/docs/reference/grpc-api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/api/processapi/processapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type MsgProcess struct {
Ktime uint64
Filename string
Args string
Username string
}

type MsgExitInfo struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ func initProcessInternalExec(
Docker: containerID,
ParentExecId: parentExecID,
Refcnt: 0,
Username: process.Username,
},
capabilities: apiCaps,
apiCreds: apiCreds,
Expand Down
15 changes: 13 additions & 2 deletions pkg/sensors/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"bytes"
"encoding/binary"
"fmt"
"os/user"
"strconv"
"unsafe"

"github.com/cilium/tetragon/pkg/api"
Expand All @@ -18,6 +20,7 @@ import (
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/observer"
"github.com/cilium/tetragon/pkg/process"
"github.com/cilium/tetragon/pkg/reader/namespace"
"github.com/cilium/tetragon/pkg/sensors"
"github.com/cilium/tetragon/pkg/sensors/exec/procevents"
"github.com/cilium/tetragon/pkg/sensors/program"
Expand Down Expand Up @@ -68,7 +71,7 @@ func msgToExecveKubeUnix(m *processapi.MsgExecveEvent, exec_id string, filename
return kube
}

func execParse(reader *bytes.Reader) (processapi.MsgProcess, bool, error) {
func execParse(reader *bytes.Reader, getUsername bool) (processapi.MsgProcess, bool, error) {
proc := processapi.MsgProcess{}
exec := processapi.MsgExec{}

Expand All @@ -89,6 +92,12 @@ func execParse(reader *bytes.Reader) (processapi.MsgProcess, bool, error) {
proc.Nlink = exec.Nlink
proc.Ino = exec.Ino

if getUsername {
if username, err := user.LookupId(strconv.FormatUint(uint64(proc.UID), 10)); err == nil {
proc.Username = username.Name
}
}

size := exec.Size - processapi.MSG_SIZEOF_EXECVE
if size > processapi.MSG_SIZEOF_BUFFER-processapi.MSG_SIZEOF_EXECVE {
err := fmt.Errorf("msg exec size larger than argsbuffer")
Expand Down Expand Up @@ -182,7 +191,9 @@ func handleExecve(r *bytes.Reader) ([]observer.Event, error) {
return nil, err
}
msgUnix := msgToExecveUnix(&m)
msgUnix.Unix.Process, empty, err = execParse(r)
getUsername := namespace.GetCurrentNamespace().Mnt.Inum == msgUnix.Unix.Msg.Namespaces.MntInum &&
namespace.GetCurrentNamespace().User.Inum == msgUnix.Unix.Msg.Namespaces.UserInum
msgUnix.Unix.Process, empty, err = execParse(r, getUsername)
if err != nil && empty {
msgUnix.Unix.Process = nopMsgProcess()
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5d48ea0

Please sign in to comment.