@@ -841,6 +841,8 @@ 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 ;
845847 size_t sz = 0 ;
846848 WCHAR h [MAX_PATH ];
@@ -946,8 +948,6 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
946948 if (ret == WS_SUCCESS ) {
947949 STARTUPINFOW si ;
948950 PCWSTR conCmd = L"wolfsshd.exe -r " ;
949- PWSTR conCmdPtr ;
950- size_t conCmdSz ;
951951
952952 SetHandleInformation (ptyIn , HANDLE_FLAG_INHERIT , 0 );
953953 SetHandleInformation (ptyOut , HANDLE_FLAG_INHERIT , 0 );
@@ -980,13 +980,18 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
980980 & si , & processInfo ) != TRUE) {
981981 wolfSSH_Log (WS_LOG_ERROR ,
982982 "[SSHD] Issue creating process, Windows error %d" , GetLastError ());
983- return WS_FATAL_ERROR ;
983+ ret = WS_FATAL_ERROR ;
984+ goto cleanup ;
984985 }
985986
987+ /* Release the parent's copies of the child's stdio handles now that
988+ * the child has inherited them. Holding cnslOut (write-end of stdout
989+ * pipe) open would prevent the OS from signalling EOF when the child
990+ * exits, making any future blocking ReadFile loop hang indefinitely. */
986991 CloseHandle (cnslIn );
992+ cnslIn = NULL ;
987993 CloseHandle (cnslOut );
988-
989- WFREE (conCmdPtr , NULL , DYNTYPE_SSHD );
994+ cnslOut = NULL ;
990995 }
991996
992997 if (ret == WS_SUCCESS ) {
@@ -1136,9 +1141,6 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
11361141 }
11371142 } while (1 );
11381143
1139- if (cmd != NULL ) {
1140- WFREE (cmd , NULL , DYNTYPE_SSHD );
1141- }
11421144 wolfSSH_Log (WS_LOG_INFO ,
11431145 "[SSHD] Closing down process for console" );
11441146
@@ -1152,10 +1154,32 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
11521154 "status" );
11531155 }
11541156
1157+ }
1158+
1159+ cleanup :
1160+ if (conCmdPtr != NULL ) {
1161+ WFREE (conCmdPtr , NULL , DYNTYPE_SSHD );
1162+ }
1163+ if (cnslIn != NULL ) {
1164+ CloseHandle (cnslIn );
1165+ }
1166+ if (cnslOut != NULL ) {
1167+ CloseHandle (cnslOut );
1168+ }
1169+ if (ptyIn != NULL ) {
1170+ CloseHandle (ptyIn );
1171+ }
1172+ if (ptyOut != NULL ) {
1173+ CloseHandle (ptyOut );
1174+ }
1175+ if (processInfo .hProcess != NULL ) {
11551176 CloseHandle (processInfo .hThread );
1177+ CloseHandle (processInfo .hProcess );
11561178 CloseHandle (wolfSSHD_GetAuthToken (conn -> auth ));
11571179 }
1158-
1180+ if (cmd != NULL ) {
1181+ WFREE (cmd , NULL , DYNTYPE_SSHD );
1182+ }
11591183 RevertToSelf ();
11601184 return ret ;
11611185}
0 commit comments