Re: [PATCH v2 bpf-next] libbpf: Fix signed overflow in ringbuf_process_ring

From: Brendan Jackman
Date: Tue May 04 2021 - 05:01:54 EST


On Mon, 3 May 2021 at 19:46, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote:
>
> On Mon, May 3, 2021 at 5:01 AM Brendan Jackman <jackmanb@xxxxxxxxxx> wrote:
> >
> > On Fri, 30 Apr 2021 at 18:31, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote:

> So while doing that I noticed that you didn't fix ring_buffer__poll(),
> so I had to fix it up a bit more extensively. Please check the end
> result in bpf tree and let me know if there are any problems with it:
>
> 2a30f9440640 ("libbpf: Fix signed overflow in ringbuf_process_ring")

Ah, thanks for that. Yep, the additional fix looks good to me.

I think it actually fixes another very niche issue:

int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms)
{
- int i, cnt, err, res = 0;
+ int i, cnt;
+ int64_t err, res = 0;

cnt = epoll_wait(rb->epoll_fd, rb->events, rb->ring_cnt, timeout_ms);
+ if (cnt < 0)
+ return -errno;
+
for (i = 0; i < cnt; i++) {
__u32 ring_id = rb->events[i].data.fd;
struct ring *ring = &rb->rings[ring_id];
@@ -280,7 +290,9 @@ int ring_buffer__poll(struct ring_buffer *rb, int
timeout_ms)
return err;
res += err;
}
- return cnt < 0 ? -errno : res;

If the callback returns an error but errno is 0 this fails to report the error.

errno(3) says "the value of errno is never set to zero by any system
call or library function" but then describes a scenario where an
application might usefully set it to zero itself. Maybe it can also be
0 in new threads, depending on your metaphysical interpretation of "by
a system call or library function".

+ if (res > INT_MAX)
+ return INT_MAX;
+ return res;