Skip to content
Snippets Groups Projects
Commit db690fad authored by Deon George's avatar Deon George
Browse files

Improve btox() so that it doesnt lead to any buffer overruns

parent 6d0ec492
No related branches found
No related tags found
2 merge requests!463MRC mods by Codefenix (2024-10-20),!11Enable obtaining source IP address if HAproxy is used
......@@ -416,11 +416,15 @@ static bool read_socket(SOCKET sock,unsigned char *buffer,int len)
}
/* Convert a binary variable into a hex string - used for printing in the debug log */
static void btox(char *hexstr, const unsigned char *srcbuf, int srcbuflen)
static void btox(char *hexstr, const unsigned char *srcbuf, int srcbuflen, int hexstrlen)
{
int i;
for (i=0;i<srcbuflen;i++) sprintf(hexstr+i*2,"%02X",srcbuf[i]);
hexstr[i*2] = 0; // null terminate the string
if (hexstrlen < srcbuflen*2) {
lprintf(LOG_WARNING,"btox Hexstr buffer too small [%d] - not all data will be shown",hexstrlen);
srcbuflen = hexstrlen/2;
}
*hexstr = '\0';
for (int i=0;i<srcbuflen;i++) sprintf(hexstr+strlen(hexstr),"%02X",srcbuf[i]);
}
/* TODO: IPv6 */
......@@ -5609,7 +5613,7 @@ NO_SSH:
// OK, just for sanity, our next 6 chars should be v2...
if (read_socket(client_socket,hapstr,6)==false || strcmp((char *)hapstr,"\nQUIT\n") != 0) {
btox(haphex,hapstr,6);
btox(haphex,hapstr,6,sizeof(haphex));
lprintf(LOG_ERR,"%04d * HAPROXY Something went wrong [%s] incomplete v2 setup",client_socket,haphex);
close_socket(client_socket);
continue;
......@@ -5617,7 +5621,7 @@ NO_SSH:
// Command and Version
if (read_socket(client_socket,hapstr,1)==false) {
btox(haphex,hapstr,1);
btox(haphex,hapstr,1,sizeof(haphex));
lprintf(LOG_ERR,"%04d * HAPROXY, looking for Verson/Command - failed [%s]",client_socket,haphex);
close_socket(client_socket);
continue;
......@@ -5627,7 +5631,7 @@ NO_SSH:
// Protocol and Family
if (read_socket(client_socket,hapstr,1)==false) {
btox(haphex,hapstr,1);
btox(haphex,hapstr,1,sizeof(haphex));
lprintf(LOG_ERR,"%04d * HAPROXY, looking for Protocol/Family - failed [%s]",client_socket,haphex);
close_socket(client_socket);
continue;
......@@ -5638,7 +5642,7 @@ NO_SSH:
// Address Length - 2 bytes
if (read_socket(client_socket,hapstr,2)==false) {
btox(haphex,hapstr,2);
btox(haphex,hapstr,2,sizeof(haphex));
lprintf(LOG_ERR,"%04d * HAPROXY, looking for address length - failed [%s]",client_socket,haphex);
close_socket(client_socket);
continue;
......@@ -5656,7 +5660,7 @@ NO_SSH:
}
if (read_socket(client_socket,hapstr,i)==false) {
btox(haphex,hapstr,i);
btox(haphex,hapstr,i,sizeof(haphex));
lprintf(LOG_ERR,"%04d * HAPROXY looking for IPv4 address - failed [%s]",client_socket,haphex);
close_socket(client_socket);
continue;
......@@ -5674,7 +5678,7 @@ NO_SSH:
}
if (read_socket(client_socket,hapstr,i)==false) {
btox(haphex,hapstr,i);
btox(haphex,hapstr,i,sizeof(haphex));
lprintf(LOG_ERR,"%04d * HAPROXY looking for IPv6 address - failed [%s]",client_socket,haphex);
close_socket(client_socket);
continue;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment