From fa23052e34f064ed675cb9ae1a3b6cfa3ba9d8b9 Mon Sep 17 00:00:00 2001 From: Dan Everton Date: Wed, 24 Nov 2021 15:30:53 +1000 Subject: [PATCH] Add wrapper and docs for for bpf_get_current_uid_gid --- bpf/aya-bpf/src/helpers.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/bpf/aya-bpf/src/helpers.rs b/bpf/aya-bpf/src/helpers.rs index 47fb51fd..1b7ea5b4 100644 --- a/bpf/aya-bpf/src/helpers.rs +++ b/bpf/aya-bpf/src/helpers.rs @@ -418,3 +418,26 @@ pub fn bpf_get_current_comm() -> Result<[c_char; 16], c_long> { pub fn bpf_get_current_pid_tgid() -> u64 { unsafe { gen::bpf_get_current_pid_tgid() } } + +/// Read the user id and group id associated with the current task struct as +/// a `u64`. +/// +/// In the return value, the upper 32 bits are the `gid`, and the lower 32 bits are the +/// `uid`. That is, the returned value is equal to: `(gid << 32) | uid`. A caller may +/// access the individual fields by either casting to a `u32` or performing a `>> 32` bit +/// shift and casting to a `u32`. +/// +/// # Examples +/// +/// ```no_run +/// # #![allow(dead_code)] +/// # use aya_bpf::helpers::bpf_get_current_uid_gid; +/// let gid = (bpf_get_current_uid_gid() >> 32) as u32; +/// let uid = bpf_get_current_uid_gid() as u32; +/// +/// // Do something with uid and gid +/// ``` +#[inline] +pub fn bpf_get_current_uid_gid() -> u64 { + unsafe { gen::bpf_get_current_uid_gid() } +}