Skip to content

Commit 863714a

Browse files
authored
Merge pull request #670 from ejohnstown/msgid-filter
Messaging Filtering
2 parents 902cff3 + 039aea5 commit 863714a

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

src/internal.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ const char* GetErrorString(int err)
445445
case WS_SFTP_NOT_FILE_E:
446446
return "not a regular file";
447447

448+
case WS_MSGID_NOT_ALLOWED_E:
449+
return "message not allowed before user authentication";
450+
448451
default:
449452
return "Unknown error code";
450453
}
@@ -557,6 +560,84 @@ static void HandshakeInfoFree(HandshakeInfo* hs, void* heap)
557560
}
558561

559562

563+
#ifndef NO_WOLFSSH_SERVER
564+
INLINE static int IsMessageAllowedServer(WOLFSSH *ssh, byte msg)
565+
{
566+
/* Has client userauth started? */
567+
if (ssh->acceptState < ACCEPT_KEYED) {
568+
if (msg > MSGID_KEXDH_LIMIT) {
569+
return 0;
570+
}
571+
}
572+
/* Is server userauth complete? */
573+
if (ssh->acceptState < ACCEPT_SERVER_USERAUTH_SENT) {
574+
/* Explicitly check for messages not allowed before user
575+
* authentication has comleted. */
576+
if (msg >= MSGID_USERAUTH_LIMIT) {
577+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by server "
578+
"before user authentication is complete", msg);
579+
return 0;
580+
}
581+
/* Explicitly check for the user authentication messages that
582+
* only the server sends, it shouldn't receive them. */
583+
if (msg > MSGID_USERAUTH_RESTRICT) {
584+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by server "
585+
"during user authentication", msg);
586+
return 0;
587+
}
588+
}
589+
return 1;
590+
}
591+
#endif /* NO_WOLFSSH_SERVER */
592+
593+
594+
#ifndef NO_WOLFSSH_CLIENT
595+
INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
596+
{
597+
/* Has client userauth started? */
598+
if (ssh->connectState < CONNECT_CLIENT_KEXDH_INIT_SENT) {
599+
if (msg >= MSGID_KEXDH_LIMIT) {
600+
return 0;
601+
}
602+
}
603+
/* Is client userauth complete? */
604+
if (ssh->connectState < CONNECT_SERVER_USERAUTH_ACCEPT_DONE) {
605+
/* Explicitly check for messages not allowed before user
606+
* authentication has comleted. */
607+
if (msg >= MSGID_USERAUTH_LIMIT) {
608+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by client "
609+
"before user authentication is complete", msg);
610+
return 0;
611+
}
612+
/* Explicitly check for the user authentication message that
613+
* only the client sends, it shouldn't receive it. */
614+
if (msg == MSGID_USERAUTH_RESTRICT) {
615+
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by client "
616+
"during user authentication", msg);
617+
return 0;
618+
}
619+
}
620+
return 1;
621+
}
622+
#endif /* NO_WOLFSSH_CLIENT */
623+
624+
625+
INLINE static int IsMessageAllowed(WOLFSSH *ssh, byte msg)
626+
{
627+
#ifndef NO_WOLFSSH_SERVER
628+
if (ssh->ctx->side == WOLFSSH_ENDPOINT_SERVER) {
629+
return IsMessageAllowedServer(ssh, msg);
630+
}
631+
#endif /* NO_WOLFSSH_SERVER */
632+
#ifndef NO_WOLFSSH_CLIENT
633+
if (ssh->ctx->side == WOLFSSH_ENDPOINT_CLIENT) {
634+
return IsMessageAllowedClient(ssh, msg);
635+
}
636+
#endif /* NO_WOLFSSH_CLIENT */
637+
return 0;
638+
}
639+
640+
560641
#ifdef DEBUG_WOLFSSH
561642

562643
static const char cannedBanner[] =
@@ -8019,6 +8100,10 @@ static int DoPacket(WOLFSSH* ssh, byte* bufferConsumed)
80198100
return WS_OVERFLOW_E;
80208101
}
80218102

8103+
if (!IsMessageAllowed(ssh, msg)) {
8104+
return WS_MSGID_NOT_ALLOWED_E;
8105+
}
8106+
80228107
switch (msg) {
80238108

80248109
case MSGID_DISCONNECT:

wolfssh/error.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ enum WS_ErrorCodes {
132132
WS_KEY_CHECK_VAL_E = -1091, /* OpenSSH key check value fail */
133133
WS_KEY_FORMAT_E = -1092, /* OpenSSH key format fail */
134134
WS_SFTP_NOT_FILE_E = -1093, /* Not a regular file */
135+
WS_MSGID_NOT_ALLOWED_E = -1094, /* Message not allowed before userauth */
135136

136-
WS_LAST_E = -1093 /* Update this to indicate last error */
137+
WS_LAST_E = -1094 /* Update this to indicate last error */
137138
};
138139

139140

wolfssh/internal.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,20 @@ enum WS_MessageIds {
11231123
};
11241124

11251125

1126+
#define MSGID_KEXDH_LIMIT 30
1127+
1128+
/* The endpoints should not allow message IDs greater than or
1129+
* equal to msgid 80 before user authentication is complete.
1130+
* Per RFC 4252 section 6. */
1131+
#define MSGID_USERAUTH_LIMIT 80
1132+
1133+
/* The client should only send the user auth request message
1134+
* (50), it should not accept it. The server should only receive
1135+
* the user auth request message, it should not accept the other
1136+
* user auth messages, it sends them. (>50) */
1137+
#define MSGID_USERAUTH_RESTRICT 50
1138+
1139+
11261140
#define CHANNEL_EXTENDED_DATA_STDERR WOLFSSH_EXT_DATA_STDERR
11271141

11281142

0 commit comments

Comments
 (0)