Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2692886ybl; Thu, 29 Aug 2019 11:33:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqwUT92FyfvG/bTXhMfH1rlmEdVJD7KTSo/PL1qXU7t3Svt4+EAwf9cKkiF0B/IioCMZfrSq X-Received: by 2002:a65:6102:: with SMTP id z2mr9549549pgu.391.1567103604584; Thu, 29 Aug 2019 11:33:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567103604; cv=none; d=google.com; s=arc-20160816; b=V7t4BMK2owd2BYbkvc1iFwQKwdUnTFPiC+c6kz3NKHScC0modZoFQXzG/TEVfgHyBq a68820TVgbJOWJUZXMy++0W1wrUq9Utt4ul7cUCQBtlCZJaNhMHcO50vEU1/uu6yA4FH ptUgKWXrf3jdMP1E11GmocS4kRwFYgXUf63C3lidcy1wMvavwx1c8J1HrJc+4uLIVPcj fIRB12st4Z6/droQOd3Xer84/hrLK2qr5u/ZebCqxddC8lxPbmH8IeqxrVwkjqBOrmCE NGzmoL8jk9ma8cZ6hU4W7V03UE+hH1pek8yThJu7h45kODofk5JaETA9l+wDf3HZblIQ qsbQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uMdpNgcUSp/7wxwkRUzbCoHfORO4mM+xvKqEEjzctjw=; b=NhNEUaM2P5dL/IYNU1tEkCAHDVL4/H3BwIZ2LvfcFSf2TNn8MD6rmLNnXs4Kb1yCp7 x8GS0Wt2k0jQHhqTifqgtlSnNV808DQMTmoYTbyPc8/PLy4rHfI4uT6UrVF+KTh+O81d vBKkVuxrwIeopnWiuiobxDuQJUmo2vhOb+Ek9wYbSuTQzD+NVauuP9FZLldLNa9h4uMc /FzurRQI411Q7fCjseYQotJ9PUCURI7DZBvE+HZTDNgiMu6LA1ZxFdOW7ePFmDzwde1r v3D19QAuq9iYEZlyK8cDkjEbuI2/plH6/4VZYuKaoRewSvDyqCX3XvIdQQZL7oQpd54a al+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="aB+Pt/Xe"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q28si3690130pfg.187.2019.08.29.11.33.09; Thu, 29 Aug 2019 11:33:24 -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="aB+Pt/Xe"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728808AbfH2SO0 (ORCPT + 99 others); Thu, 29 Aug 2019 14:14:26 -0400 Received: from mail.kernel.org ([198.145.29.99]:55770 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728708AbfH2SOD (ORCPT ); Thu, 29 Aug 2019 14:14:03 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 923D5233FF; Thu, 29 Aug 2019 18:14:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567102442; bh=ELasd/krHemGHvL2GqE3FAxmL4MQO+r/h4KTacutGvk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aB+Pt/XeB4nI2bXDn6jo67aYqMKUtctORiEnxL2vZa4FPHHE5uOyQdwfjVwKFNiAC v+zwZ+r5mPpub6Op7+EgJ8FTcTcTLP62QfBA/v6c/Xzg+E2nJkTM4A5V/ThDamb795 ribWY5x0sfOwfiWIV7BykjMidoipv0yY0SyVTKgY= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Dmitry Fomichev , Mike Christie , Damien Le Moal , "Martin K . Petersen" , Sasha Levin , linux-scsi@vger.kernel.org, target-devel@vger.kernel.org Subject: [PATCH AUTOSEL 5.2 30/76] scsi: target: tcmu: avoid use-after-free after command timeout Date: Thu, 29 Aug 2019 14:12:25 -0400 Message-Id: <20190829181311.7562-30-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190829181311.7562-1-sashal@kernel.org> References: <20190829181311.7562-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dmitry Fomichev [ 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 b43d6385a1a09..95b2371fb67b6 100644 --- a/drivers/target/target_core_user.c +++ b/drivers/target/target_core_user.c @@ -1132,14 +1132,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); @@ -1152,6 +1154,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; @@ -1307,6 +1310,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); @@ -2024,6 +2028,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