[PATCH v3 03/16] lib: overflow: Report test failures

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This adjusts the overflow test to report failures, and prepares to
add allocation tests.

Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>
---
 lib/test_overflow.c | 75 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 54 insertions(+), 21 deletions(-)

diff --git a/lib/test_overflow.c b/lib/test_overflow.c
index e1e45ba17ff0..482d71c880fa 100644
--- a/lib/test_overflow.c
+++ b/lib/test_overflow.c
@@ -212,42 +212,59 @@ DEFINE_TEST_ARRAY(s64) = {
 };
 
 #define DEFINE_TEST_FUNC(t, fmt)					\
-static void __init do_test_ ## t(const struct test_ ## t *p)		\
+static int __init do_test_ ## t(const struct test_ ## t *p)		\
 {							   		\
 	t r;								\
+	int err = 0;							\
 	bool of;							\
 									\
 	of = check_add_overflow(p->a, p->b, &r);			\
-	if (of != p->s_of)						\
+	if (of != p->s_of) {						\
 		pr_warn("expected "fmt" + "fmt" to%s overflow (type %s)\n", \
 			p->a, p->b, p->s_of ? "" : " not", #t);		\
-	if (r != p->sum)						\
+		err = 1;						\
+	}								\
+	if (r != p->sum) {						\
 		pr_warn("expected "fmt" + "fmt" == "fmt", got "fmt" (type %s)\n", \
 			p->a, p->b, p->sum, r, #t);			\
+		err = 1;						\
+	}								\
 									\
 	of = check_sub_overflow(p->a, p->b, &r);			\
-	if (of != p->d_of)						\
+	if (of != p->d_of) {						\
 		pr_warn("expected "fmt" - "fmt" to%s overflow (type %s)\n", \
 			p->a, p->b, p->s_of ? "" : " not", #t);		\
-	if (r != p->diff)						\
+		err = 1;						\
+	}								\
+	if (r != p->diff) {						\
 		pr_warn("expected "fmt" - "fmt" == "fmt", got "fmt" (type %s)\n", \
 			p->a, p->b, p->diff, r, #t);			\
+		err = 1;						\
+	}								\
 									\
 	of = check_mul_overflow(p->a, p->b, &r);			\
-	if (of != p->p_of)						\
+	if (of != p->p_of) {						\
 		pr_warn("expected "fmt" * "fmt" to%s overflow (type %s)\n", \
 			p->a, p->b, p->p_of ? "" : " not", #t);		\
-	if (r != p->prod)						\
+		err = 1;						\
+	}								\
+	if (r != p->prod) {						\
 		pr_warn("expected "fmt" * "fmt" == "fmt", got "fmt" (type %s)\n", \
 			p->a, p->b, p->prod, r, #t);			\
+		err = 1;						\
+	}								\
+									\
+	return err;							\
 }									\
 									\
-static void __init test_ ## t ## _overflow(void) {			\
+static int __init test_ ## t ## _overflow(void) {			\
 	unsigned i;							\
+	int err = 0;							\
 									\
 	pr_info("%-3s: %zu tests\n", #t, ARRAY_SIZE(t ## _tests));	\
 	for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i)			\
-		do_test_ ## t(&t ## _tests[i]);				\
+		err |= do_test_ ## t(&t ## _tests[i]);			\
+	return err;							\
 }
 
 DEFINE_TEST_FUNC(u8, "%d");
@@ -260,26 +277,42 @@ DEFINE_TEST_FUNC(s16, "%d");
 DEFINE_TEST_FUNC(s32, "%d");
 DEFINE_TEST_FUNC(s64, "%lld");
 
-static int __init test_overflow(void)
+static int __init test_overflow_calculation(void)
+{
+	int err = 0;
+
+	err |= test_u8_overflow();
+	err |= test_u16_overflow();
+	err |= test_u32_overflow();
+	err |= test_u64_overflow();
+
+	err |= test_s8_overflow();
+	err |= test_s16_overflow();
+	err |= test_s32_overflow();
+	err |= test_s64_overflow();
+
+	return err;
+}
+
+static int __init test_module_init(void)
 {
-	test_u8_overflow();
-	test_u16_overflow();
-	test_u32_overflow();
-	test_u64_overflow();
+	int err = 0;
 
-	test_s8_overflow();
-	test_s16_overflow();
-	test_s32_overflow();
-	test_s64_overflow();
+	err |= test_overflow_calculation();
 
-	pr_info("done\n");
+	if (err) {
+		pr_warn("FAIL!\n");
+		err = -EINVAL;
+	} else {
+		pr_info("all tests passed\n");
+	}
 
-	return 0;
+	return err;
 }
 
 static void __exit test_module_exit(void)
 { }
 
-module_init(test_overflow);
+module_init(test_module_init);
 module_exit(test_module_exit);
 MODULE_LICENSE("Dual MIT/GPL");
-- 
2.17.0




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux