Skip to contentSkip to conversation

lib: sbi_pmu: Avoid out of bounds access

PATCH New Heinrich Schuchardt 3 participants 5 messages Jul 3, 2023 – Jul 5, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi_pmu: Avoid out of bounds access

On a misconfigured system we could access phs->active_events[] out of bounds. Check that num_hw_ctrs is less or equal SBI_PMU_HW_CTR_MAX.

Addresses-Coverity-ID: 1566113 ("Out-of-bounds read")
Addresses-Coverity-ID: 1566114 ("Out-of-bounds write")

---
 lib/sbi/sbi_pmu.c | 2 ++
 1 file changed, 2 insertions(+)
lib/sbi/sbi_pmu.c +2 -0
index c73e6ef..7213a53 100644
@@ -933,6 +933,8 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
 
 		/* mcycle & minstret is available always */
 		num_hw_ctrs = sbi_hart_mhpm_count(scratch) + 3;
+		if (num_hw_ctrs > SBI_PMU_HW_CTR_MAX)
+			return SBI_EINVAL;
 		total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
 	}
 
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/1 lib: sbi_pmu: Avoid out of bounds access

Quoted 26 lines · Heinrich Schuchardt

On a misconfigured system we could access phs->active_events[] out of bounds. Check that num_hw_ctrs is less or equal SBI_PMU_HW_CTR_MAX.

Addresses-Coverity-ID: 1566113 ("Out-of-bounds read")
Addresses-Coverity-ID: 1566114 ("Out-of-bounds write")

---
 lib/sbi/sbi_pmu.c | 2 ++
 1 file changed, 2 insertions(+)
lib/sbi/sbi_pmu.c +2 -0
index c73e6ef..7213a53 100644
@@ -933,6 +933,8 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
 
 		/* mcycle & minstret is available always */
 		num_hw_ctrs = sbi_hart_mhpm_count(scratch) + 3;
+		if (num_hw_ctrs > SBI_PMU_HW_CTR_MAX)
+			return SBI_EINVAL;
 		total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
 	}
 
Signature · 1 line
2.40.1

Should we instead cap and warn in hart_detect_features()?

Thanks,
drew

#2 lore raw

PATCH 1/1 lib: sbi_pmu: Avoid out of bounds access

Quoted 24 lines · Andrew Jones
Quoted 23 lines · Heinrich Schuchardt

On a misconfigured system we could access phs->active_events[] out of bounds. Check that num_hw_ctrs is less or equal SBI_PMU_HW_CTR_MAX.

Addresses-Coverity-ID: 1566113 ("Out-of-bounds read")
Addresses-Coverity-ID: 1566114 ("Out-of-bounds write")

---
  lib/sbi/sbi_pmu.c | 2 ++
  1 file changed, 2 insertions(+)
lib/sbi/sbi_pmu.c +2 -0
index c73e6ef..7213a53 100644
@@ -933,6 +933,8 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
 
  		/* mcycle & minstret is available always */
  		num_hw_ctrs = sbi_hart_mhpm_count(scratch) + 3;
+		if (num_hw_ctrs > SBI_PMU_HW_CTR_MAX)
+			return SBI_EINVAL;
  		total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
  	}
 
Quoted 5 lines
Quoted 3 lines
Signature · 1 line
2.40.1

Should we instead cap and warn in hart_detect_features()?

Thanks for reviewing.

By default U-Boot invokes OpenSBI with SBI_SCRATCH_NO_BOOT_PRINTS (to save a few ms boot time). You might never see the warning.

mhpm_count is hard coded, e.g.

platform/generic/allwinner/sun20i-d1.c:275:
hfeatures->mhpm_count = 29;

If this value is too high, the coding needs to be changed.

Best regards

Heinrich

#3 lore raw

PATCH 1/1 lib: sbi_pmu: Avoid out of bounds access

Quoted 34 lines · Heinrich Schuchardt
Quoted 11 lines · Andrew Jones
Quoted 10 lines · Heinrich Schuchardt

On a misconfigured system we could access phs->active_events[] out of bounds. Check that num_hw_ctrs is less or equal SBI_PMU_HW_CTR_MAX.

Addresses-Coverity-ID: 1566113 ("Out-of-bounds read")
Addresses-Coverity-ID: 1566114 ("Out-of-bounds write")

---
  lib/sbi/sbi_pmu.c | 2 ++
  1 file changed, 2 insertions(+)
Quoted 16 lines
Quoted 14 lines
lib/sbi/sbi_pmu.c +2 -0
index c73e6ef..7213a53 100644
@@ -933,6 +933,8 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
  		/* mcycle & minstret is available always */
  		num_hw_ctrs = sbi_hart_mhpm_count(scratch) + 3;
+		if (num_hw_ctrs > SBI_PMU_HW_CTR_MAX)
+			return SBI_EINVAL;
  		total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
  	}
Signature · 1 line
2.40.1

Should we instead cap and warn in hart_detect_features()?

Thanks for reviewing.

By default U-Boot invokes OpenSBI with SBI_SCRATCH_NO_BOOT_PRINTS (to save a few ms boot time). You might never see the warning.

Quoted 7 lines

mhpm_count is hard coded, e.g.

platform/generic/allwinner/sun20i-d1.c:275:
hfeatures->mhpm_count = 29;

If this value is too high, the coding needs to be changed.

Yeah, scratch the cap/warn suggestion. Despite mhpm_count being detected, not coded, for most platforms, the architecture states their can never be more than 29. It's definitely a fail scenario if more are detected, or coded.

Thanks,
drew

Applied
#4 lore raw

PATCH 1/1 lib: sbi_pmu: Avoid out of bounds access

In reply to Heinrich Schuchardt
Quoted 7 lines · On Mon, Jul 3, 2023 at 7:13?PM Heinrich Schuchardt…

On a misconfigured system we could access phs->active_events[] out of bounds. Check that num_hw_ctrs is less or equal SBI_PMU_HW_CTR_MAX.

Addresses-Coverity-ID: 1566113 ("Out-of-bounds read")
Addresses-Coverity-ID: 1566114 ("Out-of-bounds write")

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 20 lines
---
 lib/sbi/sbi_pmu.c | 2 ++
 1 file changed, 2 insertions(+)
lib/sbi/sbi_pmu.c +2 -1
index c73e6ef..7213a53 100644
@@ -933,6 +933,8 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
 
                /* mcycle & minstret is available always */
                num_hw_ctrs = sbi_hart_mhpm_count(scratch) + 3;
+               if (num_hw_ctrs > SBI_PMU_HW_CTR_MAX)
+                       return SBI_EINVAL;
                total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
        }
 
--

2.40.1