Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DenisBelmondo/28491ac19c70df42071e06a22aed214d to your computer and use it in GitHub Desktop.
Save DenisBelmondo/28491ac19c70df42071e06a22aed214d to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
struct page_region pr;
char *region = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
if (region == MAP_FAILED) {
puts("mmap failed");
exit(1);
}
int page_map = open("/proc/self/pagemap", O_RDONLY);
if (page_map < 0) {
printf("page_map was %d\n", page_map);
exit(1);
}
struct pm_scan_arg a = {
.size = sizeof (struct pm_scan_arg),
.flags = 0,
.start = (uintptr_t)region,
.end = (uintptr_t)region + 4096,
.vec = (uintptr_t)&pr,
.vec_len = 1,
.max_pages = 0,
.category_inverted = 0,
.category_mask = 0,
.category_anyof_mask = PAGE_IS_WRITTEN,
.return_mask = PAGE_IS_WRITTEN,
};
*(int *)region = 123;
int ioctl_result = ioctl(page_map, PAGEMAP_SCAN, &a);
perror("ioctl");
printf("%d, %d\n", ioctl_result, errno);
printf("%d\n", *(int *)region);
printf("0x%llx\n", pr.start);
if (close(page_map) < 0) {
puts("close failed");
exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment