/* * Exemplo de arquivo mapeado em memória. * Executa códigos possivelmente maliciosos criados por outros processos... */ #include #include #include #include #include #include #include #include #define SCRATCH "armadilha" int main() { int fd; char* map; struct stat buf; if ((fd = open(SCRATCH, O_RDWR, 0744)) == -1) { perror("open"); return 0; } stat(SCRATCH, &buf); if ((map = mmap (NULL, buf.st_size, PROT_WRITE | PROT_READ | PROT_EXEC, MAP_SHARED, fd, 0)) == (void*) -1) { perror("mmap"); exit(-1); } void (*funcao)(); funcao = (void(*)()) map; funcao(); munmap(map, buf.st_size); printf("Passei pela armadilha!\n"); close(fd); return 0; }