Skip to content

Commit 6e97752

Browse files
authored
Merge pull request #855 from ejohnstown/vvv
Out Of Order Messaging Checking
2 parents a473a05 + 2086f34 commit 6e97752

File tree

10 files changed

+813
-108
lines changed

10 files changed

+813
-108
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
bin_PROGRAMS =
33
noinst_HEADERS =
44
lib_LTLIBRARIES =
5+
noinst_LTLIBRARIES =
56
noinst_PROGRAMS =
67
nobase_include_HEADERS =
78
check_PROGRAMS =

apps/wolfssh/common.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ static byte* userPublicKey = userPublicKeyBuf;
4545
static const byte* userPublicKeyType = NULL;
4646
static byte userPassword[256];
4747
static const byte* userPrivateKeyType = NULL;
48+
static byte userPublicKeyAlloc = 0;
4849
static word32 userPublicKeySz = 0;
4950
static byte pubKeyLoaded = 0; /* was a public key loaded */
5051
static byte userPrivateKeyBuf[1191];
5152
static byte* userPrivateKey = userPrivateKeyBuf;
53+
static byte userPrivateKeyAlloc = 0;
5254
static word32 userPublicKeyTypeSz = 0;
5355
static word32 userPrivateKeySz = sizeof(userPrivateKeyBuf);
5456
static word32 userPrivateKeyTypeSz = 0;
@@ -670,6 +672,13 @@ int ClientUseCert(const char* certName)
670672
userPublicKeyType = publicKeyType;
671673
userPublicKeyTypeSz = (word32)WSTRLEN((const char*)publicKeyType);
672674
pubKeyLoaded = 1;
675+
userPublicKeyAlloc = 1;
676+
}
677+
else {
678+
userPublicKey = userPublicKeyBuf;
679+
userPublicKeySz = 0;
680+
userPublicKeyType = NULL;
681+
userPublicKeyAlloc = 0;
673682
}
674683
#else
675684
fprintf(stderr, "Certificate support not compiled in");
@@ -687,12 +696,22 @@ int ClientSetPrivateKey(const char* privKeyName)
687696
{
688697
int ret;
689698

699+
userPrivateKeyAlloc = 0;
690700
userPrivateKey = NULL; /* create new buffer based on parsed input */
691701
ret = wolfSSH_ReadKey_file(privKeyName,
692702
(byte**)&userPrivateKey, &userPrivateKeySz,
693703
(const byte**)&userPrivateKeyType, &userPrivateKeyTypeSz,
694704
&isPrivate, NULL);
695705

706+
if (ret == 0) {
707+
userPrivateKeyAlloc = 1;
708+
}
709+
else {
710+
userPrivateKey = userPrivateKeyBuf;
711+
userPrivateKeySz = sizeof(userPrivateKeyBuf);
712+
userPrivateKeyType = NULL;
713+
}
714+
696715
return ret;
697716
}
698717

@@ -703,6 +722,7 @@ int ClientUsePubKey(const char* pubKeyName)
703722
{
704723
int ret;
705724

725+
userPublicKeyAlloc = 0;
706726
userPublicKey = NULL; /* create new buffer based on parsed input */
707727
ret = wolfSSH_ReadKey_file(pubKeyName,
708728
&userPublicKey, &userPublicKeySz,
@@ -711,6 +731,11 @@ int ClientUsePubKey(const char* pubKeyName)
711731

712732
if (ret == 0) {
713733
pubKeyLoaded = 1;
734+
userPublicKeyAlloc = 1;
735+
}
736+
else {
737+
userPublicKey = userPublicKeyBuf;
738+
userPublicKeySz = 0;
714739
}
715740

716741
return ret;
@@ -747,11 +772,17 @@ int ClientLoadCA(WOLFSSH_CTX* ctx, const char* caCert)
747772

748773
void ClientFreeBuffers(void)
749774
{
750-
if (userPublicKey != userPublicKeyBuf) {
775+
if (userPublicKeyAlloc && userPublicKey != NULL) {
751776
WFREE(userPublicKey, NULL, DYNTYPE_PRIVKEY);
777+
userPublicKey = userPublicKeyBuf;
778+
userPublicKeySz = 0;
779+
userPublicKeyAlloc = 0;
752780
}
753781

754-
if (userPrivateKey != userPrivateKeyBuf) {
782+
if (userPrivateKeyAlloc && userPrivateKey != NULL) {
755783
WFREE(userPrivateKey, NULL, DYNTYPE_PRIVKEY);
784+
userPrivateKey = userPrivateKeyBuf;
785+
userPrivateKeySz = sizeof(userPrivateKeyBuf);
786+
userPrivateKeyAlloc = 0;
756787
}
757788
}

apps/wolfssh/wolfssh.c

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ typedef struct thread_args {
220220
wolfSSL_Mutex lock;
221221
byte rawMode;
222222
byte quit;
223+
int readError;
223224
} thread_args;
224225

225226
#ifdef _POSIX_THREADS
@@ -390,6 +391,7 @@ static THREAD_RET readPeer(void* in)
390391
int bufSz = sizeof(buf);
391392
thread_args* args = (thread_args*)in;
392393
int ret = 0;
394+
int stop = 0;
393395
int fd = wolfSSH_get_fd(args->ssh);
394396
word32 bytes;
395397
#ifdef USE_WINDOWS_API
@@ -398,11 +400,6 @@ static THREAD_RET readPeer(void* in)
398400
fd_set readSet;
399401
fd_set errSet;
400402

401-
FD_ZERO(&readSet);
402-
FD_ZERO(&errSet);
403-
FD_SET(fd, &readSet);
404-
FD_SET(fd, &errSet);
405-
406403
#ifdef USE_WINDOWS_API
407404
if (args->rawMode == 0) {
408405
DWORD wrd;
@@ -431,9 +428,13 @@ static THREAD_RET readPeer(void* in)
431428
#endif
432429

433430
while (ret >= 0) {
434-
#if defined(WOLFSSH_TERM) && defined(USE_WINDOWS_API)
431+
#if defined(WOLFSSH_TERM) && defined(USE_WINDOWS_API)
435432
(void)windowMonitor(args);
436-
#endif
433+
#endif
434+
FD_ZERO(&readSet);
435+
FD_ZERO(&errSet);
436+
FD_SET(fd, &readSet);
437+
FD_SET(fd, &errSet);
437438

438439
bytes = select(fd + 1, &readSet, NULL, &errSet, NULL);
439440
wc_LockMutex(&args->lock);
@@ -458,18 +459,18 @@ static THREAD_RET readPeer(void* in)
458459
} while (ret > 0);
459460
}
460461
else if (ret <= 0) {
461-
if (ret == WS_FATAL_ERROR) {
462-
ret = wolfSSH_get_error(args->ssh);
463-
if (ret == WS_WANT_READ) {
464-
continue;
465-
}
466-
#ifdef WOLFSSH_AGENT
467-
else if (ret == WS_CHAN_RXD) {
468-
byte agentBuf[512];
469-
int rxd, txd;
470-
word32 channel = 0;
462+
int err = (ret == WS_FATAL_ERROR) ?
463+
wolfSSH_get_error(args->ssh) : ret;
464+
if (err == WS_WANT_READ) {
465+
bytes = 0;
466+
}
467+
#ifdef WOLFSSH_AGENT
468+
else if (err == WS_CHAN_RXD) {
469+
byte agentBuf[512];
470+
int rxd, txd;
471+
word32 channel = 0;
471472

472-
wolfSSH_GetLastRxId(args->ssh, &channel);
473+
wolfSSH_GetLastRxId(args->ssh, &channel);
473474
rxd = wolfSSH_ChannelIdRead(args->ssh, channel,
474475
agentBuf, sizeof(agentBuf));
475476
if (rxd > 4) {
@@ -495,9 +496,17 @@ static THREAD_RET readPeer(void* in)
495496
WMEMSET(agentBuf, 0, sizeof(agentBuf));
496497
continue;
497498
}
498-
#endif /* WOLFSSH_AGENT */
499+
#endif /* WOLFSSH_AGENT */
500+
else if (err == WS_CBIO_ERR_CONN_CLOSE ||
501+
err == WS_SOCKET_ERROR_E ||
502+
err == WS_MSGID_NOT_ALLOWED_E) {
503+
args->readError = err;
504+
ret = err;
505+
stop = 1;
506+
bytes = 0;
499507
}
500-
else if (ret != WS_EOF) {
508+
else if (err != WS_EOF) {
509+
wc_UnLockMutex(&args->lock);
501510
err_sys("Stream read failed.");
502511
}
503512
}
@@ -517,12 +526,16 @@ static THREAD_RET readPeer(void* in)
517526
}
518527
#endif
519528
}
520-
ret = wolfSSH_stream_peek(args->ssh, buf, bufSz);
521-
if (ret <= 0) {
522-
bytes = 0; /* read it all */
529+
if (!stop) {
530+
ret = wolfSSH_stream_peek(args->ssh, buf, bufSz);
531+
if (ret <= 0) {
532+
bytes = 0; /* read it all */
533+
}
523534
}
524535
}
525536
wc_UnLockMutex(&args->lock);
537+
if (stop)
538+
break;
526539
}
527540
#if !defined(WOLFSSH_NO_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
528541
wc_ecc_fp_free(); /* free per thread cache */
@@ -791,7 +804,8 @@ static int config_parse_command_line(struct config* config,
791804
if (found != NULL) {
792805
*found = '\0';
793806
if (config->user) {
794-
free(config->user);
807+
WFREE(config->user, NULL, 0);
808+
config->user = NULL;
795809
}
796810
sz = WSTRLEN(cursor);
797811
config->user = (char*)WMALLOC(sz + 1, NULL, 0);
@@ -818,7 +832,7 @@ static int config_parse_command_line(struct config* config,
818832
strcpy(config->hostname, cursor);
819833
}
820834

821-
free(dest);
835+
WFREE(dest, NULL, 0);
822836
myoptind++;
823837
}
824838

@@ -874,18 +888,23 @@ static int config_cleanup(struct config* config)
874888
{
875889
if (config->user) {
876890
WFREE(config->user, NULL, 0);
891+
config->user = NULL;
877892
}
878893
if (config->hostname) {
879894
WFREE(config->hostname, NULL, 0);
895+
config->hostname = NULL;
880896
}
881897
if (config->keyFile) {
882898
WFREE(config->keyFile, NULL, 0);
899+
config->keyFile = NULL;
883900
}
884901
if (config->pubKeyFile) {
885902
WFREE(config->pubKeyFile, NULL, 0);
903+
config->pubKeyFile = NULL;
886904
}
887905
if (config->command) {
888906
WFREE(config->command, NULL, 0);
907+
config->command = NULL;
889908
}
890909

891910
return 0;
@@ -900,6 +919,7 @@ static THREAD_RETURN WOLFSSH_THREAD wolfSSH_Client(void* args)
900919
SOCKADDR_IN_T clientAddr;
901920
socklen_t clientAddrSz = sizeof(clientAddr);
902921
int ret = 0;
922+
int ioErr = 0;
903923
byte keepOpen = 1;
904924
#ifdef USE_WINDOWS_API
905925
byte rawMode = 0;
@@ -1037,6 +1057,7 @@ static THREAD_RETURN WOLFSSH_THREAD wolfSSH_Client(void* args)
10371057

10381058
wc_InitMutex(&arg.lock);
10391059
arg.ssh = ssh;
1060+
arg.readError = 0;
10401061
#ifdef WOLFSSH_TERM
10411062
arg.quit = 0;
10421063
#if (defined(__OSX__) || defined(__APPLE__))
@@ -1082,12 +1103,14 @@ static THREAD_RETURN WOLFSSH_THREAD wolfSSH_Client(void* args)
10821103
sem_destroy(&windowSem);
10831104
#endif
10841105
#endif /* WOLFSSH_TERM */
1106+
ioErr = arg.readError;
10851107
#elif defined(_MSC_VER)
10861108
thread_args arg;
10871109
HANDLE thread[2];
10881110

10891111
arg.ssh = ssh;
10901112
arg.rawMode = rawMode;
1113+
arg.readError = 0;
10911114
wc_InitMutex(&arg.lock);
10921115

10931116
if (config.command) {
@@ -1107,6 +1130,7 @@ static THREAD_RETURN WOLFSSH_THREAD wolfSSH_Client(void* args)
11071130
WaitForSingleObject(thread[1], INFINITE);
11081131
CloseHandle(thread[0]);
11091132
CloseHandle(thread[1]);
1133+
ioErr = arg.readError;
11101134
#else
11111135
err_sys("No threading to use");
11121136
#endif
@@ -1139,10 +1163,13 @@ static THREAD_RETURN WOLFSSH_THREAD wolfSSH_Client(void* args)
11391163
#if defined(WOLFSSH_TERM) || defined(WOLFSSH_SHELL)
11401164
((func_args*)args)->return_code = wolfSSH_GetExitStatus(ssh);
11411165
#endif
1166+
if (ioErr != 0 && ((func_args*)args)->return_code == 0) {
1167+
((func_args*)args)->return_code = 1;
1168+
}
11421169

11431170
wolfSSH_free(ssh);
11441171
wolfSSH_CTX_free(ctx);
1145-
if (ret != WS_SUCCESS && ret != WS_SOCKET_ERROR_E) {
1172+
if ((ret != WS_SUCCESS && ret != WS_SOCKET_ERROR_E) || ioErr != 0) {
11461173
WLOG(WS_LOG_DEBUG, "Closing client stream failed");
11471174
#if defined(WOLFSSH_TERM) || defined(WOLFSSH_SHELL)
11481175
/* override return value, do not want to return success if connection

src/include.am

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,42 @@ src_libwolfssh_la_SOURCES = src/ssh.c \
1111
src_libwolfssh_la_CPPFLAGS = -DBUILDING_WOLFSSH ${AM_CPPFLAGS}
1212
src_libwolfssh_la_LDFLAGS = -no-undefined -version-info ${WOLFSSH_LIBRARY_VERSION}
1313

14+
noinst_LTLIBRARIES += src/libwolfssh_test.la
15+
src_libwolfssh_test_la_SOURCES = $(src_libwolfssh_la_SOURCES)
16+
src_libwolfssh_test_la_CPPFLAGS = -DBUILDING_WOLFSSH -DWOLFSSH_TEST_INTERNAL ${AM_CPPFLAGS}
17+
src_libwolfssh_test_la_LDFLAGS = -no-undefined
18+
1419
if !BUILD_INLINE
1520
src_libwolfssh_la_SOURCES += src/misc.c
21+
src_libwolfssh_test_la_SOURCES += src/misc.c
1622
endif
1723

1824
if BUILD_KEYGEN
1925
src_libwolfssh_la_SOURCES += src/keygen.c
26+
src_libwolfssh_test_la_SOURCES += src/keygen.c
2027
endif
2128

2229
if BUILD_SCP
2330
src_libwolfssh_la_SOURCES += src/wolfscp.c
31+
src_libwolfssh_test_la_SOURCES += src/wolfscp.c
2432
endif
2533

2634
if BUILD_SFTP
2735
src_libwolfssh_la_SOURCES += src/wolfsftp.c
36+
src_libwolfssh_test_la_SOURCES += src/wolfsftp.c
2837
endif
2938

3039
if BUILD_TERM
3140
src_libwolfssh_la_SOURCES += src/wolfterm.c
41+
src_libwolfssh_test_la_SOURCES += src/wolfterm.c
3242
endif
3343

3444
if BUILD_AGENT
3545
src_libwolfssh_la_SOURCES += src/agent.c
46+
src_libwolfssh_test_la_SOURCES += src/agent.c
3647
endif
3748

3849
if BUILD_CERTS
3950
src_libwolfssh_la_SOURCES += src/certman.c
51+
src_libwolfssh_test_la_SOURCES += src/certman.c
4052
endif

0 commit comments

Comments
 (0)