Skip to content

Commit

Permalink
b2b_logic: fix some locking issues
Browse files Browse the repository at this point in the history
Properly access the b2b logic context of the current tuple under lock.
Also, protect the access to the tuple when using the b2bl_get_entity_info()
API function.

Related to #3117

(cherry picked from commit ebf40c9)
  • Loading branch information
rvlad-patrascu committed Jul 4, 2023
1 parent af86cf6 commit b6408c9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
36 changes: 24 additions & 12 deletions modules/b2b_logic/b2b_logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,7 @@ static int b2bl_get_entity_info(str *key, struct sip_msg *msg, int entity, struc
b2bl_tuple_t *tuple;
b2bl_entity_id_t *bentity = NULL;
int locked = 0;
int rc = -1;

if (!info)
return -4;
Expand All @@ -2286,12 +2287,14 @@ static int b2bl_get_entity_info(str *key, struct sip_msg *msg, int entity, struc

if (key) {
tuple = b2bl_get_tuple(key);
if (tuple)
locked = 1;
} else {
tuple = get_ctx_tuple(&locked);

if (tuple && locked &&
if (tuple && !locked &&
b2bl_htable[tuple->hash_index].locked_by != process_no)
lock_release(&b2bl_htable[tuple->hash_index].lock);
lock_get(&b2bl_htable[tuple->hash_index].lock);
}
if (!tuple)
return -2;
Expand All @@ -2309,11 +2312,13 @@ static int b2bl_get_entity_info(str *key, struct sip_msg *msg, int entity, struc
bentity = tuple->bridge_entities[(entity == -2?0:1)];
}
} else {
if (!msg)
return -2;
if (!msg) {
rc = -2;
goto end;
}
if (get_callid(msg, &callid) < 0) {
LM_ERR("Failed to get callid from SIP message\n");
return -1;
goto end;
}
if (tuple->bridge_entities[0] && tuple->bridge_entities[0]->dlginfo) {
if (!str_strcmp(&callid,
Expand All @@ -2334,22 +2339,29 @@ static int b2bl_get_entity_info(str *key, struct sip_msg *msg, int entity, struc
} else {
bentity = tuple->bridge_entities[entity];
}
if (!bentity)
return -3;
if (!bentity) {
rc = -3;
goto end;
}
if (bentity->key.len && pkg_str_dup(&info->key, &bentity->key) < 0)
return -1;
goto end;
if (bentity->dlginfo) {
if (bentity->dlginfo->callid.len
&& pkg_str_dup(&info->callid, &bentity->dlginfo->callid) < 0)
return -1;
goto end;
if (bentity->dlginfo->fromtag.len
&& pkg_str_dup(&info->fromtag, &bentity->dlginfo->fromtag) < 0)
return -1;
goto end;
if (bentity->dlginfo->totag.len
&& pkg_str_dup(&info->totag, &bentity->dlginfo->totag) < 0)
return -1;
goto end;
}
return 0;

rc = 0;
end:
if (locked && b2bl_htable[tuple->hash_index].locked_by != process_no)
lock_release(&b2bl_htable[tuple->hash_index].lock);
return rc;
}

static void b2bl_release_entity_info(struct b2b_entity_info_t *info)
Expand Down
1 change: 1 addition & 0 deletions modules/b2b_logic/b2b_logic_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "records.h"
#include "b2b_logic_ctx.h"

/* The function will return with the lock aquired if successful */
static b2bl_tuple_t *b2bl_ctx_get_tuple(str *key)
{
b2bl_tuple_t *tuple = b2bl_get_tuple(key);
Expand Down
16 changes: 12 additions & 4 deletions modules/b2b_logic/records.c
Original file line number Diff line number Diff line change
Expand Up @@ -1019,12 +1019,20 @@ int b2b_extra_headers(struct sip_msg* msg, str* b2bl_key, str* custom_hdrs,
return 0;
}

/* The function will return with the lock aquired if successful */
b2bl_tuple_t *b2bl_get_tuple(str *key)
{
unsigned int hash_index, local_index;
b2bl_tuple_t *tuple;

if (b2bl_parse_key(key, &hash_index, &local_index) < 0)
goto error;
return b2bl_search_tuple_safe(hash_index, local_index);
error:
return NULL;
return NULL;

lock_get(&b2bl_htable[hash_index].lock);

tuple = b2bl_search_tuple_safe(hash_index, local_index);
if (!tuple)
lock_release(&b2bl_htable[hash_index].lock);

return tuple;
}

0 comments on commit b6408c9

Please sign in to comment.