I managed to avoid specialised functions, one problem however is that I seem to be unable to both modify memory and copy own memory, here's what I have for reading and modifying memory (the gasp_* stuff are just defines to either *64() or *() depending on the system features)
Code:
intptr_t proc_change_data(
int *err, proc_handle_t *handle,
intptr_t addr, void *src, size_t size ) {
intptr_t done;
#ifndef gasp_pwrite
gasp_off_t off;
#endif
errno = EXIT_SUCCESS;
if ( !handle ) {
if ( err ) *err = EINVAL;
ERRMSG( errno, "Invalid handle" );
return 0;
}
if ( handle->samepid ) {
(void)memmove( (void*)addr, src, size );
if ( err ) *err = errno;
if ( errno != EXIT_SUCCESS )
ERRMSG( errno, "Couldn't override VM" );
return size;
}
#ifdef gasp_pwrite
done = gasp_pwrite( handle->wrMemFd, src, size, addr );
if ( done > 0 ) {
if ( err ) *err = EXIT_SUCCESS;
return done;
}
if ( err ) *err = errno;
if ( errno == EIO )
return 0;
if ( errno != EXIT_SUCCESS )
ERRMSG( errno, "Couldn't override VM" );
return done;
#else
off = gasp_lseek( handle->wrMemFd, 0, SEEK_CUR );
if ( errno != EXIT_SUCCESS ) {
if ( err ) *err = errno;
ERRMSG( errno, "Couldn't seek VM address" );
return 0;
}
gasp_lseek( handle->wrMemFd, addr, SEEK_SET );
if ( errno != EXIT_SUCCESS ) {
if ( err ) *err = errno;
ERRMSG( errno, "Couldn't seek VM address" );
return 0;
}
done = gasp_write( handle->wrMemFd, src, size );
if ( errno != EXIT_SUCCESS ) {
if ( err ) *err = errno;
ERRMSG( errno, "Couldn't override VM" );
return 0;
}
gasp_lseek( handle->wrMemFd, off, SEEK_SET );
#endif
return done;
}
Edit:
Just for reference the output of my program:
Code:
./gasp.elf -D HELLO="WORLD"
gasp = 'gasp'
Found:
3B52 'gasp.elf'
rdMemFd = 5, wrMemFd = 6
Got address 0x7fca7b8805a8, gasp = 'gasp'
Got address 0x7fca7b8805b5, gasp = 'gasp'
Got address 0x7fca7b8805ee, gasp = 'gasp'
3B53 'private_gasp.el'
rdMemFd = -1, wrMemFd = -1
gasp = 'gasp'
The reason I kept printing the value of gasp is to confirm the change made it's way through