From patchwork Fri Jun 1 00:42:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10442241 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 14822603D7 for ; Fri, 1 Jun 2018 00:44:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 12E0628E43 for ; Fri, 1 Jun 2018 00:44:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 07D7F28E58; Fri, 1 Jun 2018 00:44:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 2E0EC29022 for ; Fri, 1 Jun 2018 00:44:53 +0000 (UTC) Received: (qmail 27863 invoked by uid 550); 1 Jun 2018 00:43:55 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 27824 invoked from network); 1 Jun 2018 00:43:55 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CQ8LksIQh9KsZ1H2uceEEDPecstNN01yGfGOCPPwwSo=; b=X2Rrbx+JqI3CdJeiQi/z+NhzfXvz07/PR8IOTT2AMqEMCe/SN/9z05iz6DPK0Lmq6g uQ7szkQztiaj/3Ne3d7YqzHJ1u8om4LZ+MyaCOtsLGfUkk2KtwzGUuYaxtOctQP8FDfp rMo+2+X3UFYvt4RN78mDJy3Hl/Wc3CXZNF4hw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CQ8LksIQh9KsZ1H2uceEEDPecstNN01yGfGOCPPwwSo=; b=WbDFCAkR4dnSojkq0MAnB8qd6LbeuCG08cN/C5zX7HbN09AvRKK9GmvQN6l1OdCll2 jah4YpLeo9sMox4tz0CBMGpxTvS+lakwo9SULUl2cxJZLGbmpyM/bqtboKcFU/+Zp8/C rTtx8MN3vMI2dkSDjy9yroeWwuY3hIMnYMazgSh6ty62jZEPVTVqUaxQvNH6q2bCTGWl sxuY4/xzpeRNtBU8KG0nd/ZYueB0kWmFUmtWSIctX36BHsk+HmKYKUtXNRbpQ8XskaWh eNCZL66X3p8t6A6MxqkPmhE6rmANfp5BFYzAMDOJnnrSurwCnb1tjuYIruUnMoWQ9BSo TNCQ== X-Gm-Message-State: ALKqPwePQvM+jFqQn9OHTlqjg46JKmD08MBuSLZPpeuL2m5WbqWmNvTr 7tJjSqdbD8c1VAPvOoZZ6+x2oA== X-Google-Smtp-Source: ADUXVKLJf68l0gHanyLEhr7CAR1CYwoE5tW4NkFgzIvFdrqHGrrskT5GOR20XOemwAS8yZrhuamc+w== X-Received: by 2002:a17:902:3124:: with SMTP id w33-v6mr4471859plb.235.1527813823197; Thu, 31 May 2018 17:43:43 -0700 (PDT) From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Linus Torvalds , Rasmus Villemoes , Matthew Wilcox , LKML , Linux-MM , Kernel Hardening Subject: [PATCH v3 03/16] lib: overflow: Report test failures Date: Thu, 31 May 2018 17:42:20 -0700 Message-Id: <20180601004233.37822-4-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180601004233.37822-1-keescook@chromium.org> References: <20180601004233.37822-1-keescook@chromium.org> X-Virus-Scanned: ClamAV using ClamSMTP This adjusts the overflow test to report failures, and prepares to add allocation tests. Signed-off-by: Kees Cook --- 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");