* [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer
@ 2022-11-16 7:23 Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 1/4] libbpf: Use page size as max_entries when probing ring buffer map Hou Tao
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Hou Tao @ 2022-11-16 7:23 UTC (permalink / raw)
To: bpf, Andrii Nakryiko, David Vernet
Cc: Martin KaFai Lau, Song Liu, Hao Luo, Yonghong Song,
Alexei Starovoitov, Daniel Borkmann, KP Singh, Stanislav Fomichev,
Jiri Olsa, John Fastabend, houtao1
From: Hou Tao <houtao1@huawei.com>
Hi,
The patch set tries to fix the problems found when testing ringbuf by
using 4KB and 2GB size. Patch 1 fixes the probe of ring buffer map on
host with 64KB page (e.g., an ARM64 host). Patch 2 & 3 fix the overflow
of length when mmaping 2GB kernel ringbuf or user ringbuf on libbpf.
Patch 4 just reject the reservation with invalid size.
Please see individual patch for details. And comments are always
welcome.
Change Log:
v2:
* patch 1: use page size instead of adjust_ringbuf_sz(4096) as main_entries (suggested by Stanislav)
* patch 2 & 3: use "mmap_sz" instead of "ro_size/wr_size" as name of mmap length (From Andrii)
v1: https://lore.kernel.org/bpf/20221111092642.2333724-1-houtao@huaweicloud.com
Hou Tao (4):
libbpf: Use page size as max_entries when probing ring buffer map
libbpf: Handle size overflow for ringbuf mmap
libbpf: Handle size overflow for user ringbuf mmap
libbpf: Check the validity of size in user_ring_buffer__reserve()
tools/lib/bpf/libbpf_probes.c | 2 +-
tools/lib/bpf/ringbuf.c | 26 ++++++++++++++++++++++----
2 files changed, 23 insertions(+), 5 deletions(-)
--
2.29.2
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf v2 1/4] libbpf: Use page size as max_entries when probing ring buffer map
2022-11-16 7:23 [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer Hou Tao
@ 2022-11-16 7:23 ` Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 2/4] libbpf: Handle size overflow for ringbuf mmap Hou Tao
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Hou Tao @ 2022-11-16 7:23 UTC (permalink / raw)
To: bpf, Andrii Nakryiko, David Vernet
Cc: Martin KaFai Lau, Song Liu, Hao Luo, Yonghong Song,
Alexei Starovoitov, Daniel Borkmann, KP Singh, Stanislav Fomichev,
Jiri Olsa, John Fastabend, houtao1
From: Hou Tao <houtao1@huawei.com>
Using page size as max_entries when probing ring buffer map, else the
probe may fail on host with 64KB page size (e.g., an ARM64 host).
After the fix, the output of "bpftool feature" on above host will be
correct.
Before :
eBPF map_type ringbuf is NOT available
eBPF map_type user_ringbuf is NOT available
After :
eBPF map_type ringbuf is available
eBPF map_type user_ringbuf is available
Signed-off-by: Hou Tao <houtao1@huawei.com>
---
tools/lib/bpf/libbpf_probes.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
index f3a8e8e74eb8..d504d96adc83 100644
--- a/tools/lib/bpf/libbpf_probes.c
+++ b/tools/lib/bpf/libbpf_probes.c
@@ -234,7 +234,7 @@ static int probe_map_create(enum bpf_map_type map_type)
case BPF_MAP_TYPE_USER_RINGBUF:
key_size = 0;
value_size = 0;
- max_entries = 4096;
+ max_entries = sysconf(_SC_PAGE_SIZE);
break;
case BPF_MAP_TYPE_STRUCT_OPS:
/* we'll get -ENOTSUPP for invalid BTF type ID for struct_ops */
--
2.29.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH bpf v2 2/4] libbpf: Handle size overflow for ringbuf mmap
2022-11-16 7:23 [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 1/4] libbpf: Use page size as max_entries when probing ring buffer map Hou Tao
@ 2022-11-16 7:23 ` Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 3/4] libbpf: Handle size overflow for user " Hou Tao
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Hou Tao @ 2022-11-16 7:23 UTC (permalink / raw)
To: bpf, Andrii Nakryiko, David Vernet
Cc: Martin KaFai Lau, Song Liu, Hao Luo, Yonghong Song,
Alexei Starovoitov, Daniel Borkmann, KP Singh, Stanislav Fomichev,
Jiri Olsa, John Fastabend, houtao1
From: Hou Tao <houtao1@huawei.com>
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>
---
tools/lib/bpf/ringbuf.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
index d285171d4b69..5b1dc8794b06 100644
--- a/tools/lib/bpf/ringbuf.c
+++ b/tools/lib/bpf/ringbuf.c
@@ -77,6 +77,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
__u32 len = sizeof(info);
struct epoll_event *e;
struct ring *r;
+ __u64 mmap_sz;
void *tmp;
int err;
@@ -129,8 +130,14 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
* data size to allow simple reading of samples that wrap around the
* end of a ring buffer. See kernel implementation for details.
* */
- tmp = mmap(NULL, rb->page_size + 2 * info.max_entries, PROT_READ,
- MAP_SHARED, map_fd, rb->page_size);
+ mmap_sz = rb->page_size + 2 * (__u64)info.max_entries;
+ if (mmap_sz != (__u64)(size_t)mmap_sz) {
+ pr_warn("ringbuf: ring buffer size (%u) is too big\n",
+ info.max_entries);
+ return libbpf_err(-E2BIG);
+ }
+ tmp = mmap(NULL, (size_t)mmap_sz, PROT_READ, MAP_SHARED, map_fd,
+ rb->page_size);
if (tmp == MAP_FAILED) {
err = -errno;
ringbuf_unmap_ring(rb, r);
--
2.29.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH bpf v2 3/4] libbpf: Handle size overflow for user ringbuf mmap
2022-11-16 7:23 [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 1/4] libbpf: Use page size as max_entries when probing ring buffer map Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 2/4] libbpf: Handle size overflow for ringbuf mmap Hou Tao
@ 2022-11-16 7:23 ` Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 4/4] libbpf: Check the validity of size in user_ring_buffer__reserve() Hou Tao
2022-11-18 0:00 ` [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer patchwork-bot+netdevbpf
4 siblings, 0 replies; 6+ messages in thread
From: Hou Tao @ 2022-11-16 7:23 UTC (permalink / raw)
To: bpf, Andrii Nakryiko, David Vernet
Cc: Martin KaFai Lau, Song Liu, Hao Luo, Yonghong Song,
Alexei Starovoitov, Daniel Borkmann, KP Singh, Stanislav Fomichev,
Jiri Olsa, John Fastabend, houtao1
From: Hou Tao <houtao1@huawei.com>
Similar with the overflow problem on ringbuf mmap, in user_ringbuf_map()
2 * max_entries may overflow u32 when mapping writeable region.
Fixing it by casting the size of writable mmap region into a __u64 and
checking whether or not there will be overflow during mmap.
Fixes: b66ccae01f1d ("bpf: Add libbpf logic for user-space ring buffer")
Signed-off-by: Hou Tao <houtao1@huawei.com>
---
tools/lib/bpf/ringbuf.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
index 5b1dc8794b06..277e49137a95 100644
--- a/tools/lib/bpf/ringbuf.c
+++ b/tools/lib/bpf/ringbuf.c
@@ -355,6 +355,7 @@ static int user_ringbuf_map(struct user_ring_buffer *rb, int map_fd)
{
struct bpf_map_info info;
__u32 len = sizeof(info);
+ __u64 mmap_sz;
void *tmp;
struct epoll_event *rb_epoll;
int err;
@@ -391,8 +392,14 @@ static int user_ringbuf_map(struct user_ring_buffer *rb, int map_fd)
* simple reading and writing of samples that wrap around the end of
* the buffer. See the kernel implementation for details.
*/
- tmp = mmap(NULL, rb->page_size + 2 * info.max_entries,
- PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, rb->page_size);
+ mmap_sz = rb->page_size + 2 * (__u64)info.max_entries;
+ if (mmap_sz != (__u64)(size_t)mmap_sz) {
+ pr_warn("user ringbuf: ring buf size (%u) is too big\n",
+ info.max_entries);
+ return -E2BIG;
+ }
+ tmp = mmap(NULL, (size_t)mmap_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
+ map_fd, rb->page_size);
if (tmp == MAP_FAILED) {
err = -errno;
pr_warn("user ringbuf: failed to mmap data pages for map fd=%d: %d\n",
--
2.29.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH bpf v2 4/4] libbpf: Check the validity of size in user_ring_buffer__reserve()
2022-11-16 7:23 [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer Hou Tao
` (2 preceding siblings ...)
2022-11-16 7:23 ` [PATCH bpf v2 3/4] libbpf: Handle size overflow for user " Hou Tao
@ 2022-11-16 7:23 ` Hou Tao
2022-11-18 0:00 ` [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer patchwork-bot+netdevbpf
4 siblings, 0 replies; 6+ messages in thread
From: Hou Tao @ 2022-11-16 7:23 UTC (permalink / raw)
To: bpf, Andrii Nakryiko, David Vernet
Cc: Martin KaFai Lau, Song Liu, Hao Luo, Yonghong Song,
Alexei Starovoitov, Daniel Borkmann, KP Singh, Stanislav Fomichev,
Jiri Olsa, John Fastabend, houtao1
From: Hou Tao <houtao1@huawei.com>
The top two bits of size are used as busy and discard flags, so reject
the reservation that has any of these special bits in the size. With the
addition of validity check, these is also no need to check whether or
not total_size is overflowed.
Signed-off-by: Hou Tao <houtao1@huawei.com>
---
tools/lib/bpf/ringbuf.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c
index 277e49137a95..3b65f04ec46e 100644
--- a/tools/lib/bpf/ringbuf.c
+++ b/tools/lib/bpf/ringbuf.c
@@ -490,6 +490,10 @@ void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size)
__u64 cons_pos, prod_pos;
struct ringbuf_hdr *hdr;
+ /* The top two bits are used as special flags */
+ if (size & (BPF_RINGBUF_BUSY_BIT | BPF_RINGBUF_DISCARD_BIT))
+ return errno = E2BIG, NULL;
+
/* Synchronizes with smp_store_release() in __bpf_user_ringbuf_peek() in
* the kernel.
*/
--
2.29.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer
2022-11-16 7:23 [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer Hou Tao
` (3 preceding siblings ...)
2022-11-16 7:23 ` [PATCH bpf v2 4/4] libbpf: Check the validity of size in user_ring_buffer__reserve() Hou Tao
@ 2022-11-18 0:00 ` patchwork-bot+netdevbpf
4 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-11-18 0:00 UTC (permalink / raw)
To: Hou Tao
Cc: bpf, andrii, void, martin.lau, song, haoluo, yhs, ast, daniel,
kpsingh, sdf, jolsa, john.fastabend, houtao1
Hello:
This series was applied to bpf/bpf.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
On Wed, 16 Nov 2022 15:23:47 +0800 you wrote:
> From: Hou Tao <houtao1@huawei.com>
>
> Hi,
>
> The patch set tries to fix the problems found when testing ringbuf by
> using 4KB and 2GB size. Patch 1 fixes the probe of ring buffer map on
> host with 64KB page (e.g., an ARM64 host). Patch 2 & 3 fix the overflow
> of length when mmaping 2GB kernel ringbuf or user ringbuf on libbpf.
> Patch 4 just reject the reservation with invalid size.
>
> [...]
Here is the summary with links:
- [bpf,v2,1/4] libbpf: Use page size as max_entries when probing ring buffer map
https://git.kernel.org/bpf/bpf/c/689eb2f1ba46
- [bpf,v2,2/4] libbpf: Handle size overflow for ringbuf mmap
https://git.kernel.org/bpf/bpf/c/927cbb478adf
- [bpf,v2,3/4] libbpf: Handle size overflow for user ringbuf mmap
https://git.kernel.org/bpf/bpf/c/64176bff2446
- [bpf,v2,4/4] libbpf: Check the validity of size in user_ring_buffer__reserve()
https://git.kernel.org/bpf/bpf/c/05c1558bfcb6
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-11-18 0:00 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-16 7:23 [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 1/4] libbpf: Use page size as max_entries when probing ring buffer map Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 2/4] libbpf: Handle size overflow for ringbuf mmap Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 3/4] libbpf: Handle size overflow for user " Hou Tao
2022-11-16 7:23 ` [PATCH bpf v2 4/4] libbpf: Check the validity of size in user_ring_buffer__reserve() Hou Tao
2022-11-18 0:00 ` [PATCH bpf v2 0/4] libbpf: Fixes for ring buffer patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox