Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp383151pxj; Thu, 27 May 2021 02:28:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRftqwD8fUPC/AudE8ydx7tKA+mujHMQclG1UoazbdLdcrDgzVWcngwZob8OYMJDVtk5AO X-Received: by 2002:a6b:7947:: with SMTP id j7mr990523iop.0.1622107715409; Thu, 27 May 2021 02:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622107715; cv=none; d=google.com; s=arc-20160816; b=PH9fZKC0ESur4UnaEnP0N72/58TJul/3ADNThihbWPZZv06NNwoikcIFhpxV+fm54x xrYutEnnA6OTvxb1hcYsKUZGLilAQ6WTNfwR3E7b5FLPUvqHv7S8bYpbhw5+S7QP73YJ XjFit6MaFPsXXIb9t+wPnbS6nkI93+8FHH4NwHsFhtzm5DivDBYrDz6Ab4BYibgNYHj7 RcLxYJkl2w9gbOxFBqd/d7zQhKawIDARfzTES9gk2HbdD3tRdGGFBPqr3w1t/Rms1XG1 qwb52Uf2HzK/tXFyTprykH+dC9LP8nydI4MSl7gqP4Pw+g7fmKzvewrYlEYx6NluDQjT 8TvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=tpMhr1a1ucqmSn8aEv/I+3Jaj5nsK3OJYJ5e/mt9yGY=; b=SpLDjxbjXuNtDjSs/iGLi4vjThDChAxKjThW+y5J2bAjSZaBT4/qZkr9G94n98uOmp PEoU822RDonnifur/iaQKC+U0naxJ8yJgwuLHDcndzGewYmRozQ18aZtewfHQrTkHxu3 ZO9TQ9fjLuCtfmZQe7t0t9T3X1l452vO7U4Mo9PMAtkrrXUpQ3OQo57mNzCo1GiMOk/v R56DEG3m30eD4cems0Oplu6r7gz6kvAok2xp+B78M/uuCTnAMHKzO7RW+O3dbtsqtYqw J9ghUrDiCVcHfg43smJS5bL2XxFxl+DihD0htAUqrWwtbI7XSaAtpeNLC+NgorPUha46 ymEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="Q/vJeL9s"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s4si1983683jan.21.2021.05.27.02.28.15; Thu, 27 May 2021 02:28:35 -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=@google.com header.s=20161025 header.b="Q/vJeL9s"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235720AbhE0J1r (ORCPT + 99 others); Thu, 27 May 2021 05:27:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235649AbhE0J1p (ORCPT ); Thu, 27 May 2021 05:27:45 -0400 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFA0DC061760 for ; Thu, 27 May 2021 02:26:12 -0700 (PDT) Received: by mail-ed1-x549.google.com with SMTP id da10-20020a056402176ab029038f0fea1f51so1084edb.13 for ; Thu, 27 May 2021 02:26:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=tpMhr1a1ucqmSn8aEv/I+3Jaj5nsK3OJYJ5e/mt9yGY=; b=Q/vJeL9sXW+UWiU6U9hHz5sKys3i1VhTtuYfdmDOH6LWAJv59c7xS+WCzSmCqg2YYY oWgTSehjunfrSUIU9EvVXy+QBtMvEcc54tDlaraHYWHHR+Ub0ShwT0O+odW2IfZFZH+X LsaLOuJk+QZtLb07YaaF+ZpjF1d9LWfPowMGmtkeUXB+BwxP9oxHvBU7DyANkveEESI9 xEYfQFccuWJOV3H/lLV33lDC/8VOP9eON2h8UK+IWLIPgIh/1O3QSpmNGGwF9LfW2rNo FLvtC/+C6Ks0XV5ljtiUfzZaPd5jgzusKowd/LtQU5mxdEdSBbshbyxdmiQSJcZxJGfQ GOAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=tpMhr1a1ucqmSn8aEv/I+3Jaj5nsK3OJYJ5e/mt9yGY=; b=K9aRfIwkACwZ4THds74e9zZBVj1xZZsAlllUsA7mKUFPNPxAESdFuroYX1MONcVOH2 hDTPbfozxddFLx7/PQcFVN01XBk7wwjnFEEZsP9FI8FzbgroqTzbJBEbi/P/+0FrKLhP NJjJQEudeGaPGOPkjPo6clagDSNMVsaCBjrPJ+2OJ972tYiPryDYLkU98AZ9OmXAtlf3 +4zF6iujMhhna6IwxFAbCNZ2KtSvoc7++2UG7i0hWHCKDev+vwMpxOhyhUBYC+WiKVud O0xwtIU9rsxX5fPfDDkJj37ngVF+a9nkUSHA3l6fevZRRxOABKj3mlG0S0xkit7LSSTq xMLA== X-Gm-Message-State: AOAM531WKWSJd1MKKZKVkwH4lKp47wnbw4MqItyKVMuF1uaTrtXpfa5q hXn6HiLvc8TX9/bKWBMheLwtUIFJ5A== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:74ba:ff42:8494:7f35]) (user=elver job=sendgmr) by 2002:a05:6402:1c97:: with SMTP id cy23mr3063078edb.213.1622107570950; Thu, 27 May 2021 02:26:10 -0700 (PDT) Date: Thu, 27 May 2021 11:25:48 +0200 Message-Id: <20210527092547.2656514-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.818.g46aad6cb9e-goog Subject: [PATCH] io_uring: fix data race to avoid potential NULL-deref From: Marco Elver To: elver@google.com, axboe@kernel.dk, asml.silence@gmail.com, io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Cc: kasan-dev@googlegroups.com, dvyukov@google.com, syzbot+bf2b3d0435b9b728946c@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit ba5ef6dc8a82 ("io_uring: fortify tctx/io_wq cleanup") introduced setting tctx->io_wq to NULL a bit earlier. This has caused KCSAN to detect a data race between accesses to tctx->io_wq: write to 0xffff88811d8df330 of 8 bytes by task 3709 on cpu 1: io_uring_clean_tctx fs/io_uring.c:9042 [inline] __io_uring_cancel fs/io_uring.c:9136 io_uring_files_cancel include/linux/io_uring.h:16 [inline] do_exit kernel/exit.c:781 do_group_exit kernel/exit.c:923 get_signal kernel/signal.c:2835 arch_do_signal_or_restart arch/x86/kernel/signal.c:789 handle_signal_work kernel/entry/common.c:147 [inline] exit_to_user_mode_loop kernel/entry/common.c:171 [inline] ... read to 0xffff88811d8df330 of 8 bytes by task 6412 on cpu 0: io_uring_try_cancel_iowq fs/io_uring.c:8911 [inline] io_uring_try_cancel_requests fs/io_uring.c:8933 io_ring_exit_work fs/io_uring.c:8736 process_one_work kernel/workqueue.c:2276 ... With the config used, KCSAN only reports data races with value changes: this implies that in the case here we also know that tctx->io_wq was non-NULL. Therefore, depending on interleaving, we may end up with: [CPU 0] | [CPU 1] io_uring_try_cancel_iowq() | io_uring_clean_tctx() if (!tctx->io_wq) // false | ... ... | tctx->io_wq = NULL io_wq_cancel_cb(tctx->io_wq, ...) | ... -> NULL-deref | Note: It is likely that thus far we've gotten lucky and the compiler optimizes the double-read into a single read into a register -- but this is never guaranteed, and can easily change with a different config! Fix the data race by restoring the previous behaviour, where both setting io_wq to NULL and put of the wq are _serialized_ after concurrent io_uring_try_cancel_iowq() via acquisition of the uring_lock and removal of the node in io_uring_del_task_file(). Fixes: ba5ef6dc8a82 ("io_uring: fortify tctx/io_wq cleanup") Suggested-by: Pavel Begunkov Reported-by: syzbot+bf2b3d0435b9b728946c@syzkaller.appspotmail.com Signed-off-by: Marco Elver Cc: Jens Axboe --- fs/io_uring.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 5f82954004f6..08830b954fbf 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -9039,11 +9039,16 @@ static void io_uring_clean_tctx(struct io_uring_task *tctx) struct io_tctx_node *node; unsigned long index; - tctx->io_wq = NULL; xa_for_each(&tctx->xa, index, node) io_uring_del_task_file(index); - if (wq) + if (wq) { + /* + * Must be after io_uring_del_task_file() (removes nodes under + * uring_lock) to avoid race with io_uring_try_cancel_iowq(). + */ + tctx->io_wq = NULL; io_wq_put_and_exit(wq); + } } static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) -- 2.31.1.818.g46aad6cb9e-goog