Skip to content

Commit

Permalink
Merge pull request #670 from ejohnstown/msgid-filter
Browse files Browse the repository at this point in the history
Messaging Filtering
  • Loading branch information
dgarske authored Mar 20, 2024
2 parents 902cff3 + 039aea5 commit 863714a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
85 changes: 85 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ const char* GetErrorString(int err)
case WS_SFTP_NOT_FILE_E:
return "not a regular file";

case WS_MSGID_NOT_ALLOWED_E:
return "message not allowed before user authentication";

default:
return "Unknown error code";
}
Expand Down Expand Up @@ -557,6 +560,84 @@ static void HandshakeInfoFree(HandshakeInfo* hs, void* heap)
}


#ifndef NO_WOLFSSH_SERVER
INLINE static int IsMessageAllowedServer(WOLFSSH *ssh, byte msg)
{
/* Has client userauth started? */
if (ssh->acceptState < ACCEPT_KEYED) {
if (msg > MSGID_KEXDH_LIMIT) {
return 0;
}
}
/* Is server userauth complete? */
if (ssh->acceptState < ACCEPT_SERVER_USERAUTH_SENT) {
/* Explicitly check for messages not allowed before user
* authentication has comleted. */
if (msg >= MSGID_USERAUTH_LIMIT) {
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by server "
"before user authentication is complete", msg);
return 0;
}
/* Explicitly check for the user authentication messages that
* only the server sends, it shouldn't receive them. */
if (msg > MSGID_USERAUTH_RESTRICT) {
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by server "
"during user authentication", msg);
return 0;
}
}
return 1;
}
#endif /* NO_WOLFSSH_SERVER */


#ifndef NO_WOLFSSH_CLIENT
INLINE static int IsMessageAllowedClient(WOLFSSH *ssh, byte msg)
{
/* Has client userauth started? */
if (ssh->connectState < CONNECT_CLIENT_KEXDH_INIT_SENT) {
if (msg >= MSGID_KEXDH_LIMIT) {
return 0;
}
}
/* Is client userauth complete? */
if (ssh->connectState < CONNECT_SERVER_USERAUTH_ACCEPT_DONE) {
/* Explicitly check for messages not allowed before user
* authentication has comleted. */
if (msg >= MSGID_USERAUTH_LIMIT) {
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by client "
"before user authentication is complete", msg);
return 0;
}
/* Explicitly check for the user authentication message that
* only the client sends, it shouldn't receive it. */
if (msg == MSGID_USERAUTH_RESTRICT) {
WLOG(WS_LOG_DEBUG, "Message ID %u not allowed by client "
"during user authentication", msg);
return 0;
}
}
return 1;
}
#endif /* NO_WOLFSSH_CLIENT */


INLINE static int IsMessageAllowed(WOLFSSH *ssh, byte msg)
{
#ifndef NO_WOLFSSH_SERVER
if (ssh->ctx->side == WOLFSSH_ENDPOINT_SERVER) {
return IsMessageAllowedServer(ssh, msg);
}
#endif /* NO_WOLFSSH_SERVER */
#ifndef NO_WOLFSSH_CLIENT
if (ssh->ctx->side == WOLFSSH_ENDPOINT_CLIENT) {
return IsMessageAllowedClient(ssh, msg);
}
#endif /* NO_WOLFSSH_CLIENT */
return 0;
}


#ifdef DEBUG_WOLFSSH

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

if (!IsMessageAllowed(ssh, msg)) {
return WS_MSGID_NOT_ALLOWED_E;
}

switch (msg) {

case MSGID_DISCONNECT:
Expand Down
3 changes: 2 additions & 1 deletion wolfssh/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ enum WS_ErrorCodes {
WS_KEY_CHECK_VAL_E = -1091, /* OpenSSH key check value fail */
WS_KEY_FORMAT_E = -1092, /* OpenSSH key format fail */
WS_SFTP_NOT_FILE_E = -1093, /* Not a regular file */
WS_MSGID_NOT_ALLOWED_E = -1094, /* Message not allowed before userauth */

WS_LAST_E = -1093 /* Update this to indicate last error */
WS_LAST_E = -1094 /* Update this to indicate last error */
};


Expand Down
14 changes: 14 additions & 0 deletions wolfssh/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,20 @@ enum WS_MessageIds {
};


#define MSGID_KEXDH_LIMIT 30

/* The endpoints should not allow message IDs greater than or
* equal to msgid 80 before user authentication is complete.
* Per RFC 4252 section 6. */
#define MSGID_USERAUTH_LIMIT 80

/* The client should only send the user auth request message
* (50), it should not accept it. The server should only receive
* the user auth request message, it should not accept the other
* user auth messages, it sends them. (>50) */
#define MSGID_USERAUTH_RESTRICT 50


#define CHANNEL_EXTENDED_DATA_STDERR WOLFSSH_EXT_DATA_STDERR


Expand Down

0 comments on commit 863714a

Please sign in to comment.