Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9106005pxu; Mon, 28 Dec 2020 06:47:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJxJGdif693V6zA0MV8mxzZ+anydkECwhuFJUgl/IyxH19YNIz91U0/BFmtPe8bs6yv3rHBF X-Received: by 2002:a17:906:6c8b:: with SMTP id s11mr41521707ejr.187.1609166851631; Mon, 28 Dec 2020 06:47:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609166851; cv=none; d=google.com; s=arc-20160816; b=lh4Xxp/o7+JoJzN0eVhqAOA04PS7yGGnSSiwqA5ti1ccZzvLBeZjDE84pxXjo8gA4J PqK+qRtv4oyv1WX45fmyk8Wug3GCUerGkp68+MVcSLavb1GIL61I9JnL8MdX35NOwDdk bgIvWcn2ZAhLgg80p0TNS5+VQLhrnFGMm5quSiuCqUwxJOrkBisQmao2ZJlAxrd9+4J4 GcAlgzYmGi2KP3odJoIHYALoxh8aY4YAzIs4x5kGgfnS03v2zmd4O4zGn4vDE6t6dVB1 dPIIZ+ZUY+dKghYC83mCc6ziOPd4zJVVI7J+pcbj6PRsLNGv2WkeLSVOiTg5lkE4H1nU bDTQ== 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=bU9kK4LGiwe5Oq8X7ghdDg/69n39OJzPcmhjyrvaFgc=; b=Vq7cIIIGIfeGoEMD7rX3tpx8zd7EAjsNBhPCN+OcrnFmcHcsqQU8dhm8N4Wwg8QjTj U2mlwRvBVCPpEhJ/QZcQ1K9AIRBQR3wDRj7ssMhTaMYO4uL6YwNgcA/PEVa9IBgLTOcI oGzfGQ/PUspku0+c9DR3DzFq7jON8ffVj0aaveCTzuBS/UZhXJTGkKzAe/1NvPas4jFX VPE7k+tpyfzCK07xaeDec/sI9th4h1NdsHrNt/jBCbHDfv895hCn9+mC7NU/0ZxSgFLD HIPTN4KbvQG3+H49aCuquTc1rfcJAMTXUJu9ZyDZRTMxnG7TJ7JBXrX2Jh1nVfcwyIdN 6w8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eIbDlZiZ; 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 c12si18483729ejb.751.2020.12.28.06.47.09; Mon, 28 Dec 2020 06:47:31 -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=eIbDlZiZ; 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 S2503190AbgL1OZZ (ORCPT + 99 others); Mon, 28 Dec 2020 09:25:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:33142 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2439268AbgL1OZJ (ORCPT ); Mon, 28 Dec 2020 09:25:09 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 54E5A206D4; Mon, 28 Dec 2020 14:24:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165468; bh=+QVct9MyLaeNCifX8GQtBc8ROXA/1RbiTn2kv0bnhdI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eIbDlZiZUJWVD5sGSNsGvbnDpHhKyXaQYXlbjy/3F1Bonlia5BGv8OQmCppYVF1sv Mcouw7dp/JUnoTQs0zp3sTFfcQB31CV62UjG82qBHLtU0vg8uanJjMYmt8aZMcHX58 Eq/QcZ7qov+spiq1yMqrwpBiNppVaJH5pFA6nUWU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Abaci Fuzz , Xiaoguang Wang , Pavel Begunkov , Joseph Qi , Jens Axboe Subject: [PATCH 5.10 512/717] io_uring: always let io_iopoll_complete() complete polled io Date: Mon, 28 Dec 2020 13:48:30 +0100 Message-Id: <20201228125045.497020421@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@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: Xiaoguang Wang commit dad1b1242fd5717af18ae4ac9d12b9f65849e13a upstream. Abaci Fuzz reported a double-free or invalid-free BUG in io_commit_cqring(): [ 95.504842] BUG: KASAN: double-free or invalid-free in io_commit_cqring+0x3ec/0x8e0 [ 95.505921] [ 95.506225] CPU: 0 PID: 4037 Comm: io_wqe_worker-0 Tainted: G B W 5.10.0-rc5+ #1 [ 95.507434] Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011 [ 95.508248] Call Trace: [ 95.508683] dump_stack+0x107/0x163 [ 95.509323] ? io_commit_cqring+0x3ec/0x8e0 [ 95.509982] print_address_description.constprop.0+0x3e/0x60 [ 95.510814] ? vprintk_func+0x98/0x140 [ 95.511399] ? io_commit_cqring+0x3ec/0x8e0 [ 95.512036] ? io_commit_cqring+0x3ec/0x8e0 [ 95.512733] kasan_report_invalid_free+0x51/0x80 [ 95.513431] ? io_commit_cqring+0x3ec/0x8e0 [ 95.514047] __kasan_slab_free+0x141/0x160 [ 95.514699] kfree+0xd1/0x390 [ 95.515182] io_commit_cqring+0x3ec/0x8e0 [ 95.515799] __io_req_complete.part.0+0x64/0x90 [ 95.516483] io_wq_submit_work+0x1fa/0x260 [ 95.517117] io_worker_handle_work+0xeac/0x1c00 [ 95.517828] io_wqe_worker+0xc94/0x11a0 [ 95.518438] ? io_worker_handle_work+0x1c00/0x1c00 [ 95.519151] ? __kthread_parkme+0x11d/0x1d0 [ 95.519806] ? io_worker_handle_work+0x1c00/0x1c00 [ 95.520512] ? io_worker_handle_work+0x1c00/0x1c00 [ 95.521211] kthread+0x396/0x470 [ 95.521727] ? _raw_spin_unlock_irq+0x24/0x30 [ 95.522380] ? kthread_mod_delayed_work+0x180/0x180 [ 95.523108] ret_from_fork+0x22/0x30 [ 95.523684] [ 95.523985] Allocated by task 4035: [ 95.524543] kasan_save_stack+0x1b/0x40 [ 95.525136] __kasan_kmalloc.constprop.0+0xc2/0xd0 [ 95.525882] kmem_cache_alloc_trace+0x17b/0x310 [ 95.533930] io_queue_sqe+0x225/0xcb0 [ 95.534505] io_submit_sqes+0x1768/0x25f0 [ 95.535164] __x64_sys_io_uring_enter+0x89e/0xd10 [ 95.535900] do_syscall_64+0x33/0x40 [ 95.536465] entry_SYSCALL_64_after_hwframe+0x44/0xa9 [ 95.537199] [ 95.537505] Freed by task 4035: [ 95.538003] kasan_save_stack+0x1b/0x40 [ 95.538599] kasan_set_track+0x1c/0x30 [ 95.539177] kasan_set_free_info+0x1b/0x30 [ 95.539798] __kasan_slab_free+0x112/0x160 [ 95.540427] kfree+0xd1/0x390 [ 95.540910] io_commit_cqring+0x3ec/0x8e0 [ 95.541516] io_iopoll_complete+0x914/0x1390 [ 95.542150] io_do_iopoll+0x580/0x700 [ 95.542724] io_iopoll_try_reap_events.part.0+0x108/0x200 [ 95.543512] io_ring_ctx_wait_and_kill+0x118/0x340 [ 95.544206] io_uring_release+0x43/0x50 [ 95.544791] __fput+0x28d/0x940 [ 95.545291] task_work_run+0xea/0x1b0 [ 95.545873] do_exit+0xb6a/0x2c60 [ 95.546400] do_group_exit+0x12a/0x320 [ 95.546967] __x64_sys_exit_group+0x3f/0x50 [ 95.547605] do_syscall_64+0x33/0x40 [ 95.548155] entry_SYSCALL_64_after_hwframe+0x44/0xa9 The reason is that once we got a non EAGAIN error in io_wq_submit_work(), we'll complete req by calling io_req_complete(), which will hold completion_lock to call io_commit_cqring(), but for polled io, io_iopoll_complete() won't hold completion_lock to call io_commit_cqring(), then there maybe concurrent access to ctx->defer_list, double free may happen. To fix this bug, we always let io_iopoll_complete() complete polled io. Cc: # 5.5+ Reported-by: Abaci Fuzz Signed-off-by: Xiaoguang Wang Reviewed-by: Pavel Begunkov Reviewed-by: Joseph Qi Signed-off-by: Pavel Begunkov Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- fs/io_uring.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -6083,8 +6083,19 @@ static struct io_wq_work *io_wq_submit_w } if (ret) { - req_set_fail_links(req); - io_req_complete(req, ret); + /* + * io_iopoll_complete() does not hold completion_lock to complete + * polled io, so here for polled io, just mark it done and still let + * io_iopoll_complete() complete it. + */ + if (req->ctx->flags & IORING_SETUP_IOPOLL) { + struct kiocb *kiocb = &req->rw.kiocb; + + kiocb_done(kiocb, ret, NULL); + } else { + req_set_fail_links(req); + io_req_complete(req, ret); + } } return io_steal_work(req);