CAP_GET_FILE(3)
π CAP_GET_FILE β capability manipulation on files π
okay bestie, sit down, hydrate, and let me tell you about the libcap file-capability girlies. this is CAP_GET_FILE(3) from the Linux Programmer's Manual and it's literally the whole "get/set capabilities on a file" friend group. no cap. π
β¨ NAME β¨
cap_get_file, cap_set_file, cap_get_fd, cap_set_fd, cap_get_nsowner, cap_set_nsowner β capability manipulation on files π¦
πΈ SYNOPSIS πΈ
#include <sys/capability.h>
cap_t cap_get_file(const char *path_p);
int cap_set_file(const char *path_p, cap_t cap_p);
cap_t cap_get_fd(int fd);
int cap_set_fd(int fd, cap_t caps);
uid_t cap_get_nsowner(cap_t caps);
int cap_set_nsowner(cap_t caps, uid_t rootuid);
Link with -lcap. (yes, you have to actually link it, it's not gonna manifest itself, bestie π
)
π«Ά DESCRIPTION π«Ά
So cap_get_file() and cap_get_fd() allocate a capability state in working storage and set it to represent the capability state of the pathname pointed to by path_p, or the file open on descriptor fd. They hand you back a pointer to that freshly created capability state. β¨
heads up: reading the capability state from any file that is not a regular file? the effects are undefined. so don't. π
And when you're done with that capability state in working storage, you should free any releasable memory by calling cap_free() with the used cap_t as an argument. clean up after yourself, queen. π§
Now, cap_set_file() and cap_set_fd() β they set the values for all capability flags for all capabilities, either for the pathname pointed to by path_p or the file open on descriptor fd, using the capability state identified by cap_p. the new capability state of the file is completely determined by the contents of cap_p. iconic. π
A NULL value for cap_p means "hey, delete the capabilities for this file." and for these functions to succeed, the calling process needs the CAP_SETFCAP capability in its effective set, and either the effective user ID of the process must match the file owner, or the calling process must have the CAP_FOWNER capability in its effective capability set. no shortcuts, bestie. π₯
same warning, different function: writing the capability state to any file type other than a regular file is undefined. don't test fate. π
A capability set held in memory can be associated with the root user ID in use in a specific user namespace. you can get and set this value (in the memory copy) with cap_get_nsowner() and cap_set_nsowner() respectively. π
The root user ID is ignored by the libcap library in all cases other than when the capability is written to a file. Only if the value is non-zero will the library attempt to include it in the written file capability set. that's the one and only moment it matters. periodt. π
β RETURN VALUE β
cap_get_file()andcap_get_fd()return a non-NULLvalue on success, andNULLon failure. πcap_set_file()andcap_set_fd()return zero on success, and-1on failure. π«
On failure, errno is set to one of: EACCES, EBADFD, ENAMETOOLONG, ENOENT, ENOMEM, ENOTDIR, EPERM, or EROFS. π
π CONFORMING TO π
These functions are specified by the withdrawn POSIX.1e draft specification. (withdrawn, bestie β it's giving vintage, it's giving archive-core π°οΈ)
π NOTES π
Support for file capabilities is provided on Linux since version 2.6.24. π§
On Linux, the file Effective set is a single bit. let that sink in. If it is enabled, then all Permitted capabilities are enabled in the Effective set of the calling process when the file is executed; otherwise, no capabilities are enabled in the process's Effective set following an execve(2). π€―
Because the file Effective set is a single bit, if any capability is enabled in the Effective set of the cap_t given to cap_set_file() or cap_set_fd(), then all capabilities whose Permitted or Inheritable flag is enabled must also have the Effective flag enabled. conversely, if the Effective bit is enabled on a file, then the cap_t returned by cap_get_file() and cap_get_fd() will have the Effective flag enabled for each capability that has the Permitted or Inheritable flag enabled. it's all or nothing, main character energy only. β¨
π₯ SEE ALSO π₯
libcap(3)cap_clear(3)cap_copy_ext(3)cap_from_text(3)cap_get_proc(3)cap_init(3)capabilities(7)user_namespaces(7)
and that's the whole page, bestie β written up on 2022-10-16, straight from the Linux Programmer's Manual, CAP_GET_FILE(3), and yes you read all of it, I'm so proud, slay πβ¨π
π¦ where this page comes from
- alpine 3.22: libcap-doc 2.78-r0 Β· BSD-3-Clause OR GPL-2.0-only
- alpine 3.23: libcap-doc 2.78-r0 Β· BSD-3-Clause OR GPL-2.0-only
- alpine 3.24: libcap-doc 2.78-r0 Β· BSD-3-Clause OR GPL-2.0-only
- arch rolling: libcap 2.78-1 Β· BSD-3-Clause OR GPL-2.0-only
- centos-stream 10: libcap-devel 2.69-7.el10 Β· BSD-3-Clause OR GPL-2.0-only
- debian trixie: libcap-dev 1:2.75-10+deb13u1+b3 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 43: libcap-devel 2.76-3.fc43 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 43: libcap-devel 2.76-3.fc43 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 43: libcap-devel 2.76-4.fc43 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 43: libcap-devel 2.76-4.fc43 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 44: libcap-devel 2.77-2.fc44 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 44: libcap-devel 2.77-2.fc44 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 44: libcap-devel 2.78-1.fc44 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 44: libcap-devel 2.78-1.fc44 Β· BSD-3-Clause OR GPL-2.0-only
- nixpkgs unstable: libcap 2.78
- opensuse-leap 16.0: libcap-devel 2.73-160000.3.1 Β· BSD-3-Clause OR GPL-2.0-only
- opensuse-leap 16.0: libcap-devel 2.73-160000.3.1 Β· BSD-3-Clause OR GPL-2.0-only
- opensuse-leap 16.0: libcap-devel 2.73-160000.3.1 Β· BSD-3-Clause OR GPL-2.0-only
- opensuse-leap 16.0: libcap-devel 2.73-160000.3.1 Β· BSD-3-Clause OR GPL-2.0-only
- ubuntu questing: libcap-dev 1:2.75-7ubuntu2
- ubuntu questing: libcap-dev 1:2.75-7ubuntu2.2
- ubuntu resolute: libcap-dev 1:2.75-10ubuntu2 Β· BSD-3-Clause OR GPL-2.0-only
- ubuntu stonking: libcap-dev 1:2.78-1ubuntu1 Β· BSD-3-Clause OR GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms