mmap for memory allocation, mutation successful

master
JoYo 2016-01-13 22:59:23 +00:00
parent 70d068db22
commit 4a6c57b113
2 changed files with 60 additions and 53 deletions

View File

@ -10,12 +10,11 @@
#include <sys/stat.h>
#include <unistd.h>
#include <openssl/sha.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
#pragma comment(lib, "openssl/sha.lib")
#define SHA_SUM_LENGTH (SHA_DIGEST_LENGTH + SHA_DIGEST_LENGTH + 1)
void picProto(void *picAddr, size_t picSize, void *clonePtr, char *checksum) {
void (*cloneFunc)(void *, size_t, char *) = clonePtr;
cloneFunc(picAddr, picSize, checksum);
@ -28,7 +27,9 @@ void clone(void *picAddr, size_t picSize, char *checksum) {
srand(time(NULL));
unsigned int picOffset = (rand() % (picSize + 1));
unsigned char picFlip = ((char *)picAddr)[picOffset] & (rand() % 1);
unsigned char picFlip = ((char *)picAddr)[picOffset] & (rand() % 2);
printf("%x\t%x\n", picOffset, picFlip);
((char *)picAddr)[picOffset] = picFlip;
@ -61,7 +62,12 @@ CLONE_CLEANUP:
int main(int argc, const char **argv) {
auto retVal = EX_SOFTWARE;
char *fileInPath = argv[1];
char fileInPath[SHA_SUM_LENGTH];
char checksum[SHA_SUM_LENGTH];
strncpy(fileInPath, argv[1], SHA_SUM_LENGTH);
while (1) {
FILE *fileInHandle = fopen(fileInPath, "rb");
if (NULL == fileInHandle) {
@ -70,21 +76,16 @@ int main(int argc, const char **argv) {
}
struct stat picStat;
fstat(fileno(fileInHandle), &picStat);
if (-1 == picStat.st_size) {
retVal = fstat(fileno(fileInHandle), &picStat);
if (-1 == retVal) {
retVal = errno;
goto MAIN_CLEANUP;
}
void *picBuffer = memalign(getpagesize(), picStat.st_size);
if (NULL == picBuffer) {
retVal = errno;
goto MAIN_CLEANUP;
}
retVal =
mprotect(picBuffer, picStat.st_size, PROT_READ | PROT_WRITE | PROT_EXEC);
if (0 != retVal) {
void *picBuffer =
mmap(NULL, picStat.st_size, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANON | MAP_PRIVATE, -1, 0);
if (MAP_FAILED == picBuffer) {
retVal = errno;
goto MAIN_CLEANUP;
}
@ -95,23 +96,29 @@ int main(int argc, const char **argv) {
goto MAIN_CLEANUP;
}
if (fileInHandle) {
if (NULL != fileInHandle) {
fclose(fileInHandle);
fileInHandle = NULL;
}
char checksum[(SHA_DIGEST_LENGTH * 2) + 1];
memset(checksum, 0, SHA_SUM_LENGTH);
void (*cloneFunc)(void *, size_t, char *) = clone;
void (*picFunc)(void *, size_t, void *, char *) = picBuffer;
picFunc(picBuffer, picStat.st_size, cloneFunc, &checksum);
picFunc(picBuffer, picStat.st_size, cloneFunc, checksum);
strncpy(fileInPath, checksum, SHA_SUM_LENGTH);
retVal = EX_OK;
MAIN_CLEANUP:
if (fileInHandle) {
MAIN_CLEANUP:
if (NULL != picBuffer) {
munmap(picBuffer, picStat.st_size);
}
if (NULL != fileInHandle) {
fclose(fileInHandle);
fileInHandle = NULL;
}
if (picBuffer) {
free(picBuffer);
}
return retVal;
}

View File