Skip to contentSkip to conversation

io_uring/rsrc: initialize io_rsrc_data nodes array

PATCH Applied Caleb Sander Mateos 2 participants 2 messages Sep 5, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring/rsrc: initialize io_rsrc_data nodes array

io_rsrc_data_alloc() allocates an array of io_rsrc_node pointers and assigns it to io_rsrc_data's nodes field along with the size in the nr field. However, it doesn't initialize the io_rsrc_node pointers in the array. If an error in io_sqe_buffers_register(), io_alloc_file_tables(), io_sqe_files_register(), or io_clone_buffers() causes them to exit before all the io_rsrc_node pointers in the array have been assigned, io_rsrc_data_free() will read the uninitialized elements, triggering undefined behavior.
Additionally, if dst_off exceeds the current size of the destination buffer table in io_clone_buffers(), the io_rsrc_node pointers in between won't be initialized. Any access to those registered buffer indices will result in undefined behavior.
Allocate the array with kvcalloc() instead of kvmalloc_array() to ensure the io_rsrc_node pointers are initialized to NULL (indicating no registered buffer/file node).

Fixes 7029acd8a950 ("io_uring/rsrc: get rid of per-ring io_rsrc_node list")
---
 io_uring/rsrc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
io_uring/rsrc.c +2 -2
index f75f5e43fa4a..3f3f355f6613 100644
@@ -209,12 +209,12 @@ __cold void io_rsrc_data_free(struct io_ring_ctx *ctx,
 	data->nr = 0;
 }
 
 __cold int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr)
 {
-	data->nodes = kvmalloc_array(nr, sizeof(struct io_rsrc_node *),
-					GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+	data->nodes = kvcalloc(nr, sizeof(struct io_rsrc_node *),
+			       GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (data->nodes) {
 		data->nr = nr;
 		return 0;
 	}
 	return -ENOMEM;
Signature · 1 line
2.45.2
maintainer Applied
#1 lore raw
Quoted 16 lines · Caleb Sander Mateos

io_rsrc_data_alloc() allocates an array of io_rsrc_node pointers and assigns it to io_rsrc_data's nodes field along with the size in the nr field. However, it doesn't initialize the io_rsrc_node pointers in the array. If an error in io_sqe_buffers_register(), io_alloc_file_tables(), io_sqe_files_register(), or io_clone_buffers() causes them to exit before all the io_rsrc_node pointers in the array have been assigned, io_rsrc_data_free() will read the uninitialized elements, triggering undefined behavior.
Additionally, if dst_off exceeds the current size of the destination buffer table in io_clone_buffers(), the io_rsrc_node pointers in between won't be initialized. Any access to those registered buffer indices will result in undefined behavior.
Allocate the array with kvcalloc() instead of kvmalloc_array() to ensure the io_rsrc_node pointers are initialized to NULL (indicating no registered buffer/file node).

Quoted 1 line

[...]

Applied, thanks!

[1/1] io_uring/rsrc: initialize io_rsrc_data nodes array

      commit: 0f51a5c0a89921deca72e42583683e44ff742d06

Best regards,

Signature · 1 line
Jens Axboe