Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp4109315ybe; Mon, 9 Sep 2019 04:28:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqxvaXDpJsWCcHXiHyvdScFVqDyxA42h0RS0PPyOZ8ERH7/AUaoH6N/3fFA6cTQX673Vv+V3 X-Received: by 2002:a17:906:938f:: with SMTP id l15mr18468513ejx.225.1568028499589; Mon, 09 Sep 2019 04:28:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568028499; cv=none; d=google.com; s=arc-20160816; b=AEx6SBH24p2Tmfvf2oiVZlGybH98Duw0qIVLDVf9VpzDO8S8z3RWU0FTBMt3TpTokC pkXsCKm1tacoYL3lqo8NKKtkSESr829wom3k6DnmZeVHRWYWP56gOdEFE5AkB6OHW6Uz qmH2It0Dz2G8GXgYAUj809k5fK7d3ajETl4/FH4Jt1bIFAt19j65klrqiblePbSEdM5D jOk6kMGgb4nwWBuRTWNcE2lH1FhvR8jsXSSSv4vfo+FAyLuRGgf/nnsne0PSHzBVEQAS kt7wnuimL0Wvk1Rlz1zg+dy/gDwYxsxNZkVahXbA/p+W1HgWra2I8w6GAIviXBA7A8bn fBgQ== 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=qGGQ8rXRlIUXbbY7In5V3SdBmGCnpZJDxSlACpQqr4o=; b=Hgp1kXt8SgkYmGvPzNNPn7yZI+yn8Ps40+IKr8VHBAXkP8+w7AGHMRcRo0l1RvN8pg rIbsIlelqfggYwIQDI8g/UxXJsFOCADKN5beb7AH7RvzKGShjMFFLTARgCYd9xrbmEy6 bKEHYQbW0roZhTxKgGurBH5f2Lf4qtHkFjCXC/o6X6br1NfAULb9tMkHkGXqInPWG0Pe PJ7FWIzSpFkgePvJQqjlgmEpKUWluV6uvs4xDJLrsVZN9TgjKowGaZUBGi62EKlxZK7C OKxtQOMI3WU5dNb0RvEg5YMfbDT5Rvcmw1JDQTse3FI9MiEcFN6OQSmfw9zTU3f6iScm Sg6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Q1JGD/E+"; 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 e50si8649078edb.177.2019.09.09.04.27.55; Mon, 09 Sep 2019 04:28:19 -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; dkim=pass header.i=@kernel.org header.s=default header.b="Q1JGD/E+"; 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 S1730762AbfIHMrk (ORCPT + 99 others); Sun, 8 Sep 2019 08:47:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:36130 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730756AbfIHMri (ORCPT ); Sun, 8 Sep 2019 08:47:38 -0400 Received: from localhost (unknown [62.28.240.114]) (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 4F132218AF; Sun, 8 Sep 2019 12:47:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567946857; bh=VQW4JxtBSA8uCuI6Kk12pcVkUqkLC1Wx3tvKKIQOyxg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q1JGD/E+234v8Lhkb6+/ZYPBoVHJoLoqHg66zRanilKQPuFNV7sKEgh3z/HbR51Pq l31jf6TmwHt7RKZZiHOwY0L0LMHgXlUBwk3xCPfKCn2ImqOjaKD15RkmyiT3Vp0SRi PXzT8oA5Ic+w7WlYE0JGNz6yQs2knOP4x5sQxwu0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dmitry Fomichev , Mike Christie , Damien Le Moal , "Martin K. Petersen" , Sasha Levin Subject: [PATCH 4.19 26/57] scsi: target: tcmu: avoid use-after-free after command timeout Date: Sun, 8 Sep 2019 13:41:50 +0100 Message-Id: <20190908121136.330463012@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190908121125.608195329@linuxfoundation.org> References: <20190908121125.608195329@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 [ Upstream commit a86a75865ff4d8c05f355d1750a5250aec89ab15 ] In tcmu_handle_completion() function, the variable called read_len is always initialized with a value taken from se_cmd structure. If this function is called to complete an expired (timed out) out command, the session command pointed by se_cmd is likely to be already deallocated by the target core at that moment. As the result, this access triggers a use-after-free warning from KASAN. This patch fixes the code not to touch se_cmd when completing timed out TCMU commands. It also resets the pointer to se_cmd at the time when the TCMU_CMD_BIT_EXPIRED flag is set because it is going to become invalid after calling target_complete_cmd() later in the same function, tcmu_check_expired_cmd(). Signed-off-by: Dmitry Fomichev Acked-by: Mike Christie Reviewed-by: Damien Le Moal Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/target/target_core_user.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/target/target_core_user.c b/drivers/target/target_core_user.c index c46efa47d68a5..7159e8363b83b 100644 --- a/drivers/target/target_core_user.c +++ b/drivers/target/target_core_user.c @@ -1143,14 +1143,16 @@ static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry * struct se_cmd *se_cmd = cmd->se_cmd; struct tcmu_dev *udev = cmd->tcmu_dev; bool read_len_valid = false; - uint32_t read_len = se_cmd->data_length; + uint32_t read_len; /* * cmd has been completed already from timeout, just reclaim * data area space and free cmd */ - if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) + if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { + WARN_ON_ONCE(se_cmd); goto out; + } list_del_init(&cmd->queue_entry); @@ -1163,6 +1165,7 @@ static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry * goto done; } + read_len = se_cmd->data_length; if (se_cmd->data_direction == DMA_FROM_DEVICE && (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) { read_len_valid = true; @@ -1318,6 +1321,7 @@ static int tcmu_check_expired_cmd(int id, void *p, void *data) */ scsi_status = SAM_STAT_CHECK_CONDITION; list_del_init(&cmd->queue_entry); + cmd->se_cmd = NULL; } else { list_del_init(&cmd->queue_entry); idr_remove(&udev->commands, id); @@ -2036,6 +2040,7 @@ static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level) idr_remove(&udev->commands, i); if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { + WARN_ON(!cmd->se_cmd); list_del_init(&cmd->queue_entry); if (err_level == 1) { /* -- 2.20.1