Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5452982pxu; Tue, 22 Dec 2020 18:27:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJwvGW8+RgQ9PuieCZUDrxWx6BCOo5dwtEvuvvZUU2ayXMVpMfYGchdQLHm1rJQcCBq4VflA X-Received: by 2002:a17:906:d19b:: with SMTP id c27mr22819061ejz.234.1608690460632; Tue, 22 Dec 2020 18:27:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608690460; cv=none; d=google.com; s=arc-20160816; b=quIL77mchYjRxJC7Vi9cWyUpLL3RW3zyrW4NGHtV56z6C13Fn64o8Aey67/qae7N2M vF5cA1eFEYoVq21q0F7eVyegY+5d3ll+vzwvXAZy6EP5Spl0ofEgZYMZifQFhZqnYSic U6txZA/a4tSgKdo0l6ureQWd2THlpwMq/5UKhgj9Gx4Hxg2p1/di/IRqy4dbB2Do8PL5 PyTmf6R9zfQVmxzcWThL2jD1dPx/jlpzXYXhVz+fMCnct6PXqjBYy2qehh9vkBvOuIHi lEzUWtSYKv+qkCgGmJl1vwGs4YLINjJzOHK25annfU3eeHYPWdeIHheulD18BWGcaJTk DwWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uT4mF30/jnk3iZEl/MfHEXo5ITbFuyPSDfopdAGa19I=; b=0753VZ+pzz0atlPzrIjZFH3/20hy+8+IwLBNA+4MIHjO9JGwGbgS88FXyqb1xyR25V PQbLtaBgNE0E+S+BbNZhq1LebiFNQXf/AOfCbpgixffuqHuycEEIm8TWgB0lJik/aXUj Z4S/yZsxOSoz3WWFsyKZt21lTq40rkArYcEo+sZy/1FX/ku0zPSrJmE33/a/nuTAK3Qw y3bq1POF6Uj/aoxgx/rxKX2ZxItmoBG4ksRv6BhSAJTdvD1RHh0c/9NxFs57CJx2OUuK 1e5cj3FNnB/5YlbaqMsSM/GnqA7G+X0Bqc0Gj3KSSefjBdV1JU7JaVoE/obIZIJrIZr1 +xGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=S6xcJY+0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id qk27si11460554ejb.615.2020.12.22.18.27.18; Tue, 22 Dec 2020 18:27:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=S6xcJY+0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1730005AbgLWCX3 (ORCPT + 99 others); Tue, 22 Dec 2020 21:23:29 -0500 Received: from mail.kernel.org ([198.145.29.99]:51348 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729970AbgLWCXW (ORCPT ); Tue, 22 Dec 2020 21:23:22 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6FFD12222D; Wed, 23 Dec 2020 02:23:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1608690181; bh=fCdlmXLgDPyrQ6dRErNiTpZF3JOEKAXLORvAfBzKnfY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S6xcJY+0DcMiqrMzgl6bh5SYJzi7ht0KAacRTMZprOQVIvKXuHAk1hBZuQVZXjVOA R6VecXvIIpQizJCFGJe82lRrMI/Ml2V+mN+5pxXRpdwXtxRQWDvmCraC6TmLgweD5y gINbkasK4vfsXRHAnaVYegC7AnSp19q48i9d1I8BuaNhAl/9jecat6ybVLUlzrfOTY OXGURxv3XFpFEO7KBPKnP2JMguB5xXrDBxG1ntuUDGcgmcSAvuID9ZeeKd8i5qLrto 7BqYXwgQYKq15q77oalEoCtw5pLR2H5SIrL8l0r8ySMt4hovKyeNdGGWiFqKO5NXtB HQaY2Fc6z+abg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: yuuzheng , Jack Wang , Viswas G , Ruksar Devadi , Radha Ramachandran , "Martin K . Petersen" , Sasha Levin , linux-scsi@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 06/66] scsi: pm80xx: Fix pm8001_mpi_get_nvmd_resp() race condition Date: Tue, 22 Dec 2020 21:21:52 -0500 Message-Id: <20201223022253.2793452-6-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201223022253.2793452-1-sashal@kernel.org> References: <20201223022253.2793452-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: yuuzheng [ Upstream commit 1f889b58716a5f5e3e4fe0e6742c1a4472f29ac1 ] A use-after-free or null-pointer error occurs when the 251-byte response data is copied from IOMB buffer to response message buffer in function pm8001_mpi_get_nvmd_resp(). After sending the command get_nvmd_data(), the caller begins to sleep by calling wait_for_complete() and waits for the wake-up from calling complete() in pm8001_mpi_get_nvmd_resp(). Due to unexpected events (e.g., interrupt), if response buffer gets freed before memcpy(), a use-after-free error will occur. To fix this, the complete() should be called after memcpy(). Link: https://lore.kernel.org/r/20201102165528.26510-5-Viswas.G@microchip.com.com Acked-by: Jack Wang Signed-off-by: yuuzheng Signed-off-by: Viswas G Signed-off-by: Ruksar Devadi Signed-off-by: Radha Ramachandran Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/pm8001/pm8001_hwi.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/scsi/pm8001/pm8001_hwi.c b/drivers/scsi/pm8001/pm8001_hwi.c index f374abfb7f1f8..44a4630fdb2f8 100644 --- a/drivers/scsi/pm8001/pm8001_hwi.c +++ b/drivers/scsi/pm8001/pm8001_hwi.c @@ -3196,10 +3196,15 @@ pm8001_mpi_get_nvmd_resp(struct pm8001_hba_info *pm8001_ha, void *piomb) pm8001_ha->memoryMap.region[NVMD].virt_ptr, fw_control_context->len); kfree(ccb->fw_control_context); + /* To avoid race condition, complete should be + * called after the message is copied to + * fw_control_context->usrAddr + */ + complete(pm8001_ha->nvmd_completion); + PM8001_MSG_DBG(pm8001_ha, pm8001_printk("Set nvm data complete!\n")); ccb->task = NULL; ccb->ccb_tag = 0xFFFFFFFF; pm8001_tag_free(pm8001_ha, tag); - complete(pm8001_ha->nvmd_completion); } int pm8001_mpi_local_phy_ctl(struct pm8001_hba_info *pm8001_ha, void *piomb) -- 2.27.0