CAP_RIGHTS_LIMIT(2)
β¨ CAP_RIGHTS_LIMIT(2) β¨
okay so bestie, we're doing a system call today β it lives in the System Calls Manual (that's the section 2 girlies π
) and it's called cap_rights_limit(2). yes it's giving serious security energy, no it's not scary, come sit with me π«Ά
π NAME π
cap_rights_limit β limit capability rights
lowkey the whole plot in four words. iconique.
π LIBRARY π
Standard C Library (libc, -lc)
so you just link -lc like always and she's there for you, no cap.
πΈ SYNOPSIS πΈ
#include <sys/capsicum.h>
int
cap_rights_limit(int fd, const cap_rights_t *rights);
two args, one int coming back, and <sys/capsicum.h> doing the heavy lifting β¨
π DESCRIPTION π
okay so here's the tea. when a file descriptor gets created by literally any of these besties β accept(2), accept4(2), fhopen(2), kqueue(2), mq_open(2), open(2), openat(2), pdfork(2), pipe(2), shm_open(2), socket(2) or socketpair(2) β it shows up holding all the capability rights. like, all of them. every single one. main character energy from birth π
those rights can be reduced by calling the cap_rights_limit() system call β and bestie, listen to me, never expanded. reduced only. the vibes only go one direction, and that direction is down π
once you've reduced the capability rights, everything you do on that file descriptor is limited to just the stuff rights permits. that's it. that's the boundary. respect the boundary π«Ά
the rights argument? you gotta prepare it with the cap_rights_init(3) family of functions. that's the pre-game routine, don't skip it.
and if you wanna see what capability rights a file descriptor already has, that's cap_rights_get(3). peek before you tweak, periodt.
the complete list of capability rights lives in the rights(4) manual page. go read her, she's thorough.
π¦ RETURN VALUES π¦
- on success, you get the value
0back β slay π - otherwise you get the value
-1back, and the global variableerrnogets set to tell you what the error was. the classic -1-and-errno combo, we've all been there π
π EXAMPLES π
here's a little demo showing how to limit file descriptor capability rights so that only reading is allowed. watch her work β¨
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");
see how she opens with O_RDWR, enters capabilities mode with cap_enter(), then slaps CAP_READ on there and expects the write() to blow up? that's the whole point bestie, we shrank the rights and now write can't sit with us π
π₯ 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, we can't work with this π[EINVAL]β an invalid right has been requested inrights. you asked for something that isn't a thing. not the move π[ENOTCAPABLE]β therightsargument contains capability rights that are not present for the given file descriptor. and bestie, say it with me: the capability rights list can only be reduced, never expanded. there is no glow-up option here, only glow-down π
π 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)
a whole friend group, honestly. all of them in the group chat π
π HISTORY π
the cap_rights_limit() function first appeared in FreeBSD 8.3. support for capabilities and capabilities mode was developed as part of the TrustedBSD Project. vintage, but make it secure π
π AUTHORS π
this function was created by Pawel Jakub Dawidek <pawel@dawidek.net> under sponsorship of the FreeBSD Foundation. giving credit where credit is due, always π«Ά
and that's the page bestie!! this one came from Debian, dated May 5, 2020, and it's the CAP_RIGHTS_LIMIT(2) page from the System Calls Manual π go forth, limit those rights, never expand them, and stay capsicum-pilled β¨π¦πΈ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2008-2010 Robert N. M. Watson
- Copyright (c) 2012-2013 The FreeBSD Foundation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms