public inbox for [email protected]
 help / color / mirror / Atom feed
From: Stefano Garzarella <[email protected]>
To: Joseph Qi <[email protected]>
Cc: Jens Axboe <[email protected]>,
	Xiaoguang Wang <[email protected]>,
	[email protected]
Subject: Re: [PATCH] io_uring: fix shift-out-of-bounds when round up cq size
Date: Tue, 24 Nov 2020 09:38:12 +0100	[thread overview]
Message-ID: <20201124083812.bnvshaiur4ifa3lr@steredhat> (raw)
In-Reply-To: <[email protected]>

On Tue, Nov 24, 2020 at 03:03:03PM +0800, Joseph Qi wrote:
>Abaci Fuzz reported a shift-out-of-bounds BUG in io_uring_create():
>
>[ 59.598207] UBSAN: shift-out-of-bounds in ./include/linux/log2.h:57:13
>[ 59.599665] shift exponent 64 is too large for 64-bit type 'long unsigned int'
>[ 59.601230] CPU: 0 PID: 963 Comm: a.out Not tainted 5.10.0-rc4+ #3
>[ 59.602502] Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011
>[ 59.603673] Call Trace:
>[ 59.604286] dump_stack+0x107/0x163
>[ 59.605237] ubsan_epilogue+0xb/0x5a
>[ 59.606094] __ubsan_handle_shift_out_of_bounds.cold+0xb2/0x20e
>[ 59.607335] ? lock_downgrade+0x6c0/0x6c0
>[ 59.608182] ? rcu_read_lock_sched_held+0xaf/0xe0
>[ 59.609166] io_uring_create.cold+0x99/0x149
>[ 59.610114] io_uring_setup+0xd6/0x140
>[ 59.610975] ? io_uring_create+0x2510/0x2510
>[ 59.611945] ? lockdep_hardirqs_on_prepare+0x286/0x400
>[ 59.613007] ? syscall_enter_from_user_mode+0x27/0x80
>[ 59.614038] ? trace_hardirqs_on+0x5b/0x180
>[ 59.615056] do_syscall_64+0x2d/0x40
>[ 59.615940] entry_SYSCALL_64_after_hwframe+0x44/0xa9
>[ 59.617007] RIP: 0033:0x7f2bb8a0b239
>
>This is caused by roundup_pow_of_two() if the input entries larger
>enough, e.g. 2^32-1. For sq_entries, it will check first and we allow
>at most IORING_MAX_ENTRIES, so it is okay. But for cq_entries, we do
>round up first, that may overflow and truncate it to 0, which is not
>the expected behavior. So check the cq size first and then do round up.
>
>Fixes: 88ec3211e463 ("io_uring: round-up cq size before comparing with rounded sq size")
>Reported-by: Abaci Fuzz <[email protected]>
>Signed-off-by: Joseph Qi <[email protected]>
>---
> fs/io_uring.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
>diff --git a/fs/io_uring.c b/fs/io_uring.c
>index a8c136a..f971589 100644
>--- a/fs/io_uring.c
>+++ b/fs/io_uring.c
>@@ -9252,14 +9252,16 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p,
> 		 * to a power-of-two, if it isn't already. We do NOT impose
> 		 * any cq vs sq ring sizing.
> 		 */
>-		p->cq_entries = roundup_pow_of_two(p->cq_entries);
>-		if (p->cq_entries < p->sq_entries)
>+		if (!p->cq_entries)
> 			return -EINVAL;
> 		if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
> 			if (!(p->flags & IORING_SETUP_CLAMP))
> 				return -EINVAL;
> 			p->cq_entries = IORING_MAX_CQ_ENTRIES;
> 		}
>+		p->cq_entries = roundup_pow_of_two(p->cq_entries);
>+		if (p->cq_entries < p->sq_entries)
>+			return -EINVAL;

Your changes reflect what we do for sq_entries, so it feels right to do 
the same for cq_entries. Also moving the check after the roundup 
prevents the issues fixed in 88ec3211e463, so:

Reviewed-by: Stefano Garzarella <[email protected]>

Thanks,
Stefano


  reply	other threads:[~2020-11-24  8:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-24  7:03 [PATCH] io_uring: fix shift-out-of-bounds when round up cq size Joseph Qi
2020-11-24  8:38 ` Stefano Garzarella [this message]
2020-11-24 14:45 ` Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201124083812.bnvshaiur4ifa3lr@steredhat \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox