From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.88; helo=mga01.intel.com; envelope-from=star.zeng@intel.com; receiver=edk2-devel@lists.01.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 3747E21179256 for ; Mon, 15 Oct 2018 22:28:06 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Oct 2018 22:28:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,387,1534834800"; d="scan'208";a="100577376" Received: from shzintpr01.sh.intel.com (HELO [10.7.209.21]) ([10.239.4.80]) by orsmga002.jf.intel.com with ESMTP; 15 Oct 2018 22:28:04 -0700 To: Ruiyu Ni , edk2-devel@lists.01.org Cc: Hao Wu , Jiewen Yao , star.zeng@intel.com References: <20181015063833.61304-1-ruiyu.ni@intel.com> <20181015063833.61304-12-ruiyu.ni@intel.com> From: "Zeng, Star" Message-ID: Date: Tue, 16 Oct 2018 13:27:33 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20181015063833.61304-12-ruiyu.ni@intel.com> Subject: Re: [PATCH 11/11] MdeModulePkg/Bus/Ufs: Ensure device not return more data than expected X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Oct 2018 05:28:06 -0000 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit On 2018/10/15 14:38, Ruiyu Ni wrote: > From: Hao Wu > > This commit adds checks to make sure the UFS devices do not return more > data than the driver expected. > > Cc: Ruiyu Ni > Cc: Jiewen Yao > Cc: Star Zeng > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Hao Wu Thanks for the patch. Reviewed-by: Star Zeng Star > --- > MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c | 19 ++++++++++++-- > .../Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c | 30 +++++++++++++++++++--- > 2 files changed, 43 insertions(+), 6 deletions(-) > > diff --git a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c b/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c > index 936f25da5e..9b87835ed8 100644 > --- a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c > +++ b/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c > @@ -857,6 +857,14 @@ UfsRwDeviceDesc ( > SwapLittleEndianToBigEndian ((UINT8*)&ReturnDataSize, sizeof (UINT16)); > > if (Read) { > + // > + // Make sure the hardware device does not return more data than expected. > + // > + if (ReturnDataSize > Packet.InTransferLength) { > + Status = EFI_DEVICE_ERROR; > + goto Exit; > + } > + > CopyMem (Packet.InDataBuffer, (QueryResp + 1), ReturnDataSize); > Packet.InTransferLength = ReturnDataSize; > } else { > @@ -1170,8 +1178,15 @@ UfsExecScsiCmds ( > SwapLittleEndianToBigEndian ((UINT8*)&SenseDataLen, sizeof (UINT16)); > > if ((Packet->SenseDataLength != 0) && (Packet->SenseData != NULL)) { > - CopyMem (Packet->SenseData, Response->SenseData, SenseDataLen); > - Packet->SenseDataLength = (UINT8)SenseDataLen; > + // > + // Make sure the hardware device does not return more data than expected. > + // > + if (SenseDataLen <= Packet->SenseDataLength) { > + CopyMem (Packet->SenseData, Response->SenseData, SenseDataLen); > + Packet->SenseDataLength = (UINT8)SenseDataLen; > + } else { > + Packet->SenseDataLength = 0; > + } > } > > // > diff --git a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c b/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c > index 5756f637fd..0238264878 100644 > --- a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c > +++ b/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c > @@ -833,6 +833,7 @@ UfsStopExecCmd ( > @param[in] QueryResp Pointer to the query response. > > @retval EFI_INVALID_PARAMETER Packet or QueryResp are empty or opcode is invalid. > + @retval EFI_DEVICE_ERROR Data returned from device is invalid. > @retval EFI_SUCCESS Data extracted. > > **/ > @@ -853,6 +854,13 @@ UfsGetReturnDataFromQueryResponse ( > case UtpQueryFuncOpcodeRdDesc: > ReturnDataSize = QueryResp->Tsf.Length; > SwapLittleEndianToBigEndian ((UINT8*)&ReturnDataSize, sizeof (UINT16)); > + // > + // Make sure the hardware device does not return more data than expected. > + // > + if (ReturnDataSize > Packet->TransferLength) { > + return EFI_DEVICE_ERROR; > + } > + > CopyMem (Packet->DataBuffer, (QueryResp + 1), ReturnDataSize); > Packet->TransferLength = ReturnDataSize; > break; > @@ -1469,8 +1477,15 @@ UfsExecScsiCmds ( > SwapLittleEndianToBigEndian ((UINT8*)&SenseDataLen, sizeof (UINT16)); > > if ((Packet->SenseDataLength != 0) && (Packet->SenseData != NULL)) { > - CopyMem (Packet->SenseData, Response->SenseData, SenseDataLen); > - Packet->SenseDataLength = (UINT8)SenseDataLen; > + // > + // Make sure the hardware device does not return more data than expected. > + // > + if (SenseDataLen <= Packet->SenseDataLength) { > + CopyMem (Packet->SenseData, Response->SenseData, SenseDataLen); > + Packet->SenseDataLength = (UINT8)SenseDataLen; > + } else { > + Packet->SenseDataLength = 0; > + } > } > > // > @@ -2226,8 +2241,15 @@ ProcessAsyncTaskList ( > SwapLittleEndianToBigEndian ((UINT8*)&SenseDataLen, sizeof (UINT16)); > > if ((Packet->SenseDataLength != 0) && (Packet->SenseData != NULL)) { > - CopyMem (Packet->SenseData, Response->SenseData, SenseDataLen); > - Packet->SenseDataLength = (UINT8)SenseDataLen; > + // > + // Make sure the hardware device does not return more data than expected. > + // > + if (SenseDataLen <= Packet->SenseDataLength) { > + CopyMem (Packet->SenseData, Response->SenseData, SenseDataLen); > + Packet->SenseDataLength = (UINT8)SenseDataLen; > + } else { > + Packet->SenseDataLength = 0; > + } > } > > // >