Skip to content

Instantly share code, notes, and snippets.

@TheCleric
Created May 2, 2023 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheCleric/0811b9537af9a6418a683765462fd0b6 to your computer and use it in GitHub Desktop.
Save TheCleric/0811b9537af9a6418a683765462fd0b6 to your computer and use it in GitHub Desktop.
Reserve bad memory addresses before another app uses them
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
// 512 bytes of padding on each side
#define PADDING 0x200
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: badmem <badmemfile.csv>\n");
return 1;
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Could not find file: %s\n", argv[1]);
return 1;
}
char line_buffer[1024];
int result_code = 0;
while(fgets(line_buffer, 1024, fp) != NULL) {
long long address = strtoll(line_buffer, NULL, 16);
long long start = address - PADDING;
void *result = mmap((void *)start, PADDING * 2, PROT_NONE, MAP_ANON | MAP_SHARED, 0, 0);
if (result == MAP_FAILED) {
printf("Error executing mmap on address = %llx", address);
result_code = 1;
}
}
return result_code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment