---
name: "cap_mode"
section: "3"
platform: "linux"
package: "libcap"
version: "2.78-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/cap_get_proc"
---

# cap\_mode(3)

capability manipulation on processes

Part of [cap\_get\_proc(3)](/cap_get_proc/3.md).

## NAME

cap\_get\_proc, cap\_set\_proc, capgetp, cap\_get\_bound, cap\_drop\_bound, cap\_get\_ambient, cap\_set\_ambient, cap\_reset\_ambient, cap\_get\_secbits, cap\_set\_secbits, cap\_get\_mode, cap\_set\_mode, cap\_mode\_name, cap\_get\_pid, cap\_setuid, cap\_prctl, cap\_prctlw, cap\_setgroups - capability manipulation on processes

## SYNOPSIS

```
#include <sys/capability.h>
cap_t cap_get_proc(void);
int cap_set_proc(cap_t cap_p);
int cap_get_bound(cap_value_t cap);
CAP_IS_SUPPORTED(cap_value_t cap);
int cap_drop_bound(cap_value_t cap);
int cap_get_ambient(cap_value_t cap);
int cap_set_ambient(cap_value_t cap, cap_flag_value_t value);
int cap_reset_ambient(void);
CAP_AMBIENT_SUPPORTED();
unsigned cap_get_secbits(void);
int cap_set_secbits(unsigned bits);
cap_mode_t cap_get_mode(void);
const char *cap_mode_name(cap_mode_t mode);
int cap_prctl(long int pr_cmd, long int arg1, long int arg2,
	      long int arg3, long int arg4, long int arg5);
int cap_prctlw(long int pr_cmd, long int arg1, long int arg2,
	       long int arg3, long int arg4, long int arg5);
int cap_set_mode(cap_mode_t mode);
#include <sys/types.h>
cap_t cap_get_pid(pid_t pid);
int cap_setuid(uid_t uid);
int cap_setgroups(gid_t gid, size_t ngroups, const gid_t groups);
```

Link with *-lcap*.

## DESCRIPTION

**cap\_get\_proc**() allocates a capability state in working storage, sets its state to that of the calling process, and returns a pointer to this newly created capability state.  The caller should free any releasable memory, when the capability state in working storage is no longer required, by calling **cap\_free**() with the *cap\_t* as an argument.

**cap\_set\_proc**() sets the values for all capability flags for all capabilities to the capability state identified by *cap\_p*. The new capability state of the process will be completely determined by the contents of *cap\_p* upon successful return from this function.  If any flag in *cap\_p* is set for any capability not currently permitted for the calling process, the function will fail, and the capability state of the process will remain unchanged.

**cap\_get\_pid**() returns a *cap\_t*, see  [**cap\_init**(3)](/cap_init/3.md), with the process capabilities of the process known to the caller as *pid*. If *pid* is 0, then the calling process's capabilities are returned. This information can also be obtained from the */proc/\<pid\>/status* file. (The entries in that file can be translated with the **capsh --decode=***XXX* command line.) When the caller is operating within a (**CLONE\_NEWPID**) namespace, the numerical *pid* argument is interpreted in the range of that namespace. As such, the caller's idea of the target *pid* may differ from that of the target process when they are operating in different pid namespaces. See [**pid\_namespaces**(7)](/pid_namespaces/7.md) for details. Further, the returned *cap\_t* value holds the capabilities that the target *pid* thinks it has. If the target is operating in a (**CLONE\_NEWUSER**) namespace, the system wide privilege of those user namespace capabilities may be substantially reduced. See [**user\_namespaces**(7)](/user_namespaces/7.md) for details.

**cap\_get\_bound**() with a *cap* as an argument returns the current value of this bounding set capability flag in effect for the calling process. This operation is unprivileged. Note, a macro function **CAP\_IS\_SUPPORTED(cap\_value\_t** cap**)** is provided that evaluates to true (1) if the system supports the specified capability, *cap*. If the system does not support the capability, this function returns 0. This macro works by testing for an error condition with **cap\_get\_bound**().

**cap\_drop\_bound**() can be used to lower the specified bounding set capability, **cap**. To complete successfully, the prevailing *effective* capability set must have a raised **CAP\_SETPCAP**.

**cap\_get\_ambient**() returns the prevailing value of the specified ambient capability, or -1 if the capability is not supported by the running kernel.  A macro **CAP\_AMBIENT\_SUPPORTED**() uses this function to determine if ambient capabilities are supported by the kernel.

**cap\_set\_ambient**() sets the specified ambient capability to a specific value. To raise a specific ambient capability the *inheritable* and *permitted* flags of the calling process must contain the specified capability value. Raised ambient bits will only be retained as long as this remains true of the inheritable and permitted flags.

**cap\_reset\_ambient**() resets all of the ambient capabilities for the calling process to their lowered value. Note, the ambient set is intended to operate in a legacy environment where the application has limited awareness of capabilities in general. Executing a file, with associated filesystem capabilities, the kernel will implicitly reset the ambient set of the process. Further, changes to the inheritable set by the program code without explicitly fixing up the ambient set can also drop ambient bits.

**cap\_get\_secbits**() returns the securebits of the calling process. These bits affect the way in which the calling process implements things like setuid-root fixup and ambient capabilities.

**cap\_set\_secbits**() attempts to modify the securebits of the calling process. Note **CAP\_SETPCAP** must be in the effective capability set for this to be effective. Some settings lock the sub-states of the securebits, so attempts to set values may be denied by the kernel even when the **CAP\_SETPCAP** capability is raised.

To help manage the complexity of the securebits, libcap provides a combined securebit and capability set concept called a libcap mode. **cap\_get\_mode**() attempts to summarize the prevailing security environment in the form of a numerical **cap\_mode\_t** value. A text representation of the mode can be obtained via the **cap\_mode\_name**() function. The vast majority of combinations of these values are not well defined in terms of a libcap mode, and for these states **cap\_get\_mode**() returns (**cap\_mode\_t**)0 which **cap\_get\_name**() identifies as \`\`*UNCERTAIN*''. Supported modes are: **CAP\_MODE\_NOPRIV**, **CAP\_MODE\_HYBRID**, **CAP\_MODE\_PURE1E** and  **CAP\_MODE\_PURE1E\_INIT**.

**cap\_prctl**() can be used to read state via the **prctl***()* **system call.**

**cap\_prctlw**() **can be used to write state via the prctl***()* **system call.**

**cap\_set\_mode**() **can be used to set the desired mode. The permitted capability** **CAP\_SETPCAP** **is required for this function to succeed.**

**cap\_setuid**() **is a convenience function for the** [**setuid**(2)](/setuid/2.md) **system call. Where** **cap\_setuid**() **arranges for the right effective capability to be raised in order to** **perform the system call, and also arranges to preserve the** **availability of permitted capabilities after the uid has** **changed. Following this call all effective capabilities are lowered.**

**cap\_setgroups**() **is a convenience function for performing both** [**setgid**(2)](/setgid/2.md) **and** [**setgroups**(2)](/setgroups/2.md) **calls in one call. The** **cap\_setgroups**() **call raises the right effective capability for the duration of the** **call, and empties the effective capability set before returning.**

## RETURN VALUE

**The functions** **cap\_get\_proc**() **and** **cap\_get\_pid**() **return a non-NULL value on success, and NULL on failure.**

**The function** **cap\_get\_bound**() **returns -1 if the requested capability is unknown, otherwise the** **return value reflects the current state of that capability in the** **prevailing bounding set. Note, a macro function,**

**The all of the setting functions such as** **cap\_set\_proc**() **and** **cap\_drop\_bound**() **return zero for success, and -1 on failure.**

**On failure,** *errno* **is set to** **EINVAL**, **EPERM**, **or** **ENOMEM**.

## CONFORMING TO

**cap\_set\_proc**() **and** **cap\_get\_proc**() **are specified in the withdrawn POSIX.1e draft specification.** **cap\_get\_pid**() **is a Linux extension.**

## NOTES

**Neither glibc, nor the Linux kernel honors POSIX semantics for setting** **capabilities and securebits in the presence of pthreads. That is,** **changing capability sets, by default, only affect the running** **thread. To be meaningfully secure, however, the capability sets should** **be mirrored by all threads within a common program because threads are** **not memory isolated. As a workaround for this,** **libcap** **is packaged with a separate POSIX semantics system call library:** **libpsx**. **If your program uses POSIX threads, to achieve meaningful POSIX** **semantics capability manipulation, you should link your program with:**

**ld ... -lcap $(pkg-config --libs --cflags libpsx)**

**or,**

**gcc ... -lcap $(pkg-config --libs --cflags libpsx)**

**When linked this way, due to linker magic, libcap uses** [**psx\_syscall**(3)](/psx_syscall/3.md) and [**psx\_syscall6**(3)](/psx_syscall6/3.md) **to perform state setting system calls. Notably, this also ensures that** **cap\_prctlw***()* **can be used to ensure process control bits are shared over all threads** **of a single process.**

### capgetp() and capsetp()

**The library also supports the deprecated functions:**

**int capgetp(pid\_t** *pid***, cap\_t** *cap\_d***);**

**int capsetp(pid\_t** *pid***, cap\_t** *cap\_d***);**

**capgetp**() **attempts to obtain the capabilities of some other process; storing the** **capabilities in a pre-allocated** *cap\_d*. **See** **cap\_init**() **for information on allocating an empty capability set. This function** **is deprecated; you should use** **cap\_get\_pid**().

**capsetp**() **attempts to set the capabilities of the calling process or of** **some other process(es),** *pid*. **Note that setting capabilities of another process is only possible on older** **kernels that do not provide VFS support for setting file capabilities.** **See** [**capset**(2)](/capset/2.md) **for information on which kernels provide such support.**

**If** *pid* **is positive it refers to a specific process;  if it is zero, it refers** **to the calling process; -1 refers to all processes other than the** **calling process and process '1' (typically**  [**init**(8)](/init/8.md)); **other negative values refer to the** *-pid* **process group.**

**In order to use this function, the kernel must support** **it and the calling process must have** **CAP\_SETPCAP** **raised in its Effective capability set. The capabilities set in the** **target process(es) are those contained in** *cap\_d*.

**Kernels that support filesystem capabilities redefine the semantics of** **CAP\_SETPCAP** **and on such systems,** **capsetp**() **will always fail for any target not** **equal to the calling process.** **capsetp**() **returns zero for success, and -1 on failure.**

**On kernels where it is (was) supported,** **capsetp**() **should be used with care.  It existed, primarily, to overcome an early** **lack of support for capabilities in the filesystems supported by** **Linux.  Note that on older kernels where** **capsetp**() **could be used to set the capabilities of another process,** **the only processes that had** **CAP\_SETPCAP** **available to them by default were processes started as kernel threads.** **(Typically this includes** [**init**(8)](/init/8.md), **kflushd and kswapd.) A kernel recompilation was needed to modify** **this default.**

## EXAMPLE

**The code segment below raises the** **CAP\_FOWNER** **and** **CAP\_SETFCAP** **effective capabilities for the caller:**

```
    ...
    cap_t caps;
    const cap_value_t cap_list[2] = {CAP_FOWNER, CAP_SETFCAP};
    if (!CAP_IS_SUPPORTED(CAP_SETFCAP))
        /* handle error */
    caps = cap_get_proc();
    if (caps == NULL)
        /* handle error */;
    if (cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_list, CAP_SET) == -1)
        /* handle error */;
    if (cap_set_proc(caps) == -1)
        /* handle error */;
    if (cap_free(caps) == -1)
        /* handle error */;
    ...
```

**Alternatively, to completely drop privilege in a program launched** **setuid-root but wanting to run as a specific user ID etc. in such a** **way that neither it, nor any of its children can acquire privilege** **again:**

```
    ...
    uid_t nobody = 65534;
    const gid_t groups[] = {65534};
    if (cap_setgroups(groups[0], 1, groups) != 0)
        /* handle error */;
    if (cap_setuid(nobody) != 0)
        /* handle error */;
    /*
     * privilege is still available here
     */
    if (cap_set_mode(CAP_MODE_NOPRIV) != 0)
        /* handle error */
    ...
```

**Note, the above sequence can be performed by the** **capsh** **tool as follows:**

**sudo capsh --user=nobody --mode=NOPRIV --print**

**where** **--print** **displays the resulting privilege state.**

## SEE ALSO

[**libcap**(3)](/libcap/3.md), [**libpsx**(3)](/libpsx/3.md), [**capsh**(1)](/capsh/1.md), [**cap\_clear**(3)](/cap_clear/3.md), [**cap\_copy\_ext**(3)](/cap_copy_ext/3.md), [**cap\_from\_text**(3)](/cap_from_text/3.md), [**cap\_get\_file**(3)](/cap_get_file/3.md), [**cap\_init**(3)](/cap_init/3.md), [**namespaces**(7)](/namespaces/7.md), [**pid\_namespaces**(7)](/pid_namespaces/7.md), [**user\_namespaces**(7)](/user_namespaces/7.md), [**psx\_syscall**(3)](/psx_syscall/3.md), [**capabilities**(7)](/capabilities/7.md).
