Skip to contentSkip to conversation

soc: qcom: mdt_loader: fix off-by-one heap overflow in segment name

PATCH Applied Kenneth Kabogo 4 participants 4 messages Sep 12 – Nov 8

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

PATCH 1/1 soc: qcom: mdt_loader: fix off-by-one heap overflow in segment name

mdt_load_split_segment() builds a segment firmware filename by overwriting the last three characters of a kstrdup'd copy of fw_name with "b%02d" formatted from segment. %02d is a minimum field width, not a cap: for segment in [0, 99] this produces exactly "bNN" + NUL, which fits the buffer, but for segment >= 100 it writes "b100" or wider, one or more bytes past the end of the kstrdup allocation.

segment is not bounded to [0, 99] anywhere before this point. It is a raw index into the firmware's ELF program header table, ultimately bounded only by e_phnum, a 16-bit ELF field (max 65535). The existing check in mdt_header_valid() only verifies that the program header table fits inside the firmware file, not that e_phnum itself is sane, so a small firmware file can supply e_phnum > 100 and reach this path with segment >= 100 from either qcom_mdt_read_metadata() (the hash segment scan) or the main loadable-segment loop in qcom_mdt_load_no_init(), whenever the image uses the split-firmware convention.

segment was never valid past 99 under this driver's own "bNN" naming scheme, so reject it outright instead of widening the buffer.

Found by code inspection while auditing the Qualcomm PIL/PAS firmware-authentication flow. No reproducer beyond code inspection is available; I don't have affected hardware to trigger it on.

---
 drivers/soc/qcom/mdt_loader.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/soc/qcom/mdt_loader.c +3 -0
index 002100fe2..0d55e9e0a 100644
@@ -77,6 +77,9 @@ static ssize_t mdt_load_split_segment(void *ptr, const struct elf32_phdr *phdrs,
 	if (strlen(fw_name) < 4)
 		return -EINVAL;
 
+	if (segment > 99)
+		return -EINVAL;
+
 	char *seg_name __free(kfree) = kstrdup(fw_name, GFP_KERNEL);
 	if (!seg_name)
 		return -ENOMEM;
Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Kenneth Kabogo

mdt_load_split_segment() builds a segment firmware filename by overwriting the last three characters of a kstrdup'd copy of fw_name with "b%02d" formatted from segment. %02d is a minimum field width, not a cap: for segment in [0, 99] this produces exactly "bNN" + NUL, which fits the buffer, but for segment >= 100 it writes "b100" or wider, one or more bytes past the end of the kstrdup allocation.

segment is not bounded to [0, 99] anywhere before this point. It is a raw index into the firmware's ELF program header table, ultimately bounded only by e_phnum, a 16-bit ELF field (max 65535). The existing check in mdt_header_valid() only verifies that the program header table fits inside the firmware file, not that e_phnum itself is sane, so a small firmware file can supply e_phnum > 100 and reach this path with segment >= 100 from either qcom_mdt_read_metadata() (the hash segment scan) or the main loadable-segment loop in qcom_mdt_load_no_init(), whenever the image uses the split-firmware convention.

segment was never valid past 99 under this driver's own "bNN" naming scheme, so reject it outright instead of widening the buffer.

Quoted 6 lines

Found by code inspection while auditing the Qualcomm PIL/PAS firmware-authentication flow. No reproducer beyond code inspection is available; I don't have affected hardware to trigger it on.

---

Konrad

Reviewed-by on v1 1/1
#2 lore raw
In reply to Kenneth Kabogo
Quoted 18 lines · Kenneth Kabogo

mdt_load_split_segment() builds a segment firmware filename by overwriting the last three characters of a kstrdup'd copy of fw_name with "b%02d" formatted from segment. %02d is a minimum field width, not a cap: for segment in [0, 99] this produces exactly "bNN" + NUL, which fits the buffer, but for segment >= 100 it writes "b100" or wider, one or more bytes past the end of the kstrdup allocation.

segment is not bounded to [0, 99] anywhere before this point. It is a raw index into the firmware's ELF program header table, ultimately bounded only by e_phnum, a 16-bit ELF field (max 65535). The existing check in mdt_header_valid() only verifies that the program header table fits inside the firmware file, not that e_phnum itself is sane, so a small firmware file can supply e_phnum > 100 and reach this path with segment >= 100 from either qcom_mdt_read_metadata() (the hash segment scan) or the main loadable-segment loop in qcom_mdt_load_no_init(), whenever the image uses the split-firmware convention.

Quoted 8 lines

segment was never valid past 99 under this driver's own "bNN" naming scheme, so reject it outright instead of widening the buffer.

Found by code inspection while auditing the Qualcomm PIL/PAS firmware-authentication flow. No reproducer beyond code inspection is available; I don't have affected hardware to trigger it on.

maintainer Applied
#3 lore raw
In reply to Kenneth Kabogo
Quoted 8 lines · Kenneth Kabogo

mdt_load_split_segment() builds a segment firmware filename by overwriting the last three characters of a kstrdup'd copy of fw_name with "b%02d" formatted from segment. %02d is a minimum field width, not a cap: for segment in [0, 99] this produces exactly "bNN" + NUL, which fits the buffer, but for segment >= 100 it writes "b100" or wider, one or more bytes past the end of the kstrdup allocation.

[...]

Applied, thanks!

[1/1] soc: qcom: mdt_loader: fix off-by-one heap overflow in segment name

      commit: 3e0d5015fc90859ca8c3a2edacdbaad546969f54

Best regards,

Signature · 1 line
Bjorn Andersson <[email protected]>