diff --git a/tests/common b/tests/common index f428721..a210971 100644 --- a/tests/common +++ b/tests/common @@ -628,6 +628,19 @@ function get_filemode() fi } +# Get the file owner uid and gid +# +# @1: filename +function get_fileowner() +{ + if [[ "$(uname -s)" =~ (Linux|CYGWIN_NT-) ]]; then + stat -c"%u %g" $1 + else + # BSDs + stat -f"%u %g" $1 + fi +} + # Get the SHA1 of a file # # @1: filename diff --git a/tests/test_ctrlchannel b/tests/test_ctrlchannel index 01a92fe..d5dbf57 100755 --- a/tests/test_ctrlchannel +++ b/tests/test_ctrlchannel @@ -31,6 +31,12 @@ source ${TESTDIR}/common # Test 1: test the control channel on the socket tpm +# OS X would not allow nobody to access the $TPMDIR easily; skip it +if [ $(id -u) -eq 0 ] && [ "$(uname -s)" != "Darwin" ]; then + FOWNER=",uid=nobody,gid=$(id -Gn nobody | cut -d" " -f1)" + FILEOWNER="$(id -u nobody) $(id -G nobody | cut -d" " -f1)" +fi + if [[ "$(uname -s)" =~ CYGWIN_NT- ]]; then FILEMODE=661 else @@ -56,7 +62,7 @@ $SWTPM_EXE socket \ --fd 100 \ --tpmstate dir=$TPMDIR \ --pid file=$PID_FILE \ - --ctrl type=unixio,path=$SWTPM_CTRL_UNIX_PATH,mode=$FILEMODE \ + --ctrl type=unixio,path=$SWTPM_CTRL_UNIX_PATH,mode=${FILEMODE}${FOWNER} \ --log file=$LOG_FILE,level=20 & exec 100>&- @@ -90,6 +96,14 @@ if [ "$filemode" != "$FILEMODE" ]; then exit 1 fi +fileowner=$(get_fileowner $SWTPM_CTRL_UNIX_PATH) +if [ -n "$FILEOWNER" ] && [ "$fileowner" != "$FILEOWNER" ]; then + echo "File ownership is wrong" + echo "Expected: $FILEOWNER" + echo "Actual : $fileowner" + exit 1 +fi + # Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags res="$(swtpm_ctrl_tx ${SWTPM_INTERFACE} '\x00\x00\x00\x02\x00\x00\x00\x00')" exp=" 00 00 00 00" diff --git a/tests/test_tpm2_ctrlchannel2 b/tests/test_tpm2_ctrlchannel2 index ceae4dd..f502210 100755 --- a/tests/test_tpm2_ctrlchannel2 +++ b/tests/test_tpm2_ctrlchannel2 @@ -30,6 +30,10 @@ function cleanup() } # Test 1: test the control channel on the chardev tpm +if [ $(id -u) -eq 0 ]; then + FOWNER=",uid=$(id -u nobody),gid=$(id -G nobody | cut -d" " -f1)" + FILEOWNER="$(id -u nobody) $(id -G nobody | cut -d" " -f1)" +fi FILEMODE=621 # use a pseudo terminal @@ -38,7 +42,7 @@ $SWTPM_EXE chardev \ --fd 100 \ --tpmstate dir=$TPMDIR \ --pid file=$PID_FILE \ - --ctrl type=unixio,path=$SOCK_PATH,mode=$FILEMODE \ + --ctrl type=unixio,path=$SOCK_PATH,mode=${FILEMODE}${FOWNER} \ --tpm2 & if wait_for_file $PID_FILE 3; then @@ -63,6 +67,14 @@ if [ "$filemode" != "$FILEMODE" ]; then exit 1 fi +fileowner=$(get_fileowner $SOCK_PATH) +if [ -n "$FILEOWNER" ] && [ "$fileowner" != "$FILEOWNER" ]; then + echo "File ownership is wrong" + echo "Expected: $FILEOWNER" + echo "Actual : $fileowner" + exit 1 +fi + exp="ptm capability is 0x([[:xdigit:]]+)" if ! [[ "$act" =~ ^${exp}$ ]]; then echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."