Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4376714pxy; Tue, 27 Apr 2021 03:42:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxkDTq+6G9VP4M3melztTo9/1wbaWgluiYP7L0Xw5clGdPccY6PsMyqKhIlT8xIXaYeq9NL X-Received: by 2002:a62:2d6:0:b029:204:9b3b:dced with SMTP id 205-20020a6202d60000b02902049b3bdcedmr22475793pfc.36.1619520140174; Tue, 27 Apr 2021 03:42:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619520140; cv=none; d=google.com; s=arc-20160816; b=SqWl/aAlHTiBKoi9XlPqFVy3cWMNbeZh5kA0nGpZvBmg5JZVTbJ/DqzYmPCMn1WPW1 19uJOaNle7s1tLnKrWv6wjHB2B3KkPqNzQp/bMBshDOq8WntQJKd29Xp3q4PNNA05tu3 CINSmSpRae4E7G+Xue+i0NXBSkqYxU6aUBnU3lyfqbnPG/ofepmCh9czAeET7G7EaY2D wZ+LOpatMINmn0P4a6WdnkKECIB1Yne/qwi4iegc4OITzoxvK519LdTYkaBxwSXKp1z9 TyDMxgE09IySLYJhnoPtloZtMAOuro/fpM5esAqcZDiN1kpunYVxxHsqrRmgI/7J4a+p 5rZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=xvhwewPL3C0UjcOHd+QXOzMItbY0AkXlRWPtFoN6OHY=; b=KXbZ8qbdxaTZH0RBjtdx0dTZabZL9LVx9V+8HYS8QTowdBo6fAFdfL9t3LI2s3HOxC oRWvMgvgM2GIwTwSopQ9nsfE7OgVCJi6sU+MbuA8RHHx85FMtbgm1VkLtKMKx/v2qyzh uAt8oDyXZsOeSJmLQ2ZfKzYze5Re4xAoVhGC8ZkZm2eL+NfQnttuacyPZmxc8PmnR9Q1 bX+W3yTmkCxZ+WwrdTcS/z6Y6zrw4/DTe8d6iYHFRqkvVIFJpchKQNQXTYwj/PVkMMuU udVUHyYBqQxah73ps05c8AxcrQW/vEKJH7Q5A8469D6jelRNZOc/3MQqfzZVBvvJg2/2 +5tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ibIOQAeG; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y130si3249517pfc.151.2021.04.27.03.42.07; Tue, 27 Apr 2021 03:42:20 -0700 (PDT) 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=@gmail.com header.s=20161025 header.b=ibIOQAeG; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235835AbhD0Kk6 (ORCPT + 99 others); Tue, 27 Apr 2021 06:40:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235755AbhD0Kk5 (ORCPT ); Tue, 27 Apr 2021 06:40:57 -0400 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B684AC061574; Tue, 27 Apr 2021 03:40:12 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id 12so92685578lfq.13; Tue, 27 Apr 2021 03:40:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xvhwewPL3C0UjcOHd+QXOzMItbY0AkXlRWPtFoN6OHY=; b=ibIOQAeGtqcrfPKVr8TVA50kIyzQZdD6xNy8AsaxOZ84egAcbeTj0TghFbEwctr6NI uhEWtLwyOWsYJ4/O2t4jwLDL3FSQeYtn1/y0I7H721fFkQG6pk9JVYpoXAevyLpdtAGJ yzslufotAM5sbtYYsZVgG4u6e89N3vNRsgHUAiUNqdTTnoDq2wrIGgdtVP5OjfU+bZ0G d7c/4rAyJRyl5kXFwvjjSeohNbwLVYHFnltRpjPkzcW3mYLBBXlj9DJ10yHT2CMTAsA2 e4iq8Ho8RdrEHeRDBd9K0Cg5wd/EEFBMPlB1gbGrZhY7aTCf0XBAQGWMXHF8Hi/EE+jl dZmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=xvhwewPL3C0UjcOHd+QXOzMItbY0AkXlRWPtFoN6OHY=; b=qf90eio8HowOFl0AKaYqkRcsIUDtj/pBk2o4kWx3aMmvlae3tBKyXbUGAAkNFw8xoS hKzQZPtqdnh539HXD0gIkQtlS1wxBdVSAxome2oPFxr71YXxK6jvlSSs1ankTZQz8Nat PC3DDdNEYaYRVTJZSrbEx3JSKRxnWuf3QaW5qReJOZimQWOzVLic1T+aOTNzaxh0nWtU ebbKSGKOOd0CWinAgOwCS23lctY4qBFL4x5hl3fYkPh2jf8l4r3rX/eI5QL9jGkrfHtl M03jx4ozXBE+Myx9TWHolxKxQU8COBVVpjLll9tBvEENSJo6rKD81PQmQ6hxk/sdaEo4 QhWg== X-Gm-Message-State: AOAM5338TOp3xUY5EDuzbLQtHr5iWD8IutTXtmvjmmQ6r04tp9T5+3fA WuFmCqhgrMsSt0FjfkaoPjNufEGAzgwtUDMbsqQ= X-Received: by 2002:a05:6512:3b89:: with SMTP id g9mr15453703lfv.499.1619520011115; Tue, 27 Apr 2021 03:40:11 -0700 (PDT) MIME-Version: 1.0 References: <00000000000022ebeb05bc39f582@google.com> In-Reply-To: From: Palash Oswal Date: Tue, 27 Apr 2021 16:09:59 +0530 Message-ID: Subject: Re: KASAN: null-ptr-deref Write in io_uring_cancel_sqpoll To: Pavel Begunkov Cc: Dmitry Vyukov , Jens Axboe , io-uring@vger.kernel.org, LKML , syzkaller-bugs , syzbot+be51ca5a4d97f017cd50@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 27, 2021 at 2:07 PM Pavel Begunkov wrote: > > io_sq_offload_create() { > ... > ret = io_uring_alloc_task_context(tsk, ctx); > wake_up_new_task(tsk); > if (ret) > goto err; > } > > Shouldn't happen unless offload create has failed. Just add > a return in *cancel_sqpoll() for this case. It's failing > so no requests has been submitted and no cancellation is needed. io_uring_cancel_sqpoll can be called by two flows: 1. io_uring_task_cancel() -> io_sqpoll_cancel_sync() -> io_uring_cancel_sqpoll ; which properly sanitises current->io_uring to be non NULL. ( https://elixir.bootlin.com/linux/v5.12/source/include/linux/io_uring.h#L21 ) 2. io_sq_offload_create -> io_sq_thread -> io_uring_cancel_sqpoll ; which does not check the value of current->io_uring In the second flow, https://elixir.bootlin.com/linux/v5.12/source/fs/io_uring.c#L7970 The initialization of current->io_uring (i.e io_uring_alloc_task_context() ) happens after calling io_sq_thread. And, therefore io_uring_cancel_sqpoll receives a NULL value for current->io_uring. The backtrace from the crash confirms the second scenario: [ 70.661551] ================================================================== [ 70.662764] BUG: KASAN: null-ptr-deref in io_uring_cancel_sqpoll+0x203/0x350 [ 70.663834] Write of size 4 at addr 0000000000000060 by task iou-sqp-750/755 [ 70.664025] [ 70.664025] CPU: 1 PID: 755 Comm: iou-sqp-750 Not tainted 5.12.0 #101 [ 70.664025] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-1 04/01/2014 [ 70.664025] Call Trace: [ 70.664025] dump_stack+0xe9/0x168 [ 70.664025] ? io_uring_cancel_sqpoll+0x203/0x350 [ 70.664025] __kasan_report+0x166/0x1c0 [ 70.664025] ? io_uring_cancel_sqpoll+0x203/0x350 [ 70.664025] kasan_report+0x4f/0x70 [ 70.664025] kasan_check_range+0x2f3/0x340 [ 70.664025] __kasan_check_write+0x14/0x20 [ 70.664025] io_uring_cancel_sqpoll+0x203/0x350 [ 70.664025] ? io_sq_thread_unpark+0xd0/0xd0 [ 70.664025] ? mutex_lock+0xbb/0x130 [ 70.664025] ? init_wait_entry+0xe0/0xe0 [ 70.664025] ? wait_for_completion_killable_timeout+0x20/0x20 [ 70.664025] io_sq_thread+0x174c/0x18c0 [ 70.664025] ? io_rsrc_put_work+0x380/0x380 [ 70.664025] ? init_wait_entry+0xe0/0xe0 [ 70.664025] ? _raw_spin_lock_irq+0xa5/0x180 [ 70.664025] ? _raw_spin_lock_irqsave+0x190/0x190 [ 70.664025] ? calculate_sigpending+0x6b/0xa0 [ 70.664025] ? io_rsrc_put_work+0x380/0x380 [ 70.664025] ret_from_fork+0x22/0x30 We might want to add additional validation before calling io_uring_cancel_sqpoll. I did verify that the reproducer stopped producing the bug after the following change. --- diff --git a/fs/io_uring.c b/fs/io_uring.c index dff34975d86b..36fc9abe8022 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -6832,8 +6832,10 @@ static int io_sq_thread(void *data) timeout = jiffies + sqd->sq_thread_idle; } - list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) - io_uring_cancel_sqpoll(ctx); + list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { + if (current->io_uring) + io_uring_cancel_sqpoll(ctx); + } sqd->thread = NULL; list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) io_ring_set_wakeup_flag(ctx);