我们使用 Coverity 来检测代码中的漏洞。基本上这是代码片段:
static int vendor_request(
const struct OFPHDR *oh,
size_t length,
const struct OFPUTIL_MT **typep
)
{
const struct OFPSM *osr;
ovs_be32 vendor;
osr = (const struct OFPSM *) oh;
memcpy(&vendor, ((char*)osr + sizeof(struct OFPSM)), sizeof( vendor ));
if (vendor == htonl(VENDOR_A))
return (functionA(oh, typep));
if (vendor == htonl(VENDOR_B))
return (functionB(oh, length, typep));
else
return 0;
}
这里,
sizeof(struct OFPSM) = 12 bytes.
sizeof(struct OFPHDR) = 8 bytes.
覆盖 说:
CID xxxxx (#1 of 2): Out-of-bounds access (OVERRUN)
1. overrun-buffer-val: Overrunning struct type OFPHDR of 8 bytes by passing it to a function which accesses it at byte offset 12. Pointer osr indexed by constant 12U through dereference in call to memcpy.
基本上 struct OFPHDR 是 TCP 层之上的一个 PDU,它的大小是 8 个字节,但它可以根据它是什么类型的 OFP 消息而变化。Coverity 说我在字节偏移索引 12 处取消引用 *oh,这是出站访问索引。
但我不明白这个问题,因为我将 OFPHDR 类型转换为 12 字节的正确结构,然后取消引用它。那么,如何避免这个错误呢?