Skip to content

Commit

Permalink
Re-aquire client and upstream pointers after a name resolution. As we…
Browse files Browse the repository at this point in the history
…'re leaving the locked area for the resolve, we cannot control if the shared memory object changed meanwhile. If it did, then the pointers will point into nowhere, leading to a SEGV_MAPERR.

Signed-off-by: DL6ER <dl6er@dl6er.de>
  • Loading branch information
DL6ER committed May 27, 2020
1 parent 7ee2124 commit ee0790a
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/resolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,17 @@ void resolveClients(const bool onlynew)
int skipped = 0;
for(int clientID = 0; clientID < clientscount; clientID++)
{
// Get client pointer
// Memory access needs to get locked
lock_shm();
// Get client pointer for the first time (reading data)
clientsData* client = getClient(clientID, true);
if(client == NULL)
{
logg("ERROR: Unable to get client pointer with ID %i, skipping...", clientID);
logg("ERROR: Unable to get client pointer (1) with ID %i, skipping...", clientID);
skipped++;
continue;
}

// Memory access needs to get locked
lock_shm();
bool newflag = client->new;
size_t ippos = client->ippos;
size_t oldnamepos = client->namepos;
Expand All @@ -330,6 +330,18 @@ void resolveClients(const bool onlynew)
size_t newnamepos = resolveAndAddHostname(ippos, oldnamepos);

lock_shm();
// Get client pointer for the second time (writing data)
// We cannot use the same pointer again as we released
// the lock in between so we cannot know if something
// happened to the shared memory object (resize event)
client = getClient(clientID, true);
if(client == NULL)
{
logg("ERROR: Unable to get client pointer (2) with ID %i, skipping...", clientID);
skipped++;
continue;
}

// Store obtained host name (may be unchanged)
client->namepos = newnamepos;
// Mark entry as not new
Expand All @@ -355,7 +367,9 @@ void resolveForwardDestinations(const bool onlynew)
int skipped = 0;
for(int upstreamID = 0; upstreamID < upstreams; upstreamID++)
{
// Get upstream pointer
// Memory access needs to get locked
lock_shm();
// Get upstream pointer for the first time (reading data)
upstreamsData* upstream = getUpstream(upstreamID, true);
if(upstream == NULL)
{
Expand All @@ -364,8 +378,6 @@ void resolveForwardDestinations(const bool onlynew)
continue;
}

// Memory access needs to get locked
lock_shm();
bool newflag = upstream->new;
size_t ippos = upstream->ippos;
size_t oldnamepos = upstream->namepos;
Expand All @@ -383,6 +395,18 @@ void resolveForwardDestinations(const bool onlynew)
size_t newnamepos = resolveAndAddHostname(ippos, oldnamepos);

lock_shm();
// Get upstream pointer for the second time (writing data)
// We cannot use the same pointer again as we released
// the lock in between so we cannot know if something
// happened to the shared memory object (resize event)
upstream = getUpstream(upstreamID, true);
if(upstream == NULL)
{
logg("ERROR: Unable to get upstream pointer with ID %i, skipping...", upstreamID);
skipped++;
continue;
}

// Store obtained host name (may be unchanged)
upstream->namepos = newnamepos;
// Mark entry as not new
Expand Down

0 comments on commit ee0790a

Please sign in to comment.