Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/bin/sh PR bin/55979



details:   https://anonhg.NetBSD.org/src/rev/730ce1b6b6d4
branches:  trunk
changeset: 959502:730ce1b6b6d4
user:      kre <kre%NetBSD.org@localhost>
date:      Tue Feb 16 09:46:24 2021 +0000

description:
PR bin/55979

Add a sh ATF test to demonstrate a bug in the way that \0 characters
are dropped from scripts.   This test will eventually be extended to
test other potential sh script input related issues.

When initially committed, this test should fail.  It should succeed
when the fix for the PR is committed (soon).

Nb: this tests only the \0 related issues from the PR, the MSAN
detected uninitialised variable (struct field) can only be detected
by MSAN, as it has no visible impact on the operation of the shell
when running on any real (or even emulated) hardware.
(It will, however, also be fixed).

diffstat:

 distrib/sets/lists/tests/mi |    3 +-
 tests/bin/sh/Makefile       |    3 +-
 tests/bin/sh/t_input.sh     |  178 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 182 insertions(+), 2 deletions(-)

diffs (216 lines):

diff -r c9a0d3604bba -r 730ce1b6b6d4 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Tue Feb 16 07:27:12 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Tue Feb 16 09:46:24 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1018 2021/02/14 20:16:17 rillig Exp $
+# $NetBSD: mi,v 1.1019 2021/02/16 09:46:24 kre Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -1296,6 +1296,7 @@
 ./usr/tests/bin/sh/t_expand                            tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_fsplit                            tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_here                              tests-bin-tests         compattestfile,atf
+./usr/tests/bin/sh/t_input                             tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_option                            tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_patterns                          tests-bin-tests         compattestfile,atf
 ./usr/tests/bin/sh/t_redir                             tests-bin-tests         compattestfile,atf
diff -r c9a0d3604bba -r 730ce1b6b6d4 tests/bin/sh/Makefile
--- a/tests/bin/sh/Makefile     Tue Feb 16 07:27:12 2021 +0000
+++ b/tests/bin/sh/Makefile     Tue Feb 16 09:46:24 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.14 2018/12/05 02:45:06 kre Exp $
+# $NetBSD: Makefile,v 1.15 2021/02/16 09:46:24 kre Exp $
 #
 
 .include <bsd.own.mk>
@@ -15,6 +15,7 @@
 TESTS_SH+=     t_expand
 TESTS_SH+=     t_fsplit
 TESTS_SH+=     t_here
+TESTS_SH+=     t_input
 TESTS_SH+=     t_option
 TESTS_SH+=     t_patterns
 TESTS_SH+=     t_redir
diff -r c9a0d3604bba -r 730ce1b6b6d4 tests/bin/sh/t_input.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/bin/sh/t_input.sh   Tue Feb 16 09:46:24 2021 +0000
@@ -0,0 +1,178 @@
+# $NetBSD: t_input.sh,v 1.1 2021/02/16 09:46:24 kre Exp $
+#
+# Copyright (c) 2021 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# the implementation of "sh" to test
+: ${TEST_SH:="/bin/sh"}
+
+# This set of tests checks the low level shell (script) input
+# systrem (reading script files, nested files, fines read while
+# reading strings, ..) and correctly dropping nul bytes from file data
+
+# other shell input (the read builtin for example) is not covered here.
+
+atf_test_case nul_elimination
+
+nul_elimination_head() {
+       atf_set "descr" "verifies that \0 chars in input are properly ignored"
+}
+
+nul_elimination_body() {
+       atf_require_prog printf
+       atf_require_prog stat
+
+       # these really should always be present, but...
+       atf_require_prog dd
+       atf_require_prog cat
+       atf_require_prog rm
+
+       # please do not make even trivial changes (like correcting spelling)
+       # to the following script without taking care to fix the following
+       # tests, even minor changes can defeat the purpose of the test
+       cat > helper.sh <<- EOF
+               # a line of commentary that does nothing
+               # another line of comments (these just make the script bigger)
+               printf A
+               eval "printf B"
+               if printf C
+               then
+                       printf D
+               fi
+               for x in E F G H
+               do
+                       printf "\$x"
+               done
+               printf \\
+                       I
+               printf '\n'
+               exit 0
+       EOF
+
+       # this first test simply verifies that the script works as
+       # expected, should it fail, it is not a problem with \0 chars
+       atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
+               ${TEST_SH} helper.sh
+
+       size=$(stat -f %z helper.sh)
+
+       # Now insert \0 chars at specifically chosen spots in script
+       for loc in 0 10 40 41 42 104 105 106 110 111 112 113 119 127 128 \
+               144 150 162 165 168 187 202 203 204 205 214 215 216 224 225
+       do
+               # at each location try varying the number of \0's inserted
+               for N in 1 2 4 7
+               do
+                       OF="helper-${N}@${loc}.sh"
+
+                       test "${loc}" -gt 0 && 
+                               dd if=helper.sh bs=1 count="${loc}" \
+                                  of="${OF}" >/dev/null 2>&1
+                       dd if=helper.sh bs=1 skip="${loc}" \
+                               oseek="$(( loc + N ))" \
+                               of="${OF}" >/dev/null 2>&1
+
+                       test "$(stat -f %z "${OF}")" -eq  "$(( size + N ))" ||
+                                       atf_fail "${N}@${loc}: build failure"
+
+                       atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
+                               ${TEST_SH} "${OF}"
+
+                       rm -f "${OF}"
+               done
+                       
+       done
+
+       # Next insert \0 chars at multiple chosen locations
+       # nb: offsets in each arg must be non-decreasing
+       for locs in '0 225' '0 224' '0 127 223' '0 10 15' '0 48 55' \
+               '0 0 0 225 225 225' '0 0 127 128 224 224 225' \
+               '104 106' '105 110' '113 119' '113 119 122' '113 127' \
+               '129 130 131 132 133 136 139 140'  '184 185 186 187' \
+               '202 203 203 204 204 205 205 206 207'
+       do
+               set -- $locs
+               gaps=$#
+
+               IFS=-
+               OF="helper-${*}.sh"
+               unset IFS
+
+               loc=$1; shift
+               test "${loc}" -gt 0 && 
+                       dd if=helper.sh bs=1 count="${loc}" \
+                          of="${OF}" >/dev/null 2>&1 
+               G=1
+               for N
+               do
+                       count=$(( N - loc ))
+                       if [ "${count}" -eq 0 ]
+                       then
+                               printf '\0' >> "${OF}"
+                       else
+                               dd if=helper.sh bs=1 skip="${loc}" \
+                                   oseek="$(( loc + G ))" count="${count}" \
+                                   of="${OF}" >/dev/null 2>&1
+                       fi
+                       loc=$N
+                       G=$(( G + 1 ))
+               done
+
+               if [ "${loc}" -lt "${size}" ]
+               then
+                       dd if=helper.sh bs=1 skip="${loc}" \
+                           oseek="$(( loc + G ))" of="${OF}" >/dev/null 2>&1
+               else
+                       printf '\0' >> "${OF}"
+               fi
+
+               test "$(stat -f %z "${OF}")" -eq  "$(( size + gaps ))" ||
+                               atf_fail "${locs}: build failure"
+
+               atf_check -s exit:0 -o 'inline:ABCDEFGHI\n' -e empty \
+                       ${TEST_SH} "${OF}"
+
+               rm -f "${OF}"
+       done
+
+       # Now just insert \0's in random locations in the script,
+       # hoping that if the somewhat carefully selected insertions
+       # above fail to trigger a bug, then if any (bugs) remain,
+       # eventually Murphy will prevail, and one of these tests will catch it.
+
+       test "${RANDOM}" = "${RANDOM}" &&
+               atf_skip 'ATF shell does not support $RANDOM'
+
+       # To be added later...
+
+       return 0
+}
+
+atf_init_test_cases() {
+       atf_add_test_case nul_elimination
+       # atf_add_test_case file_recursion
+       # atf_add_test_case file_string_recursion
+       # atf_add_test_case file_recursion_nul
+       # atf_add_test_case file_string_recursion_nul
+}



Home | Main Index | Thread Index | Old Index