Skip to content

Commit

Permalink
nathelper: Fix rare message corruption due to bitwise opts on strings
Browse files Browse the repository at this point in the history
Credits to Damien Sandras for the report and initial PR!

Closes #2695

(cherry picked from commit ecbd985)
(cherry picked from commit 237997b)
  • Loading branch information
liviuchircu committed Nov 25, 2021
1 parent 06a35a6 commit 7ffbd41
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
7 changes: 2 additions & 5 deletions modules/nathelper/sip_pinger.h
Expand Up @@ -342,7 +342,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;
Expand All @@ -361,7 +361,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;
Expand All @@ -379,9 +379,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) +
Expand Down
39 changes: 37 additions & 2 deletions socket_info.h
Expand Up @@ -400,11 +400,46 @@ 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_HEP_UDP:
memcpy(p, STR_L("HEP_UDP"));
break;
case PROTO_HEP_TCP:
memcpy(p, STR_L("HEP_TCP"));
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 */
Expand Down

0 comments on commit 7ffbd41

Please sign in to comment.