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

process:bpf: report euid as the process.uid #2575

Merged
merged 2 commits into from
Jun 21, 2024

Conversation

tixxdz
Copy link
Member

@tixxdz tixxdz commented Jun 17, 2024

Instead of reporting the real uid of the task that is the owner of the task, let's report the effective uid that is used to calculate the privileges of the current task when acting upon other objects. This allows to be compatible with 'ps' too.

See commits for further details.

@tixxdz tixxdz requested a review from a team as a code owner June 17, 2024 23:47
@tixxdz tixxdz requested a review from olsajiri June 17, 2024 23:47
@tixxdz tixxdz marked this pull request as draft June 17, 2024 23:47
@tixxdz tixxdz added the release-note/minor This PR introduces a minor user-visible change label Jun 17, 2024
Instead of reporting the real uid of the task that is the owner of
the task in 'process.uid' when collecting execve info in bpf, let's
report the effective uid that is used to calculate the privileges
of the current task when acting upon other objects. This allows to
be compatible with 'ps'.

We already do this for processes that we collect from /proc, the
process.uid == euid actually. So align bpf as well.

For /proc it is a bit more complicated, since the processes may
change their uids, drop/gain privs, etc after their execve and before
tetragon starts, so we do not have that much context. For processes
that start after tetragon, we can record at execve time and cache
it.

The real uid != euid at execve time happens when we are executing
a setuid/setgid binary where the inode->i_uid|i_gid are mapped to
current idmapping mount and reflected to the new euid and egid. Then
if the capability LSM allows setuid/setgid execution then the new
euid and egid will be propagated to suid/fsuid and sgid/fsgid while
the uid and gid still have the values of the original task performing
execve call.

The following details why the kernel has both real and effective uid
and how they are used in permission checks. We start by real uid:

* Real uid is used to determine the task owner (uid that started it).

* Change privileges of a task drop/raise while keeping it possible
  to restore the effective, saved and other uids back that are used
  for various permission checks to the original real uid that started
  the task, in case of setuid system calls.

* When mounting some file systems to auto fill up the owner of the fs.

* When a process is sending a signal, its real uid is used
  to fill up the siginfo_t.uid field but also it is used to determine
  if we can signal the remote task, as the permission check is:
  caller->euid == target->suid || caller->euid == target->uid

  If the caller euid does not match the kill permission check switches
  to use the real uid:
  caller->uid == target->suid || caller->uid == target->uid.

* When ptracing a target task. The ptrace call has different access
  modes. To be precise accessing a task is possible through filesystem
  like /proc or directly through a syscall by pid, where caller may
  specify to use PTRACE_MODE_FSCREDS fsuid,fsgid for the former or
  PTRACE_MODE_REALCREDS real uid/gid for the later.

  In case of PTRACE_MODE_REALCREDS then the real task owner real
  uid/gid will be computed against the target task:
  caller->uid == target->euid == target->suid && target->uid &&
  caller->gid == target->egid == target->sgid && target->gid

  This is another case where the real uid of caller could be used
  to calculate privileges.

Now for the effective uid it is used for most permission checks when
acting upong other objects:

* A task is considered privileged if its euid == user_ns->owner,
  so a task is privileged if its euid equals the current user namespace
  owner, or one of the parent user namespaces owners. Even if that
  task has no capabilities, it has full privileges as being the
  owner of one user namespace, including init host user namespace.

  This allows access to mostly everything, even for the kill signal
  permission check, as the capability check will eventually fallback
  to the euid to determine if it has ownership on task's user
  namespace.

* The inode i_uid owner is usually initialized with caller fsuid,
  where the fsuid always matches the euid at execve time.

* The fsuid that is used for file system access permission always
  matches the euid at execve time, in this context access permission
  aligns with euid.

If users want more details about all different uids, then they can
specify the --enable-process-cred flag which will export the
'process.process_credentials' struct with all the uids/gids.

Signed-off-by: Djalal Harouni <[email protected]>
@tixxdz tixxdz force-pushed the pr/tixxdz/2024-06-process-uid-fix-docs branch from 8d2fe6d to d2dcefa Compare June 18, 2024 15:23
Copy link

netlify bot commented Jun 18, 2024

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit f3d4ea1
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/6671a90973ea45000838d4d2
😎 Deploy Preview https://deploy-preview-2575--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@tixxdz tixxdz marked this pull request as ready for review June 18, 2024 15:25
@tixxdz tixxdz requested review from kkourt and jrfastab June 18, 2024 15:25
@tixxdz tixxdz force-pushed the pr/tixxdz/2024-06-process-uid-fix-docs branch from d2dcefa to f3d4ea1 Compare June 18, 2024 15:34
@tixxdz tixxdz added needs-backport/1.0 This PR needs backporting to 1.0 needs-backport/1.1 This PR needs backporting to 1.1 labels Jun 18, 2024
Copy link
Contributor

@kkourt kkourt left a comment

Choose a reason for hiding this comment

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

LGTM!
Thanks for the detailed commit message and the doc updates.

@tixxdz tixxdz merged commit d308a15 into main Jun 21, 2024
52 checks passed
@tixxdz tixxdz deleted the pr/tixxdz/2024-06-process-uid-fix-docs branch June 21, 2024 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-backport/1.0 This PR needs backporting to 1.0 needs-backport/1.1 This PR needs backporting to 1.1 release-note/minor This PR introduces a minor user-visible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants