Skip to content

Commit

Permalink
auth_diameter: add missing allocation checks, convert to memory loggi…
Browse files Browse the repository at this point in the history
…ng helper, clarify logging
  • Loading branch information
pantelisk98 authored and henningw committed Mar 29, 2023
1 parent 811ede4 commit bc47875
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/modules/auth_diameter/auth_diameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static int mod_child_init(int r)
rb = (rd_buf_t*)pkg_malloc(sizeof(rd_buf_t));
if(!rb)
{
LM_DBG("no more free pkg memory\n");
PKG_MEM_ERROR;
return -1;
}
rb->buf = 0;
Expand Down Expand Up @@ -241,7 +241,7 @@ static int group_fixup(void** param, int param_no)
s = (str*)pkg_malloc(sizeof(str));
if (!s)
{
LM_ERR("no pkg memory left\n");
PKG_MEM_ERROR;
return E_UNSPEC;
}
s->s = (char*)*param;
Expand Down
40 changes: 30 additions & 10 deletions src/modules/auth_diameter/authorize.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
{
user_name.len += 2;
user_name.s = (char*)ad_malloc(user_name.len*sizeof(char));
if(!(user_name.s))
{
PKG_MEM_ERROR;
return -1;
}
memset(user_name.s, 0, user_name.len);

memcpy(user_name.s, uri->user.s, uri->user.len);
Expand All @@ -333,7 +338,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_User_Name, 0, 0, user_name.s,
user_name.len, AVP_FREE_DATA)) == 0)
{
LM_ERR("no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
if(user_name.len>0)
pkg_free(user_name.s);
goto error;
Expand All @@ -352,7 +357,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_User_Name, 0, 0, cred->username.whole.s,
cred->username.whole.len, AVP_DUPLICATE_DATA)) == 0)
{
LM_ERR("no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
goto error;
}

Expand All @@ -371,7 +376,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
user_name.s = ad_malloc(user_name.len);
if (!user_name.s)
{
LM_ERR(" no more pkg memory left\n");
PKG_MEM_ERROR;
goto error;
}
memcpy(user_name.s, cred->username.whole.s,
Expand All @@ -389,7 +394,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_User_Name, 0, 0, user_name.s,
user_name.len, AVP_FREE_DATA)) == 0)
{
LM_ERR(" no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
if(user_name.len>0)
pkg_free(user_name.s);
goto error;
Expand All @@ -409,7 +414,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_SIP_MSGID, 0, 0, (char*)(&tmp),
sizeof(m_id), AVP_DUPLICATE_DATA)) == 0)
{
LM_ERR(" no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
goto error;
}
if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)
Expand All @@ -422,7 +427,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_Service_Type, 0, 0, SIP_AUTHENTICATION,
SERVICE_LEN, AVP_DUPLICATE_DATA)) == 0)
{
LM_ERR(" no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
goto error;
}
if( AAAAddAVPToMessage(req, avp, 0)!= AAA_ERR_SUCCESS)
Expand All @@ -435,7 +440,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_Destination_Realm, 0, 0, uri->host.s,
uri->host.len, AVP_DUPLICATE_DATA)) == 0)
{
LM_ERR(" no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
goto error;
}

Expand All @@ -452,6 +457,11 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
/* Resource AVP */
user_name.len = ruri->user.len + ruri->host.len + ruri->port.len + 2;
user_name.s = (char*)ad_malloc(user_name.len*sizeof(char));
if(!(user_name.s))
{
PKG_MEM_ERROR;
return -1;
}
memset(user_name.s, 0, user_name.len);
memcpy(user_name.s, ruri->user.s, ruri->user.len);

Expand Down Expand Up @@ -479,7 +489,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_Resource, 0, 0, user_name.s,
user_name.len, AVP_FREE_DATA)) == 0)
{
LM_ERR(" no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
if(user_name.s)
pkg_free(user_name.s);
goto error;
Expand All @@ -496,7 +506,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_Response, 0, 0, hdr->body.s,
hdr->body.len, AVP_DUPLICATE_DATA)) == 0)
{
LM_ERR(" no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
goto error;
}

Expand All @@ -512,7 +522,7 @@ int diameter_authorize(struct hdr_field* hdr, str* p_method, sip_uri_t *uri,
if( (avp=AAACreateAVP(AVP_Method, 0, 0, p_method->s,
p_method->len, AVP_DUPLICATE_DATA)) == 0)
{
LM_ERR(" no more pkg memory left!\n");
LM_ERR("could not create AVP\n");
goto error;
}

Expand Down Expand Up @@ -603,6 +613,11 @@ int srv_response(struct sip_msg* msg, rd_buf_t * rb, int hftype)
{
auth_hf_len = WWW_AUTH_CHALLENGE_LEN+rb->chall_len;
auth_hf = (char*)ad_malloc(auth_hf_len*(sizeof(char)));
if(!(auth_hf))
{
PKG_MEM_ERROR;
return -1;
}
memset(auth_hf, 0, auth_hf_len);
memcpy(auth_hf,WWW_AUTH_CHALLENGE, WWW_AUTH_CHALLENGE_LEN);
memcpy(auth_hf+WWW_AUTH_CHALLENGE_LEN, rb->chall,
Expand All @@ -615,6 +630,11 @@ int srv_response(struct sip_msg* msg, rd_buf_t * rb, int hftype)
{
auth_hf_len = PROXY_AUTH_CHALLENGE_LEN+rb->chall_len;
auth_hf = (char*)ad_malloc(auth_hf_len*(sizeof(char)));
if(!(auth_hf))
{
PKG_MEM_ERROR;
return -1;
}
memset(auth_hf, 0, auth_hf_len);
memcpy(auth_hf, PROXY_AUTH_CHALLENGE, PROXY_AUTH_CHALLENGE_LEN);
memcpy(auth_hf + PROXY_AUTH_CHALLENGE_LEN, rb->chall,
Expand Down
6 changes: 3 additions & 3 deletions src/modules/auth_diameter/avp.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ AAA_AVP* AAACreateAVP(

return avp;
error:
LM_ERR("no more pkg memory left!\n");
PKG_MEM_ERROR;
if(avp) ad_free(avp);
return 0;
}
Expand Down Expand Up @@ -420,7 +420,7 @@ AAA_AVP* AAACloneAVP( AAA_AVP *avp , unsigned char clone_data)
/* clone the avp structure */
n_avp = (AAA_AVP*)ad_malloc( sizeof(AAA_AVP) );
if (!n_avp) {
LM_ERR(" cannot get free pkg memory!!\n");
PKG_MEM_ERROR;
goto error;
}
memcpy( n_avp, avp, sizeof(AAA_AVP));
Expand All @@ -430,7 +430,7 @@ AAA_AVP* AAACloneAVP( AAA_AVP *avp , unsigned char clone_data)
/* clone the avp data */
n_avp->data.s = (char*)ad_malloc( avp->data.len );
if (!(n_avp->data.s)) {
LM_ERR("cannot get free pkg memory!!\n");
PKG_MEM_ERROR;
ad_free( n_avp );
goto error;
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/auth_diameter/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ AAAReturnCode AAABuildMsgBuffer( AAAMessage *msg )
/* allocate some memory */
msg->buf.s = (char*)ad_malloc( msg->buf.len );
if (!msg->buf.s) {
LM_ERR(" no more pkg memory!\n");
PKG_MEM_ERROR;
goto error;
}
memset(msg->buf.s, 0, msg->buf.len);
Expand Down Expand Up @@ -209,7 +209,7 @@ AAAMessage* AAATranslateMessage( unsigned char* source, unsigned int sourceLen,
/* alloc a new message structure */
msg = (AAAMessage*)ad_malloc(sizeof(AAAMessage));
if (!msg) {
LM_ERR(" no more free memory!!\n");
PKG_MEM_ERROR;
goto error;
}
memset(msg,0,sizeof(AAAMessage));
Expand Down Expand Up @@ -325,7 +325,7 @@ AAAMessage* AAAInMessage(AAACommandCode commandCode, AAAApplicationId appId)
msg = (AAAMessage*)ad_malloc(sizeof(AAAMessage));
if (!msg)
{
LM_ERR("no more pkg memory!\n");
PKG_MEM_ERROR;
return NULL;
}
memset(msg, 0, sizeof(AAAMessage));
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auth_diameter/tcp_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int do_read( int socket, rd_buf_t *p)
//LM_DBG("message length = %d(%x)\n",len,len);
if ( (p->buf=pkg_malloc(len))==0 )
{
LM_ERR("no more pkg memory\n");
PKG_MEM_ERROR;
goto error;
}
*((unsigned int*)p->buf) = p->first_4bytes;
Expand Down Expand Up @@ -307,7 +307,7 @@ int tcp_send_recv(int sockfd, char* buf, int len, rd_buf_t* rb,
rb->chall = (unsigned char*)pkg_malloc(avp->data.len*sizeof(unsigned char));
if(rb->chall == NULL)
{
LM_ERR("no more pkg memory\n");
PKG_MEM_ERROR;
rb->ret_code = AAA_SRVERR;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/auth_diameter/user_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ int diameter_is_user_in(struct sip_msg* _m, char* _hf, char* _group)
p = (char*)pkg_malloc(user_name.len);
if (!p)
{
LM_ERR("no pkg memory left\n");
PKG_MEM_ERROR;
return -6;
}
user_name.s = p;
Expand Down

0 comments on commit bc47875

Please sign in to comment.