diff mbox series

[1/2] acpi: refactor by fwts_acpi_structure_range_check

Message ID 20210513192036.1692916-1-alex.hung@canonical.com
State Accepted
Headers show
Series [1/2] acpi: refactor by fwts_acpi_structure_range_check | expand

Commit Message

Alex Hung May 13, 2021, 7:20 p.m. UTC
Signed-off-by: Alex Hung <alex.hung@canonical.com>
---
 src/acpi/hmat/hmat.c | 7 +++----
 src/acpi/nfit/nfit.c | 6 ++++++
 src/acpi/pmtt/pmtt.c | 5 +++++
 src/acpi/pptt/pptt.c | 7 +++----
 src/acpi/sdev/sdev.c | 6 ++----
 5 files changed, 19 insertions(+), 12 deletions(-)

Comments

Colin Ian King May 13, 2021, 7:33 p.m. UTC | #1
On 13/05/2021 20:20, Alex Hung wrote:
> Signed-off-by: Alex Hung <alex.hung@canonical.com>
> ---
>  src/acpi/hmat/hmat.c | 7 +++----
>  src/acpi/nfit/nfit.c | 6 ++++++
>  src/acpi/pmtt/pmtt.c | 5 +++++
>  src/acpi/pptt/pptt.c | 7 +++----
>  src/acpi/sdev/sdev.c | 6 ++----
>  5 files changed, 19 insertions(+), 12 deletions(-)
> 
> diff --git a/src/acpi/hmat/hmat.c b/src/acpi/hmat/hmat.c
> index 7de8b077..c2efdc93 100644
> --- a/src/acpi/hmat/hmat.c
> +++ b/src/acpi/hmat/hmat.c
> @@ -199,13 +199,12 @@ static int hmat_test1(fwts_framework *fw)
>  			break;
>  		}
>  
> -		if ((offset += entry->length) > table->length) {
> +		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "HMAT", table->length, offset)) {
>  			passed = false;
> -			fwts_failed(fw, LOG_LEVEL_CRITICAL,
> -				"HMATBadTableLength",
> -				"HMAT has more subtypes than its size can handle");
>  			break;
>  		}
> +
>  		entry = (fwts_acpi_table_hmat_header *) (table->data + offset);
>  		fwts_log_nl(fw);
>  	}
> diff --git a/src/acpi/nfit/nfit.c b/src/acpi/nfit/nfit.c
> index a819fa3e..7e250aba 100644
> --- a/src/acpi/nfit/nfit.c
> +++ b/src/acpi/nfit/nfit.c
> @@ -516,7 +516,13 @@ static int nfit_test1(fwts_framework *fw)
>  
>  		fwts_acpi_reserved_zero_check("NFIT", "Reserved", reserved_passed, &passed);
>  		fwts_log_nl(fw);
> +
>  		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "NFIT", nfit_table->length, offset)) {
> +			passed = false;
> +			break;
> +		}
> +
>  		entry = (fwts_acpi_table_nfit_struct_header *)(nfit_table->data + offset);
>  	}
>  
> diff --git a/src/acpi/pmtt/pmtt.c b/src/acpi/pmtt/pmtt.c
> index 7cd0952b..74e49717 100644
> --- a/src/acpi/pmtt/pmtt.c
> +++ b/src/acpi/pmtt/pmtt.c
> @@ -206,6 +206,11 @@ static int pmtt_test1(fwts_framework *fw)
>  		pmtt_memory_device(fw, entry, offset, &passed);
>  
>  		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "PMTT", table->length, offset)) {
> +			passed = false;
> +			break;
> +		}
> +
>  		entry = (fwts_acpi_table_pmtt_header *) (table->data + offset);
>  		fwts_log_nl(fw);
>  	}
> diff --git a/src/acpi/pptt/pptt.c b/src/acpi/pptt/pptt.c
> index 817b0c77..2a4513a6 100644
> --- a/src/acpi/pptt/pptt.c
> +++ b/src/acpi/pptt/pptt.c
> @@ -163,13 +163,12 @@ static int pptt_test1(fwts_framework *fw)
>  			break;
>  		}
>  
> -		if ((offset += entry->length) > table->length) {
> +		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "PPTT", table->length, offset)) {
>  			passed = false;
> -			fwts_failed(fw, LOG_LEVEL_CRITICAL,
> -				"PPTTBadTableLength",
> -				"PPTT has more subtypes than its size can handle");
>  			break;
>  		}
> +
>  		entry = (fwts_acpi_table_pptt_header *) (table->data + offset);
>  		fwts_log_nl(fw);
>  	}
> diff --git a/src/acpi/sdev/sdev.c b/src/acpi/sdev/sdev.c
> index f03566e8..8946c5e9 100644
> --- a/src/acpi/sdev/sdev.c
> +++ b/src/acpi/sdev/sdev.c
> @@ -104,11 +104,9 @@ static int sdev_test1(fwts_framework *fw)
>  			break;
>  		}
>  
> -		if ((offset += entry->length) > table->length) {
> +		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "SDEV", table->length, offset)) {
>  			passed = false;
> -			fwts_failed(fw, LOG_LEVEL_CRITICAL,
> -				"SDEVBadTableLength",
> -				"SDEV has more subtypes than its size can handle");
>  			break;
>  		}
>  
> 
Acked-by: Colin Ian King <colin.king@canonical.com>
Ivan Hu May 17, 2021, 6:02 a.m. UTC | #2
On 5/14/21 3:20 AM, Alex Hung wrote:
> Signed-off-by: Alex Hung <alex.hung@canonical.com>
> ---
>  src/acpi/hmat/hmat.c | 7 +++----
>  src/acpi/nfit/nfit.c | 6 ++++++
>  src/acpi/pmtt/pmtt.c | 5 +++++
>  src/acpi/pptt/pptt.c | 7 +++----
>  src/acpi/sdev/sdev.c | 6 ++----
>  5 files changed, 19 insertions(+), 12 deletions(-)
> 
> diff --git a/src/acpi/hmat/hmat.c b/src/acpi/hmat/hmat.c
> index 7de8b077..c2efdc93 100644
> --- a/src/acpi/hmat/hmat.c
> +++ b/src/acpi/hmat/hmat.c
> @@ -199,13 +199,12 @@ static int hmat_test1(fwts_framework *fw)
>  			break;
>  		}
>  
> -		if ((offset += entry->length) > table->length) {
> +		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "HMAT", table->length, offset)) {
>  			passed = false;
> -			fwts_failed(fw, LOG_LEVEL_CRITICAL,
> -				"HMATBadTableLength",
> -				"HMAT has more subtypes than its size can handle");
>  			break;
>  		}
> +
>  		entry = (fwts_acpi_table_hmat_header *) (table->data + offset);
>  		fwts_log_nl(fw);
>  	}
> diff --git a/src/acpi/nfit/nfit.c b/src/acpi/nfit/nfit.c
> index a819fa3e..7e250aba 100644
> --- a/src/acpi/nfit/nfit.c
> +++ b/src/acpi/nfit/nfit.c
> @@ -516,7 +516,13 @@ static int nfit_test1(fwts_framework *fw)
>  
>  		fwts_acpi_reserved_zero_check("NFIT", "Reserved", reserved_passed, &passed);
>  		fwts_log_nl(fw);
> +
>  		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "NFIT", nfit_table->length, offset)) {
> +			passed = false;
> +			break;
> +		}
> +
>  		entry = (fwts_acpi_table_nfit_struct_header *)(nfit_table->data + offset);
>  	}
>  
> diff --git a/src/acpi/pmtt/pmtt.c b/src/acpi/pmtt/pmtt.c
> index 7cd0952b..74e49717 100644
> --- a/src/acpi/pmtt/pmtt.c
> +++ b/src/acpi/pmtt/pmtt.c
> @@ -206,6 +206,11 @@ static int pmtt_test1(fwts_framework *fw)
>  		pmtt_memory_device(fw, entry, offset, &passed);
>  
>  		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "PMTT", table->length, offset)) {
> +			passed = false;
> +			break;
> +		}
> +
>  		entry = (fwts_acpi_table_pmtt_header *) (table->data + offset);
>  		fwts_log_nl(fw);
>  	}
> diff --git a/src/acpi/pptt/pptt.c b/src/acpi/pptt/pptt.c
> index 817b0c77..2a4513a6 100644
> --- a/src/acpi/pptt/pptt.c
> +++ b/src/acpi/pptt/pptt.c
> @@ -163,13 +163,12 @@ static int pptt_test1(fwts_framework *fw)
>  			break;
>  		}
>  
> -		if ((offset += entry->length) > table->length) {
> +		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "PPTT", table->length, offset)) {
>  			passed = false;
> -			fwts_failed(fw, LOG_LEVEL_CRITICAL,
> -				"PPTTBadTableLength",
> -				"PPTT has more subtypes than its size can handle");
>  			break;
>  		}
> +
>  		entry = (fwts_acpi_table_pptt_header *) (table->data + offset);
>  		fwts_log_nl(fw);
>  	}
> diff --git a/src/acpi/sdev/sdev.c b/src/acpi/sdev/sdev.c
> index f03566e8..8946c5e9 100644
> --- a/src/acpi/sdev/sdev.c
> +++ b/src/acpi/sdev/sdev.c
> @@ -104,11 +104,9 @@ static int sdev_test1(fwts_framework *fw)
>  			break;
>  		}
>  
> -		if ((offset += entry->length) > table->length) {
> +		offset += entry->length;
> +		if (fwts_acpi_structure_range_check(fw, "SDEV", table->length, offset)) {
>  			passed = false;
> -			fwts_failed(fw, LOG_LEVEL_CRITICAL,
> -				"SDEVBadTableLength",
> -				"SDEV has more subtypes than its size can handle");
>  			break;
>  		}
>  
> 


Acked-by: Ivan Hu <ivan.hu@canonical.com>
diff mbox series

Patch

diff --git a/src/acpi/hmat/hmat.c b/src/acpi/hmat/hmat.c
index 7de8b077..c2efdc93 100644
--- a/src/acpi/hmat/hmat.c
+++ b/src/acpi/hmat/hmat.c
@@ -199,13 +199,12 @@  static int hmat_test1(fwts_framework *fw)
 			break;
 		}
 
-		if ((offset += entry->length) > table->length) {
+		offset += entry->length;
+		if (fwts_acpi_structure_range_check(fw, "HMAT", table->length, offset)) {
 			passed = false;
-			fwts_failed(fw, LOG_LEVEL_CRITICAL,
-				"HMATBadTableLength",
-				"HMAT has more subtypes than its size can handle");
 			break;
 		}
+
 		entry = (fwts_acpi_table_hmat_header *) (table->data + offset);
 		fwts_log_nl(fw);
 	}
diff --git a/src/acpi/nfit/nfit.c b/src/acpi/nfit/nfit.c
index a819fa3e..7e250aba 100644
--- a/src/acpi/nfit/nfit.c
+++ b/src/acpi/nfit/nfit.c
@@ -516,7 +516,13 @@  static int nfit_test1(fwts_framework *fw)
 
 		fwts_acpi_reserved_zero_check("NFIT", "Reserved", reserved_passed, &passed);
 		fwts_log_nl(fw);
+
 		offset += entry->length;
+		if (fwts_acpi_structure_range_check(fw, "NFIT", nfit_table->length, offset)) {
+			passed = false;
+			break;
+		}
+
 		entry = (fwts_acpi_table_nfit_struct_header *)(nfit_table->data + offset);
 	}
 
diff --git a/src/acpi/pmtt/pmtt.c b/src/acpi/pmtt/pmtt.c
index 7cd0952b..74e49717 100644
--- a/src/acpi/pmtt/pmtt.c
+++ b/src/acpi/pmtt/pmtt.c
@@ -206,6 +206,11 @@  static int pmtt_test1(fwts_framework *fw)
 		pmtt_memory_device(fw, entry, offset, &passed);
 
 		offset += entry->length;
+		if (fwts_acpi_structure_range_check(fw, "PMTT", table->length, offset)) {
+			passed = false;
+			break;
+		}
+
 		entry = (fwts_acpi_table_pmtt_header *) (table->data + offset);
 		fwts_log_nl(fw);
 	}
diff --git a/src/acpi/pptt/pptt.c b/src/acpi/pptt/pptt.c
index 817b0c77..2a4513a6 100644
--- a/src/acpi/pptt/pptt.c
+++ b/src/acpi/pptt/pptt.c
@@ -163,13 +163,12 @@  static int pptt_test1(fwts_framework *fw)
 			break;
 		}
 
-		if ((offset += entry->length) > table->length) {
+		offset += entry->length;
+		if (fwts_acpi_structure_range_check(fw, "PPTT", table->length, offset)) {
 			passed = false;
-			fwts_failed(fw, LOG_LEVEL_CRITICAL,
-				"PPTTBadTableLength",
-				"PPTT has more subtypes than its size can handle");
 			break;
 		}
+
 		entry = (fwts_acpi_table_pptt_header *) (table->data + offset);
 		fwts_log_nl(fw);
 	}
diff --git a/src/acpi/sdev/sdev.c b/src/acpi/sdev/sdev.c
index f03566e8..8946c5e9 100644
--- a/src/acpi/sdev/sdev.c
+++ b/src/acpi/sdev/sdev.c
@@ -104,11 +104,9 @@  static int sdev_test1(fwts_framework *fw)
 			break;
 		}
 
-		if ((offset += entry->length) > table->length) {
+		offset += entry->length;
+		if (fwts_acpi_structure_range_check(fw, "SDEV", table->length, offset)) {
 			passed = false;
-			fwts_failed(fw, LOG_LEVEL_CRITICAL,
-				"SDEVBadTableLength",
-				"SDEV has more subtypes than its size can handle");
 			break;
 		}