Skip to contentSkip to conversation

remoteproc: qcom_q6v5_mss: Validate p_filesz in ELF loader

PATCH Superseded Bjorn Andersson 2 participants 4 messages Jan 7, 2021 – Jan 19, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 remoteproc: qcom_q6v5_mss: Validate p_filesz in ELF loader

Analog to the issue in the common mdt_loader code the MSS ELF loader does not validate that p_filesz bytes will fit in the memory region and that the loaded segments are not truncated. Fix this in the same way as proposed for the mdt_loader.

Fixes 135b9e8d1cd8 ("remoteproc: qcom_q6v5_mss: Validate modem blob firmware size before load")
---
 drivers/remoteproc/qcom_q6v5_mss.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/remoteproc/qcom_q6v5_mss.c +17 -0
index 66106ba25ba3..2b59e0cbdce1 100644
@@ -1210,6 +1210,14 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 			goto release_firmware;
 		}
 
+		if (phdr->p_filesz > phdr->p_memsz) {
+			dev_err(qproc->dev,
+				"refusing to load segment %d with p_filesz > p_memsz\n",
+				i);
+			ret = -EINVAL;
+			break;
+		}
+
 		ptr = memremap(qproc->mpss_phys + offset, phdr->p_memsz, MEMREMAP_WC);
 		if (!ptr) {
 			dev_err(qproc->dev,
@@ -1241,6 +1249,15 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 				goto release_firmware;
 			}
 
+			if (seg_fw->size != phdr->p_filesz) {
+				dev_err(qproc->dev,
+					"failed to load segment %d from truncated file %s\n",
+					i, fw_name);
+				ret = -EINVAL;
+				memunmap(ptr);
+				break;
+			}
+
 			release_firmware(seg_fw);
 		}
 
Signature · 1 line
2.29.2
maintainer
#1 lore raw

Hi Bjorn,

Quoted 16 lines · Bjorn Andersson

Analog to the issue in the common mdt_loader code the MSS ELF loader does not validate that p_filesz bytes will fit in the memory region and that the loaded segments are not truncated. Fix this in the same way as proposed for the mdt_loader.

Fixes 135b9e8d1cd8 ("remoteproc: qcom_q6v5_mss: Validate modem blob firmware size before load")
---
 drivers/remoteproc/qcom_q6v5_mss.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/remoteproc/qcom_q6v5_mss.c +0 -0
index 66106ba25ba3..2b59e0cbdce1 100644

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1218

Quoted 9 lines
drivers/remoteproc/qcom_q6v5_mss.c +6 -0
@@ -1210,6 +1210,14 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 			goto release_firmware;
 		}
 
+		if (phdr->p_filesz > phdr->p_memsz) {
+			dev_err(qproc->dev,
+				"refusing to load segment %d with p_filesz > p_memsz\n",
+				i);
+			ret = -EINVAL;
+			break;

Based on the error handling for the above and below conditions, I would have expected a "goto release_firmware" rather than a "break".

Quoted 6 lines
drivers/remoteproc/qcom_q6v5_mss.c +2 -0
+		}
+
 		ptr = memremap(qproc->mpss_phys + offset, phdr->p_memsz, MEMREMAP_WC);
 		if (!ptr) {
 			dev_err(qproc->dev,

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1258

Quoted 10 lines
drivers/remoteproc/qcom_q6v5_mss.c +7 -0
@@ -1241,6 +1249,15 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 				goto release_firmware;
 			}
 
+			if (seg_fw->size != phdr->p_filesz) {
+				dev_err(qproc->dev,
+					"failed to load segment %d from truncated file %s\n",
+					i, fw_name);
+				ret = -EINVAL;
+				memunmap(ptr);
+				break;

Same here.

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1262

Quoted 4 lines
drivers/remoteproc/qcom_q6v5_mss.c +2 -0
+			}
+
 			release_firmware(seg_fw);
 		}

Thanks,
Mathieu

Quoted 4 lines
Signature · 2 lines
-- 
2.29.2
#2 lore raw
Quoted 22 lines · Mathieu Poirier

Hi Bjorn,

Quoted 19 lines · Bjorn Andersson

Analog to the issue in the common mdt_loader code the MSS ELF loader does not validate that p_filesz bytes will fit in the memory region and that the loaded segments are not truncated. Fix this in the same way as proposed for the mdt_loader.

Fixes 135b9e8d1cd8 ("remoteproc: qcom_q6v5_mss: Validate modem blob firmware size before load")
---
 drivers/remoteproc/qcom_q6v5_mss.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/remoteproc/qcom_q6v5_mss.c +0 -0
index 66106ba25ba3..2b59e0cbdce1 100644
@@ -1210,6 +1210,14 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 			goto release_firmware;
 		}
 
Quoted 10 lines

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1218

Quoted 6 lines
drivers/remoteproc/qcom_q6v5_mss.c +6 -0
+		if (phdr->p_filesz > phdr->p_memsz) {
+			dev_err(qproc->dev,
+				"refusing to load segment %d with p_filesz > p_memsz\n",
+				i);
+			ret = -EINVAL;
+			break;

Based on the error handling for the above and below conditions, I would have expected a "goto release_firmware" rather than a "break".

You're certainly right!

Yet another reason for the duplication between this function, the mdt_loader and the remoteproc_elf_loader is a bad idea - still not sure how to refactor any one of them to fit the three.

Thank you,
Bjorn

Quoted 31 lines
Quoted 6 lines
drivers/remoteproc/qcom_q6v5_mss.c +2 -0
+		}
+
 		ptr = memremap(qproc->mpss_phys + offset, phdr->p_memsz, MEMREMAP_WC);
 		if (!ptr) {
 			dev_err(qproc->dev,

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1258

Quoted 10 lines
drivers/remoteproc/qcom_q6v5_mss.c +7 -0
@@ -1241,6 +1249,15 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 				goto release_firmware;
 			}
 
+			if (seg_fw->size != phdr->p_filesz) {
+				dev_err(qproc->dev,
+					"failed to load segment %d from truncated file %s\n",
+					i, fw_name);
+				ret = -EINVAL;
+				memunmap(ptr);
+				break;

Same here.

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1262

Quoted 4 lines
drivers/remoteproc/qcom_q6v5_mss.c +2 -0
+			}
+
 			release_firmware(seg_fw);
 		}

Thanks,
Mathieu

Quoted 4 lines
Signature · 2 lines
-- 
2.29.2
maintainer
#3 lore raw
Quoted 35 lines · Bjorn Andersson
Quoted 22 lines · Mathieu Poirier

Hi Bjorn,

Quoted 19 lines · Bjorn Andersson

Analog to the issue in the common mdt_loader code the MSS ELF loader does not validate that p_filesz bytes will fit in the memory region and that the loaded segments are not truncated. Fix this in the same way as proposed for the mdt_loader.

Fixes 135b9e8d1cd8 ("remoteproc: qcom_q6v5_mss: Validate modem blob firmware size before load")
---
 drivers/remoteproc/qcom_q6v5_mss.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/remoteproc/qcom_q6v5_mss.c +0 -0
index 66106ba25ba3..2b59e0cbdce1 100644
@@ -1210,6 +1210,14 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 			goto release_firmware;
 		}
 
Quoted 10 lines

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1218

Quoted 6 lines
drivers/remoteproc/qcom_q6v5_mss.c +6 -0
+		if (phdr->p_filesz > phdr->p_memsz) {
+			dev_err(qproc->dev,
+				"refusing to load segment %d with p_filesz > p_memsz\n",
+				i);
+			ret = -EINVAL;
+			break;

Based on the error handling for the above and below conditions, I would have expected a "goto release_firmware" rather than a "break".

Quoted 5 lines

You're certainly right!

Yet another reason for the duplication between this function, the mdt_loader and the remoteproc_elf_loader is a bad idea - still not sure how to refactor any one of them to fit the three.

I feel the same way about duplication. On the flip side I don't think we have to take immediate action... The solution will come out at some point when there is more convergence. At least we know it is an area that needs attention.

Quoted 35 lines

Thank you,
Bjorn

Quoted 31 lines
Quoted 6 lines
drivers/remoteproc/qcom_q6v5_mss.c +2 -0
+		}
+
 		ptr = memremap(qproc->mpss_phys + offset, phdr->p_memsz, MEMREMAP_WC);
 		if (!ptr) {
 			dev_err(qproc->dev,

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1258

Quoted 10 lines
drivers/remoteproc/qcom_q6v5_mss.c +7 -0
@@ -1241,6 +1249,15 @@ static int q6v5_mpss_load(struct q6v5 *qproc)
 				goto release_firmware;
 			}
 
+			if (seg_fw->size != phdr->p_filesz) {
+				dev_err(qproc->dev,
+					"failed to load segment %d from truncated file %s\n",
+					i, fw_name);
+				ret = -EINVAL;
+				memunmap(ptr);
+				break;

Same here.

drivers/remoteproc/qcom_q6v5_mss.c static int q6v5_mpss_load(struct q6v5 *qproc) · line 1262

Quoted 4 lines
drivers/remoteproc/qcom_q6v5_mss.c +2 -0
+			}
+
 			release_firmware(seg_fw);
 		}

Thanks,
Mathieu

Quoted 4 lines
Signature · 2 lines
-- 
2.29.2