Skip to contentSkip to conversation

io_uring/kbuf: fix unintentional sign extension on shift of reg.bgid

PATCH Applied Colin Ian King 2 participants 2 messages Dec 4, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring/kbuf: fix unintentional sign extension on shift of reg.bgid

Shifting reg.bgid << IORING_OFF_PBUF_SHIFT results in a promotion from __u16 to a 32 bit signed integer, this is then sign extended to a 64 bit unsigned long on 64 bit architectures. If reg.bgid is greater than 0x7fff then this leads to a sign extended result where all the upper 32 bits of mmap_offset are set to 1. Fix this by casting reg.bgid to the same type as mmap_offset before performing the shift.

Fixes ff4afde8a61f ("io_uring/kbuf: use region api for pbuf rings")
---
 io_uring/kbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/kbuf.c +1 -1
index e91260a6156b..15e5e6ec5968 100644
@@ -640,7 +640,7 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
 			return -ENOMEM;
 	}
 
-	mmap_offset = reg.bgid << IORING_OFF_PBUF_SHIFT;
+	mmap_offset = (unsigned long)reg.bgid << IORING_OFF_PBUF_SHIFT;
 	ring_size = flex_array_size(br, bufs, reg.ring_entries);
 
 	memset(&rd, 0, sizeof(rd));
Signature · 1 line
2.39.5
maintainer Applied
#1 lore raw
Quoted 9 lines · Colin Ian King

Shifting reg.bgid << IORING_OFF_PBUF_SHIFT results in a promotion from __u16 to a 32 bit signed integer, this is then sign extended to a 64 bit unsigned long on 64 bit architectures. If reg.bgid is greater than 0x7fff then this leads to a sign extended result where all the upper 32 bits of mmap_offset are set to 1. Fix this by casting reg.bgid to the same type as mmap_offset before performing the shift.

[...]

Applied, thanks!

[1/1] io_uring/kbuf: fix unintentional sign extension on shift of reg.bgid

      commit: e54fb80b57e4534ae91e65ea14cc66b9f21b4b7a

Best regards,

Signature · 1 line
Jens Axboe