[prev in list] [next in list] [prev in thread] [next in thread] 

List:       fwts-devel
Subject:    [PATCH] lib: fwts_acpi_object_eval: avoid any division by zero
From:       Colin King <colin.king () canonical ! com>
Date:       2020-04-07 18:45:32
Message-ID: 20200407184532.948596-1-colin.king () canonical ! com
[Download RAW message or body]

From: Colin Ian King <colin.king@canonical.com>

There are a couple of divisions by granularity + 1 and
granularity - 1 that may be overflow or underflowed to zero
causing a division by zero error. Add a sanity check and
flag an error if granularity is going to cause this division
error.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_acpi_object_eval.c | 79 +++++++++++++++++++----------
 1 file changed, 51 insertions(+), 28 deletions(-)

diff --git a/src/lib/src/fwts_acpi_object_eval.c b/src/lib/src/fwts_acpi_object_eval.c
index 692e4ec0..3845d424 100644
--- a/src/lib/src/fwts_acpi_object_eval.c
+++ b/src/lib/src/fwts_acpi_object_eval.c
@@ -1304,37 +1304,60 @@ static void method_test_CRS_mif_maf(
 			fwts_advice(fw, "%s", mif_maf_advice);
 			*passed = false;
 		}
-		if ((mif == 1) && (min % (granularity + 1) != 0)) {
-			snprintf(tmp, sizeof(tmp), "Method%s%sMinNotMultipleOfGran", objname, tag);
-			if (fw->flags & FWTS_FLAG_TEST_SBBR)
+
+		if (mif == 1) {
+			const uint64_t tmpgran = granularity + 1;
+
+			if (tmpgran == 0) {
+				/* Avoid min % tmp division by zero, flag an error */
+				snprintf(tmp, sizeof(tmp), "Method%s%sGranTooLarge", objname, tag);
 				fwts_warning(fw, tmp,
-					"%s %s _MIN address is not a multiple "
-					"of the granularity when _MIF is 1.",
-					name, type);
-			else
-				fwts_failed(fw, LOG_LEVEL_MEDIUM,
-					tmp,
-					"%s %s _MIN address is not a multiple "
-					"of the granularity when _MIF is 1.",
-					name, type);
-			fwts_advice(fw, "%s", mif_maf_advice);
-			*passed = false;
+					"%s %s granularity 0x%" PRIx64 " is too large when _MIF is 1.",
+					name, type, granularity);
+				*passed = false;
+			} else if (min % tmpgran != 0) {
+				snprintf(tmp, sizeof(tmp), "Method%s%sMinNotMultipleOfGran", objname, tag);
+				if (fw->flags & FWTS_FLAG_TEST_SBBR)
+					fwts_warning(fw, tmp,
+						"%s %s _MIN address is not a multiple "
+						"of the granularity when _MIF is 1.",
+						name, type);
+				else
+					fwts_failed(fw, LOG_LEVEL_MEDIUM,
+						tmp,
+						"%s %s _MIN address is not a multiple "
+						"of the granularity when _MIF is 1.",
+						name, type);
+				fwts_advice(fw, "%s", mif_maf_advice);
+				*passed = false;
+			}
 		}
-		if ((maf == 1) && (max % (granularity - 1) != 0)) {
-			snprintf(tmp, sizeof(tmp), "Method%s%sMaxNotMultipleOfGran", objname, tag);
-			if (fw->flags & FWTS_FLAG_TEST_SBBR)
+		if (maf == 1) {
+			const uint64_t tmpgran = granularity - 1;
+
+			if (tmpgran == 0) {
+				/* Avoid max % tmp division by zero, flag an error */
+				snprintf(tmp, sizeof(tmp), "Method%s%sGranTooSmall", objname, tag);
 				fwts_warning(fw, tmp,
-					"%s %s _MAX address is not a multiple "
-					"of the granularity when _MAF is 1.",
-					name, type);
-			else
-				fwts_failed(fw, LOG_LEVEL_MEDIUM,
-					tmp,
-					"%s %s _MAX address is not a multiple "
-					"of the granularity when _MAF is 1.",
-					name, type);
-			fwts_advice(fw, "%s", mif_maf_advice);
-			*passed = false;
+					"%s %s granularity 0x%" PRIx64 " is too small when _MAF is 1.",
+					name, type, granularity);
+				*passed = false;
+			} else if (max % tmpgran != 0) {
+				snprintf(tmp, sizeof(tmp), "Method%s%sMaxNotMultipleOfGran", objname, tag);
+				if (fw->flags & FWTS_FLAG_TEST_SBBR)
+					fwts_warning(fw, tmp,
+						"%s %s _MAX address is not a multiple "
+						"of the granularity when _MAF is 1.",
+						name, type);
+				else
+					fwts_failed(fw, LOG_LEVEL_MEDIUM,
+						tmp,
+						"%s %s _MAX address is not a multiple "
+						"of the granularity when _MAF is 1.",
+						name, type);
+				fwts_advice(fw, "%s", mif_maf_advice);
+				*passed = false;
+			}
 		}
 	} else {
 		if ((mif == 0) && (maf == 0) &&
-- 
2.25.1


-- 
fwts-devel mailing list
fwts-devel@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/fwts-devel

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic