Linux 来自 kzalloc 的 memset 中的内核 NULL 指针取消引用

Linux kernel NULL-pointer dereference in memset from kzalloc

偶然在内核丛林中偶然发现了一些代码并且有点困惑。 kzalloc()有两种实现方式:在tools/virtio/linux/kernel.h and the main one in linux/slab.h中。显然,大多数情况下使用第二种。但有时会使用 "virtio" kzalloc()

"virtio" kzalloc() 看起来像这样:

static inline void *kzalloc(size_t s, gfp_t gfp)
{
    void *p = kmalloc(s, gfp);

    memset(p, 0, s);
    return p;
}

我的困惑是 "fake" kmalloc() 在 "tools" 目录中使用可以 return NULL 指针。此外,memset() 实现似乎不检查 NULL 指针,因此可能存在 NULL 指针取消引用。 是错误还是我遗漏了什么?

header主要用于用户空间测试,如virtio_test.

来自 tools/virtio/virtio_test.c 的 git-日志:

This is the userspace part of the tool: it includes a bunch of stubs for linux APIs, somewhat simular to linuxsched. This makes it possible to recompile the ring code in userspace.

A small test example is implemented combining this with vhost_test module.

所以是的,代码有点不安全(干净的编码会在 memset() 之前测试 NULL 指针并通过适当的错误消息退出),但由于它只是一个测试工具,跳过这个测试似乎被认为是不重要的。

是的,这看起来确实像一个错误。

tools/子目录是用户space工具的集合(顾名思义)。您还可以从包含多个 C 标准库头文件的事实中看出这一点。所以这当然不是内核错误(那将是非常糟糕的),只是 virtio 测试工具中的一个小疏忽。

那个 virtio 测试工具似乎重新定义了一些内核 API 来模拟它们在用户 space 中的行为。该功能虽然似乎从未在实践中使用过,但只是定义了。

marco:~/git/linux/tools/virtio$ grep -r kzalloc
linux/kernel.h:static inline void *kzalloc(size_t s, gfp_t gfp)
ringtest/ptr_ring.c:static inline void *kzalloc(unsigned size, gfp_t flags)
marco:~/git/linux/tools/virtio$

它可能是供希望在用户中测试一些 virtio 内核代码的人使用的space。


无论如何,你可以试试reporting the bugget_mantainer.pl 脚本建议:

$ perl scripts/get_maintainer.pl -f tools/virtio/linux/kernel.h
Bad divisor in main::vcs_assign: 0
"Michael S. Tsirkin" <mst@redhat.com> (maintainer:VIRTIO CORE AND NET DRIVERS)
Jason Wang <jasowang@redhat.com> (maintainer:VIRTIO CORE AND NET DRIVERS)
virtualization@lists.linux-foundation.org (open list:VIRTIO CORE AND NET DRIVERS)
linux-kernel@vger.kernel.org (open list)