int capget(cap_user_header_t hdrp, cap_user_data_t datap);
int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
As of Linux 2.2,
the power of the superuser (root) has been partitioned into
a set of discrete capabilities.
Each thread has a set of effective capabilities identifying
which capabilities (if any) it may currently exercise.
Each thread also has a set of inheritable capabilities that may be
passed through an
call, and a set of permitted capabilities
that it can make effective or inheritable.
These two functions are the raw kernel interface for getting and
setting thread capabilities.
Not only are these system calls specific to Linux,
but the kernel API is likely to change and use of
these functions (in particular the format of the
types) is subject to extension with each kernel revision,
but old programs will keep working.
The portable interfaces are
if possible you should use those interfaces in applications.
If you wish to use the Linux extensions in applications, you should
use the easier-to-use interfaces
Now that you have been warned, some current kernel details.
The structures are defined as follows.
effective, permitted, inheritable
are bitmasks of the capabilities defined in
values are bit indexes and need to be bit-shifted before ORing into
the bit fields.
To define the structures for passing to the system call you have to use the
names because the typedefs are only pointers.
Kernels prior to 2.6.25 prefer
32-bit capabilities with version
and kernels 2.6.25+ prefer 64-bit capabilities with version
Note, 64-bit capabilities use
whereas 32-bit capabilities only use
Another change affecting the behavior of these system calls is kernel
support for file capabilities (VFS capability support).
This support is currently a compile time option (added in kernel 2.6.24).
calls, one can probe the capabilities of any process by specifying its
process ID with the
With VFS Capability Support
VFS Capability support creates a file-attribute method for adding
capabilities to privileged executables.
This privilege model obsoletes kernel support for one process
asynchronously setting the capabilities of another.
That is, with VFS support, for
calls the only permitted values for
are 0 or
which are equivalent.
Without VFS Capability Support
When the kernel does not support VFS capabilities,
calls can operate on the capabilities of the thread specified by the
when that is nonzero, or on the capabilities of the calling thread if
refers to a single-threaded process, then
can be specified as a traditional process ID;
operating on a thread of a multithreaded process requires a thread ID
of the type returned by
can also be: -1, meaning perform the change on all threads except the
or a value less than -1, in which case the change is applied
to all members of the process group whose ID is -pid.
On success, zero is returned.
On error, -1 is returned, and
is set appropriately.
The calls will fail with the error
and set the
to the kernel preferred value of
when an unsupported
value is specified.
In this way, one can probe what the current
preferred capability revision is.
Bad memory address.
must not be NULL.
may only be NULL when the user is trying to determine the preferred
capability version format supported by the kernel.
One of the arguments was invalid.
An attempt was made to add a capability to the Permitted set, or to set
a capability in the Effective or Inheritable sets that is not in the
The caller attempted to use
to modify the capabilities of a thread other than itself,
but lacked sufficient privilege.
For kernels supporting VFS
capabilities, this is never permitted.
For kernels lacking VFS
capability is required.
(A bug in kernels before 2.6.11 meant that this error could also
occur if a thread without this capability tried to change its
own capabilities by specifying the
field as a nonzero value (i.e., the value returned by
instead of 0.)