Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

test_sysctl: test against int proc_dointvec() array support

Add a few initial respective tests for an array:

o Echoing values separated by spaces works
o Echoing only first elements will set first elements
o Confirm PAGE_SIZE limit still applies even if an array is used

Link: http://lkml.kernel.org/r/20170630224431.17374-7-mcgrof@kernel.org
Signed-off-by: Luis R. Rodriguez <mcgrof@kernel.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Luis R. Rodriguez and committed by
Linus Torvalds
7c43a657 2920fad3

+102
+13
lib/test_sysctl.c
··· 42 42 struct test_sysctl_data { 43 43 int int_0001; 44 44 int int_0002; 45 + int int_0003[4]; 45 46 46 47 unsigned int uint_0001; 47 48 ··· 52 51 static struct test_sysctl_data test_data = { 53 52 .int_0001 = 60, 54 53 .int_0002 = 1, 54 + 55 + .int_0003[0] = 0, 56 + .int_0003[1] = 1, 57 + .int_0003[2] = 2, 58 + .int_0003[3] = 3, 55 59 56 60 .uint_0001 = 314, 57 61 ··· 78 72 .procname = "int_0002", 79 73 .data = &test_data.int_0002, 80 74 .maxlen = sizeof(int), 75 + .mode = 0644, 76 + .proc_handler = proc_dointvec, 77 + }, 78 + { 79 + .procname = "int_0003", 80 + .data = &test_data.int_0003, 81 + .maxlen = sizeof(test_data.int_0003), 81 82 .mode = 0644, 82 83 .proc_handler = proc_dointvec, 83 84 },
+89
tools/testing/selftests/sysctl/sysctl.sh
··· 33 33 ALL_TESTS="$ALL_TESTS 0002:1:1" 34 34 ALL_TESTS="$ALL_TESTS 0003:1:1" 35 35 ALL_TESTS="$ALL_TESTS 0004:1:1" 36 + ALL_TESTS="$ALL_TESTS 0005:3:1" 36 37 37 38 test_modprobe() 38 39 { ··· 109 108 echo "$0: You need getconf installed" 110 109 exit 1 111 110 fi 111 + if ! which diff 2> /dev/null > /dev/null; then 112 + echo "$0: You need diff installed" 113 + exit 1 114 + fi 112 115 } 113 116 114 117 function load_req_mod() ··· 170 165 return 1 171 166 fi 172 167 return 0 168 + } 169 + 170 + verify_diff_w() 171 + { 172 + echo "$TEST_STR" | diff -q -w -u - $1 173 + return $? 173 174 } 174 175 175 176 test_rc() ··· 363 352 test_rc 364 353 } 365 354 355 + # You used an int array 356 + run_limit_digit_int_array() 357 + { 358 + echo -n "Testing array works as expected ... " 359 + TEST_STR="4 3 2 1" 360 + echo -n $TEST_STR > $TARGET 361 + 362 + if ! verify_diff_w "${TARGET}"; then 363 + echo "FAIL" >&2 364 + rc=1 365 + else 366 + echo "ok" 367 + fi 368 + test_rc 369 + 370 + echo -n "Testing skipping trailing array elements works ... " 371 + # Do not reset_vals, carry on the values from the last test. 372 + # If we only echo in two digits the last two are left intact 373 + TEST_STR="100 101" 374 + echo -n $TEST_STR > $TARGET 375 + # After we echo in, to help diff we need to set on TEST_STR what 376 + # we expect the result to be. 377 + TEST_STR="100 101 2 1" 378 + 379 + if ! verify_diff_w "${TARGET}"; then 380 + echo "FAIL" >&2 381 + rc=1 382 + else 383 + echo "ok" 384 + fi 385 + test_rc 386 + 387 + echo -n "Testing PAGE_SIZE limit on array works ... " 388 + # Do not reset_vals, carry on the values from the last test. 389 + # Even if you use an int array, you are still restricted to 390 + # MAX_DIGITS, this is a known limitation. Test limit works. 391 + LIMIT=$((MAX_DIGITS -1)) 392 + TEST_STR="9" 393 + (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \ 394 + dd of="${TARGET}" 2>/dev/null 395 + 396 + TEST_STR="9 101 2 1" 397 + if ! verify_diff_w "${TARGET}"; then 398 + echo "FAIL" >&2 399 + rc=1 400 + else 401 + echo "ok" 402 + fi 403 + test_rc 404 + 405 + echo -n "Testing exceeding PAGE_SIZE limit fails as expected ... " 406 + # Do not reset_vals, carry on the values from the last test. 407 + # Now go over limit. 408 + LIMIT=$((MAX_DIGITS)) 409 + TEST_STR="7" 410 + (perl -e 'print " " x '$LIMIT';'; echo "${TEST_STR}") | \ 411 + dd of="${TARGET}" 2>/dev/null 412 + 413 + TEST_STR="7 101 2 1" 414 + if verify_diff_w "${TARGET}"; then 415 + echo "FAIL" >&2 416 + rc=1 417 + else 418 + echo "ok" 419 + fi 420 + test_rc 421 + } 422 + 366 423 # You are using an unsigned int 367 424 run_limit_digit_uint() 368 425 { ··· 591 512 run_limit_digit_uint 592 513 } 593 514 515 + sysctl_test_0005() 516 + { 517 + TARGET="${SYSCTL}/int_0003" 518 + reset_vals 519 + ORIG=$(cat "${TARGET}") 520 + 521 + run_limit_digit_int_array 522 + } 523 + 594 524 list_tests() 595 525 { 596 526 echo "Test ID list:" ··· 612 524 echo "0002 x $(get_test_count 0002) - tests proc_dostring()" 613 525 echo "0003 x $(get_test_count 0003) - tests proc_dointvec()" 614 526 echo "0004 x $(get_test_count 0004) - tests proc_douintvec()" 527 + echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array" 615 528 } 616 529 617 530 test_reqs