Skip to content

Commit eb3a47d

Browse files
Custom identification strings are unchecked until deep in the system: better error checking
1 parent 7325678 commit eb3a47d

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/internal.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13195,6 +13195,26 @@ int DoProtoId(WOLFSSH* ssh)
1319513195
return ret;
1319613196
}
1319713197

13198+
/* ProtoIdStr is checked for validity and will be rejected unless
13199+
* it adhears to these criteria:
13200+
* MUST be between 3 and 255 bytes in length
13201+
* MUST end with '\r\n'
13202+
* MUST NOT contain '\r' or '\n' in the body */
13203+
int ValidateProtoId(const char* protoIdStr, word32 len)
13204+
{
13205+
/* Length is checked first: the terminator and body checks below index
13206+
* and subtract from len. Minimum is one body byte plus CRLF. */
13207+
if (protoIdStr == NULL || len < 3 || protoIdStr[len-1] != '\n' ||
13208+
protoIdStr[len-2] != '\r' || len > WOLFSSH_PROTOID_LIMIT ||
13209+
WSTRNSTR(protoIdStr, "\n", len - 2) != NULL ||
13210+
WSTRNSTR(protoIdStr, "\r", len - 2) != NULL) {
13211+
WLOG(WS_LOG_ERROR, "Proto Id was invalid: it must end in \\r\\n, "
13212+
"be no longer than %d bytes, and must not contain "
13213+
"\\r or \\n in the body of the line", WOLFSSH_PROTOID_LIMIT);
13214+
return WS_BAD_ARGUMENT;
13215+
}
13216+
return WS_SUCCESS;
13217+
}
1319813218

1319913219
int SendProtoId(WOLFSSH* ssh)
1320013220
{

src/ssh.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,10 +3447,18 @@ int wolfSSH_GetMaxAuthAttempts(WOLFSSH* ssh)
34473447
int wolfSSH_CTX_SetSshProtoIdStr(WOLFSSH_CTX* ctx,
34483448
const char* protoIdStr)
34493449
{
3450+
int ret;
3451+
WLOG(WS_LOG_DEBUG, "Entering wolfSSH_CTX_SetSshProtoIdStr()");
3452+
34503453
if (!ctx || !protoIdStr) {
34513454
return WS_BAD_ARGUMENT;
34523455
}
34533456

3457+
if ((ret = ValidateProtoId(protoIdStr, (word32)WSTRLEN(protoIdStr))) !=
3458+
WS_SUCCESS) {
3459+
return ret;
3460+
}
3461+
34543462
ctx->sshProtoIdStr = protoIdStr;
34553463
return WS_SUCCESS;
34563464
}

wolfssh/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,7 @@ WOLFSSH_LOCAL int DoProtoId(WOLFSSH* ssh);
15471547
WOLFSSH_LOCAL int wolfSSH_SendPacket(WOLFSSH* ssh);
15481548
WOLFSSH_LOCAL int wolfSSH_OutputPending(WOLFSSH* ssh);
15491549
WOLFSSH_LOCAL int SendProtoId(WOLFSSH* ssh);
1550+
WOLFSSH_LOCAL int ValidateProtoId(const char* protoIdStr, word32 len);
15501551
WOLFSSH_LOCAL int SendKexInit(WOLFSSH* ssh);
15511552
WOLFSSH_LOCAL int SendKexDhInit(WOLFSSH* ssh);
15521553
WOLFSSH_LOCAL int SendKexDhReply(WOLFSSH* ssh);

wolfssh/ssh.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,13 @@ WOLFSSH_API int wolfSSH_SetUsername(WOLFSSH* ssh, const char* username);
588588
WOLFSSH_API char* wolfSSH_GetUsername(WOLFSSH* ssh);
589589

590590
WOLFSSH_API int wolfSSH_CTX_SetBanner(WOLFSSH_CTX* ctx, const char* newBanner);
591+
/* ProtoIdStr is checked for validity and will be rejected unless
592+
* it adhears to these criteria:
593+
* MUST be between 3 and 255 bytes in length
594+
* MUST end with '\r\n'
595+
* MUST NOT contain '\r' or '\n' in the body
596+
* If these are not adheared to the function will return WS_BAD_ARGUMENT
597+
* and not load the ProtoId in to the WOLFSSH_CTX struct */
591598
WOLFSSH_API int wolfSSH_CTX_SetSshProtoIdStr(WOLFSSH_CTX* ctx,
592599
const char* protoIdStr);
593600
/* Set the server-side limit on failed userauth attempts per connection. The

0 commit comments

Comments
 (0)