Skip to contentSkip to conversation

io_uring: cast id to u64 before shifting in io_allocate_rbuf_ring()

PATCH In review Anas Iqbal 3 participants 3 messages Mar 16

Subsystems (4)
lore
Share & tools
lore

Anas Iqbal

author
#0 lore raw

PATCH 1/1 io_uring: cast id to u64 before shifting in io_allocate_rbuf_ring()

Smatch warns:
io_uring/zcrx.c:393 io_allocate_rbuf_ring() warn: should 'id << 16' be a 64 bit type?

The expression 'id << IORING_OFF_PBUF_SHIFT' is evaluated using 32-bit arithmetic because id is a u32. This may overflow before being promoted to the 64-bit mmap_offset.

Cast id to u64 before shifting to ensure the shift is performed in 64-bit arithmetic.

---
 io_uring/zcrx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/zcrx.c +1 -1
index 62d693287457..d96d2802f3da 100644
@@ -390,7 +390,7 @@ static int io_allocate_rbuf_ring(struct io_ring_ctx *ctx,
 		return -EINVAL;
 
 	mmap_offset = IORING_MAP_OFF_ZCRX_REGION;
-	mmap_offset += id << IORING_OFF_PBUF_SHIFT;
+	mmap_offset += (u64)id << IORING_OFF_PBUF_SHIFT;
 
 	ret = io_create_region(ctx, &ifq->region, rd, mmap_offset);
 	if (ret < 0)
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 9 lines · Anas Iqbal

Smatch warns:
io_uring/zcrx.c:393 io_allocate_rbuf_ring() warn: should 'id << 16' be a 64 bit type?

The expression 'id << IORING_OFF_PBUF_SHIFT' is evaluated using 32-bit arithmetic because id is a u32. This may overflow before being promoted to the 64-bit mmap_offset.

Cast id to u64 before shifting to ensure the shift is performed in 64-bit arithmetic.

I'd be impressed if 'id' could be large enough to cause this to overflow. AFAICT, you'd need more than 64K interface queues registered to hit this. So I think this should be reframed as a cleanup, to appease smatch.

I'll defer to Pavel on that one.

Signature · 1 line
Jens Axboe
maintainer
#2 lore raw
Quoted 8 lines · Jens Axboe
Quoted 7 lines · Anas Iqbal

Smatch warns:
io_uring/zcrx.c:393 io_allocate_rbuf_ring() warn: should 'id << 16' be a 64 bit type?

The expression 'id << IORING_OFF_PBUF_SHIFT' is evaluated using 32-bit arithmetic because id is a u32. This may overflow before being promoted to the 64-bit mmap_offset.

Quoted 7 lines
Quoted 2 lines

Cast id to u64 before shifting to ensure the shift is performed in 64-bit arithmetic.

I'd be impressed if 'id' could be large enough to cause this to overflow. AFAICT, you'd need more than 64K interface queues registered to hit this. So I think this should be reframed as a cleanup, to appease smatch.

Pretty much so. I'll queue up the patch targeting 7.1, thanks

Signature · 1 line
Pavel Begunkov