have u ever been so sloppy as to allocate the same shared memory region multiple times, but with different page protections
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/mman.h>#include <sys/stat.h> #include <sys/types.h>
/*
the mem u get
0x7ffff796e000 0x7ffff7a6e000 r-xp 100000 0 /dev/shm/ipc 0x7ffff7a6e000 0x7ffff7b6e000 rw-p 100000 0 /dev/shm/ipc 0x7ffff7b6e000 0x7ffff7c6e000 r-xp 100000 0 /SYSVffffffff (deleted) 0x7ffff7c6e000 0x7ffff7d6e000 rw-p 100000 0 /SYSVffffffff (deleted)
info leakz:
$ ipcs --human -m
------ Shared Memory Segments --------key shmid owner perms size nattch status 0x00000000 753665 user 600 4M 2 dest 0x00000000 32772 user 600 4M 2 dest 0x00000000 458761 user 600 4M 2 dest 0x78010245 753703 user 777 1M 0
This tool parses /proc/sysvipc/shm
$ cat /proc/sysvipc/shm key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap 0 753665 1600 4194304 2910 1104205 2 1000 1000 1000 1000 1629106017 1629106017 1628959863 954368 0 0 32772 1600 4194304 40240 1105580 2 1000 1000 1000 1000 1629107109 1629107109 1628538418 524288 0 0 458761 1600 4194304 643918 1486 2 1000 1000 1000 1000 1628788417 0 1628788417 524288 02013332037 753703 777 1048576 868317 1105644 0 1000 1000 1000 1000 1629107168 1629107168 1629016773 4096 0
*/
int alloc_shmget(unsigned int len, unsigned char ** writeable, unsigned char ** executable) { key_t shm_key= ftok("xoxo", 'x');
int shm_id = shmget(shm_key, len, IPC_CREAT | 0777); *writeable = (unsigned char *) shmat(shm_id, NULL, 0); *executable = (unsigned char *) shmat(shm_id, NULL, SHM_EXEC | SHM_RDONLY);}
/*Side effect: this will create a file named 'ipc' under /dev/shm.
-rw------- 1 user user 1048576 Aug 16 10:57 ipc
*/int alloc_shm_open(unsigned int len, unsigned char ** writeable, unsigned char ** executable) { int fd = shm_open("/ipc", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
// set the size of the shared memory object: if (ftruncate(fd, len) == -1) { return 0; }
*writeable = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd,0); *executable = mmap(0, len, PROT_READ|PROT_EXEC , MAP_SHARED, fd,0);}
int main() { unsigned char * executable, *writeable;
alloc_shmget(1024 * 1024, &writeable, &executable);
// or
alloc_shm_open(1024 * 1024, &writeable, &executable);
*writeable = 0xcc;
printf("ptrz - %p %p %x\n", writeable, executable, *executable);
void (*f)(); f = (void *) executable; f();
return 0;}