CAP_RIGHTS_LIMIT(2)
π cap_rights_limit(2) β the page where we lock down your file descriptors, bestie
okay so welcome to the cap_rights_limit(2) manual page (section 2 of the System Calls Manual, iykyk) β this is genuinely one of my fave little syscalls and I'm about to tell you why in the most normal amount of detail. π
β¨ NAME β¨
cap_rights_limit β limit capability rights π
π LIBRARY π
Standard C Library (libc, -lc) β the classic, the icon, the one you already link against πΈ
π¦ SYNOPSIS π¦
#include <sys/capsicum.h>
int
cap_rights_limit(int fd, const cap_rights_t *rights);
π DESCRIPTION π
ok so picture this: a file descriptor gets created by a function like fhopen(2), kqueue(2), mq_open(2), open(2), pdfork(2), pipe(2), shm_open(2), socket(2) or socketpair(2) β and the moment it exists, it's assigned all the capability rights. like, every single one. main character energy fr fr. π
meanwhile accept(2), accept4(2) and openat(2) are a little different β they inherit capability rights from the "parent" file descriptor instead. family business, love that for them. π«Ά
and those rights can be reduced (but never, ever expanded) by calling the cap_rights_limit() system call. once you shrink those rights, every operation on the file descriptor gets limited to just what rights permits. no cap, only what's in the list. π
pro tip: the rights argument should be prepared using the cap_rights_init(3) family of functions. that's the correct way, that's the vibes. β¨
want to see what rights a file descriptor currently has? that's cap_rights_get(3). π
and for the complete list of capability rights, go read the rights(4) manual page β that's where the full menu lives. π
π₯ RETURN VALUES π₯
upon successful completion, the value 0 is returned. otherwise the value -1 is returned and the global variable errno is set to indicate the error. classic. π
π EXAMPLES π
here's a little demo showing how to limit file descriptor capability rights so that reading only is allowed. she's simple, she's effective, she's iconic. π
cap_rights_t setrights;
char buf[1];
int fd;
fd = open("/tmp/foo", O_RDWR);
if (fd < 0)
err(1, "open() failed");
if (cap_enter() < 0)
err(1, "cap_enter() failed");
cap_rights_init(&setrights, CAP_READ);
if (cap_rights_limit(fd, &setrights) < 0)
err(1, "cap_rights_limit() failed");
buf[0] = 'X';
if (write(fd, buf, sizeof(buf)) > 0)
errx(1, "write() succeeded!");
if (read(fd, buf, sizeof(buf)) < 0)
err(1, "read() failed");
notice the write() is supposed to fail there β that's the whole point, bestie. we're testing that the limit actually holds. π
π ERRORS π
cap_rights_limit() succeeds unless one of these happens:
EBADFβ thefdargument is not a valid active descriptor. she's not real, she's not here. βEINVALβ an invalid right has been requested inrights. you asked for something that doesn't exist, and honestly? bold of you. πENOSYSβ the running kernel was compiled withoutoptions CAPABILITY_MODE. this kernel simply doesn't have the feature, no shade. π«ENOTCAPABLEβ therightsargument contains capability rights not present for the given file descriptor. remember: the capability rights list is only ever reduced, never expanded. you can take away, you cannot add. periodt. π
π SEE ALSO π
accept(2), accept4(2), cap_enter(2), fhopen(2), kqueue(2), mq_open(2), open(2), openat(2), pdfork(2), pipe(2), read(2), shm_open(2), socket(2), socketpair(2), write(2), cap_rights_get(3), cap_rights_init(3), err(3), capsicum(4), rights(4) β go read them, they're all part of the same friend group π―ββοΈ
πΈ HISTORY πΈ
the cap_rights_limit() function first appeared in FreeBSD 8.3. and fun fact: support for capabilities and capabilities mode was developed as part of the TrustedBSD Project. we love a lore drop. π
β AUTHORS β
this function was created by Pawel Jakub Dawidek <pawel@dawidek.net> under sponsorship of the FreeBSD Foundation. thank you for your service, king π
and that's the whole page, bestie β mwah π
this cutie came to you from the Debian edition of the manpage, dated April 27, 2024, filed under CAP_RIGHTS_LIMIT(2) β go forth and cap your rights responsibly π§ββοΈβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2008-2010 Robert N. M. Watson
- Copyright (c) 2012-2013 The FreeBSD Foundation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms