When developing a program that uses/requires specific capabilities (e.g. cap_net_raw), what is the recommended method for integrating the process of granting capabilities into the build process?
If I've understood the capabilities inheritance correctly, in order for the build system (e.g. CMake) to be able to invoke setcap to modify the capabilities of the build outputs, it would have to have the cap_setfcap capability itself, or be ran with sudo. And same for the parent context, be it a shell (e.g. bash) or an IDE (e.g. VS Code).
The scheme above works nicely for debugging, e.g. to debug a program that requires cap_X, I needed to give the same capability to gdb and configure it to NOT start the program in a shell (to avoid having to give the same capability to bash; I.e. the method outlined in this excellent thread: gdb appears to ignore executable capabilities)
Now, my obvious first choice would be to give the build system ( CMake) the cap_setfcap capability so that it can give the required capabilities to the build targets. This doesn't feel like proper solution, but more like an attempt to circumvent the whole capabilities system/framework, instead of operating within it.
Invoking setcap using sudo from the build also sounds like a bad idea, because it doesn't work nicely when invoking builds from within an IDE.
I figured I could add the following into sudo configuration:
<my-username-here> ALL=(ALL) NOPASSWD: /sbin/setcap
But this (use of sudo) also feels like a workaround and not a proper solution.
Make a copy of the
setcapprogram. Make it executable only by you (or your build user) and give it theCAP_SETFCAPcapability.This binary is dangerous to have around, but you seem to be contemplating sudo with no password so this is equivalent.
Another method is to use an inheritable file capability:
which will place you in a shell where
./build-setcapappears to be privileged. The last line causes a shell to start that has been granted an inheritable process capability. This is what unlocks the file-inheritable capability.