From e68ce1cc10789c471b25fd6ff8658d4172dfdeec Mon Sep 17 00:00:00 2001 From: Philip Paeps Date: Wed, 10 Jul 2019 17:42:04 +0000 Subject: [PATCH] telnet: fix a couple of snprintf() buffer overflows Obtained from: Juniper Networks MFC after: 1 week --- contrib/telnet/telnet/commands.c | 9 +++++---- contrib/telnet/telnet/telnet.c | 4 ++-- contrib/telnet/telnet/utilities.c | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/contrib/telnet/telnet/commands.c b/contrib/telnet/telnet/commands.c index 02a0de524d3f..a2dcd26efd30 100644 --- a/contrib/telnet/telnet/commands.c +++ b/contrib/telnet/telnet/commands.c @@ -1655,10 +1655,11 @@ env_init(void) char hbuf[256+1]; char *cp2 = strchr((char *)ep->value, ':'); - gethostname(hbuf, 256); - hbuf[256] = '\0'; - cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1); - sprintf((char *)cp, "%s%s", hbuf, cp2); + gethostname(hbuf, sizeof(hbuf)); + hbuf[sizeof(hbuf)-1] = '\0'; + unsigned int buflen = strlen(hbuf) + strlen(cp2) + 1; + cp = (char *)malloc(sizeof(char)*buflen); + snprintf((char *)cp, buflen, "%s%s", hbuf, cp2); free(ep->value); ep->value = (unsigned char *)cp; } diff --git a/contrib/telnet/telnet/telnet.c b/contrib/telnet/telnet/telnet.c index 80f43b2fc0af..33a2ed5335ab 100644 --- a/contrib/telnet/telnet/telnet.c +++ b/contrib/telnet/telnet/telnet.c @@ -785,7 +785,7 @@ suboption(void) name = gettermname(); len = strlen(name) + 4 + 2; if (len < NETROOM()) { - sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE, + snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE, TELQUAL_IS, name, IAC, SE); ring_supply_data(&netoring, temp, len); printsub('>', &temp[2], len-2); @@ -807,7 +807,7 @@ suboption(void) TerminalSpeeds(&ispeed, &ospeed); - sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED, + snprintf((char *)temp, sizeof(temp), "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED, TELQUAL_IS, ospeed, ispeed, IAC, SE); len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */ diff --git a/contrib/telnet/telnet/utilities.c b/contrib/telnet/telnet/utilities.c index 8d1ea2aee919..f10c04068a25 100644 --- a/contrib/telnet/telnet/utilities.c +++ b/contrib/telnet/telnet/utilities.c @@ -629,7 +629,7 @@ printsub(char direction, unsigned char *pointer, int length) } { char tbuf[64]; - sprintf(tbuf, "%s%s%s%s%s", + snprintf(tbuf, sizeof(tbuf), "%s%s%s%s%s", pointer[2]&MODE_EDIT ? "|EDIT" : "", pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "", pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",