Skip to content

Instantly share code, notes, and snippets.

@lorenzo-stoakes
Created November 3, 2023 14:42
Show Gist options
  • Save lorenzo-stoakes/30b3df4e715711d9fabb0108222544c7 to your computer and use it in GitHub Desktop.
Save lorenzo-stoakes/30b3df4e715711d9fabb0108222544c7 to your computer and use it in GitHub Desktop.
mmap F_SEAL_WRITE memfd read-only
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
int main()
{
const long page_size = sysconf(_SC_PAGESIZE);
int fd = memfd_create("test", MFD_ALLOW_SEALING);
char *buf;
if (fd == -1) {
perror("memfd_create");
return EXIT_FAILURE;
}
write(fd, "test", sizeof("test"));
if (fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE) == -1) {
perror("fcntl");
return EXIT_FAILURE;
}
buf = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
return EXIT_FAILURE;
}
close(fd);
printf("%s\n", buf);
munmap(buf, page_size);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment