 io_uring/kbuf: always use READ_ONCE() to read ring provided buffer le… · gregkh/linux@91f262e · GitHub Skip to content Navigation Menu Toggle navigation Sign in Appearance settings Platform AI CODE CREATION GitHub Copilot Write better code with AI GitHub Spark Build and deploy intelligent apps GitHub Models Manage and compare prompts MCP Registry New Integrate external tools DEVELOPER WORKFLOWS Actions Automate any workflow Codespaces Instant dev environments Issues Plan and track work Code Review Manage code changes APPLICATION SECURITY GitHub Advanced Security Find and fix vulnerabilities Code security Secure your code as you build Secret protection  EXPLORE Why GitHub Documentation Blog Changelog Marketplace View all features Solutions BY COMPANY SIZE Enterprises Small and medium teams Startups Nonprofits BY USE CASE App Modernization DevSecOps DevOps CI/CD View all use cases BY INDUSTRY Healthcare Financial services Manufacturing Government View all industries View all solutions Resources EXPLORE BY TOPIC AI Software Development DevOps Security View all topics EXPLORE BY TYPE Customer stories Events &amp; webinars Ebooks &amp; reports Business insights GitHub Skills SUPPORT &amp; SERVICES Documentation Customer support Community forum Trust center Partners View all resources Open Source COMMUNITY GitHub Sponsors Fund open source developers PROGRAMS Security Lab Maintainer Community Accelerator GitHub Stars Archive Program REPOSITORIES Topics Trending Collections Enterprise ENTERPRISE SOLUTIONS Enterprise platform AI-powered developer platform AVAILABLE ADD-ONS GitHub Advanced Security Enterprise-grade security features Copilot for Business Enterprise-grade AI features Premium Support Enterprise-grade 24/7 support Pricing Search or jump to... Search code, repositories, users, issues, pull requests... --> Search Clear Search syntax tips Provide feedback --> We read every piece of feedback, and take your input very seriously.  Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly --> Name Query To see all available qualifiers, see our documentation . Cancel Create saved search Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} gregkh / linux Public Notifications You must be signed in to change notification settings Fork 686 Star 644 Code Actions Security and quality 0 Insights Additional navigation options Code Actions Security and quality Insights Commit 91f262e Browse files Browse files axboe authored and gregkh committed io_uring/kbuf: always use READ_ONCE() to read ring provided buffer lengths Commit 98b6fa6 upstream. Since the buffers are mapped from userspace, it is prudent to use READ_ONCE() to read the value into a local variable, and use that for any other actions taken. Having a stable read of the buffer length avoids worrying about it changing after checking, or being read multiple times. Similarly, the buffer may well change in between it being picked and being committed. Ensure the looping for incremental ring buffer commit stops if it hits a zero sized buffer, as no further progress can be made at that point. Fixes: ae98dbf ("io_uring/kbuf: add support for incremental buffer consumption") Link: https://lore.kernel.org/io-uring/tencent_000C02641F6250C856D0C26228DE29A3D30A@qq.com/ Reported-by: Qingyue Zhang &lt;chunzhennn@qq.com&gt; Reported-by: Suoxing Zhang &lt;aftern00n@qq.com&gt; Signed-off-by: Jens Axboe &lt;axboe@kernel.dk&gt; Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt; 1 parent c4dbca5 commit 91f262e Copy full SHA for 91f262e 1 file changed + 13 - 7 Lines changed: 13 additions &amp; 7 deletions File tree Expand file tree Collapse file tree Open diff view settings Filter options io_uring kbuf.c Expand file tree Collapse file tree Open diff view settings Collapse file ‎ io_uring/kbuf.c ‎ Copy file name to clipboard Expand all lines: io_uring/kbuf.c + 13 - 7 Lines changed: 13 additions &amp; 7 deletions Original file line number Diff line number Diff line change @@ -36,15 +36,19 @@ static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len) 36 36 { 37 37 while ( len ) { 38 38 struct io_uring_buf * buf ; 39 - u32 this_len ; 39 + u32 buf_len , this_len ; 40 40 41 41 buf = io_ring_head_to_buf ( bl -&gt; buf_ring , bl -&gt; head , bl -&gt; mask ); 42 - this_len = min_t ( u32 , len , buf -&gt; len ); 43 - buf -&gt; len -= this_len ; 44 - if ( buf -&gt; len ) { 42 + buf_len = READ_ONCE ( buf -&gt; len ); 43 + this_len = min_t ( u32 , len , buf_len ); 44 + buf_len -= this_len ; 45 + /* Stop looping for invalid buffer length of 0 */ 46 + if ( buf_len || ! this_len ) { 45 47 buf -&gt; addr += this_len ; 48 + buf -&gt; len = buf_len ; 46 49 return false; 47 50 } 51 + buf -&gt; len = 0 ; 48 52 bl -&gt; head ++ ; 49 53 len -= this_len ; 50 54 } @@ -167,6 +171,7 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len, 167 171 __u16 tail , head = bl -&gt; head ; 168 172 struct io_br_sel sel = { }; 169 173 struct io_uring_buf * buf ; 174 + u32 buf_len ; 170 175 171 176 tail = smp_load_acquire ( &amp; br -&gt; tail ); 172 177 if ( unlikely ( tail == head )) @@ -176,8 +181,9 @@ static struct io_br_sel io_ring_buffer_select(struct io_kiocb *req, size_t *len, 176 181 req -&gt; flags |= REQ_F_BL_EMPTY ; 177 182 178 183 buf = io_ring_head_to_buf ( br , head , bl -&gt; mask ); 179 - if ( * len == 0 || * len &gt; buf -&gt; len ) 180 - * len = buf -&gt; len ; 184 + buf_len = READ_ONCE ( buf -&gt; len ); 185 + if ( * len == 0 || * len &gt; buf_len ) 186 + * len = buf_len ; 181 187 req -&gt; flags |= REQ_F_BUFFER_RING | REQ_F_BUFFERS_COMMIT ; 182 188 req -&gt; buf_index = buf -&gt; bid ; 183 189 sel . buf_list = bl ; @@ -274,7 +280,7 @@ static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg, 274 280 275 281 req -&gt; buf_index = buf -&gt; bid ; 276 282 do { 277 - u32 len = buf -&gt; len ; 283 + u32 len = READ_ONCE ( buf -&gt; len ) ; 278 284 279 285 /* truncate end piece, if needed, for non partial buffers */ 280 286 if ( len &gt; arg -&gt; max_len ) { 0 commit comments Comments 0 ( 0 ) Footer &copy; 2026 GitHub,&nbsp;Inc. Footer navigation Terms Privacy Security Status Community Docs Contact Manage cookies Do not share my personal information You can’t perform that action at this time. 