// SPDX-License-Identifier: GPL-2.012// Copyright (C) 2024 Google LLC.34//! Credentials management.5//!6//! C header: [`include/linux/cred.h`](srctree/include/linux/cred.h).7//!8//! Reference: <https://www.kernel.org/doc/html/latest/security/credentials.html>910use crate::{bindings, sync::aref::AlwaysRefCounted, task::Kuid, types::Opaque};1112/// Wraps the kernel's `struct cred`.13///14/// Credentials are used for various security checks in the kernel.15///16/// Most fields of credentials are immutable. When things have their credentials changed, that17/// happens by replacing the credential instead of changing an existing credential. See the [kernel18/// documentation][ref] for more info on this.19///20/// # Invariants21///22/// Instances of this type are always ref-counted, that is, a call to `get_cred` ensures that the23/// allocation remains valid at least until the matching call to `put_cred`.24///25/// [ref]: https://www.kernel.org/doc/html/latest/security/credentials.html26#[repr(transparent)]27pub struct Credential(Opaque<bindings::cred>);2829// SAFETY:30// - `Credential::dec_ref` can be called from any thread.31// - It is okay to send ownership of `Credential` across thread boundaries.32unsafe impl Send for Credential {}3334// SAFETY: It's OK to access `Credential` through shared references from other threads because35// we're either accessing properties that don't change or that are properly synchronised by C code.36unsafe impl Sync for Credential {}3738impl Credential {39/// Creates a reference to a [`Credential`] from a valid pointer.40///41/// # Safety42///43/// The caller must ensure that `ptr` is valid and remains valid for the lifetime of the44/// returned [`Credential`] reference.45#[inline]46pub unsafe fn from_ptr<'a>(ptr: *const bindings::cred) -> &'a Credential {47// SAFETY: The safety requirements guarantee the validity of the dereference, while the48// `Credential` type being transparent makes the cast ok.49unsafe { &*ptr.cast() }50}5152/// Get the id for this security context.53#[inline]54pub fn get_secid(&self) -> u32 {55let mut secid = 0;56// SAFETY: The invariants of this type ensures that the pointer is valid.57unsafe { bindings::security_cred_getsecid(self.0.get(), &mut secid) };58secid59}6061/// Returns the effective UID of the given credential.62#[inline]63pub fn euid(&self) -> Kuid {64// SAFETY: By the type invariant, we know that `self.0` is valid. Furthermore, the `euid`65// field of a credential is never changed after initialization, so there is no potential66// for data races.67Kuid::from_raw(unsafe { (*self.0.get()).euid })68}69}7071// SAFETY: The type invariants guarantee that `Credential` is always ref-counted.72unsafe impl AlwaysRefCounted for Credential {73#[inline]74fn inc_ref(&self) {75// SAFETY: The existence of a shared reference means that the refcount is nonzero.76unsafe { bindings::get_cred(self.0.get()) };77}7879#[inline]80unsafe fn dec_ref(obj: core::ptr::NonNull<Credential>) {81// SAFETY: The safety requirements guarantee that the refcount is nonzero. The cast is okay82// because `Credential` has the same representation as `struct cred`.83unsafe { bindings::put_cred(obj.cast().as_ptr()) };84}85}868788