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

tetragon: resolve uid to username for exec events from /proc fs #2588

Merged
merged 4 commits into from
Jun 26, 2024
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
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/en/docs/reference/metrics.md

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

21 changes: 14 additions & 7 deletions pkg/metrics/errormetrics/errormetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ const (
HandlerError
// An event finalizer on Process failed
EventFinalizeProcessInfoFailed
// Failed to resolve Process uid to username
ProcessMetadataUsernameFailed
// The username resolution was skipped since the process is not in host
// namespaces.
ProcessMetadataUsernameIgnoredNotInHost
)

var errorTypeLabelValues = map[ErrorType]string{
ProcessCacheMissOnGet: "process_cache_miss_on_get",
ProcessCacheEvicted: "process_cache_evicted",
ProcessCacheMissOnRemove: "process_cache_miss_on_remove",
ProcessPidTidMismatch: "process_pid_tid_mismatch",
EventMissingProcessInfo: "event_missing_process_info",
HandlerError: "handler_error",
EventFinalizeProcessInfoFailed: "event_finalize_process_info_failed",
ProcessCacheMissOnGet: "process_cache_miss_on_get",
ProcessCacheEvicted: "process_cache_evicted",
ProcessCacheMissOnRemove: "process_cache_miss_on_remove",
ProcessPidTidMismatch: "process_pid_tid_mismatch",
EventMissingProcessInfo: "event_missing_process_info",
HandlerError: "handler_error",
EventFinalizeProcessInfoFailed: "event_finalize_process_info_failed",
ProcessMetadataUsernameFailed: "process_metadata_username_failed",
ProcessMetadataUsernameIgnoredNotInHost: "process_metadata_username_ignored_not_in_host_namespaces",
}

func (e ErrorType) String() string {
Expand Down
13 changes: 13 additions & 0 deletions pkg/reader/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ func GetCurrentNamespace() *tetragon.Namespaces {
return retVal
}

func IsMsgNsInHostMntUser(ns *processapi.MsgNamespaces) (bool, error) {
hostNs, err := InitHostNamespace()
if err != nil {
return false, err
}

if ns.MntInum == hostNs.Mnt.Inum && ns.UserInum == hostNs.User.Inum {
return true, nil
}

return false, nil
}

func GetMsgNamespaces(ns processapi.MsgNamespaces) (*tetragon.Namespaces, error) {
hostNs, err := InitHostNamespace()
if err != nil {
Expand Down
25 changes: 9 additions & 16 deletions pkg/sensors/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ import (
exec "github.com/cilium/tetragon/pkg/grpc/exec"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/observer"
"github.com/cilium/tetragon/pkg/option"
"github.com/cilium/tetragon/pkg/process"
"github.com/cilium/tetragon/pkg/reader/namespace"
"github.com/cilium/tetragon/pkg/reader/userdb"
"github.com/cilium/tetragon/pkg/sensors"
"github.com/cilium/tetragon/pkg/sensors/exec/procevents"
"github.com/cilium/tetragon/pkg/sensors/exec/userinfo"
"github.com/cilium/tetragon/pkg/sensors/program"
"github.com/cilium/tetragon/pkg/strutils"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -72,18 +70,6 @@ func msgToExecveKubeUnix(m *processapi.MsgExecveEvent, exec_id string, filename
return kube
}

func msgToExecveAccountUnix(m *exec.MsgExecveEventUnix) {
if option.Config.UsernameMetadata == int(option.USERNAME_METADATA_UNIX) {
if ns, err := namespace.GetMsgNamespaces(m.Unix.Msg.Namespaces); err == nil {
if ns.Mnt.IsHost && ns.User.IsHost {
if username, err := userdb.UsersCache.LookupUser(m.Unix.Process.UID); err == nil {
m.Unix.Process.User.Name = username
}
}
}
}
}

func execParse(reader *bytes.Reader) (processapi.MsgProcess, bool, error) {
proc := processapi.MsgProcess{}
exec := processapi.MsgExec{}
Expand Down Expand Up @@ -203,7 +189,14 @@ func handleExecve(r *bytes.Reader) ([]observer.Event, error) {
msgUnix.Unix.Process = nopMsgProcess()
}
if err == nil && !empty {
msgToExecveAccountUnix(msgUnix)
err = userinfo.MsgToExecveAccountUnix(msgUnix)
if err != nil {
logger.GetLogger().WithFields(logrus.Fields{
"process.pid": msgUnix.Unix.Process.PID,
"process.binary": msgUnix.Unix.Process.Filename,
"process.uid": msgUnix.Unix.Process.UID,
}).WithError(err).Trace("Resolving process uid to username record failed")
}
}
msgUnix.Unix.Kube = msgToExecveKubeUnix(&m, process.GetExecID(&msgUnix.Unix.Process), msgUnix.Unix.Process.Filename)
return []observer.Event{msgUnix}, nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/sensors/exec/procevents/proc_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/cilium/tetragon/pkg/sensors"
"github.com/cilium/tetragon/pkg/sensors/base"
"github.com/cilium/tetragon/pkg/sensors/exec/execvemap"
"github.com/cilium/tetragon/pkg/sensors/exec/userinfo"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -279,6 +281,15 @@ func pushExecveEvents(p procs) {
m.Unix.Process.Filename = filename
m.Unix.Process.Args = args

err := userinfo.MsgToExecveAccountUnix(&m)
if err != nil {
logger.GetLogger().WithFields(logrus.Fields{
"process.pid": p.pid,
"process.binary": filename,
"process.uid": m.Unix.Process.UID,
}).WithError(err).Trace("Resolving process uid to username record failed")
}

observer.AllListeners(&m)
}
}
Expand Down
53 changes: 53 additions & 0 deletions pkg/sensors/exec/userinfo/userinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
tixxdz marked this conversation as resolved.
Show resolved Hide resolved

package userinfo

import (
"errors"

"github.com/cilium/tetragon/pkg/api/processapi"
"github.com/cilium/tetragon/pkg/grpc/exec"
"github.com/cilium/tetragon/pkg/metrics/errormetrics"
"github.com/cilium/tetragon/pkg/option"
"github.com/cilium/tetragon/pkg/reader/namespace"
"github.com/cilium/tetragon/pkg/reader/userdb"
)

// Errors
var (
ErrNotInHostNs = errors.New("process is not in host namespaces")
)

func getAccountUnix(uid uint32, ns *processapi.MsgNamespaces) (string, error) {
inHost, err := namespace.IsMsgNsInHostMntUser(ns)
if err != nil {
return "", err
}
if inHost {
username, err := userdb.UsersCache.LookupUser(uid)
if err != nil {
return "", err
}
return username, nil
}
return "", ErrNotInHostNs
}

func MsgToExecveAccountUnix(m *exec.MsgExecveEventUnix) error {
if option.Config.UsernameMetadata == int(option.USERNAME_METADATA_UNIX) {
username, err := getAccountUnix(m.Unix.Process.UID, &m.Unix.Msg.Namespaces)
if err == nil {
m.Unix.Process.User.Name = username
return nil
}

if errors.Is(err, ErrNotInHostNs) {
errormetrics.ErrorTotalInc(errormetrics.ProcessMetadataUsernameIgnoredNotInHost)
} else {
errormetrics.ErrorTotalInc(errormetrics.ProcessMetadataUsernameFailed)
}
return err
}
return nil
}
36 changes: 36 additions & 0 deletions pkg/sensors/exec/userinfo/userinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package userinfo

import (
"testing"

"github.com/cilium/tetragon/pkg/api/processapi"
"github.com/cilium/tetragon/pkg/reader/namespace"
"github.com/stretchr/testify/require"
)

func TestAccountUnix(t *testing.T) {
hostNs, err := namespace.InitHostNamespace()
require.NoError(t, err)

ns := processapi.MsgNamespaces{}
name, err := getAccountUnix(0, &ns)
require.Equal(t, ErrNotInHostNs, err)
require.Empty(t, name)

ns = processapi.MsgNamespaces{
UserInum: hostNs.User.Inum,
MntInum: hostNs.Mnt.Inum,
}

name, err = getAccountUnix(0, &ns)
require.NoError(t, err)
require.Equal(t, name, "root")

ns.MntInum += 0x1000
name, err = getAccountUnix(1, &ns)
require.Equal(t, ErrNotInHostNs, err)
require.Empty(t, name)
}
Loading