私たちは 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.
基本的に、構造体 OFPHDR は TCP 層の上の PDU です。サイズは 8 バイトですが、OFP メッセージのタイプによって異なります。Coverity によると、アウトバウンド アクセス インデックスであるバイト オフセット インデックス 12 で *oh を逆参照しています。
しかし、OFPHDRを12バイトの適切な構造に型キャストしてから逆参照しているため、問題がわかりません。では、このエラーを回避するにはどうすればよいでしょうか。