Skip to contentSkip to conversation

soc: qcom: mdt_loader: Validate that p_filesz < p_memsz

PATCH Applied Bjorn Andersson 3 participants 3 messages Jan 7, 2021 – Apr 9, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 soc: qcom: mdt_loader: Validate that p_filesz < p_memsz

The code validates that segments of p_memsz bytes of a segment will fit in the provided memory region, but does not validate that p_filesz bytes will, which means that an incorrectly crafted ELF header might write beyond the provided memory region.

Fixes 051fb70fd4ea ("remoteproc: qcom: Driver for the self-authenticating Hexagon v5")
---
 drivers/soc/qcom/mdt_loader.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/soc/qcom/mdt_loader.c +8 -0
index e01d18e9ad2b..5180b5996830 100644
@@ -230,6 +230,14 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
 			break;
 		}
 
+		if (phdr->p_filesz > phdr->p_memsz) {
+			dev_err(dev,
+				"refusing to load segment %d with p_filesz > p_memsz\n",
+				i);
+			ret = -EINVAL;
+			break;
+		}
+
 		ptr = mem_region + offset;
 
 		if (phdr->p_filesz && phdr->p_offset < fw->size) {
Signature · 1 line
2.29.2
Reviewed-by on v1 1/1
#1 lore raw

Hey Bjorn,
Thanks for the patch!

Quoted 17 lines · Bjorn Andersson

The code validates that segments of p_memsz bytes of a segment will fit in the provided memory region, but does not validate that p_filesz bytes will, which means that an incorrectly crafted ELF header might write beyond the provided memory region.

Fixes 051fb70fd4ea ("remoteproc: qcom: Driver for the self-authenticating Hexagon v5")
---
  drivers/soc/qcom/mdt_loader.c | 8 ++++++++
  1 file changed, 8 insertions(+)
drivers/soc/qcom/mdt_loader.c +0 -0
index e01d18e9ad2b..5180b5996830 100644
@@ -230,6 +230,14 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw,
  			break;

drivers/soc/qcom/mdt_loader.c static int __qcom_mdt_load(struct device *dev, const struct firmware *fw, · line 240

Quoted 10 lines
drivers/soc/qcom/mdt_loader.c +8 -0
  		}
 
+		if (phdr->p_filesz > phdr->p_memsz) {
+			dev_err(dev,
+				"refusing to load segment %d with p_filesz > p_memsz\n",
+				i);
+			ret = -EINVAL;
+			break;
+		}
+
Quoted 4 lines
drivers/soc/qcom/mdt_loader.c +0 -0
  		ptr = mem_region + offset;
 
  		if (phdr->p_filesz && phdr->p_offset < fw->size) {
Signature · 3 lines
Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc, is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw
In reply to Bjorn Andersson
This patch was applied to qcom/linux.git (refs/heads/for-next):

Hello:

This patch was applied to qcom/linux.git (refs/heads/for-next):

Quoted 9 lines · On Thu, 7 Jan 2021 15:31:19 -0800 you wrote:

The code validates that segments of p_memsz bytes of a segment will fit in the provided memory region, but does not validate that p_filesz bytes will, which means that an incorrectly crafted ELF header might write beyond the provided memory region.

Fixes 051fb70fd4ea ("remoteproc: qcom: Driver for the self-authenticating Hexagon v5")

[...]

Here is the summary with links:
- soc: qcom: mdt_loader: Validate that p_filesz < p_memsz

    https://git.kernel.org/qcom/c/84168d1b54e7

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html