@@ -841,7 +841,10 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
841841#endif
842842 PWSTR cmd = NULL ;
843843 size_t cmdSz = 0 ;
844+ PWSTR conCmdPtr = NULL ;
845+ size_t conCmdSz = 0 ;
844846 PROCESS_INFORMATION processInfo ;
847+ int processCreated = 0 ;
845848 size_t sz = 0 ;
846849 WCHAR h [MAX_PATH ];
847850 char * forcedCmd ;
@@ -946,8 +949,6 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
946949 if (ret == WS_SUCCESS ) {
947950 STARTUPINFOW si ;
948951 PCWSTR conCmd = L"wolfsshd.exe -r " ;
949- PWSTR conCmdPtr ;
950- size_t conCmdSz ;
951952
952953 SetHandleInformation (ptyIn , HANDLE_FLAG_INHERIT , 0 );
953954 SetHandleInformation (ptyOut , HANDLE_FLAG_INHERIT , 0 );
@@ -980,13 +981,20 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
980981 & si , & processInfo ) != TRUE) {
981982 wolfSSH_Log (WS_LOG_ERROR ,
982983 "[SSHD] Issue creating process, Windows error %d" , GetLastError ());
983- return WS_FATAL_ERROR ;
984+ ret = WS_FATAL_ERROR ;
985+ goto cleanup ;
984986 }
985987
988+ processCreated = 1 ;
989+
990+ /* Release the parent's copies of the child's stdio handles now that
991+ * the child has inherited them. Holding cnslOut (write-end of stdout
992+ * pipe) open would prevent the OS from signalling EOF when the child
993+ * exits, making any future blocking ReadFile loop hang indefinitely. */
986994 CloseHandle (cnslIn );
995+ cnslIn = NULL ;
987996 CloseHandle (cnslOut );
988-
989- WFREE (conCmdPtr , NULL , DYNTYPE_SSHD );
997+ cnslOut = NULL ;
990998 }
991999
9921000 if (ret == WS_SUCCESS ) {
@@ -1136,9 +1144,6 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
11361144 }
11371145 } while (1 );
11381146
1139- if (cmd != NULL ) {
1140- WFREE (cmd , NULL , DYNTYPE_SSHD );
1141- }
11421147 wolfSSH_Log (WS_LOG_INFO ,
11431148 "[SSHD] Closing down process for console" );
11441149
@@ -1152,10 +1157,32 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
11521157 "status" );
11531158 }
11541159
1160+ }
1161+
1162+ cleanup :
1163+ if (conCmdPtr != NULL ) {
1164+ WFREE (conCmdPtr , NULL , DYNTYPE_SSHD );
1165+ }
1166+ if (cnslIn != NULL ) {
1167+ CloseHandle (cnslIn );
1168+ }
1169+ if (cnslOut != NULL ) {
1170+ CloseHandle (cnslOut );
1171+ }
1172+ if (ptyIn != NULL ) {
1173+ CloseHandle (ptyIn );
1174+ }
1175+ if (ptyOut != NULL ) {
1176+ CloseHandle (ptyOut );
1177+ }
1178+ if (processCreated ) {
11551179 CloseHandle (processInfo .hThread );
1180+ CloseHandle (processInfo .hProcess );
11561181 CloseHandle (wolfSSHD_GetAuthToken (conn -> auth ));
11571182 }
1158-
1183+ if (cmd != NULL ) {
1184+ WFREE (cmd , NULL , DYNTYPE_SSHD );
1185+ }
11591186 RevertToSelf ();
11601187 return ret ;
11611188}
0 commit comments