Received: by 2002:a17:90a:9103:0:0:0:0 with SMTP id k3csp11787616pjo; Thu, 2 Jan 2020 14:16:57 -0800 (PST) X-Google-Smtp-Source: APXvYqyNDWPwM8cRxCzllzyJSbbfteYwgvFT+5KPBKG+MzRnnFZblCpPbpRNuO3y28OXPmrqExbB X-Received: by 2002:a05:6830:2361:: with SMTP id r1mr88598850oth.88.1578003417544; Thu, 02 Jan 2020 14:16:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578003417; cv=none; d=google.com; s=arc-20160816; b=EPjc81Rg7o/FSkhiAmOFjE9w+YyGn/tMBAkTnQYBq6VIKsSvdtUk8yyM51j6/CmgAx FtXRS1gUIjg6ruWKy9GT+SM/TVgxVTUifIl/d9ZAZldEkpRb34c6bu68q5ZZy0NXnwRn mbnSGaDaciFIH1g+x/cgUDXmiha2k2hbIHqw1/Ub1qo98j3GS7Dtfvh0IAqM1GJW9lDj b49ZkBDXXProPLpj6LT4gje+4UORjGrPg1cuTzWJXC0oXvVsEtqL7A1aDyshlZ0iwipB Xk1a0Dka0B7GJbpIRoE0RLsJVb+MpWs09fwJozFW6pIad66luK31csyRhrDqmHqxqvFO ofLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pcCcS4NR6vDRhIVBk0+N4k+daiOMdcWF6yCu7uMHRf8=; b=MJsUNUTR7XyeWlrL8jFHav0wGBG7kWC7y9wVMaUoiPi99Op2T0wWS/vyMtJGmGQWkV tvjlwmScmJhO+I//HbJZlIJ6+1f48RQeTX3NqfqMoROryhhu5ESYOTtQZny6T4n6B3s1 nLeoRVic6Qr/yvwibR0u1dr+Uu8OAcgFSozs/AGi4WgL44xHfCNbD0yTiVTJsp9Ho9GZ AiZcRb/hLMirP9JLF8dK8XdSb8xdyuP2WsssFdRvJpaQZv1cCM5h+ec5O67OqcRPc1De CJ3B29HgCLwRxPuJbQ6fQ6nJm+kIwcbb4cFVAVttPB2VPyI6ad0tvsIulVMwg6pvLQNm LZZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MUX3nYB5; 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 g5si30795772otn.232.2020.01.02.14.16.45; Thu, 02 Jan 2020 14:16:57 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=MUX3nYB5; 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 S1727847AbgABWPj (ORCPT + 99 others); Thu, 2 Jan 2020 17:15:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:56606 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727233AbgABWPd (ORCPT ); Thu, 2 Jan 2020 17:15:33 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 40308227BF; Thu, 2 Jan 2020 22:15:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578003332; bh=myurz9Mbl4cu2OyNTosGleoyKh9RLVlKrS/4ZRULdsI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MUX3nYB5xN9rxjRkTX/ll18Bw2SxSMOxWakMajXiwPh3b8/Kjm5+xD6/3rY2ZEkfH u4UhWvAJlTYquBLsGj++75bmMgwlUe1axlDHVEdL8BOwLkOqILmSOm+rum2ApMIvqL OXOcySdvK+6IIfiZzFbtmkvgYjavZ88BioaD1Pg8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mike Christie , Roman Bolshakov , Bart Van Assche , "Martin K. Petersen" , Sasha Levin Subject: [PATCH 5.4 091/191] scsi: target: core: Release SPC-2 reservations when closing a session Date: Thu, 2 Jan 2020 23:06:13 +0100 Message-Id: <20200102215839.679018970@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200102215829.911231638@linuxfoundation.org> References: <20200102215829.911231638@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bart Van Assche [ Upstream commit 80647a89eaf3f2549741648f3230cd6ff68c23b4 ] The SCSI specs require releasing SPC-2 reservations when a session is closed. Make sure that the target core does this. Running the libiscsi tests triggers the KASAN complaint shown below. This patch fixes that use-after-free. BUG: KASAN: use-after-free in target_check_reservation+0x171/0x980 [target_core_mod] Read of size 8 at addr ffff88802ecd1878 by task iscsi_trx/17200 CPU: 0 PID: 17200 Comm: iscsi_trx Not tainted 5.4.0-rc1-dbg+ #1 Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011 Call Trace: dump_stack+0x8a/0xd6 print_address_description.constprop.0+0x40/0x60 __kasan_report.cold+0x1b/0x34 kasan_report+0x16/0x20 __asan_load8+0x58/0x90 target_check_reservation+0x171/0x980 [target_core_mod] __target_execute_cmd+0xb1/0xf0 [target_core_mod] target_execute_cmd+0x22d/0x4d0 [target_core_mod] transport_generic_new_cmd+0x31f/0x5b0 [target_core_mod] transport_handle_cdb_direct+0x6f/0x90 [target_core_mod] iscsit_execute_cmd+0x381/0x3f0 [iscsi_target_mod] iscsit_sequence_cmd+0x13b/0x1f0 [iscsi_target_mod] iscsit_process_scsi_cmd+0x4c/0x130 [iscsi_target_mod] iscsit_get_rx_pdu+0x8e8/0x15f0 [iscsi_target_mod] iscsi_target_rx_thread+0x105/0x1b0 [iscsi_target_mod] kthread+0x1bc/0x210 ret_from_fork+0x24/0x30 Allocated by task 1079: save_stack+0x23/0x90 __kasan_kmalloc.constprop.0+0xcf/0xe0 kasan_slab_alloc+0x12/0x20 kmem_cache_alloc+0xfe/0x3a0 transport_alloc_session+0x29/0x80 [target_core_mod] iscsi_target_login_thread+0xceb/0x1920 [iscsi_target_mod] kthread+0x1bc/0x210 ret_from_fork+0x24/0x30 Freed by task 17193: save_stack+0x23/0x90 __kasan_slab_free+0x13a/0x190 kasan_slab_free+0x12/0x20 kmem_cache_free+0xc8/0x3e0 transport_free_session+0x179/0x2f0 [target_core_mod] transport_deregister_session+0x121/0x170 [target_core_mod] iscsit_close_session+0x12c/0x350 [iscsi_target_mod] iscsit_logout_post_handler+0x136/0x380 [iscsi_target_mod] iscsit_response_queue+0x8fa/0xc00 [iscsi_target_mod] iscsi_target_tx_thread+0x28e/0x390 [iscsi_target_mod] kthread+0x1bc/0x210 ret_from_fork+0x24/0x30 The buggy address belongs to the object at ffff88802ecd1860 which belongs to the cache se_sess_cache of size 352 The buggy address is located 24 bytes inside of 352-byte region [ffff88802ecd1860, ffff88802ecd19c0) The buggy address belongs to the page: page:ffffea0000bb3400 refcount:1 mapcount:0 mapping:ffff8880bef2ed00 index:0x0 compound_mapcount: 0 flags: 0x1000000000010200(slab|head) raw: 1000000000010200 dead000000000100 dead000000000122 ffff8880bef2ed00 raw: 0000000000000000 0000000080270027 00000001ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff88802ecd1700: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ffff88802ecd1780: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb >ffff88802ecd1800: fb fb fb fb fc fc fc fc fc fc fc fc fb fb fb fb ^ ffff88802ecd1880: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ffff88802ecd1900: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb Cc: Mike Christie Link: https://lore.kernel.org/r/20191113220508.198257-2-bvanassche@acm.org Reviewed-by: Roman Bolshakov Signed-off-by: Bart Van Assche Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/target/target_core_transport.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c index 7f06a62f8661..eda8b4736c15 100644 --- a/drivers/target/target_core_transport.c +++ b/drivers/target/target_core_transport.c @@ -584,6 +584,15 @@ void transport_free_session(struct se_session *se_sess) } EXPORT_SYMBOL(transport_free_session); +static int target_release_res(struct se_device *dev, void *data) +{ + struct se_session *sess = data; + + if (dev->reservation_holder == sess) + target_release_reservation(dev); + return 0; +} + void transport_deregister_session(struct se_session *se_sess) { struct se_portal_group *se_tpg = se_sess->se_tpg; @@ -600,6 +609,12 @@ void transport_deregister_session(struct se_session *se_sess) se_sess->fabric_sess_ptr = NULL; spin_unlock_irqrestore(&se_tpg->session_lock, flags); + /* + * Since the session is being removed, release SPC-2 + * reservations held by the session that is disappearing. + */ + target_for_each_device(target_release_res, se_sess); + pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n", se_tpg->se_tpg_tfo->fabric_name); /* -- 2.20.1