Skip to content

Instantly share code, notes, and snippets.

@compholio
Created June 9, 2015 21:36
Show Gist options
  • Save compholio/c7a7a774bf888d1467ad to your computer and use it in GitHub Desktop.
Save compholio/c7a7a774bf888d1467ad to your computer and use it in GitHub Desktop.
Update struct on memory read
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
struct region
{
int count;
char buf[10000];
int id;
};
int main ( int argc, char *argv[] )
{
struct sigaction act;
struct region *rptr;
pid_t child;
int fd;
fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (ftruncate(fd, 0) == -1) return -1;
rptr = mmap(NULL, sizeof(struct region), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (rptr == MAP_FAILED) return -1;
child = fork();
if(child == 0)
{
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
fprintf(stderr, "rptr: %d\n", rptr->count);
fprintf(stderr, "rptr: %d\n", rptr->count);
fprintf(stderr, "rptr: %d %s\n", rptr->count, rptr->buf);
fprintf(stderr, "rptr: %d %s\n", rptr->count, rptr->buf);
fprintf(stderr, "rptr: %d\n", rptr->count);
fprintf(stderr, "rptr: %d 0x%x\n", rptr->count, rptr->id);
fprintf(stderr, "rptr: %d\n", rptr->count);
}
else
{
struct region mydata = { 0, "test tmp", 0xdeadbeef };
int status = ~0;
size_t i;
while(!WIFEXITED(status))
{
if(!WIFSTOPPED(status) || WSTOPSIG(status) != SIGBUS)
{
ptrace(PTRACE_CONT, child, NULL, NULL);
wait(&status);
continue;
}
mydata.count++;
lseek(fd, 0, SEEK_SET);
write(fd, &mydata, sizeof(mydata));
ptrace(PTRACE_SINGLESTEP, child, NULL, NULL);
wait(&status);
if (ftruncate(fd, 0) == -1) return -1;
ptrace(PTRACE_CONT, child, NULL, NULL);
wait(&status);
}
}
shm_unlink("/myregion");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment