UCRED(9)
π UCRED(9) β user credential functions, but make it make sense π
okay so this is the Kernel Developer's Manual page for UCRED(9), and it's all about user credentials in the kernel. Yes bestie, we're getting into struct ucred lore. Buckle up π
πΈ NAME πΈ
ucred, crget, crhold, crfree, crcopy, crdup, cru2x β functions related to user credentials
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/ucred.h>
struct ucred *
crget(void);
struct ucred *
crhold(struct ucred *cr);
void
crfree(struct ucred *cr);
void
crcopy(struct ucred *dest, struct ucred *src);
struct ucred *
crcopysafe(struct proc *p, struct ucred *cr);
struct ucred *
crdup(struct ucred *cr);
void
crsetgroups(struct ucred *cr, int ngrp, gid_t *groups);
void
crsetgroups_and_egid(struct ucred *cr, int ngrp, gid_t *groups,
gid_t default_egid);
void
cru2x(struct ucred *cr, struct xucred *xcr);
π DESCRIPTION π
okay so the whole ucred family is here to manage user credential structures (struct ucred) inside the kernel. It's giving gatekeeping but for permissions, and honestly iconic π
crget()β allocates memory for a new structure, sets its reference count to1, and initializes its lock. Fresh, new, untouched vibes β¨crhold()β increases the reference count on the credential. Like adding another bestie to the group chat π«Άcrfree()β decreases the reference count on the credential. If the count drops to0, the storage for the structure is freed. Periodt πcrcopy()β copies the contents of the source (template) credential into the destination template. Theuidinfostructure within the destination is referenced by callinguihold(9).crcopysafe()β copies the current credential associated with the processpinto the newly allocated credentialcr. The process lock onpmust be held and will be dropped and reacquired as needed to allocate group storage space incr. Lock drama, but make it safe πcrdup()β allocates memory for a new structure and copies the contents ofcrinto it. The actual copying is performed bycrcopy().crsetgroups()β sets thecr_groupsandcr_ngroupsvariables representing the supplementary groups, allocating space as needed. It also truncates the group list to the current maximum number of groups.crsetgroups_and_egid()β similar, but interprets separately the first group ofgroupsas the effective GID to set, only setting the subsequent groups as supplementary ones. It will usedefault_egidas the new effective GID ifgroupsis empty. No other mechanism should be used to modify thecr_groupsarray. That's the rule, bestie β no side-door edits πͺcru2x()β converts aucredstructure to anxucredstructure. That is, it copies data fromcrtoxcr; it ignores fields in the former that are not present in the latter (e.g.,cr_uidinfo), and appropriately sets fields in the latter that are not present in the former (e.g.,cr_version). Glow-up conversion energy π
π«Ά RETURN VALUES π«Ά
crget(), crhold(), crdup(), and crcopysafe() all return a pointer to a ucred structure.
π§ USAGE NOTES π§
okay this is the part where the kernel-dev lore gets real, so listen up bestie π§
As of FreeBSD 5.0, the ucred structure contains extensible fields. This means that the correct protocol must always be followed to create a fresh and writable credential structure: new credentials must always be derived from existing credentials using crget(), crcopy(), and crcopysafe().
In the common case, credentials required for access control decisions are used in a read-only manner. In these circumstances, the thread credential td_ucred should be used, as it requires no locking to access safely, and remains stable for the duration of the call even in the face of a multi-threaded application changing the process credentials from another thread.
During a process credential update, the process lock must be held across check and update, to prevent race conditions. The process credential, td->td_proc->p_ucred, must be used both for check and update. If a process credential is updated during a system call and checks against the thread credential are to be made later during the same system call, the thread credential must also be refreshed from the process credential so as to prevent use of a stale value. To avoid this scenario, it is recommended that system calls updating the process credential be designed to avoid other authorization functions.
If temporarily elevated privileges are required for a thread, the thread credential can be replaced for the duration of an activity, or for the remainder of the system call. However, as a thread credential is often shared, appropriate care should be taken to make sure modifications are made to a writable credential through the use of crget() and crcopy().
Caution should be exercised when checking authorization for a thread or process to perform an operation on another thread or process. As a result of temporary elevation, the target thread credential should never be used as the target credential in an access control decision: the process credential associated with the thread, td->td_proc->p_ucred, should be used instead. For example, p_candebug(9) accepts a target process, not a target thread, for access control purposes.
bestie this is the "do not use the wrong credential" warning. it is not a drill π
π₯ SEE ALSO π₯
π AUTHORS π
This manual page was written by Chad David <davidc@acns.ab.ca>.
okay bye bestie π«Ά β this page is from the Debian version, dated August 29, 2025. UCRED(9) out. π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (C) 2001 Chad David <davidc@acns.ab.ca>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms