Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4302464pxu; Tue, 1 Dec 2020 01:19:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJw+IZpTMHKtlRDJFf8D3rhQXP/9jmxqW+Qo5sNHSs2tBV+RVMeXUZ4Hu0pQ/Odfe9LX5pkJ X-Received: by 2002:a50:8741:: with SMTP id 1mr2011442edv.349.1606814357547; Tue, 01 Dec 2020 01:19:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606814357; cv=none; d=google.com; s=arc-20160816; b=Cow6oDebZ7h3LxxJtvswYHYpi0vnoPUhEoDlmobHINmA+scRsSW5L4WnUDhhdo8nsH B+bDY24eujknYWCiluFtGXxm7q3OfS1dZ3mp4CZST7aS3IeiMEZcsUnN2cSxDJj1d56d 1x16HR8zDjWovNht5lV/WJX1lVE+PnsYxIzvCBNx3QO4TriHz/atgixOlr4UM2RVWJC9 JKVlWrpSX7qky9vZfDcEF1zSW+4En+xhzybFouBmXWbpatGjIKu8PGLHjDs8uvuzU63n 5vSh2b8zR5kYEiJrfenPgfDXIL+0Ub9dSYgXF+Im4pGBwm9kcjA2BkX5b/W+BUl5Ix6k 9A8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9MFEWUNUyD2Td5hYKamXbT7fHTPKI8T3FNib6CcLlg4=; b=iMSM1tGSTgX8AgcANUyYw7gwv/hx2Us4aTuB+/j+e+AlRSlsF+sr7okQxFYnpqO345 C2NXlRMA54t1H79k9oXKcz1OPxR36IoF6PpRZRu85ZiW1oPPuOX36QpbH/txyinzNUAe TwNIYNGmVhD0gE0emxdP03j3t1l2OAsxqTzdnwPmuzC3f2JEsCHJKFPpKjOl4zxm2zW7 xeT4N5QkHf7fXJcnMFMVDLNEJrD+MyE9LtqKeqlVIHHJONvEfRA1MJ+UsOrC855TFpH3 0mwOpTLllMe1wM9AJvtvXo+XIlzOUXF2ieADRjkD1V2qvtPyRvk5f541vUBMvy7KT4df tang== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LAy16rM1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v26si549279ejg.723.2020.12.01.01.18.53; Tue, 01 Dec 2020 01:19:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LAy16rM1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727785AbgLAJPV (ORCPT + 99 others); Tue, 1 Dec 2020 04:15:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:53604 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390357AbgLAJOu (ORCPT ); Tue, 1 Dec 2020 04:14:50 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 391C621D7A; Tue, 1 Dec 2020 09:14:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1606814048; bh=gCsx68+GJcjHhskWcZ+Xxw/NwWVAdsUz9SjIgzpQH8I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LAy16rM1Gv9nNDlzCHAnLIUPtFKzwOkW/OF0dqITxJK8F2ZODtzKwF8RYJa1IdFSn 9nRl8WBoheySHuozstCwZFludoM3G6g6qCqZYvu0twdjc4ngjnerLWGNkZ2b6KotQa cpFCT4sKGUX6xnQ5TuDxypb/qZkx6+T2Nd2RmkcQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Abaci Fuzz , Joseph Qi , Stefano Garzarella , Jens Axboe , Sasha Levin Subject: [PATCH 5.9 108/152] io_uring: fix shift-out-of-bounds when round up cq size Date: Tue, 1 Dec 2020 09:53:43 +0100 Message-Id: <20201201084725.990694573@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201201084711.707195422@linuxfoundation.org> References: <20201201084711.707195422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joseph Qi [ Upstream commit eb2667b343361863da7b79be26de641e22844ba0 ] 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 Signed-off-by: Joseph Qi Reviewed-by: Stefano Garzarella Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- 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 5d9f8e40b93d3..6d729a278535e 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -8907,14 +8907,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; } else { p->cq_entries = 2 * p->sq_entries; } -- 2.27.0