-
Notifications
You must be signed in to change notification settings - Fork 114
wolfsshd: fail closed when a per-connection privilege drop fails #1067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yosuke-wolfssl
wants to merge
1
commit into
wolfSSL:master
Choose a base branch
from
yosuke-wolfssl:fix/f_5850
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Regression test for the fail-closed privilege-drop paths in wolfsshd. | ||
| # | ||
| # When the per-connection drop to the authenticated user's uid/gid fails, the | ||
| # subsystem handlers must terminate the connection process. Previously they | ||
| # attempted a fallback that is a no-op under "UsePrivilegeSeparation no" and | ||
| # then returned, leaving HandleConnection to run wolfSSH_shutdown and up to ten | ||
| # wolfSSH_worker iterations while still at the daemon's elevated privilege | ||
| # level. | ||
| # | ||
| # The drop is forced to fail with the wolfsshd_faultinj build, which honors | ||
| # WOLFSSHD_FAULT_PRIVDROP. Every subsystem that drops privileges is driven: | ||
| # exec/shell (SHELL_Subsystem), sftp (SFTP_Subsystem) and scp (SCP_Subsystem). | ||
|
|
||
| if [ -z "$1" ] || [ -z "$2" ]; then | ||
| echo "expecting host and port as arguments" | ||
| echo "./sshd_privdrop_fail_test.sh 127.0.0.1 22222" | ||
| exit 1 | ||
| fi | ||
|
|
||
| PWD=`pwd` | ||
| USER=`whoami` | ||
| TEST_HOST="$1" | ||
| TEST_PORT="$2" | ||
|
|
||
| FAULT_SSHD="./wolfsshd_faultinj" | ||
| if [ ! -x "$FAULT_SSHD" ]; then | ||
| echo "SKIP: $FAULT_SSHD not built" | ||
| exit 77 | ||
| fi | ||
|
|
||
| if [ -f ./log.txt ]; then | ||
| sudo rm -rf log.txt | ||
| fi | ||
| touch log.txt | ||
|
|
||
| TEST_CLIENT="../../../examples/client/client" | ||
| SFTP_CLIENT="../../../examples/sftpclient/wolfsftp" | ||
| SCP_CLIENT="../../../examples/scpclient/wolfscp" | ||
| PRIVATE_KEY="../../../keys/hansel-key-ecc.der" | ||
| PUBLIC_KEY="../../../keys/hansel-key-ecc.pub" | ||
|
|
||
| # Small payload for the sftp/scp transfers. The connection dies at the failed | ||
| # drop long before any data moves, so the contents do not matter. | ||
| PAYLOAD="privdrop_payload.txt" | ||
| echo "privdrop" > "$PAYLOAD" | ||
|
|
||
| source ./start_sshd.sh | ||
|
|
||
| cat <<EOF > sshd_config_test_privdrop | ||
| Port $TEST_PORT | ||
| Protocol 2 | ||
| LoginGraceTime 600 | ||
| PermitRootLogin yes | ||
| PasswordAuthentication yes | ||
| PermitEmptyPasswords no | ||
| UsePrivilegeSeparation no | ||
| UseDNS no | ||
| HostKey $PWD/../../../keys/server-key.pem | ||
| AuthorizedKeysFile $PWD/authorized_keys_test | ||
| EOF | ||
|
|
||
| # Run the fault-injecting daemon so the drop to the authenticated user fails. | ||
| # "UsePrivilegeSeparation no" is the worst case: the old fallback did nothing | ||
| # there, so the handler would have carried on as root. | ||
| SSHD_BIN="$FAULT_SSHD" | ||
| SSHD_ENV="WOLFSSHD_FAULT_PRIVDROP=1" | ||
| export SSHD_BIN SSHD_ENV | ||
|
|
||
| start_wolfsshd "sshd_config_test_privdrop" | ||
|
|
||
| # Stop the daemon and drop the generated files on every exit path, including | ||
| # the failure exits below. log.txt is left in place for debugging, matching the | ||
| # other tests in this directory. | ||
| cleanup() { | ||
| stop_wolfsshd | ||
| # stop_wolfsshd kills the pid it captured at startup, which can be a | ||
| # wrapper rather than the daemon that outlived it. The fault binary is | ||
| # unique to this test, so clearing it by name cannot hit another daemon. | ||
| sudo pkill -f wolfsshd_faultinj > /dev/null 2>&1 | ||
| rm -f sshd_config_test_privdrop "$PAYLOAD" | ||
| return 0 | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| DEADLINE=30 | ||
|
|
||
| # Drives one client and checks the daemon's behavior after the drop fails. The | ||
| # client is expected to lose its connection, so its exit status is not the | ||
| # assertion and it is killed if it outlives the deadline. | ||
| check_subsystem() { | ||
| LABEL="$1" | ||
| shift | ||
|
|
||
| BEFORE=`grep -c "Error setting user ID" log.txt` | ||
|
|
||
| "$@" > /dev/null 2>&1 & | ||
| CLIENT_PID=$! | ||
|
|
||
| # Wait, bounded, for the drop error to be logged and the connection process | ||
| # to exit, leaving only the listening daemon. A process that has exited | ||
| # cannot log anything more, so the absence check below is not a race. | ||
| WAITED=0 | ||
| while [ "$WAITED" -lt "$DEADLINE" ]; do | ||
| AFTER=`grep -c "Error setting user ID" log.txt` | ||
| LIVE=`pgrep -f wolfsshd_faultinj | wc -l | tr -d ' '` | ||
| if [ "$AFTER" -gt "$BEFORE" ] && [ "$LIVE" -le 1 ]; then | ||
| break | ||
| fi | ||
| sleep 1 | ||
| WAITED=`expr $WAITED + 1` | ||
| done | ||
|
|
||
| kill $CLIENT_PID > /dev/null 2>&1 | ||
| wait $CLIENT_PID > /dev/null 2>&1 | ||
|
|
||
| # The drop must actually have failed, otherwise the rest proves nothing. | ||
| AFTER=`grep -c "Error setting user ID" log.txt` | ||
| if [ "$AFTER" -le "$BEFORE" ]; then | ||
| echo "FAIL: $LABEL never reached the privilege drop" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # A handler still alive here never terminated, which is the failure this | ||
| # test exists to catch. | ||
| if [ "$WAITED" -ge "$DEADLINE" ]; then | ||
| echo "FAIL: $LABEL connection process still running after ${DEADLINE}s" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # The fail-closed assertion. exit(1) means the connection process is gone, | ||
| # so HandleConnection can never log this. If it appears, the handler kept | ||
| # running after the failed drop at an un-dropped privilege level. | ||
| if grep -q "Attempting to close down connection" log.txt; then | ||
| echo "FAIL: $LABEL handler continued after a failed privilege drop" | ||
| exit 1 | ||
| fi | ||
|
|
||
| printf " %s: connection process terminated on privilege-drop failure\n" \ | ||
| "$LABEL" | ||
| } | ||
|
|
||
| # SHELL_Subsystem, via an exec session. | ||
| check_subsystem "exec" \ | ||
| "$TEST_CLIENT" -c 'echo privdrop' -u "$USER" -i "$PRIVATE_KEY" \ | ||
| -j "$PUBLIC_KEY" -h "$TEST_HOST" -p "$TEST_PORT" | ||
|
|
||
| # SFTP_Subsystem. -g is a one-shot put, so the client cannot sit at a prompt. | ||
| check_subsystem "sftp" \ | ||
| "$SFTP_CLIENT" -u "$USER" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" \ | ||
| -g -l "$PAYLOAD" -r "/tmp/privdrop_remote_$$.txt" \ | ||
| -h "$TEST_HOST" -p "$TEST_PORT" | ||
|
|
||
| # SCP_Subsystem. | ||
| check_subsystem "scp" \ | ||
| "$SCP_CLIENT" -u "$USER" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" \ | ||
| -S"$PWD/$PAYLOAD:." -H "$TEST_HOST" -p "$TEST_PORT" | ||
|
|
||
| echo "PASS: all subsystems terminate on privilege-drop failure" | ||
| exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any way we can do this test without adding fault injections into the src?