#include <sys/ucred.h>
sys/ucred.h
structucred
@struct ucred
@brief In-kernel credential structure.
@discussion Note that this structure should not be used outside the kernel,
nor should it or copies of it be exported outside.
A credential has a relatively simple lifetime, with 3 phases:
1. construction
2. publication
3. death
Construction
~~~~~~~~~~~~
The construction phase happens via various MACF hooks,
typically with the "associate" or "update" suffix.
During this phase, the credential structure is completely private,
and can't be looked up. All "associate" and "update" callouts are
made serially (so no locking is required for clients to ensure
atomicity of updates) and keeping references on the cred is forbidden.
Publication
~~~~~~~~~~~
Once the credential has been constructed, it is being published
on its owning structure (typically the proc) and added into
a uniquing hash table.
After this point, the credential becomes a refcounted immutable
"value type" data structure. MACF clients which have set labels
are not allowed to modify this label pointer anymore (though
their label itself might be mutable or contain caches).
It means that while a client holds a reference on a credential,
it can consult labels without further synchronization or references.
Death
~~~~~
Credentials are managed under the smr_kauth_cred domain,
and retired according to the <kern/smr.h> rules.
Once it is safe for the credential to be freed,
callbacks will clean up the resources the credential
holds onto via the MACF cred_label_destroy() hook.
It means that under an smr_kauth_cred critical section,
clients can consult labels without further synchronization
or references, even after the credential hit a "0" refcount.
KPIs to interact with this data structure live in <sys/kauth.h>
| unnamed struct at Kernel/sys/ucred.h:146:2 | cr_link | |||||||
| ||||||||
| u_long | cr_ref | reference count | ||||||
| struct posix_cred | cr_posix | |||||||
| struct label * | cr_label | MAC label | ||||||
| struct au_session | cr_audit | NOTE: If anything else (besides the flags) added after the label, you must change kauth_cred_find(). user auditing data | ||||||
functionsuser
int suser(kauth_cred_t cred, u_short *acflag)
▾
claude-fable-5, 2026-08-23 · not from Apple sources · verified against xnu bsd/kern/kern_prot.c
Tests whether the given credentials imply super-user privilege (uid 0); if so, and acflag is non-NULL, sets the ASU accounting flag to record that privilege was used. Returns 0 if privileged, EPERM otherwise. The source marks this interface as going away; use kauth_cred_issuser() directly instead.
functionset_security_token
int set_security_token(struct proc *p, struct ucred *cred)
▾
claude-fable-5, 2026-08-23 · not from Apple sources · verified against xnu bsd/kern/kern_prot.c
Sets the Mach security and audit tokens of the process's task from the given credential: the security token carries the effective uid and gid, and the audit token carries the audit user ID, effective and real IDs, pid, session ID, and pid version. A uid 0 credential also grants the task the host privilege port, subject to a MAC check. Returns 0 on success, nonzero if the task tokens could not be set.
functioncru2x
void cru2x(kauth_cred_t cr, struct xucred *xcr)
▾
claude-fable-5, 2026-08-23 · not from Apple sources · verified against xnu bsd/kern/kern_prot.c
Fills in a struct xucred (the externalized credential representation, XUCRED_VERSION) from a kauth_cred_t: copies the effective uid, group count, and group list; other fields are zeroed.