From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FC51C433B4 for ; Tue, 27 Apr 2021 10:40:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 39A6C61168 for ; Tue, 27 Apr 2021 10:40:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235794AbhD0Kk5 (ORCPT ); Tue, 27 Apr 2021 06:40:57 -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-Google-Smtp-Source: ABdhPJw5pW1PN4nky3oRnaLB5fO2j7LNViEhBqi3B3M37v6X45Fm7nlvuurm1/cbr3PnK03igGM8Gitq9GoTdgVOoTs= 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: io-uring@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);