Skip to content

Commit b3fd4ea

Browse files
committed
Merge pull request #4446 from vladpaiu/fix_startup_route
fix duplicate startup_route execution and startup_done use-after-free (cherry picked from commit c3a85c3)
1 parent bc0e459 commit b3fd4ea

5 files changed

Lines changed: 42 additions & 30 deletions

File tree

‎main.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ static const struct main_script main_script[] = {
248248
static int main_loop(void)
249249
{
250250
static int chd_rank;
251-
int* startup_done = NULL;
251+
atomic_t *startup_done = NULL;
252252
utime_t last_check = 0;
253253
int rc;
254254
static struct internal_fork_handler profiling_handler = {
@@ -266,12 +266,12 @@ static int main_loop(void)
266266
}
267267

268268
if(sroutes->startup.a) {/* if a startup route was defined */
269-
startup_done = (int*)shm_malloc(sizeof(int));
269+
startup_done = shm_malloc(sizeof(*startup_done));
270270
if(startup_done == NULL) {
271271
LM_ERR("No more shared memory\n");
272272
goto error;
273273
}
274-
*startup_done = 0;
274+
atomic_init(startup_done, 0);
275275
}
276276

277277
/* fork for the timer process*/
@@ -308,8 +308,12 @@ static int main_loop(void)
308308
bind_address=0;
309309

310310
if (startup_done) {
311-
if (*startup_done==0)
311+
if (atomic_load(startup_done) != 1) {
312312
LM_CRIT("BUG: startup route defined, but not run :( \n");
313+
goto error;
314+
}
315+
/* Only the first SIP worker accesses the flag, and its final
316+
* store was observed before starting the remaining workers. */
313317
shm_free(startup_done);
314318
}
315319

‎net/net_tcp.c‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,7 @@ int tcp_count_processes(unsigned int *extra)
21582158
}
21592159

21602160

2161-
int tcp_start_processes(int *chd_rank, int *startup_done)
2161+
int tcp_start_processes(int *chd_rank, atomic_t *startup_done)
21622162
{
21632163
int r, n, p_id;
21642164
int reader_fd[2]; /* for comm. with the tcp workers read */
@@ -2209,6 +2209,15 @@ int tcp_start_processes(int *chd_rank, int *startup_done)
22092209
tcp_workers[r].state=STATE_ACTIVE;
22102210
tcp_workers[r].n_reqs=0;
22112211
tcp_workers[r].pt_idx=p_id;
2212+
/* Wait for the startup owner before forking more workers. */
2213+
if (*chd_rank == 1 && startup_done) {
2214+
while (atomic_load(startup_done) == 0) {
2215+
usleep(5);
2216+
handle_sigs();
2217+
}
2218+
if (atomic_load(startup_done) != 1)
2219+
goto error;
2220+
}
22122221
}else{
22132222
/* child */
22142223
set_proc_attrs("TCP receiver");
@@ -2217,21 +2226,22 @@ int tcp_start_processes(int *chd_rank, int *startup_done)
22172226
init_child(*chd_rank) < 0) {
22182227
LM_ERR("init_children failed\n");
22192228
report_failure_status();
2220-
if (startup_done)
2221-
*startup_done = -1;
2229+
if (*chd_rank == 1 && startup_done)
2230+
atomic_store(startup_done, -1);
22222231
exit(-1);
22232232
}
22242233

2225-
/* was startup route executed so far ? */
2226-
if (startup_done!=NULL && *startup_done==0 && r==0) {
2227-
LM_DBG("running startup for first TCP\n");
2228-
if(run_startup_route()< 0) {
2229-
LM_ERR("Startup route processing failed\n");
2234+
/* Only the first SIP worker owns the startup flag. In a mixed
2235+
* setup the first UDP worker has already run the startup route. */
2236+
if (*chd_rank == 1 && startup_done) {
2237+
LM_DBG("running startup route for first TCP worker\n");
2238+
if (run_startup_route() < 0) {
2239+
LM_ERR("startup route processing failed in TCP worker\n");
22302240
report_failure_status();
2231-
*startup_done = -1;
2241+
atomic_store(startup_done, -1);
22322242
exit(-1);
22332243
}
2234-
*startup_done = 1;
2244+
atomic_store(startup_done, 1);
22352245
}
22362246

22372247
report_conditional_status( (!no_daemon_mode), 0);
@@ -2240,13 +2250,6 @@ int tcp_start_processes(int *chd_rank, int *startup_done)
22402250
}
22412251
}
22422252

2243-
/* wait for the startup route to be executed */
2244-
if (startup_done)
2245-
while (!(*startup_done)) {
2246-
usleep(5);
2247-
handle_sigs();
2248-
}
2249-
22502253
return 0;
22512254
error:
22522255
return -1;

‎net/net_tcp.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define _NET_TCP_H_
2828

2929
#include "../mi/mi.h"
30+
#include "../atomic.h"
3031
#include "tcp_conn_defs.h"
3132
#include "tcp_conn_profile.h"
3233

@@ -61,7 +62,7 @@ void tcp_connect_proc_to_tcp_main( int proc_no, int chid );
6162
int tcp_count_processes(unsigned int *extra);
6263

6364
/* starts all TCP worker processes */
64-
int tcp_start_processes(int *chd_rank, int *startup_done);
65+
int tcp_start_processes(int *chd_rank, atomic_t *startup_done);
6566

6667
/* starts the TCP listening process */
6768
int tcp_start_listener(void);

‎net/net_udp.c‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static void udp_process_graceful_terminate(int sender, void *param)
472472

473473

474474
/* starts all UDP related processes */
475-
int udp_start_processes(int *chd_rank, int *startup_done)
475+
int udp_start_processes(int *chd_rank, atomic_t *startup_done)
476476
{
477477
struct socket_info_full *sif;
478478
int p_id;
@@ -518,20 +518,20 @@ int udp_start_processes(int *chd_rank, int *startup_done)
518518
init_child(*chd_rank) < 0) {
519519
report_failure_status();
520520
if (*chd_rank == 1 && startup_done)
521-
*startup_done = -1;
521+
atomic_store(startup_done, -1);
522522
exit(-1);
523523
}
524524

525-
/* first UDP proc runs statup_route (if defined) */
525+
/* Only the first SIP worker owns the startup flag. */
526526
if(*chd_rank == 1 && startup_done!=NULL) {
527527
LM_DBG("running startup for first UDP\n");
528528
if(run_startup_route()< 0) {
529529
report_failure_status();
530-
*startup_done = -1;
530+
atomic_store(startup_done, -1);
531531
LM_ERR("Startup route processing failed\n");
532532
exit(-1);
533533
}
534-
*startup_done = 1;
534+
atomic_store(startup_done, 1);
535535
}
536536

537537
report_conditional_status( (!no_daemon_mode), 0);
@@ -551,11 +551,14 @@ int udp_start_processes(int *chd_rank, int *startup_done)
551551
} else {
552552
/*parent*/
553553
/* wait for first proc to finish the startup route */
554-
if (*chd_rank == 1 && startup_done)
555-
while(!(*startup_done)) {
554+
if (*chd_rank == 1 && startup_done) {
555+
while (atomic_load(startup_done) == 0) {
556556
usleep(5);
557557
handle_sigs();
558558
}
559+
if (atomic_load(startup_done) != 1)
560+
goto error;
561+
}
559562
}
560563
} /* procs per listener */
561564
} /* looping through the listeners per proto */

‎net/net_udp.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define _NET_UDP_H_
2828

2929
#include "../socket_info.h"
30+
#include "../atomic.h"
3031

3132

3233
/**************************** Control functions ******************************/
@@ -41,7 +42,7 @@ void udp_destroy(void);
4142
int udp_count_processes(unsigned int *extra);
4243

4344
/* starts all UDP related processes */
44-
int udp_start_processes(int *chd_rank, int *startup_done);
45+
int udp_start_processes(int *chd_rank, atomic_t *startup_done);
4546

4647
/**************************** Listener functions *****************************/
4748

0 commit comments

Comments
 (0)