Home | History | Annotate | Line # | Download | only in librumphijack
      1 #       $NetBSD: t_tcpip.sh,v 1.26 2025/04/25 22:51:29 riastradh Exp $
      2 #
      3 # Copyright (c) 2011 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions
      8 # are met:
      9 # 1. Redistributions of source code must retain the above copyright
     10 #    notice, this list of conditions and the following disclaimer.
     11 # 2. Redistributions in binary form must reproduce the above copyright
     12 #    notice, this list of conditions and the following disclaimer in the
     13 #    documentation and/or other materials provided with the distribution.
     14 #
     15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25 # POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 
     28 rumpnetsrv="rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet"
     29 export RUMP_SERVER=unix://csock
     30 
     31 atf_test_case http cleanup
     32 http_head()
     33 {
     34         atf_set "descr" "Start hijacked httpd and get webpage from it"
     35 }
     36 
     37 http_body()
     38 {
     39 
     40 	atf_check -s exit:0 ${rumpnetsrv} -lrumpnet_netinet6 ${RUMP_SERVER}
     41 
     42 	# start bozo in daemon mode
     43 	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
     44 	    /usr/libexec/httpd -P ./httpd.pid -b -s $(atf_get_srcdir)
     45 
     46 	atf_check -s exit:0 -o file:"$(atf_get_srcdir)/netstat.expout" \
     47 	    rump.netstat -a
     48 
     49 	# get the webpage
     50 	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so 	\
     51 	    $(atf_get_srcdir)/h_netget 127.0.0.1 80 webfile
     52 
     53 	# check that we got what we wanted
     54 	atf_check -o match:'HTTP/1.0 200 OK' cat webfile
     55 	atf_check -o match:'Content-Length: 95' cat webfile
     56 	blank_line_re="$(printf '^\r$')" # matches a line with only <CR><LF>
     57 	atf_check -o file:"$(atf_get_srcdir)/index.html" \
     58 	    sed -n "1,/${blank_line_re}/!p" webfile
     59 }
     60 
     61 http_cleanup()
     62 {
     63 	if [ -f httpd.pid ]; then
     64 		kill -9 "$(cat httpd.pid)"
     65 		rm -f httpd.pid
     66 	fi
     67 
     68 	rump.halt
     69 }
     70 
     71 #
     72 # Starts a SSH server and sets up the client to access it.
     73 # Authentication is allowed and done using an RSA key exclusively, which
     74 # is generated on the fly as part of the test case.
     75 # XXX: Ideally, all the tests in this test program should be able to share
     76 # the generated key, because creating it can be a very slow process on some
     77 # machines.
     78 #
     79 # XXX2: copypasted from jmmv's sshd thingamob in the psshfs test.
     80 # ideally code (and keys, like jmmv notes above) could be shared
     81 #
     82 start_sshd() {
     83 	echo "Setting up SSH server configuration"
     84 	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
     85 	    $(atf_get_srcdir)/sshd_config.in >sshd_config || \
     86 	    atf_fail "Failed to create sshd_config"
     87 	atf_check -s ignore -o empty -e ignore \
     88 	    cp $(atf_get_srcdir)/ssh_host_key .
     89 	atf_check -s ignore -o empty -e ignore \
     90 	    cp $(atf_get_srcdir)/ssh_host_key.pub .
     91 	atf_check -s exit:0 -o empty -e empty chmod 400 ssh_host_key
     92 	atf_check -s exit:0 -o empty -e empty chmod 444 ssh_host_key.pub
     93 
     94 # Start in debugging mode so we don't have parent<->child privsep stuff
     95         env LD_PRELOAD=/usr/lib/librumphijack.so \
     96 	    /usr/sbin/sshd -d -e -E "$(pwd)/out" -f ./sshd_config &
     97 #	while [ ! -f sshd.pid ]; do
     98 #		sleep 0.01
     99 #	done
    100 #	echo "SSH server started (pid $(cat sshd.pid))"
    101 	sleep 1
    102 
    103 	echo "Setting up SSH client configuration"
    104 	atf_check -s exit:0 -o empty -e empty \
    105 	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
    106 	atf_check -s exit:0 -o empty -e empty \
    107 	    cp ssh_user_key.pub authorized_keys
    108 	echo "127.0.0.1,localhost,::1 " \
    109 	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
    110 	    atf_fail "Failed to create known_hosts"
    111 	atf_check -s exit:0 -o empty -e empty chmod 600 authorized_keys
    112 	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
    113 	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
    114 	    atf_fail "Failed to create ssh_config"
    115 	
    116 	echo "sshd running"
    117 }
    118 
    119 atf_test_case ssh cleanup
    120 ssh_head()
    121 {
    122         atf_set "descr" "Test that hijacked ssh/sshd works"
    123 }
    124 
    125 ssh_body()
    126 {
    127 	atf_check -s exit:0 ${rumpnetsrv} ${RUMP_SERVER}
    128 	# make sure clients die after we nuke the server
    129 	export RUMPHIJACK_RETRYCONNECT='die'
    130 
    131 	start_sshd
    132 
    133 	# create some sort of directory for us to "ls"
    134 	mkdir testdir
    135 	cd testdir
    136 	jot 11 | xargs touch
    137 	jot 11 12 | xargs mkdir
    138 	cd ..
    139 
    140 	# From the PR (https://gnats.NetBSD.org/59278):
    141 	#
    142 	# > The LDAP problem has been fixed, but the new sshd-session
    143 	# > wants to exec sshd-auth with stdin/out the network socket so the
    144 	# > hijack code tries to dup(128, 0) and fails in:
    145 	# >
    146 	# >	if (fd_isrump(oldd)) {
    147 	# >		int (*op_close)(int) = GETSYSCALL(host, CLOSE);
    148 	# >
    149 	# >		/* only allow fd 0-2 for cross-kernel dup */
    150 	# >		if (!(newd >= 0 && newd <= 2 && !fd_isrump(newd))) {
    151 	# >			errno = EBADF; <-----
    152 	# >			return -1;
    153 	# >		}
    154 	# >
    155 	# > The server client portion of the test works without rump...
    156 	#
    157 	atf_expect_fail "PR bin/59278: failing since openssh 10.0 update"
    158 
    159 	# ignore stderr for now, prints environment in debug mode
    160 	atf_check -s exit:0 -o save:ssh.out -e ignore			\
    161 	    env LD_PRELOAD=/usr/lib/librumphijack.so			\
    162 	    ssh -T -F ssh_config 127.0.0.1 env BLOCKSIZE=512		\
    163 	    ls -li $(pwd)/testdir
    164 	atf_check -s exit:0 -o file:ssh.out env BLOCKSIZE=512 		\
    165 	    ls -li $(pwd)/testdir
    166 }
    167 
    168 ssh_cleanup()
    169 {
    170 	rump.halt
    171 	# sshd dies due to RUMPHIJACK_RETRYCONNECT=1d6
    172 }
    173 
    174 test_nfs()
    175 {
    176 
    177 	magicstr='wind in my hair'
    178 	# create ffs file system we'll be serving from
    179 	atf_check -s exit:0 -o ignore newfs -F -s 10000 ffs.img
    180 
    181 	# start nfs kernel server.  this is a mouthful
    182 	export RUMP_SERVER=unix://serversock
    183 	atf_check -s exit:0 rump_server $* ${RUMP_SERVER}
    184 
    185 	atf_check -s exit:0 rump.ifconfig shmif0 create
    186 	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
    187 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.1
    188 
    189 	export RUMPHIJACK_RETRYCONNECT=die
    190 	export LD_PRELOAD=/usr/lib/librumphijack.so
    191 
    192 	atf_check -s exit:0 mkdir -p /rump/var/run
    193 	atf_check -s exit:0 mkdir -p /rump/var/db
    194 	atf_check -s exit:0 touch /rump/var/db/mountdtab
    195 	atf_check -s exit:0 mkdir /rump/etc
    196 	atf_check -s exit:0 mkdir /rump/export
    197 
    198 	atf_check -s exit:0 -x \
    199 	    'echo "/export -noresvport -noresvmnt 10.1.1.100" | \
    200 		dd of=/rump/etc/exports 2> /dev/null'
    201 
    202 	atf_check -s exit:0 rump.sysctl -q -w kern.module.autoload=1
    203 
    204 	atf_check -s exit:0 -e ignore env RUMPHIJACK='path=/rump,blanket=/dk' \
    205 		mount_ffs /dk /rump/export
    206 	atf_check -s exit:0 -x "echo ${magicstr} > /rump/export/im_alive"
    207 
    208 	# start rpcbind.  we want /var/run/rpcbind.sock
    209 	export RUMPHIJACK='blanket=/var/run,socket=all' 
    210 	atf_check -s exit:0 rpcbind
    211 
    212 	# ok, then we want mountd in the similar fashion
    213 	export RUMPHIJACK='blanket=/var/run:/var/db:/export,socket=all,path=/rump,vfs=all'
    214 	atf_check -s exit:0 mountd /rump/etc/exports
    215 
    216 	# finally, le nfschuck
    217 	export RUMPHIJACK='blanket=/var/run,socket=all,vfs=all'
    218 	atf_check -s exit:0 nfsd
    219 
    220 	#
    221 	# now, time for the client server and associated madness.
    222 	#
    223 
    224 	export RUMP_SERVER=unix://clientsock
    225 	unset RUMPHIJACK
    226 	unset LD_PRELOAD
    227 
    228 	# at least the kernel server is easier
    229 	atf_check -s exit:0 rump_server -lrumpvfs -lrumpnet		\
    230 	    -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpfs_nfs\
    231 	    ${RUMP_SERVER}
    232 
    233 	atf_check -s exit:0 rump.ifconfig shmif0 create
    234 	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
    235 	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.100
    236 
    237 	export LD_PRELOAD=/usr/lib/librumphijack.so
    238 
    239 	atf_check -s exit:0 mkdir /rump/mnt
    240 	atf_check -s exit:0 mount_nfs 10.1.1.1:/export /rump/mnt
    241 
    242 	atf_check -s exit:0 -o inline:"${magicstr}\n" cat /rump/mnt/im_alive
    243 	atf_check -s exit:0 -o match:'.*im_alive$' ls -l /rump/mnt/im_alive
    244 }
    245 
    246 
    247 atf_test_case nfs cleanup
    248 nfs_head()
    249 {
    250         atf_set "descr" "Test hijacked nfsd and mount_nfs"
    251 
    252 	# XXX Can probably make this work as nonroot, but need to
    253 	# convince rpcbind running in the rump kernel server that it
    254 	# has uid 0.
    255 	atf_set "require.user" "root"
    256 }
    257 
    258 nfs_body()
    259 {
    260 	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
    261 	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif		\
    262 	    -lrumpdev_disk -lrumpfs_ffs -lrumpfs_nfs -lrumpfs_nfsserver	\
    263 	    -d key=/dk,hostpath=ffs.img,size=host
    264 }
    265 
    266 nfs_cleanup()
    267 {
    268 	RUMP_SERVER=unix://serversock rump.halt 2> /dev/null
    269 	RUMP_SERVER=unix://clientsock rump.halt 2> /dev/null
    270 	:
    271 }
    272 
    273 atf_test_case nfs_autoload cleanup
    274 nfs_autoload_head()
    275 {
    276         atf_set "descr" "Test hijacked nfsd with autoload from /stand"
    277 
    278 	# XXX Can probably make this work as nonroot, but need to
    279 	# convince rpcbind running in the rump kernel server that it
    280 	# has uid 0.
    281 	atf_set "require.user" "root"
    282 }
    283 
    284 nfs_autoload_body()
    285 {
    286 	[ `uname -m` = "i386" ] || atf_skip "test currently valid only on i386"
    287 	atf_expect_fail "PR lib/54184"
    288 	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
    289 	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif		\
    290 	    -lrumpdev_disk -d key=/dk,hostpath=ffs.img,size=host
    291 }
    292 
    293 nfs_autoload_cleanup()
    294 {
    295 	nfs_cleanup
    296 }
    297 
    298 atf_init_test_cases()
    299 {
    300 	atf_add_test_case http
    301 	atf_add_test_case ssh
    302 	atf_add_test_case nfs
    303 	atf_add_test_case nfs_autoload
    304 }
    305