What is Pledge?
pledge is a system call.
Calling pledge in a program is to promise that the program will only use certain resources.
Another way of saying is to limit the operation of a program to its needs, e.g.,
"I pledge not to useopen any other ports except
port 63"new sockets"
"I pledge not to use any other system-call exceptlseek()only write temporary files, andfork()"not write other files"
How does it make a program more secure?
It limits the operation of a program. Example:
- You wrote a program named
xyzthat only needs thereadsystem-call. - Then you add
pledgeto use onlyreadbut nothing else. - Then a malicious user found out that in your program there is a vulnerability by which one can invoke a
rootshell. - Exploiting your program to open a
rootshell will result that the kernel will kill the process withSIGABRT(which cannot be caught/ignored) and generate a log (which you can find withdmesg).
It happens because before executing other codes of your program, it first pledge not to use anything other than read system call. But opening root shell will call several other system-calls which is forbidden because its already promised not to use any other but read.
Where is Pledge?
Its usually in a program. Usage from OpenBSD 6.5 man page:
#include <unistd.h> int pledge(const char *promises, const char *execpromises); Example Code: Example code of cat command from cat.c
........ #include <unistd.h> ........ int ch; if (pledge("stdio rpath", NULL) == -1) err(1, "pledge"); while ((ch = getopt(argc, argv, "benstuv")) != -1) ..........