Skip to content

Commit 2a30f94

Browse files
bjackmananakryiko
authored andcommitted
libbpf: Fix signed overflow in ringbuf_process_ring
One of our benchmarks running in (Google-internal) CI pushes data through the ringbuf faster htan than userspace is able to consume it. In this case it seems we're actually able to get >INT_MAX entries in a single ring_buffer__consume() call. ASAN detected that cnt overflows in this case. Fix by using 64-bit counter internally and then capping the result to INT_MAX before converting to the int return type. Do the same for the ring_buffer__poll(). Fixes: bf99c93 (libbpf: Add BPF ring buffer support) Signed-off-by: Brendan Jackman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 801c605 commit 2a30f94

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

tools/lib/bpf/ringbuf.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,11 @@ static inline int roundup_len(__u32 len)
202202
return (len + 7) / 8 * 8;
203203
}
204204

205-
static int ringbuf_process_ring(struct ring* r)
205+
static int64_t ringbuf_process_ring(struct ring* r)
206206
{
207-
int *len_ptr, len, err, cnt = 0;
207+
int *len_ptr, len, err;
208+
/* 64-bit to avoid overflow in case of extreme application behavior */
209+
int64_t cnt = 0;
208210
unsigned long cons_pos, prod_pos;
209211
bool got_new_data;
210212
void *sample;
@@ -244,12 +246,14 @@ static int ringbuf_process_ring(struct ring* r)
244246
}
245247

246248
/* Consume available ring buffer(s) data without event polling.
247-
* Returns number of records consumed across all registered ring buffers, or
248-
* negative number if any of the callbacks return error.
249+
* Returns number of records consumed across all registered ring buffers (or
250+
* INT_MAX, whichever is less), or negative number if any of the callbacks
251+
* return error.
249252
*/
250253
int ring_buffer__consume(struct ring_buffer *rb)
251254
{
252-
int i, err, res = 0;
255+
int64_t err, res = 0;
256+
int i;
253257

254258
for (i = 0; i < rb->ring_cnt; i++) {
255259
struct ring *ring = &rb->rings[i];
@@ -259,18 +263,24 @@ int ring_buffer__consume(struct ring_buffer *rb)
259263
return err;
260264
res += err;
261265
}
266+
if (res > INT_MAX)
267+
return INT_MAX;
262268
return res;
263269
}
264270

265271
/* Poll for available data and consume records, if any are available.
266-
* Returns number of records consumed, or negative number, if any of the
267-
* registered callbacks returned error.
272+
* Returns number of records consumed (or INT_MAX, whichever is less), or
273+
* negative number, if any of the registered callbacks returned error.
268274
*/
269275
int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms)
270276
{
271-
int i, cnt, err, res = 0;
277+
int i, cnt;
278+
int64_t err, res = 0;
272279

273280
cnt = epoll_wait(rb->epoll_fd, rb->events, rb->ring_cnt, timeout_ms);
281+
if (cnt < 0)
282+
return -errno;
283+
274284
for (i = 0; i < cnt; i++) {
275285
__u32 ring_id = rb->events[i].data.fd;
276286
struct ring *ring = &rb->rings[ring_id];
@@ -280,7 +290,9 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms)
280290
return err;
281291
res += err;
282292
}
283-
return cnt < 0 ? -errno : res;
293+
if (res > INT_MAX)
294+
return INT_MAX;
295+
return res;
284296
}
285297

286298
/* Get an fd that can be used to sleep until data is available in the ring(s) */

0 commit comments

Comments
 (0)