From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on archive.lwn.net X-Spam-Level: X-Spam-Status: No, score=-4.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,T_RP_MATCHES_RCVD autolearn=ham autolearn_force=no version=3.4.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by archive.lwn.net (Postfix) with ESMTP id 0FDC47E6C6 for ; Fri, 16 Mar 2018 18:16:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752415AbeCPSQm (ORCPT ); Fri, 16 Mar 2018 14:16:42 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33024 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752959AbeCPSOf (ORCPT ); Fri, 16 Mar 2018 14:14:35 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9C1244068042; Fri, 16 Mar 2018 18:14:34 +0000 (UTC) Received: from llong.com (dhcp-17-75.bos.redhat.com [10.18.17.75]) by smtp.corp.redhat.com (Postfix) with ESMTP id 546602017DF1; Fri, 16 Mar 2018 18:14:34 +0000 (UTC) From: Waiman Long To: "Luis R. Rodriguez" , Kees Cook Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, Jonathan Corbet , Andrew Morton , Al Viro , Matthew Wilcox , "Eric W. Biederman" , Waiman Long Subject: [PATCH v5 6/9] test_sysctl: Add range clamping test Date: Fri, 16 Mar 2018 14:13:47 -0400 Message-Id: <1521224030-2185-7-git-send-email-longman@redhat.com> In-Reply-To: <1521224030-2185-1-git-send-email-longman@redhat.com> References: <1521224030-2185-1-git-send-email-longman@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 16 Mar 2018 18:14:34 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 16 Mar 2018 18:14:34 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'longman@redhat.com' RCPT:'' Sender: linux-doc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-doc@vger.kernel.org Add a range clamping test to verify that the input value will be clamped if it exceeds the builtin maximum or minimum value. Below is the expected test run result: Running test: sysctl_test_0006 - run #0 Checking range minimum clamping ... ok Checking range maximum clamping ... ok Checking range minimum clamping ... ok Checking range maximum clamping ... ok Signed-off-by: Waiman Long --- lib/test_sysctl.c | 29 ++++++++++++++++++ tools/testing/selftests/sysctl/sysctl.sh | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/lib/test_sysctl.c b/lib/test_sysctl.c index 3dd801c..7bb4cf7 100644 --- a/lib/test_sysctl.c +++ b/lib/test_sysctl.c @@ -38,12 +38,18 @@ static int i_zero; static int i_one_hundred = 100; +static int signed_min = -10; +static int signed_max = 10; +static unsigned int unsigned_min = 10; +static unsigned int unsigned_max = 30; struct test_sysctl_data { int int_0001; int int_0002; int int_0003[4]; + int range_0001; + unsigned int urange_0001; unsigned int uint_0001; char string_0001[65]; @@ -58,6 +64,9 @@ struct test_sysctl_data { .int_0003[2] = 2, .int_0003[3] = 3, + .range_0001 = 0, + .urange_0001 = 20, + .uint_0001 = 314, .string_0001 = "(none)", @@ -102,6 +111,26 @@ struct test_sysctl_data { .mode = 0644, .proc_handler = proc_dostring, }, + { + .procname = "range_0001", + .data = &test_data.range_0001, + .maxlen = sizeof(test_data.range_0001), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .flags = CTL_FLAGS_CLAMP_RANGE_SIGNED, + .extra1 = &signed_min, + .extra2 = &signed_max, + }, + { + .procname = "urange_0001", + .data = &test_data.urange_0001, + .maxlen = sizeof(test_data.urange_0001), + .mode = 0644, + .proc_handler = proc_douintvec_minmax, + .flags = CTL_FLAGS_CLAMP_RANGE_UNSIGNED, + .extra1 = &unsigned_min, + .extra2 = &unsigned_max, + }, { } }; diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh index ec232c3..1aa1bba 100755 --- a/tools/testing/selftests/sysctl/sysctl.sh +++ b/tools/testing/selftests/sysctl/sysctl.sh @@ -34,6 +34,7 @@ ALL_TESTS="$ALL_TESTS 0002:1:1" ALL_TESTS="$ALL_TESTS 0003:1:1" ALL_TESTS="$ALL_TESTS 0004:1:1" ALL_TESTS="$ALL_TESTS 0005:3:1" +ALL_TESTS="$ALL_TESTS 0006:1:1" test_modprobe() { @@ -543,6 +544,38 @@ run_stringtests() test_rc } +# TARGET, RANGE_MIN & RANGE_MAX need to be defined before running test. +run_range_clamping_test() +{ + rc=0 + + echo -n "Checking range minimum clamping ... " + VAL=$((RANGE_MIN - 1)) + echo -n $VAL > "${TARGET}" 2> /dev/null + EXITVAL=$? + NEWVAL=$(cat "${TARGET}") + if [[ $EXITVAL -ne 0 || $NEWVAL -ne $RANGE_MIN ]]; then + echo "FAIL" >&2 + rc=1 + else + echo "ok" + fi + + echo -n "Checking range maximum clamping ... " + VAL=$((RANGE_MAX + 1)) + echo -n $VAL > "${TARGET}" 2> /dev/null + EXITVAL=$? + NEWVAL=$(cat "${TARGET}") + if [[ $EXITVAL -ne 0 || $NEWVAL -ne $RANGE_MAX ]]; then + echo "FAIL" >&2 + rc=1 + else + echo "ok" + fi + + test_rc +} + sysctl_test_0001() { TARGET="${SYSCTL}/int_0001" @@ -600,6 +633,25 @@ sysctl_test_0005() run_limit_digit_int_array } +sysctl_test_0006() +{ + TARGET="${SYSCTL}/range_0001" + ORIG=$(cat "${TARGET}") + RANGE_MIN=-10 + RANGE_MAX=10 + + run_range_clamping_test + set_orig + + TARGET="${SYSCTL}/urange_0001" + ORIG=$(cat "${TARGET}") + RANGE_MIN=10 + RANGE_MAX=30 + + run_range_clamping_test + set_orig +} + list_tests() { echo "Test ID list:" -- 1.8.3.1 -- To unsubscribe from this list: send the line "unsubscribe linux-doc" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html