From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753020AbdKHWbR (ORCPT ); Wed, 8 Nov 2017 17:31:17 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:53683 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752755AbdKHWau (ORCPT ); Wed, 8 Nov 2017 17:30:50 -0500 X-Google-Smtp-Source: AGs4zMZ07y1fAiLirdeYLHdrAvQ2ysESASC6DMVYgR2/J2hqRKcs0FzNPEQ7VLT9RHTq8mGCthkn8w== From: Rasmus Villemoes To: kernel-hardening@lists.openwall.com Cc: linux-kernel@vger.kernel.org, Andrew Morton , Kees Cook , Rasmus Villemoes Subject: [RFC 6/6] lib/test_printf.c: add a few fmtcheck() test cases Date: Wed, 8 Nov 2017 23:30:20 +0100 Message-Id: <20171108223020.24487-7-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171108223020.24487-1-linux@rasmusvillemoes.dk> References: <20171108223020.24487-1-linux@rasmusvillemoes.dk> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It should be trivial to add more test cases, once we figure out the exact rules for being compatible or not. Perhaps we'll have to extend the struct test with a flags element if we add flags that affect the return value. Signed-off-by: Rasmus Villemoes --- lib/test_printf.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/lib/test_printf.c b/lib/test_printf.c index 563f10e6876a..0c8490f3a9b3 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -483,6 +483,45 @@ test_pointer(void) flags(); } +static void __init +test_fmtcheck(void) +{ + struct test { const char *fmt; const char *tmpl; }; + static const struct test compatible[] __initconst = { + {"", ""}, + {"wlan%d", "%d"}, + {"aa%llxbb", "cc%Lxdd%%"}, + }; + static const struct test incompatible[] __initconst = { + {"a %d b %lx", "%d %x"}, + {"%llo", "%Lx"}, + }; + unsigned i; + const struct test *t; + const char *ret; + + for (i = 0; i < ARRAY_SIZE(compatible); ++i) { + total_tests++; + t = &compatible[i]; + ret = _fmtcheck(t->fmt, t->tmpl, FMTCHECK_SILENT); + if (ret != t->fmt) { + failed_tests++; + pr_warn("'%s' and '%s' deemed incompatible by fmtcheck()", + t->fmt, t->tmpl); + } + } + for (i = 0; i < ARRAY_SIZE(incompatible); ++i) { + total_tests++; + t = &incompatible[i]; + ret = _fmtcheck(t->fmt, t->tmpl, FMTCHECK_SILENT); + if (ret != t->tmpl) { + failed_tests++; + pr_warn("'%s' and '%s' deemed compatible by fmtcheck()", + t->fmt, t->tmpl); + } + } +} + static int __init test_printf_init(void) { @@ -496,6 +535,8 @@ test_printf_init(void) test_string(); test_pointer(); + test_fmtcheck(); + kfree(alloced_buffer); if (failed_tests == 0) -- 2.11.0 From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Villemoes Date: Wed, 8 Nov 2017 23:30:20 +0100 Message-Id: <20171108223020.24487-7-linux@rasmusvillemoes.dk> In-Reply-To: <20171108223020.24487-1-linux@rasmusvillemoes.dk> References: <20171108223020.24487-1-linux@rasmusvillemoes.dk> Subject: [kernel-hardening] [RFC 6/6] lib/test_printf.c: add a few fmtcheck() test cases To: kernel-hardening@lists.openwall.com Cc: linux-kernel@vger.kernel.org, Andrew Morton , Kees Cook , Rasmus Villemoes List-ID: It should be trivial to add more test cases, once we figure out the exact rules for being compatible or not. Perhaps we'll have to extend the struct test with a flags element if we add flags that affect the return value. Signed-off-by: Rasmus Villemoes --- lib/test_printf.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/lib/test_printf.c b/lib/test_printf.c index 563f10e6876a..0c8490f3a9b3 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -483,6 +483,45 @@ test_pointer(void) flags(); } +static void __init +test_fmtcheck(void) +{ + struct test { const char *fmt; const char *tmpl; }; + static const struct test compatible[] __initconst = { + {"", ""}, + {"wlan%d", "%d"}, + {"aa%llxbb", "cc%Lxdd%%"}, + }; + static const struct test incompatible[] __initconst = { + {"a %d b %lx", "%d %x"}, + {"%llo", "%Lx"}, + }; + unsigned i; + const struct test *t; + const char *ret; + + for (i = 0; i < ARRAY_SIZE(compatible); ++i) { + total_tests++; + t = &compatible[i]; + ret = _fmtcheck(t->fmt, t->tmpl, FMTCHECK_SILENT); + if (ret != t->fmt) { + failed_tests++; + pr_warn("'%s' and '%s' deemed incompatible by fmtcheck()", + t->fmt, t->tmpl); + } + } + for (i = 0; i < ARRAY_SIZE(incompatible); ++i) { + total_tests++; + t = &incompatible[i]; + ret = _fmtcheck(t->fmt, t->tmpl, FMTCHECK_SILENT); + if (ret != t->tmpl) { + failed_tests++; + pr_warn("'%s' and '%s' deemed compatible by fmtcheck()", + t->fmt, t->tmpl); + } + } +} + static int __init test_printf_init(void) { @@ -496,6 +535,8 @@ test_printf_init(void) test_string(); test_pointer(); + test_fmtcheck(); + kfree(alloced_buffer); if (failed_tests == 0) -- 2.11.0