lib: fix access to hfeatures->extensions
PATCH 1/1 lib: fix access to hfeatures->extensions
We may in future have more than xlen ISA extensions supported by OpenSBI. Commit 6259b2ec2d09 ("lib: utils/fdt: Fix fdt_parse_isa_extensions() implementation") therefore assumes that hfeatures->extensions is an array. We have to make this change in the rest of the code too.
Addresses-Coverity-ID: 1568357 Out-of-bounds access
--- include/sbi/sbi_hart.h | 3 ++- lib/sbi/sbi_hart.c | 46 ++++++++++++++++++++----------------- platform/generic/platform.c | 2 +- 3 files changed, 28 insertions(+), 23 deletions(-)
include/sbi/sbi_hart.h +2 -1
@@ -10,6 +10,7 @@#ifndef __SBI_HART_H__#define __SBI_HART_H__ +#include <sbi/sbi_bitops.h> #include <sbi/sbi_types.h> /** Possible privileged specification versions of a hart */
@@ -65,7 +66,7 @@ enum sbi_hart_extensions {struct sbi_hart_features {bool detected;int priv_version; -unsigned long extensions; +unsigned long extensions[BITS_TO_LONGS (SBI_HART_EXT_MAX )];unsigned int pmp_count;unsigned int pmp_addr_bits;unsigned long pmp_gran;
lib/sbi/sbi_hart.c +25 -21
79 lines not on this page · raw message
platform/generic/platform.c +1 -1
@@ -215,7 +215,7 @@ static int generic_extensions_init(struct sbi_hart_features *hfeatures)/* Parse the ISA string from FDT and enable the listed extensions */ rc =fdt_parse_isa_extensions (fdt_get_address (),current_hartid (), - &hfeatures->extensions); + hfeatures->extensions);if (rc)return rc;
Signature · 1 line
2.40.1
PATCH 1/1 lib: fix access to hfeatures->extensions
? 2023-11-11???? 04:26 +0100?Heinrich Schuchardt???
Quoted 8 lines
We may in future have more than xlen ISA extensions supported by OpenSBI. Commit
6259b2ec2d09("lib: utils/fdt: Fixfdt_parse_isa_extensions()implementation") therefore assumes thathfeatures->extensionsis an array. We have to make this change in the rest of the code too.Fixes 6259b2ec2d09 ("lib: utils/fdt: Fix fdt_parse_isa_extensions() implementation")Addresses-Coverity-ID: 1568357 Out-of-bounds access
Signed-off-by Heinrich Schuchardt <[email protected]>
Similar to this:
https://lists.infradead.org/pipermail/opensbi/2023-October/005787.html
Regards,
Xiang W
Quoted 131 lines
---
?include/sbi/sbi_hart.h????? |? 3 ++- ?lib/sbi/sbi_hart.c????????? | 46 ++++++++++++++++++++----------------- ?platform/generic/platform.c |? 2 +-?3 files changed, 28 insertions(+), 23 deletions(-)
include/sbi/sbi_hart.h +0
-0@@ -10,6 +10,7 @@?#ifndef
__SBI_HART_H__
?#define__SBI_HART_H__
?
+#include <sbi/sbi_bitops.h>
?#include <sbi/sbi_types.h>
?
?/** Possible privileged specification versions of a hart */include/sbi/sbi_hart.h +1
-1@@ -65,7 +66,7 @@ enum sbi_hart_extensions { ?struct sbi_hart_features { ? bool detected; ? int priv_version; -unsigned long extensions; +unsigned long extensions[BITS_TO_LONGS (SBI_HART_EXT_MAX )]; ? unsigned int pmp_count; ? unsigned int pmp_addr_bits; ? unsigned long pmp_gran;lib/sbi/sbi_hart.c +1
-1@@ -614,9 +614,9 @@ static inline void __sbi_hart_update_extension( ? bool enable) ?{ ? if (enable) - hfeatures->extensions |=BIT (ext); + hfeatures->extensions[BIT_WORD (ext)] |=BIT_MASK (ext);? else
lib/sbi/sbi_hart.c +1
-1- hfeatures->extensions &= ~BIT (ext); + hfeatures->extensions[BIT_WORD (ext)] &= ~BIT_MASK (ext);?}
?
?/**lib/sbi/sbi_hart.c +0
-0@@ -649,7 +649,7 @@ bool sbi_hart_has_extension(struct sbi_scratch *scratch,? struct
sbi_hart_features*hfeatures =
?sbi_scratch_offset_ptr(scratch, hart_features_offset);
?lib/sbi/sbi_hart.c +1
-1-if (hfeatures->extensions &BIT (ext)) +if (hfeatures->extensions[BIT_WORD (ext)] &BIT_MASK (ext))? return true;
? else
? return false;lib/sbi/sbi_hart.c +0
-0@@ -705,31 +705,35 @@ void sbi_hart_get_extensions_str(struct sbi_scratch *scratch, ?{? struct
sbi_hart_features*hfeatures =
?sbi_scratch_offset_ptr(scratch, hart_features_offset);lib/sbi/sbi_hart.c +1
-2-int offset =0 , ext =0 ; -char *temp; +int offset =0 ;?
? if (!extensions_str || nestr <= 0)
? return;
?sbi_memset(extensions_str, 0, nestr);
?lib/sbi/sbi_hart.c +19
-11-if (!hfeatures->extensions) - goto done; - - do { - if (hfeatures->extensions & BIT(ext)) { - temp = sbi_hart_extension_id2string(ext); - if (temp) { - sbi_snprintf(extensions_str + offset, - ???? nestr - offset, - ???? "%s,", temp); - offset = offset + sbi_strlen(temp) + 1; +for (int word =0 ; word <BITS_TO_LONGS (SBI_HART_EXT_MAX ); ++word) { +int ext; + +if (!hfeatures->extensions[word]) +continue ; + + ext = word *BITS_PER_LONG ; +for (int bit =0 ; bit <BITS_PER_LONG && ext <SBI_HART_EXT_MAX ; + ???? ++bit, ++ext) { +char *temp; + +if (hfeatures->extensions[word] &BIT (bit)) { + temp =sbi_hart_extension_id2string (ext); +if (temp) { +sbi_snprintf (extensions_str + offset, + ???? nestr - offset, + ???? "%s,", temp); + offset +=sbi_strlen (temp) +1 ; + }? }
? }lib/sbi/sbi_hart.c +1
-0+ }?
lib/sbi/sbi_hart.c +0
-4- ext++; - }while (ext <SBI_HART_EXT_MAX ); - -done:? if (offset)
? extensions_str[offset - 1] = '\0';
? elselib/sbi/sbi_hart.c +0
-0@@ -799,7 +803,7 @@ static int hart_detect_features(struct sbi_scratch *scratch) ? return 0;?
? /* Clear hart features */lib/sbi/sbi_hart.c +1
-1- hfeatures->extensions =0 ; +sbi_memset (hfeatures->extensions,0 ,sizeof (hfeatures->extensions));?
hfeatures->pmp_count= 0;
?hfeatures->mhpm_mask= 0;
?platform/generic/platform.c +0
-0@@ -215,7 +215,7 @@ static int generic_extensions_init(struct sbi_hart_features *hfeatures)?
? /* Parse the ISA string from FDT and enable the listed extensions */
? rc = fdt_parse_isa_extensions(fdt_get_address(),current_hartid(),platform/generic/platform.c +1
-1- ????? &hfeatures->extensions); + ????? hfeatures->extensions);?
? if (rc)
? return rc;Signature · 1 line
2.40.1
PATCH 1/1 lib: fix access to hfeatures->extensions
Quoted 6 lines · Xiang W
? 2023-11-11???? 04:26 +0100?Heinrich Schuchardt???
Quoted 5 lines
We may in future have more than xlen ISA extensions supported by OpenSBI. Commit
6259b2ec2d09("lib: utils/fdt: Fixfdt_parse_isa_extensions()implementation") therefore assumes thathfeatures->extensionsis an array. We have to make this change in the rest of the code too.
Quoted 8 lines
Quoted 3 lines
Fixes 6259b2ec2d09 ("lib: utils/fdt: Fix fdt_parse_isa_extensions() implementation")Addresses-Coverity-ID: 1568357 Out-of-bounds access
Signed-off-by Heinrich Schuchardt <[email protected]>Similar to this:
https://lists.infradead.org/pipermail/opensbi/2023-October/005787.htmlRegards,
Xiang W
I should have scanned the mailing list. Us two coming up with nearly identical solution confirms that we are on the right path.
Your usage of bitops macros looks nicer.
Best regards
Heinrich
Quoted 133 lines
Quoted 132 lines
---
?include/sbi/sbi_hart.h????? |? 3 ++- ?lib/sbi/sbi_hart.c????????? | 46 ++++++++++++++++++++----------------- ?platform/generic/platform.c |? 2 +-?3 files changed, 28 insertions(+), 23 deletions(-)
include/sbi/sbi_hart.h +2
-1@@ -10,6 +10,7 @@ ?#ifndef __SBI_HART_H__ ?#define __SBI_HART_H__ +#include <sbi/sbi_bitops.h> ?#include <sbi/sbi_types.h> ?/** Possible privileged specification versions of a hart */@@ -65,7 +66,7 @@ enum sbi_hart_extensions { ?struct sbi_hart_features { ? bool detected; ? int priv_version; -unsigned long extensions; +unsigned long extensions[BITS_TO_LONGS (SBI_HART_EXT_MAX )]; ? unsigned int pmp_count; ? unsigned int pmp_addr_bits; ? unsigned long pmp_gran;lib/sbi/sbi_hart.c +25
-21@@ -614,9 +614,9 @@ static inline void __sbi_hart_update_extension( ? bool enable) ?{ ? if (enable) - hfeatures->extensions |=BIT (ext); + hfeatures->extensions[BIT_WORD (ext)] |=BIT_MASK (ext); ? else - hfeatures->extensions &= ~BIT (ext); + hfeatures->extensions[BIT_WORD (ext)] &= ~BIT_MASK (ext); ?} ?/**@@ -649,7 +649,7 @@ bool sbi_hart_has_extension(struct sbi_scratch *scratch, ? struct sbi_hart_features *hfeatures = ? sbi_scratch_offset_ptr(scratch, hart_features_offset); -if (hfeatures->extensions &BIT (ext)) +if (hfeatures->extensions[BIT_WORD (ext)] &BIT_MASK (ext)) ? return true; ? else ? return false;@@ -705,31 +705,35 @@ void sbi_hart_get_extensions_str(struct sbi_scratch *scratch, ?{ ? struct sbi_hart_features *hfeatures = ? sbi_scratch_offset_ptr(scratch, hart_features_offset); -int offset =0 , ext =0 ; - char *temp; + int offset = 0; ? if (!extensions_str || nestr <= 0) ? return; ? sbi_memset(extensions_str, 0, nestr); - if (!hfeatures->extensions) - goto done; - - do { - if (hfeatures->extensions & BIT(ext)) { - temp = sbi_hart_extension_id2string(ext); - if (temp) { - sbi_snprintf(extensions_str + offset, - ???? nestr - offset, - ???? "%s,", temp); - offset = offset + sbi_strlen(temp) + 1; + for (int word = 0; word < BITS_TO_LONGS(SBI_HART_EXT_MAX); ++word) { + int ext; + + if (!hfeatures->extensions[word]) + continue; + + ext = word * BITS_PER_LONG; + for (int bit = 0; bit < BITS_PER_LONG && ext < SBI_HART_EXT_MAX; + ???? ++bit, ++ext) { + char *temp; + + if (hfeatures->extensions[word] & BIT(bit)) { + temp = sbi_hart_extension_id2string(ext); + if (temp) { + sbi_snprintf(extensions_str + offset, + ???? nestr - offset, + ???? "%s,", temp); + offset += sbi_strlen(temp) + 1; + } ? } ? } + } - ext++; - } while (ext < SBI_HART_EXT_MAX); - -done: ? if (offset) ? extensions_str[offset - 1] = '\0'; ? else@@ -799,7 +803,7 @@ static int hart_detect_features(struct sbi_scratch *scratch) ? return 0; ? /* Clear hart features */ - hfeatures->extensions =0 ; +sbi_memset (hfeatures->extensions,0 ,sizeof (hfeatures->extensions)); ? hfeatures->pmp_count = 0; ? hfeatures->mhpm_mask = 0;platform/generic/platform.c +1
-1@@ -215,7 +215,7 @@ static int generic_extensions_init(struct sbi_hart_features *hfeatures) ? /* Parse the ISA string from FDT and enable the listed extensions */ ? rc = fdt_parse_isa_extensions(fdt_get_address(), current_hartid(), - ????? &hfeatures->extensions); + ????? hfeatures->extensions); ? if (rc) ? return rc;Signature · 1 line
2.40.1