cap_get_proc






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_setgroups − capability manipulation on
processes

#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

, cap_flag_value_t int cap_set_ambient(cap_value_t cap

intcap_reset_ambient(void);

CAP_AMBIENT_SUPPORTED();

unsigned cap_get_secbits(void);

);int cap_set_secbits(unsigned bits

cap_mode_t cap_get_mode(void);


);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

, size_t int cap_setgroups(gid_t gid

Link with ‐lcap.

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 with the
cap_t as an argument.











                             ‐2‐


     sets the values for all capability flags for all
capabilities to the capability state identified by 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.

     returns see with the process capabilities of the
process indicated by This information can also be obtained
from the /proc/<pid>/status file.

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

     can be used to lower the specified bounding set
capability, To complete successfully, the prevailing
effective capability set must have a raised

     returns the prevailing value of the specified ambient
capability, or ‐1 if the capability is not supported by the
running kernel.  A macro uses this function to determine if
ambient capabilities are supported by the kernel.

     sets the specified ambient capability to a specific
value. To complete successfully, the prevailing effective
capability set must have a raised Further, to raise a
specific ambient capability the sets of the current process
must contain the specified capability, and raised ambient
bits will only be retained as long as this remains true.

     resets all of the ambient capabilities for the current
process to their lowered value. To complete successfully,
the prevailing effective capability set must have a raised
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. Also, changes to the
inheritable set by the program code without explicitly
fixing up the ambient set can also drop ambient bits.

     returns the securebits of the current process. These
bits affect the way in which the current process implements
things like setuid‐root fixup and ambient capabilities.

     attempts to modify the securebits of the current
process. Note CAP_SETPCAP must be in the effective









                             ‐3‐


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.  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 function. The vast majority of combinations of these
values are not well defined in terms of a libcap mode, and
for these states returns which declares as Supported modes
are:

     can be used to set the desired mode. The permitted
capability CAP_SETPCAP is required for this function to
succeed.

     is a convenience function for the system call. Where
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.

     is a convenience function for performing both and calls
in one call. The call raises the right effective capability
for the duration of the call, and empties the effective
capability set before returning.

The functions and return a non‐NULL value on success, and
NULL on failure.

     The function 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 and return
zero for success, and −1 on failure.

     On failure, errno is set to or

and are specified in the withdrawn POSIX.1e draft
specification.  is a Linux extension.

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









                             ‐4‐


packaged with a separate POSIX semantics system call
library: If your program uses POSIX threads, to achieve
meaninful POSIX sematics capability manipulation, you should
link your program with:

‐lcapld...

or,

‐lcapgcc...

When linked this way, due to linker magic, libcap uses to
perform state setting system calls.

     The library also supports the deprecated functions:

     , cap_t int capgetp(pid_t pid

     , cap_t int capsetp(pid_t pid

     attempts to obtain the capabilities of some other
process; storing the capabilities in a pre‐allocated for
information on allocating an empty capability set. This
function, is deprecated, you should use

     attempts to set the capabilities of some other
process(es), If pid is positive it refers to a specific
process;  if it is zero, it refers to the current process;
‐1 refers to all processes other than the current process
and process ’1’ (typically other negative values refer to
the ‐pid process group.  In order to use this function, the
kernel must support it and the current process must have
CAP_SETPCAP raised in its Effective capability set. The
capabilities set in the target process(es) are those
contained in Kernels that support filesystem capabilities
redefine the semantics of CAP_SETPCAP and on such systems
this function will always fail for any target not equal to
the current process.  returns zero for success, and −1 on
failure.

Where supported by the kernel, the function 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, by default, the only processes that have
CAP_SETPCAP available to them are processes started as a
kernel thread.  (Typically this includes kflushd and
kswapd). You will need to recompile the kernel to modify
this default.

The code segment below raises the CAP_FOWNER and CAP_SETFCAP
effective capabilities for the caller:

    ...
    cap_t caps;









                             ‐5‐


    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:

‐‐user=nobodysudo/sbin/capsh

where ‐‐print displays the resulting privilege state.