libbpf: Handle size overflow for ringbuf mmap
authorHou Tao <houtao1@huawei.com>
Wed, 16 Nov 2022 07:23:49 +0000 (15:23 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 8 Dec 2022 10:28:39 +0000 (11:28 +0100)
commit0140e079a42064680394fff1199a7b5483688dec
tree43c7b02203cca4ef7ad364f705c0d138beaa8580
parent06d5790e7df56a506d76ee69e4145eb2054d91a8
libbpf: Handle size overflow for ringbuf mmap

[ Upstream commit 927cbb478adf917e0a142b94baa37f06279cc466 ]

The maximum size of ringbuf is 2GB on x86-64 host, so 2 * max_entries
will overflow u32 when mapping producer page and data pages. Only
casting max_entries to size_t is not enough, because for 32-bits
application on 64-bits kernel the size of read-only mmap region
also could overflow size_t.

So fixing it by casting the size of read-only mmap region into a __u64
and checking whether or not there will be overflow during mmap.

Fixes: bf99c936f947 ("libbpf: Add BPF ring buffer support")
Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20221116072351.1168938-3-houtao@huaweicloud.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/lib/bpf/ringbuf.c