---
name: "setcred"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/setcred"
---

# setcred(2)

set current process credentials atomically

## NAME

**setcred** – set current process credentials atomically

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/ucred.h\>**

*int*\

**setcred**(*u\_int flags*, *const struct setcred \*wcred*, *size\_t size*);

## DESCRIPTION

The **setcred**() system call can set any combination of user-accessible credentials of the current process in an atomic manner.

This system call is normally permitted only for processes having the ID of the super-user (0) as their effective user ID, or not at all if the [sysctl(8)](/sysctl/8.md) variable *security.bsd.suser\_enabled* is zero or some active MAC policy specifically denies these processes.

Some MAC policies, such as [mac\_do(4)](/mac_do/4.md), may also allow unprivileged users to call it successfully, possibly depending on the exact credentials transition requested, once again unless any active MAC policy specifically denies that.

The *flags* argument serves to indicate which process credentials should be changed by the call. Allowed flags are:

- *SETCREDF\_UID*

  Set the effective user ID.

- *SETCREDF\_RUID*

  Set the real user ID.

- *SETCREDF\_SVUID*

  Set the saved user ID.

- *SETCREDF\_GID*

  Set the effective group ID.

- *SETCREDF\_RGID*

  Set the real group ID.

- *SETCREDF\_SVGID*

  Set the saved group ID.

- *SETCREDF\_SUPP\_GROUPS*

  Set the supplementary group list.

- *SETCREDF\_MAC\_LABEL*

  Set the MAC label.

The *struct* *setcred* structure is currently defined as:

```
struct setcred {
	uid_t	 sc_uid;		/* effective user id */
	uid_t	 sc_ruid;		/* real user id */
	uid_t	 sc_svuid;		/* saved user id */
	gid_t	 sc_gid;		/* effective group id */
	gid_t	 sc_rgid;		/* real group id */
	gid_t	 sc_svgid;		/* saved group id */
	u_int	 sc_pad;		/* padding, unused */
	u_int	 sc_supp_groups_nb;	/* supplementary groups number */
	gid_t	*sc_supp_groups;	/* supplementary groups */
	struct mac *sc_label;		/* MAC label */
};
```

Its fields are:

- *sc\_uid*

  The ID to set the effective user to, if flag SETCREDF\_UID is specified.

- *sc\_ruid*

  The ID to set the real user to, if flag SETCREDF\_RUID is specified.

- *sc\_svuid*

  The ID to set the saved user to, if flag SETCREDF\_SVUID is specified.

- *sc\_gid*

  The ID to set the effective group to, if flag SETCREDF\_GID is specified.

- *sc\_rgid*

  The ID to set the real group to, if flag SETCREDF\_RGID is specified.

- *sc\_svgid*

  The ID to set the saved group to, if flag SETCREDF\_SVGID is specified.

- *sc\_supp\_groups\_nb*

  The size of array *sc\_supp\_groups*, if flag SETCREDF\_SUPP\_GROUPS is specified. It must be less than or equal to {NGROUPS\_MAX}.

- *sc\_supp\_groups*

  An array of IDs to set the supplementary groups to, if flag SETCREDF\_SUPP\_GROUPS is specified.

- *sc\_label*

  A pointer to a valid MAC label structure, e.g., built with the [mac\_from\_text(3)](/mac_from_text/3.md) function, if flag SETCREDF\_MAC\_LABEL is specified.

For forward compatibility and security reasons, it is recommended that users always initialize objects of type *struct* *setcred* with the provided initializer: SETCRED\_INITIALIZER.

The *size* argument must be the size of the passed *wcred* structure.

## RETURN VALUES

The **setcred()** function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## ERRORS

The **setcred**() system call will fail if:

- \[EINVAL\]

  Unrecognized flags were passed in *flags*, or the *size* parameter does not match the size of *struct* *setcred*, or the field *sc\_supp\_group\_nb* has a value strictly greater than {NGROUPS\_MAX} (if flag SETCREDF\_SUPP\_GROUPS was supplied), or the MAC label pointed to by field *sc\_label* is invalid (if flag SETCREDF\_MAC\_LABEL was supplied).

- \[EFAULT\]

  The *wcred* pointer, or pointers in fields *sc\_supp\_groups* (if flag SETCREDF\_SUPP\_GROUPS was supplied) or *sc\_label* (if flag SETCREDF\_MAC\_LABEL was supplied) point to invalid locations.

- \[EPERM\]

  The user is not the super-user and/or the requested credentials transition is not allowed by the system or MAC modules.

- \[EOPNOTSUPP\]

  Some of the requested credentials have a type that the system does not support. This currently can occur only if the kernel has been compiled without MAC and SETCREDF\_MAC\_LABEL has been passed.

## SEE ALSO

[issetugid(2)](/issetugid/2.md), [setregid(2)](/setregid/2.md), [setreuid(2)](/setreuid/2.md), [setuid(2)](/setuid/2.md), [mac\_text(3)](/mac_text/3.md), [mac(4)](/mac/4.md), [mac\_do(4)](/mac_do/4.md), [maclabel(7)](/maclabel/7.md)

## STANDARDS

The **setcred**() system call is specific to FreeBSD.

A call to **setcred**() usually changes process credentials that are listed by POSIX/SUS standards. The changed values then produce the effects with respect to the rest of the system that are described in these standards, as if these changes had resulted from calling standard or traditional credentials-setting functions. Currently, all flags but SETCREDF\_MAC\_LABEL lead to modifying standard credentials.

The only differences in using **setcred**() to change standard credentials instead of standard or traditional functions are:

- All requested changes are performed atomically.

- Only the super-user or an unprivileged user authorized by some MAC module can successfully call **setcred**(), even if the standard system calls would have authorized any unprivileged user to effect the same changes. For example, **seteuid**() allows any unprivileged user to change the effective user ID to either the real or saved ones, while **setcred**() called with flag SETCREDF\_UID does not.

## HISTORY

The **setcred**() system call appeared in FreeBSD 14.3.

Traditionally in UNIX, all credential changes beyond shuffles of effective, real and saved IDs have been done by setuid binaries that successively call multiple credentials-setting system calls and in a specific order. For example, to change all user IDs to that of some unprivileged user, **setuid**() must be called last so that all other credentials-changing calls can be performed successfully beforehand, as they require super-user privileges.

This piecewise approach causes such a process to transiently hold high privilege credentials that are neither the original nor necessarily the desired final ones. Besides opening a transition window where possible vulnerabilities could have catastrophic consequences, it makes it impossible for the kernel to enforce that only certain transitions of credentials are allowed.

The necessity of an atomic, global approach to changing credentials clearly appeared while working on extending [mac\_do(4)](/mac_do/4.md) to allow rules to authorize only specific changes of primary or supplementary groups, which prompted the addition of **setcred**().

## AUTHORS

The **setcred**() system call and this manual page were written by Olivier Certner ⟨*olce.freebsd@certner.fr*⟩.

## SECURITY CONSIDERATIONS

The same considerations as those of standard or traditional credentials-setting system calls apply to **setcred**(), except for the lack of atomicity of successive such calls.

In particular, please consult section **SECURITY** **CONSIDERATIONS** of the [setuid(2)](/setuid/2.md) manual page about the absence of effect of changing standard credentials on already open files.
