[06/10] platform/x86/intel/ifs: Validate image size

Message ID 20230913183348.1349409-7-jithu.joseph@intel.com
State New
Headers
Series IFS support for GNR and SRF |

Commit Message

Jithu Joseph Sept. 13, 2023, 6:33 p.m. UTC
  Perform additional validation prior to loading IFS image.

Error out if the size of the file being loaded doesn't
match the size specified in the header.

Signed-off-by: Jithu Joseph <jithu.joseph@intel.com>
Reviewed-by: Tony Luck <tony.luck@intel.com>
Tested-by: Pengfei Xu <pengfei.xu@intel.com>
---
 drivers/platform/x86/intel/ifs/load.c | 8 ++++++++
 1 file changed, 8 insertions(+)
  

Comments

Ilpo Järvinen Sept. 15, 2023, 4:57 p.m. UTC | #1
On Wed, 13 Sep 2023, Jithu Joseph wrote:

> Perform additional validation prior to loading IFS image.
> 
> Error out if the size of the file being loaded doesn't
> match the size specified in the header.

Please fix these short lines in all your patches.
 
> Signed-off-by: Jithu Joseph <jithu.joseph@intel.com>
> Reviewed-by: Tony Luck <tony.luck@intel.com>
> Tested-by: Pengfei Xu <pengfei.xu@intel.com>
> ---
>  drivers/platform/x86/intel/ifs/load.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/platform/x86/intel/ifs/load.c b/drivers/platform/x86/intel/ifs/load.c
> index e8fb03dd8bcf..778a3b89a24d 100644
> --- a/drivers/platform/x86/intel/ifs/load.c
> +++ b/drivers/platform/x86/intel/ifs/load.c
> @@ -376,6 +376,7 @@ int ifs_load_firmware(struct device *dev)
>  {
>  	const struct ifs_test_caps *test = ifs_get_test_caps(dev);
>  	struct ifs_data *ifsd = ifs_get_data(dev);
> +	unsigned int expected_size;
>  	const struct firmware *fw;
>  	char scan_path[64];
>  	int ret = -EINVAL;
> @@ -390,6 +391,13 @@ int ifs_load_firmware(struct device *dev)
>  		goto done;
>  	}
>  
> +	expected_size = ((struct microcode_header_intel *)fw->data)->totalsize;
> +	if (fw->size != expected_size) {
> +		dev_err(dev, "File size mismatch (expected %d, actual %ld). Corrupted IFS image.\n",
> +			expected_size, fw->size);
> +		return -EBADFD;
> +	}
> +
>  	ret = image_sanity_check(dev, (struct microcode_header_intel *)fw->data);

It looks than a bit odd to add the check here and not into a function 
called image_sanity_check()?!?

>  	if (ret)
>  		goto release;
>
  
Jithu Joseph Sept. 15, 2023, 6:06 p.m. UTC | #2
On 9/15/2023 9:57 AM, Ilpo Järvinen wrote:
> On Wed, 13 Sep 2023, Jithu Joseph wrote:
> 
>> Perform additional validation prior to loading IFS image.
>>
>> Error out if the size of the file being loaded doesn't
>> match the size specified in the header.
> 
> Please fix these short lines in all your patches.

Will do

>  
>> Signed-off-by: Jithu Joseph <jithu.joseph@intel.com>
>> Reviewed-by: Tony Luck <tony.luck@intel.com>
>> Tested-by: Pengfei Xu <pengfei.xu@intel.com>
>> ---
>>  drivers/platform/x86/intel/ifs/load.c | 8 ++++++++
>>  1 file changed, 8 insertions(+)
>>
>> diff --git a/drivers/platform/x86/intel/ifs/load.c b/drivers/platform/x86/intel/ifs/load.c
>> index e8fb03dd8bcf..778a3b89a24d 100644
>> --- a/drivers/platform/x86/intel/ifs/load.c
>> +++ b/drivers/platform/x86/intel/ifs/load.c
>> @@ -376,6 +376,7 @@ int ifs_load_firmware(struct device *dev)
>>  {
>>  	const struct ifs_test_caps *test = ifs_get_test_caps(dev);
>>  	struct ifs_data *ifsd = ifs_get_data(dev);
>> +	unsigned int expected_size;
>>  	const struct firmware *fw;
>>  	char scan_path[64];
>>  	int ret = -EINVAL;
>> @@ -390,6 +391,13 @@ int ifs_load_firmware(struct device *dev)
>>  		goto done;
>>  	}
>>  
>> +	expected_size = ((struct microcode_header_intel *)fw->data)->totalsize;
>> +	if (fw->size != expected_size) {
>> +		dev_err(dev, "File size mismatch (expected %d, actual %ld). Corrupted IFS image.\n",
>> +			expected_size, fw->size);
>> +		return -EBADFD;
>> +	}
>> +
>>  	ret = image_sanity_check(dev, (struct microcode_header_intel *)fw->data);
> 
> It looks than a bit odd to add the check here and not into a function 
> called image_sanity_check()?!?

image_sanity_check() validates the contents of the image, whereas the new check
in some sense validates request_firmware_direct() results. Hence it was placed
outside of content validation / closer to request_firmware_direct()


Jithu
  

Patch

diff --git a/drivers/platform/x86/intel/ifs/load.c b/drivers/platform/x86/intel/ifs/load.c
index e8fb03dd8bcf..778a3b89a24d 100644
--- a/drivers/platform/x86/intel/ifs/load.c
+++ b/drivers/platform/x86/intel/ifs/load.c
@@ -376,6 +376,7 @@  int ifs_load_firmware(struct device *dev)
 {
 	const struct ifs_test_caps *test = ifs_get_test_caps(dev);
 	struct ifs_data *ifsd = ifs_get_data(dev);
+	unsigned int expected_size;
 	const struct firmware *fw;
 	char scan_path[64];
 	int ret = -EINVAL;
@@ -390,6 +391,13 @@  int ifs_load_firmware(struct device *dev)
 		goto done;
 	}
 
+	expected_size = ((struct microcode_header_intel *)fw->data)->totalsize;
+	if (fw->size != expected_size) {
+		dev_err(dev, "File size mismatch (expected %d, actual %ld). Corrupted IFS image.\n",
+			expected_size, fw->size);
+		return -EBADFD;
+	}
+
 	ret = image_sanity_check(dev, (struct microcode_header_intel *)fw->data);
 	if (ret)
 		goto release;