Memory protection

An application dynamically allocates, say, four pages of memory. By default, this memory is both readable and writable; we refer to these as the memory protections on the page.

Wouldn't it be nice if the application developer could dynamically modify memory protections on a per-page basis? For example, keep the first page with default protections, make the second page read-only, the third page read+execute, and on the fourth page, not allow any kind of access (a guard page, perhaps?).

Well, this feature is precisely what the mprotect(2) system call is designed for. Let's delve into how we can exploit it to do all that. Here is its signature:

#include <sys/mman.h>int mprotect(void *addr, size_t len, int prot);

It's really ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.