Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp27828imm; Thu, 31 May 2018 17:45:00 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKW6xL2zN8d5h6BCOKmtK9w9H2PF6DeHuYR1fBoBd8X5POcgpC33ZjRiIG5wDcd5ys4k5s0 X-Received: by 2002:a63:6185:: with SMTP id v127-v6mr6945690pgb.301.1527813900197; Thu, 31 May 2018 17:45:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527813900; cv=none; d=google.com; s=arc-20160816; b=Vm1goZnk9IMKkVsAUgsQ3CsyD53lw/VJMxEenlwJy6OV2mQY94tttAsXFpA6iQRrj1 k3EpC+RZx4rEjXqCmoVXI1k6uX9uuqHnrndQmceFQ5ZD8vS2d2Ug/uu0yrlfQnhzUjT8 SN2sJq70+avrnWdATPdidHl0oDbX/EACy+sL1hxyrPu+E/ju6JgsdN1+p1nD4jSOHz82 WA598yEHIIPLjo11Oq+nU83SCxlCIGdnOdv/4F0CDhKNvSa0ccNscmkzcAH6hf/ynZNc Q+QVQtuhoq/wcDq8Gs/A+KD3PBUahK0VGaHfjhYUAD54s7IwskKWtIxCzcAC314hSTEz gq/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=CQ8LksIQh9KsZ1H2uceEEDPecstNN01yGfGOCPPwwSo=; b=BWM3r/DlHzN0Fvf1ZU87ZUNt5caajzVh/OHhCZEN7159WjwwP1QRGXGLcMGp6eYxJA tGQtn1y4djVhAy+jl+/chWa9xS148yd/+D4Bqyb9+XJWBo9Z1OYAU84isTZGY9W7+/5X JfnGnuavxLnu6fx7U4v6W8N9YdCNHdIgk6KNk6zPmTxDrHjK+gsYlmDZKcWdRoX8rAMJ lDNttW6XdkfcxuRHPyrjUNHFwfAOKzM7PLCd36Ln2c2s713okkQpJTlQiB+/jFn39yaL XN4SUz+y/jrA9SVcj2BMjSS/qBkI8wPzQWlmv21H3E6OkujaWGF07D8Da40ieZTHyuUX pzQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=X2Rrbx+J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x24-v6si38091895pfk.311.2018.05.31.17.44.45; Thu, 31 May 2018 17:45:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=X2Rrbx+J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751503AbeFAAoA (ORCPT + 99 others); Thu, 31 May 2018 20:44:00 -0400 Received: from mail-pl0-f67.google.com ([209.85.160.67]:35611 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751218AbeFAAno (ORCPT ); Thu, 31 May 2018 20:43:44 -0400 Received: by mail-pl0-f67.google.com with SMTP id i5-v6so14242801plt.2 for ; Thu, 31 May 2018 17:43:43 -0700 (PDT) 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=T5/ex5fsgVP489zn3vKl5ZZ4IfRsdUbL5TBHFTtkFFrODXRiIHAgnUtdBXpDI4k5nm idAVFuls3Mb4T3BKCgIKApG7/0axVT8CA42RXEPifV6tMnDYM4hn59i3QGAFP62jddxE LktHippenpjp+iGiHDqrLuetUcMsxHOl6JY6fyXDgvjZxKM5WHI1GPUevMzVrRqaH/OQ t7FV0T/SwKtc8kCuJloArYuaa8errcNu9/n2gZ4YNIxGHbOrFlBTV7jIzeCg1n5z19z2 t4UlcFOY/9Yuabvb6MUvQtoOFbJ+kiyL6Pv/FKerPyhZzPph//79sdBBCPRzepwodDjT oJ3Q== X-Gm-Message-State: ALKqPwd9hfP1JuHuKlxOuuQJpkpa5WzGG7eDtUDOzOKB6JyAtQfkr72u LJXe+jy94VLd0yasyVroum+N+Q== X-Received: by 2002:a17:902:3124:: with SMTP id w33-v6mr4471859plb.235.1527813823197; Thu, 31 May 2018 17:43:43 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id g15-v6sm49160300pgv.58.2018.05.31.17.43.40 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 31 May 2018 17:43:41 -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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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"); -- 2.17.0