From ecbd9856efb7a420e2fceff6769951d307be8c03 Mon Sep 17 00:00:00 2001 From: Liviu Chircu Date: Thu, 25 Nov 2021 14:08:34 +0200 Subject: [PATCH] nathelper: Fix rare message corruption due to bitwise opts on strings Credits to Damien Sandras for the report and initial PR! Closes #2695 --- modules/nathelper/sip_pinger.h | 7 ++---- socket_info.h | 45 ++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/modules/nathelper/sip_pinger.h b/modules/nathelper/sip_pinger.h index 45bea4ada71..89fcc5b1bd1 100644 --- a/modules/nathelper/sip_pinger.h +++ b/modules/nathelper/sip_pinger.h @@ -351,7 +351,7 @@ build_sipping(udomain_t *d, str *curi, struct socket_info* s,str *path, { #define s_len(_s) (sizeof(_s)-1) static char buf[MAX_SIPPING_SIZE]; - char *p, proto_str[PROTO_NAME_MAX_SIZE]; + char *p, proto_str[PROTO_NAME_MAX_SIZE+1]; str *address, *port; str st; int len; @@ -370,7 +370,7 @@ build_sipping(udomain_t *d, str *curi, struct socket_info* s,str *path, sbranch.s = branch; sbranch.len = strlen(branch); - p = proto2str(s->proto, proto_str); + p = proto2upper(s->proto, proto_str); *(p++) = ' '; st.s = proto_str; st.len = p - proto_str; @@ -388,9 +388,6 @@ build_sipping(udomain_t *d, str *curi, struct socket_info* s,str *path, else port = &s->port_no_str; - /* quick proto uppercase */ - *((int *)st.s) &= ~((1 << 21) | (1 << 13) | (1 << 5)); - if ( sipping_method.len + 1 + curi->len + s_len(" SIP/2.0"CRLF) + s_len("Via: SIP/2.0/") + st.len + address->len + 1 + port->len + strlen(branch) + diff --git a/socket_info.h b/socket_info.h index e276089bf1f..e7b60ef955a 100644 --- a/socket_info.h +++ b/socket_info.h @@ -432,11 +432,52 @@ static inline char* proto2str(int proto, char *p) return p; } +static inline char* proto2upper(int proto, char *p) +{ + switch (proto) { + case PROTO_UDP: + memcpy(p, STR_L("UDP")); + break; + case PROTO_TCP: + memcpy(p, STR_L("TCP")); + break; + case PROTO_TLS: + memcpy(p, STR_L("TLS")); + break; + case PROTO_SCTP: + memcpy(p, STR_L("SCTP")); + break; + case PROTO_WS: + memcpy(p, STR_L("WS")); + break; + case PROTO_WSS: + memcpy(p, STR_L("WSS")); + break; + case PROTO_BIN: + memcpy(p, STR_L("BIN")); + break; + case PROTO_BINS: + memcpy(p, STR_L("BINS")); + break; + case PROTO_HEP_UDP: + memcpy(p, STR_L("HEP_UDP")); + break; + case PROTO_HEP_TCP: + memcpy(p, STR_L("HEP_TCP")); + break; + case PROTO_SMPP: + memcpy(p, STR_L("SMPP")); + break; + default: + LM_CRIT("unsupported proto %d\n", proto); + } + + return p; +} static inline char *proto2a(int proto) { - static char b[8]; /* IMPORTANT - keep this max aligned with the proto2str - * with an extra +1 for NULL terminator */ + static char b[PROTO_NAME_MAX_SIZE+1]; char *p; /* print the proto name */