Skip to content

Instantly share code, notes, and snippets.

@chrislattman
Last active June 27, 2024 18:25
Show Gist options
  • Save chrislattman/205eb38ae5f8c07151266208f6f2e1a4 to your computer and use it in GitHub Desktop.
Save chrislattman/205eb38ae5f8c07151266208f6f2e1a4 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE // needed for MAP_ANONYMOUS
#include <stdio.h>
#include <sys/mman.h>
int main(void)
{
void *ptr = mmap(NULL, 24, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED) {
perror("mmap");
return 1;
}
int status = mprotect(ptr, 24, PROT_READ);
if (status != 0) {
perror("mprotect");
return 1;
}
status = munmap(ptr, 24);
if (status != 0) {
perror("munmap");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment