io_uring/rsrc: initialize io_rsrc_data nodes array
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).
--- io_uring/rsrc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
io_uring/rsrc.c +2 -2
@@ -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
Quoted 16 lines · Caleb Sander Mateos
io_rsrc_data_alloc()allocates an array ofio_rsrc_nodepointers and assigns it to io_rsrc_data's nodes field along with the size in the nr field. However, it doesn't initialize theio_rsrc_nodepointers in the array. If an error inio_sqe_buffers_register(),io_alloc_file_tables(),io_sqe_files_register(), orio_clone_buffers()causes them to exit before all theio_rsrc_nodepointers in the array have been assigned,io_rsrc_data_free()will read the uninitialized elements, triggering undefined behavior.
Additionally, ifdst_offexceeds the current size of the destination buffer table inio_clone_buffers(), theio_rsrc_nodepointers in between won't be initialized. Any access to those registered buffer indices will result in undefined behavior.
Allocate the array withkvcalloc()instead ofkvmalloc_array()to ensure theio_rsrc_nodepointers 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