Skip to contentSkip to conversation

soc: qcom: mdt_loader: Add Upperbounds check for program header access

PATCH In review Auditya Bhattaram 2 participants 5 messages Feb 13, 2024 – Mar 16, 2024

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

PATCH v4 1/1 soc: qcom: mdt_loader: Add Upperbounds check for program header access

hash_index is evaluated by looping phdrs till QCOM_MDT_TYPE_HASH is found. Add an upperbound check to phdrs to access within elf size.

Fixes 64fb5eb87d58 ("soc: qcom: mdt_loader: Allow hash to reside in any segment")

---
Changes in v4:
- Added additional prints incase of Invalid access.
Link to v3 https://lore.kernel.org/stable/[email protected]
Link to v2 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t
Link to v1 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t

---
 drivers/soc/qcom/mdt_loader.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/soc/qcom/mdt_loader.c +7 -0
index 6f177e46fa0f..1a79a7bba468 100644
@@ -145,6 +143,13 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
 	if (phdrs[0].p_type == PT_LOAD)
 		return ERR_PTR(-EINVAL);
 
+	if (((size_t)(phdrs + ehdr->e_phnum)) > ((size_t)ehdr + fw->size)) {
+		dev_err(dev,
+			"Invalid phdrs access for fw: %s, e_phnum: %u, fw->size: %zu\n",
+			fw_name, ehdr->e_phnum, fw->size);
+		return ERR_PTR(-EINVAL);
+	}
+
 	for (i = 1; i < ehdr->e_phnum; i++) {
 		if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH) {
 			hash_segment = i;

--
2.17.1

maintainer
#1 lore raw
Quoted 3 lines · Auditya Bhattaram

hash_index is evaluated by looping phdrs till QCOM_MDT_TYPE_HASH is found. Add an upperbound check to phdrs to access within elf size.

How is this compatible with what is being observed on SM8450 and implemented in commit 8bd42e2341a7 ("soc: qcom: mdt_loader: Allow hash segment to be split out"?

Regards,
Bjorn

Quoted 35 lines
Fixes 64fb5eb87d58 ("soc: qcom: mdt_loader: Allow hash to reside in any segment")

---
Changes in v4:
- Added additional prints incase of Invalid access.
Link to v3 https://lore.kernel.org/stable/[email protected]
Link to v2 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t
Link to v1 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t

---
 drivers/soc/qcom/mdt_loader.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/soc/qcom/mdt_loader.c +7 -1
index 6f177e46fa0f..1a79a7bba468 100644
@@ -145,6 +143,13 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
 	if (phdrs[0].p_type == PT_LOAD)
 		return ERR_PTR(-EINVAL);
 
+	if (((size_t)(phdrs + ehdr->e_phnum)) > ((size_t)ehdr + fw->size)) {
+		dev_err(dev,
+			"Invalid phdrs access for fw: %s, e_phnum: %u, fw->size: %zu\n",
+			fw_name, ehdr->e_phnum, fw->size);
+		return ERR_PTR(-EINVAL);
+	}
+
 	for (i = 1; i < ehdr->e_phnum; i++) {
 		if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH) {
 			hash_segment = i;
--

2.17.1

#2 lore raw
Quoted 5 lines · Bjorn Andersson
Quoted 3 lines · Auditya Bhattaram

hash_index is evaluated by looping phdrs till QCOM_MDT_TYPE_HASH is found. Add an upperbound check to phdrs to access within elf size.

Quoted 7 lines

How is this compatible with what is being observed on SM8450 and implemented in commit 8bd42e2341a7 ("soc: qcom: mdt_loader: Allow hash segment to be split out"?

Regards,
Bjorn

Calculating hash_index is introduced with this commit 8bd42e2341a7 ("soc: qcom: mdt_loader: Allow hash segment to be split out"

    for (i = 1; i < ehdr->e_phnum; i++) {

if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) ...

I'm trying to add an upper bound for this access "phdrs[i]"

Quoted 35 lines
Quoted 35 lines
Fixes 64fb5eb87d58 ("soc: qcom: mdt_loader: Allow hash to reside in any segment")

---
Changes in v4:
- Added additional prints incase of Invalid access.
Link to v3 https://lore.kernel.org/stable/[email protected]
Link to v2 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t
Link to v1 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t

---
  drivers/soc/qcom/mdt_loader.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/soc/qcom/mdt_loader.c +7 -1
index 6f177e46fa0f..1a79a7bba468 100644
@@ -145,6 +143,13 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
  	if (phdrs[0].p_type == PT_LOAD)
  		return ERR_PTR(-EINVAL);
 
+	if (((size_t)(phdrs + ehdr->e_phnum)) > ((size_t)ehdr + fw->size)) {
+		dev_err(dev,
+			"Invalid phdrs access for fw: %s, e_phnum: %u, fw->size: %zu\n",
+			fw_name, ehdr->e_phnum, fw->size);
+		return ERR_PTR(-EINVAL);
+	}
+
  	for (i = 1; i < ehdr->e_phnum; i++) {
  		if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH) {
  			hash_segment = i;
--

2.17.1

#3 lore raw
Quoted 16 lines · Auditya Bhattaram
Quoted 5 lines · Bjorn Andersson
Quoted 3 lines · Auditya Bhattaram

hash_index is evaluated by looping phdrs till QCOM_MDT_TYPE_HASH is found. Add an upperbound check to phdrs to access within elf size.

Quoted 7 lines

How is this compatible with what is being observed on SM8450 and implemented in commit 8bd42e2341a7 ("soc: qcom: mdt_loader: Allow hash segment to be split out"?

Regards,
Bjorn

Quoted 7 lines

Calculating hash_index is introduced with this commit 8bd42e2341a7 ("soc: qcom: mdt_loader: Allow hash segment to be split out"

    for (i = 1; i < ehdr->e_phnum; i++) {
     if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) ...

I'm trying to add an upper bound for this access "phdrs[i]"

Any further questions on this Bjorn.

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes 64fb5eb87d58 ("soc: qcom: mdt_loader: Allow hash to reside in any segment")

---
Changes in v4:
  - Added additional prints incase of Invalid access.
Link to v3 https://lore.kernel.org/stable/[email protected]
Link to v2 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t
Link to v1 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t
---
  drivers/soc/qcom/mdt_loader.c | 7 +++++++

  1 file changed, 7 insertions(+)
drivers/soc/qcom/mdt_loader.c +7 -0
index 6f177e46fa0f..1a79a7bba468 100644
@@ -145,6 +143,13 @@ void *qcom_mdt_read_metadata(const struct 
 firmware *fw, size_t *data_len,
       if (phdrs[0].p_type == PT_LOAD)
           return ERR_PTR(-EINVAL);
 
+    if (((size_t)(phdrs + ehdr->e_phnum)) > ((size_t)ehdr + 
 fw->size)) {
+        dev_err(dev,
+            "Invalid phdrs access for fw: %s, e_phnum: %u, fw->size: 
 %zu\n",
+            fw_name, ehdr->e_phnum, fw->size);
+        return ERR_PTR(-EINVAL);
+    }
+

      for (i = 1; i < ehdr->e_phnum; i++) {
          if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH) { hash_segment = i;

Signature · 1 line
2.17.1
maintainer
#4 lore raw
In reply to Auditya Bhattaram
Quoted 16 lines · Auditya Bhattaram
Quoted 5 lines · Bjorn Andersson
Quoted 3 lines · Auditya Bhattaram

hash_index is evaluated by looping phdrs till QCOM_MDT_TYPE_HASH is found. Add an upperbound check to phdrs to access within elf size.

Quoted 7 lines

How is this compatible with what is being observed on SM8450 and implemented in commit 8bd42e2341a7 ("soc: qcom: mdt_loader: Allow hash segment to be split out"?

Regards,
Bjorn

Quoted 8 lines

Calculating hash_index is introduced with this commit 8bd42e2341a7 ("soc:
qcom: mdt_loader: Allow hash segment to be split out"

    for (i = 1; i < ehdr->e_phnum; i++) {

if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) ...

I'm trying to add an upper bound for this access "phdrs[i]"

Ahh, sorry, you're of course correct. I think I would have preferred a more obvious check of offset >= fw->size though...

That said, this code used to sit behind rproc_elf_sanity_check(), but that seem to have been lost in the refactorings. As such, your patch is incomplete and we should reintroduce a whole bunch of these sanity checks!

Regards,
Bjorn

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Fixes 64fb5eb87d58 ("soc: qcom: mdt_loader: Allow hash to reside in any segment")

---
Changes in v4:
- Added additional prints incase of Invalid access.
Link to v3 https://lore.kernel.org/stable/[email protected]
Link to v2 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t
Link to v1 https://lore.kernel.org/linux-arm-msm/[email protected]/T/#t

---
  drivers/soc/qcom/mdt_loader.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/soc/qcom/mdt_loader.c +7 -1
index 6f177e46fa0f..1a79a7bba468 100644
@@ -145,6 +143,13 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
  	if (phdrs[0].p_type == PT_LOAD)
  		return ERR_PTR(-EINVAL);
 
+	if (((size_t)(phdrs + ehdr->e_phnum)) > ((size_t)ehdr + fw->size)) {
+		dev_err(dev,
+			"Invalid phdrs access for fw: %s, e_phnum: %u, fw->size: %zu\n",
+			fw_name, ehdr->e_phnum, fw->size);
+		return ERR_PTR(-EINVAL);
+	}
+
  	for (i = 1; i < ehdr->e_phnum; i++) {
  		if ((phdrs[i].p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH) {
  			hash_segment = i;
--

2.17.1