From patchwork Fri Jul 24 02:51:16 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nicholas A. Bellinger" X-Patchwork-Id: 6856841 Return-Path: X-Original-To: patchwork-linux-scsi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E58ECC05AC for ; Fri, 24 Jul 2015 02:56:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1362F20632 for ; Fri, 24 Jul 2015 02:56:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 081FB2053C for ; Fri, 24 Jul 2015 02:56:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753357AbbGXC4C (ORCPT ); Thu, 23 Jul 2015 22:56:02 -0400 Received: from mail-oi0-f45.google.com ([209.85.218.45]:32777 "EHLO mail-oi0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752553AbbGXC4B (ORCPT ); Thu, 23 Jul 2015 22:56:01 -0400 Received: by oige126 with SMTP id e126so9843690oig.0 for ; Thu, 23 Jul 2015 19:56:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=myMWgmBG0VMP6vf6tg9LRwEoyRSWPyEiHMdwh0WBnZ4=; b=Tr/xMhrGgOXkOMWkVU/dtF0zf1YhZZJ80c1PeAf454PF6P+C0zC40LUE/ssB/j7pT6 Ta/tA/ge/92d3tMlXUI2kJazHEL0/7g7S6Gz5ib0FE+9gSy890yuPZ2mtinPW53H9MTk ijCLW6OGxDrlpECabrkySzkRjl/54yhyDUM7BJYXDdRZ8CIJdt5k+s/C2n8BoGlWCkfK +HKbyZiPoMe+gjRDaaT5EXeEpy93S7qxa449a1jsRnjMCuFgxpaa/vKZjOM70juAB9PS tnIsFVxR4y4VQziJ38hjG/5p9vqyJsoyUi2XKVFWBXCLtEj+yliaMWWjkdmq+6K+494W oeOA== X-Gm-Message-State: ALoCoQlgK/LSbd8kMqBsQ2oYDUDWBSoIzzPTxhNhUUvHXkQT03a/tCZnoyw1L00cryY9j3aLqYw0 X-Received: by 10.202.231.210 with SMTP id e201mr12452032oih.88.1437706560603; Thu, 23 Jul 2015 19:56:00 -0700 (PDT) Received: from localhost.localdomain (mail.linux-iscsi.org. [67.23.28.174]) by smtp.gmail.com with ESMTPSA id xp8sm4094706obc.10.2015.07.23.19.55.59 (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 23 Jul 2015 19:55:59 -0700 (PDT) From: "Nicholas A. Bellinger" To: target-devel Cc: linux-scsi , Sagi Grimberg , Nicholas Bellinger , Sunilkumar Nadumuttlu Subject: [PATCH 1/4] iscsi-target: Fix use-after-free during TPG session shutdown Date: Fri, 24 Jul 2015 02:51:16 +0000 Message-Id: <1437706279-10580-2-git-send-email-nab@daterainc.com> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1437706279-10580-1-git-send-email-nab@daterainc.com> References: <1437706279-10580-1-git-send-email-nab@daterainc.com> Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Nicholas Bellinger This patch fixes a use-after-free bug in iscsit_release_sessions_for_tpg() where se_portal_group->session_lock was incorrectly released/re-acquired while walking the active se_portal_group->tpg_sess_list. The can result in a NULL pointer dereference when iscsit_close_session() shutdown happens in the normal path asynchronously to this code, causing a bogus dereference of an already freed list entry to occur. To address this bug, walk the session list checking for the same state as before, but move entries to a local list to avoid dropping the lock while walking the active list. As before, signal using iscsi_session->session_restatement=1 for those list entries to be released locally by iscsit_free_session() code. Reported-by: Sunilkumar Nadumuttlu Cc: Sunilkumar Nadumuttlu Cc: # v3.1+ Signed-off-by: Nicholas Bellinger --- drivers/target/iscsi/iscsi_target.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/target/iscsi/iscsi_target.c b/drivers/target/iscsi/iscsi_target.c index 4e68b62..afab323 100644 --- a/drivers/target/iscsi/iscsi_target.c +++ b/drivers/target/iscsi/iscsi_target.c @@ -4765,6 +4765,7 @@ int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force) struct iscsi_session *sess; struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; struct se_session *se_sess, *se_sess_tmp; + LIST_HEAD(free_list); int session_count = 0; spin_lock_bh(&se_tpg->session_lock); @@ -4786,14 +4787,17 @@ int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force) } atomic_set(&sess->session_reinstatement, 1); spin_unlock(&sess->conn_lock); - spin_unlock_bh(&se_tpg->session_lock); - iscsit_free_session(sess); - spin_lock_bh(&se_tpg->session_lock); + list_move_tail(&se_sess->sess_list, &free_list); + } + spin_unlock_bh(&se_tpg->session_lock); + list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) { + sess = (struct iscsi_session *)se_sess->fabric_sess_ptr; + + iscsit_free_session(sess); session_count++; } - spin_unlock_bh(&se_tpg->session_lock); pr_debug("Released %d iSCSI Session(s) from Target Portal" " Group: %hu\n", session_count, tpg->tpgt);