Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp81082imm; Mon, 2 Jul 2018 08:02:00 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKv0t1DaumyM/KmQerZc4uNdJ53iq9DhW5G4j616f9Ag8X44GtwsLL1GgwssZxYH8KKx2uS X-Received: by 2002:a17:902:bd05:: with SMTP id p5-v6mr26556296pls.32.1530543720835; Mon, 02 Jul 2018 08:02:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530543720; cv=none; d=google.com; s=arc-20160816; b=lPYpuH+sG2TE9U0DMHQXLtutRT2IPebRlcIw9i/qPUIVmYnQah9KcryqzoJNmA44YR rNMZSFP7JZ/9i252uU5/H9rph26ndWixHD9DM+sVvPX87dSjyxRVirmfUXF/u5FXjfjb ID0ByheuygIYI0p45UtMrRb9nbiJM3qVhpUne7naN4xq69cZTY+25f/Lh3cIDKVB63MX cW+50vH1QfQb90wnufghMVQW97E7Qg3ux1AG17c0g/FwFI03uzkqcd6oarfjHkm0kfa7 MuC/BtEAylY0z04OBPFdVYyrTgE3wUf+teWg+ELDJHKdUw0g2sXHYrrj+8sk06rPGUXk dgiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from:arc-authentication-results; bh=z0fJeeBUACHt/bsGK+w0t0jTI38m0gwiOp3mDaclFI0=; b=QOXBOVjXC4LpH26cmXpgYSX8ad//yofl5pQlPibaaD51v7O6Iq2hbt8QZZliL797vc UTgoTPNZe5DaDrxY//ooKqgrDqFxTXieVwD+4cUC12cWrO3wEGxSTB3nNV1G9vNQnQn0 e7VF2Thd1Dnda44mTrEQ2u7Qkcw1cNhC8obyx3QLDHQml+0zl4FwX5hHoR1wwCp2WBU0 oB6rrayBEF8uAzM8vNoCIdUsRNCqYXseUe0rn98bmdFrtRrtzHZ05yFqrD98PBeYmqR7 9YLBHpttp7EEnf3YtaTVea5G/Y6rpOSCTGm64hZBmUblN0SGRNZ2sIitHpp0SE56Mzuc IF0w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l7-v6si11265488pgc.650.2018.07.02.08.01.39; Mon, 02 Jul 2018 08:02:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752456AbeGBPAq (ORCPT + 99 others); Mon, 2 Jul 2018 11:00:46 -0400 Received: from smtp03.citrix.com ([162.221.156.55]:30175 "EHLO SMTP03.CITRIX.COM" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751443AbeGBPAp (ORCPT ); Mon, 2 Jul 2018 11:00:45 -0400 X-IronPort-AV: E=Sophos;i="5.51,299,1526342400"; d="scan'208";a="59246822" From: Anoob Soman To: , , , , CC: , , Anoob Soman Subject: [RFC 1/1] libiscsi: Fix race between iscsi_xmit_task and iscsi_complete_task Date: Mon, 2 Jul 2018 16:00:24 +0100 Message-ID: <1530543624-130587-1-git-send-email-anoob.soman@citrix.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a target sends Check Condition, whilst initiator is busy xmiting re-queued data, could lead to race between iscsi_complete_task() and iscsi_xmit_task() and eventually crashing with the following kernel backtrace. [3326150.987523] ALERT: BUG: unable to handle kernel NULL pointer dereference at 0000000000000078 [3326150.987549] ALERT: IP: [] iscsi_xmit_task+0x2d/0xc0 [libiscsi] [3326150.987571] WARN: PGD 569c8067 PUD 569c9067 PMD 0 [3326150.987582] WARN: Oops: 0002 [#1] SMP [3326150.987593] WARN: Modules linked in: tun nfsv3 nfs fscache dm_round_robin [3326150.987762] WARN: CPU: 2 PID: 8399 Comm: kworker/u32:1 Tainted: G O 4.4.0+2 #1 [3326150.987774] WARN: Hardware name: Dell Inc. PowerEdge R720/0W7JN5, BIOS 2.5.4 01/22/2016 [3326150.987790] WARN: Workqueue: iscsi_q_13 iscsi_xmitworker [libiscsi] [3326150.987799] WARN: task: ffff8801d50f3800 ti: ffff8801f5458000 task.ti: ffff8801f5458000 [3326150.987810] WARN: RIP: e030:[] [] iscsi_xmit_task+0x2d/0xc0 [libiscsi] [3326150.987825] WARN: RSP: e02b:ffff8801f545bdb0 EFLAGS: 00010246 [3326150.987831] WARN: RAX: 00000000ffffffc3 RBX: ffff880282d2ab20 RCX: ffff88026b6ac480 [3326150.987842] WARN: RDX: 0000000000000000 RSI: 00000000fffffe01 RDI: ffff880282d2ab20 [3326150.987852] WARN: RBP: ffff8801f545bdc8 R08: 0000000000000000 R09: 0000000000000008 [3326150.987862] WARN: R10: 0000000000000000 R11: 000000000000fe88 R12: 0000000000000000 [3326150.987872] WARN: R13: ffff880282d2abe8 R14: ffff880282d2abd8 R15: ffff880282d2ac08 [3326150.987890] WARN: FS: 00007f5a866b4840(0000) GS:ffff88028a640000(0000) knlGS:0000000000000000 [3326150.987900] WARN: CS: e033 DS: 0000 ES: 0000 CR0: 0000000080050033 [3326150.987907] WARN: CR2: 0000000000000078 CR3: 0000000070244000 CR4: 0000000000042660 [3326150.987918] WARN: Stack: [3326150.987924] WARN: ffff880282d2ad58 ffff880282d2ab20 ffff880282d2abe8 ffff8801f545be18 [3326150.987938] WARN: ffffffffa05cea90 ffff880282d2abf8 ffff88026b59cc80 ffff88026b59cc00 [3326150.987951] WARN: ffff88022acf32c0 ffff880289491800 ffff880255a80800 0000000000000400 [3326150.987964] WARN: Call Trace: [3326150.987975] WARN: [] iscsi_xmitworker+0x2f0/0x360 [libiscsi] [3326150.987988] WARN: [] process_one_work+0x1fc/0x3b0 [3326150.987997] WARN: [] worker_thread+0x2a5/0x470 [3326150.988006] WARN: [] ? __schedule+0x648/0x870 [3326150.988015] WARN: [] ? rescuer_thread+0x300/0x300 [3326150.988023] WARN: [] kthread+0xd5/0xe0 [3326150.988031] WARN: [] ? kthread_stop+0x110/0x110 [3326150.988040] WARN: [] ret_from_fork+0x3f/0x70 [3326150.988048] WARN: [] ? kthread_stop+0x110/0x110 [3326150.988127] ALERT: RIP [] iscsi_xmit_task+0x2d/0xc0 [libiscsi] [3326150.988138] WARN: RSP [3326150.988144] WARN: CR2: 0000000000000078 [3326151.020366] WARN: ---[ end trace 1c60974d4678d81b ]--- Commit 6f8830f5bbab (scsi: libiscsi: add lock around task lists to fix list corruption regression) introduced "taskqueuelock" to fix list corruption during the race, but this wasn't enough. Re-setting of conn->task to NULL, could race with iscsi_xmit_task(). iscsi_complete_task() { .... if (conn->task == task) conn->task = NULL; } conn->task in iscsi_xmit_task() could be NULL and so will be task. __iscsi_get_task(task) will crash (NullPtr de-ref), trying to access refcount. iscsi_xmit_task() { struct iscsi_task *task = conn->task; __iscsi_get_task(task); } This commit will take extra conn->session->back_lock in iscsi_xmit_task() to ensure iscsi_xmit_task() waits for iscsi_complete_task(), if iscsi_complete_task() wins the race. If iscsi_xmit_task() wins the race, iscsi_xmit_task() increments task->refcount (__iscsi_get_task) ensuring iscsi_complete_task() will not iscsi_free_task(). Signed-off-by: Anoob Soman --- drivers/scsi/libiscsi.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c index d609383..aa3be6f 100644 --- a/drivers/scsi/libiscsi.c +++ b/drivers/scsi/libiscsi.c @@ -1449,7 +1449,13 @@ static int iscsi_xmit_task(struct iscsi_conn *conn) if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) return -ENODATA; + spin_lock_bh(&conn->session->back_lock); + if (conn->task == NULL) { + spin_unlock_bh(&conn->session->back_lock); + return -ENODATA; + } __iscsi_get_task(task); + spin_unlock_bh(&conn->session->back_lock); spin_unlock_bh(&conn->session->frwd_lock); rc = conn->session->tt->xmit_task(task); spin_lock_bh(&conn->session->frwd_lock); -- 1.8.3.1