All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Matthew Wilcox <mawilcox@microsoft.com>
Cc: Kees Cook <keescook@chromium.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Matthew Wilcox <willy@infradead.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Linux-MM <linux-mm@kvack.org>,
	Kernel Hardening <kernel-hardening@lists.openwall.com>
Subject: [PATCH v3 03/16] lib: overflow: Report test failures
Date: Thu, 31 May 2018 17:42:20 -0700	[thread overview]
Message-ID: <20180601004233.37822-4-keescook@chromium.org> (raw)
In-Reply-To: <20180601004233.37822-1-keescook@chromium.org>

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

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 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

  parent reply	other threads:[~2018-06-01  0:44 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-01  0:42 [PATCH v3 00/16] Provide saturating helpers for allocation Kees Cook
2018-06-01  0:42 ` [PATCH v3 01/16] compiler.h: enable builtin overflow checkers and add fallback code Kees Cook
2018-06-01  0:42 ` [PATCH v3 02/16] lib: add runtime test of check_*_overflow functions Kees Cook
2018-06-01  0:42 ` Kees Cook [this message]
2018-06-01  0:42 ` [PATCH v3 04/16] overflow.h: Add allocation size calculation helpers Kees Cook
2018-06-01  0:42 ` [PATCH v3 05/16] lib: overflow: Add memory allocation overflow tests Kees Cook
2018-06-01 10:18   ` Andy Shevchenko
2018-06-01  0:42 ` [PATCH v3 06/16] mm: Use overflow helpers in kmalloc_array*() Kees Cook
2018-06-01  0:42 ` [PATCH v3 07/16] mm: Use overflow helpers in kvmalloc() Kees Cook
2018-06-01  0:42 ` [PATCH v3 08/16] device: Use overflow helpers for devm_kmalloc() Kees Cook
2018-06-01  0:42 ` [PATCH v3 09/16] treewide: Use struct_size() for kmalloc()-family Kees Cook
2018-06-01  0:42 ` [PATCH v3 10/16] treewide: Use struct_size() for vmalloc()-family Kees Cook
2018-06-01  0:42 ` [PATCH v3 11/16] treewide: Use struct_size() for devm_kmalloc() and friends Kees Cook
2018-06-01  0:42 ` [PATCH v3 12/16] treewide: Use array_size() for kmalloc()-family Kees Cook
2018-07-01  8:46   ` SF Markus Elfring
2018-07-01  8:46     ` SF Markus Elfring
2018-07-01  8:46     ` SF Markus Elfring
2018-07-01  9:03     ` Julia Lawall
2018-07-01  9:03       ` Julia Lawall
2018-07-01  9:03       ` Julia Lawall
2018-07-01  9:22       ` SF Markus Elfring
2018-07-01  9:22         ` SF Markus Elfring
2018-06-01  0:42 ` [PATCH v3 13/16] treewide: Use array_size() for kmalloc()-family, leftovers Kees Cook
2018-06-01  0:42 ` [PATCH v3 14/16] treewide: Use array_size() for vmalloc() Kees Cook
2018-06-01  0:42 ` [PATCH v3 15/16] treewide: Use array_size() for devm_*alloc()-like Kees Cook
2018-06-01  0:42 ` [PATCH v3 16/16] treewide: Use array_size() for devm_*alloc()-like, leftovers Kees Cook
2018-06-01  0:54 ` [PATCH v3 00/16] Provide saturating helpers for allocation Linus Torvalds
2018-06-01  4:18   ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180601004233.37822-4-keescook@chromium.org \
    --to=keescook@chromium.org \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mawilcox@microsoft.com \
    --cc=torvalds@linux-foundation.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.