About mprotect
mprotect is a system call that assigns a particular permission to memory pages. The permissions can be READ, WRITE, or EXEC.
It’s often used in conjunction with mmap, which can allocate memory for you similarly to malloc.
The reason mprotect exists is, as described above, to manipulate the
memory permissions.
But why would we want to manipulate those permissions in the first place?
Here are a couple of use cases:
Protect from buffer overflows
We can allocate memory for, let’s say, user input and then append a PROT_NONE
memory page at the end of it. PROT_NONE means no permissions, as you might have
guessed, so any access to this memory (even for reading purposes) will
trigger an error.
In this case, if too much data gets into the buffer, the program is just killed
(by default).
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
int page = getpagesize();
// Allocate 2 pages: [ writable page ][ guard page ]
char *mem = mmap(NULL, page * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
mprotect(mem + page, page, PROT_NONE); // Mark second page with PROT_NONE
// Point *buf to the end of the first page
// 5 bytes before the second page starts
char *buf = mem + page - 5;
// 3 chars + \n + \0 == magic constant 5 from above
printf("Enter up to 3 chars (4+ will overflow into PROT_NONE): ");
fgets(buf, 1024, stdin);
printf("OK, you entered: %s", buf); // you won't see it if more than 3 chars are entered.
return 0;
}
Locking config data as ReadOnly
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
size_t size = getpagesize();
// Allocate memory for the config variable below.
int *config = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
// Write/load config data.
// Usually it's JSON here or some C struct, but
// we can have just a number.
*config = 42;
// Mark the whole config as read only.
mprotect(config, size, PROT_READ);
// Prove we can read it. No problem.
printf("Locked config: %d\n", *config);
// This will crash the program.
*config = 99;
return 0;
}
Why not just control the access in the code
When I saw mprotect, I didn’t fully get it because why would you mark memory with special permissions using this call instead of just using that memory in the way you intend it. As in, just never write to a memory block to make it ReadOnly.
This, of course, doesn’t work well because when the source code grows, someone (or maybe even you) will write into that memory by accident. Or, even worse, some bad actor can overwrite the memory for you.
Abstractions can solve this to some degree. Look at the example above where we created the config variable. We can agree, as engineers working on the code, that we never touch config directly and use the read_config function only. By not having a write_config function, we sort of protect ourselves from accidentally writing into it.
But the whole point of mprotect is to actually signal to the CPU (and its Memory Management Unit) that a memory block is ReadOnly, and CPU will reject the access for you. You don’t need to check anything. Despite being a much quicker way of checking the boundaries, it’s also the same abstraction layer I mentioned above, just in a different form.
Think of it as a schema definition for your database, where instead of defining
column types like
CREATE TABLE table_name (column1 INT);
you define protection modes like
mprotect(config, size, PROT_READ);
Send me an email and we can talk about this post.