Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
avoid conreset race condition reported by coverity
  • Loading branch information
Fabian Mauchle committed Jan 23, 2023
1 parent 393c588 commit c17ef97
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
10 changes: 5 additions & 5 deletions dtls.c
Expand Up @@ -35,7 +35,7 @@
static void setprotoopts(struct commonprotoopts *opts);
static char **getlistenerargs();
void *dtlslistener(void *arg);
int dtlsconnect(struct server *server, int timeout, char *text);
int dtlsconnect(struct server *server, int timeout, int reconnect);
void *dtlsclientrd(void *arg);
int clientradputdtls(struct server *server, unsigned char *rad);
void addserverextradtls(struct clsrvconf *conf);
Expand Down Expand Up @@ -489,7 +489,7 @@ static void cleanup_connection(struct server *server) {
server->ssl = NULL;
}

int dtlsconnect(struct server *server, int timeout, char *text) {
int dtlsconnect(struct server *server, int timeout, int reconnect) {
struct timeval socktimeout, now, start;
time_t wait;
int firsttry = 1;
Expand All @@ -502,7 +502,7 @@ int dtlsconnect(struct server *server, int timeout, char *text) {
char *subj;
struct list_node *entry;

debug(DBG_DBG, "dtlsconnect: called from %s", text);
debug(DBG_DBG, "dtlsconnect: %s to %s", reconnect ? "reconnecting" : "initial connection", server->conf->name);
pthread_mutex_lock(&server->lock);

if (server->state == RSP_SERVER_STATE_CONNECTED)
Expand Down Expand Up @@ -604,7 +604,7 @@ int dtlsconnect(struct server *server, int timeout, char *text) {
gettimeofday(&server->connecttime, NULL);
pthread_mutex_unlock(&server->lock);
pthread_mutex_lock(&server->newrq_mutex);
server->conreset = 1;
server->conreset = reconnect;
pthread_cond_signal(&server->newrq_cond);
pthread_mutex_unlock(&server->newrq_mutex);
if (source) freeaddrinfo(source);
Expand Down Expand Up @@ -653,7 +653,7 @@ void *dtlsclientrd(void *arg) {
debug (DBG_WARN, "tlscleintrd: connection to server %s lost", server->conf->name);
else if (server->lostrqs)
debug (DBG_WARN, "dtlsclientrd: server %s did not respond, closing connection.", server->conf->name);
dtlsconnect(server, 0, "dtlsclientrd");
dtlsconnect(server, 0, 1);
server->lostrqs = 0;
}
continue;
Expand Down
3 changes: 1 addition & 2 deletions radsecproxy.c
Expand Up @@ -1612,7 +1612,7 @@ void *clientwr(void *arg) {
laststatsrv = server->lastreply;

if (conf->pdef->connecter) {
if (!conf->pdef->connecter(server, server->dynamiclookuparg ? 5 : 0, "clientwr")) {
if (!conf->pdef->connecter(server, server->dynamiclookuparg ? 5 : 0, 0)) {
server->state = RSP_SERVER_STATE_FAILING;
if (server->dynamiclookuparg) {
debug(DBG_WARN, "%s: connect failed, sleeping %ds", __func__, ZZZ);
Expand All @@ -1627,7 +1627,6 @@ void *clientwr(void *arg) {
}
}
server->state = RSP_SERVER_STATE_CONNECTED;
server->conreset = 0;

for (;;) {
pthread_mutex_lock(&server->newrq_mutex);
Expand Down
2 changes: 1 addition & 1 deletion radsecproxy.h
Expand Up @@ -234,7 +234,7 @@ struct protodefs {
void (*setprotoopts)(struct commonprotoopts *);
char **(*getlistenerargs)();
void *(*listener)(void*);
int (*connecter)(struct server *, int, char *);
int (*connecter)(struct server *, int, int);
void *(*clientconnreader)(void*);
int (*clientradput)(struct server *, unsigned char *);
void (*addclient)(struct client *);
Expand Down
10 changes: 5 additions & 5 deletions tcp.c
Expand Up @@ -30,7 +30,7 @@
static void setprotoopts(struct commonprotoopts *opts);
static char **getlistenerargs();
void *tcplistener(void *arg);
int tcpconnect(struct server *server, int timeout, char * text);
int tcpconnect(struct server *server, int timeout, int reconnect);
void *tcpclientrd(void *arg);
int clientradputtcp(struct server *server, unsigned char *rad);
void tcpsetsrcres();
Expand Down Expand Up @@ -80,15 +80,15 @@ void tcpsetsrcres() {
AF_UNSPEC, NULL, protodefs.socktype);
}

int tcpconnect(struct server *server, int timeout, char *text) {
int tcpconnect(struct server *server, int timeout, int reconnect) {
struct timeval now, start;
int firsttry = 1;
time_t wait;
struct addrinfo *source = NULL;
struct list_node *entry;
struct hostportres *hp;

debug(DBG_DBG, "tcpconnect: called from %s", text);
debug(DBG_DBG, "tcpconnect: %s to %s", reconnect ? "reconnecting" : "initial connection", server->conf->name);
pthread_mutex_lock(&server->lock);

if (server->state == RSP_SERVER_STATE_CONNECTED)
Expand Down Expand Up @@ -143,7 +143,7 @@ int tcpconnect(struct server *server, int timeout, char *text) {
server->lostrqs = 0;
pthread_mutex_unlock(&server->lock);
pthread_mutex_lock(&server->newrq_mutex);
server->conreset = 1;
server->conreset = reconnect;
pthread_cond_signal(&server->newrq_cond);
pthread_mutex_unlock(&server->newrq_mutex);

Expand Down Expand Up @@ -244,7 +244,7 @@ void *tcpclientrd(void *arg) {
if (!buf) {
if (server->dynamiclookuparg)
break;
tcpconnect(server, 0, "tcpclientrd");
tcpconnect(server, 0, 1);
continue;
}

Expand Down
10 changes: 5 additions & 5 deletions tls.c
Expand Up @@ -30,7 +30,7 @@
static void setprotoopts(struct commonprotoopts *opts);
static char **getlistenerargs();
void *tlslistener(void *arg);
int tlsconnect(struct server *server, int timeout, char *text);
int tlsconnect(struct server *server, int timeout, int reconnect);
void *tlsclientrd(void *arg);
int clientradputtls(struct server *server, unsigned char *rad);
void tlssetsrcres();
Expand Down Expand Up @@ -92,7 +92,7 @@ static void cleanup_connection(struct server *server) {
server->ssl = NULL;
}

int tlsconnect(struct server *server, int timeout, char *text) {
int tlsconnect(struct server *server, int timeout, int reconnect) {
struct timeval now, start;
time_t wait;
int firsttry = 1;
Expand All @@ -105,7 +105,7 @@ int tlsconnect(struct server *server, int timeout, char *text) {
struct list_node *entry;
struct hostportres *hp;

debug(DBG_DBG, "tlsconnect: called from %s", text);
debug(DBG_DBG, "tlsconnect: %s to %s", reconnect ? "reconnecting" : "initial connection", server->conf->name);
pthread_mutex_lock(&server->lock);
if (server->state == RSP_SERVER_STATE_CONNECTED)
server->state = RSP_SERVER_STATE_RECONNECTING;
Expand Down Expand Up @@ -211,7 +211,7 @@ int tlsconnect(struct server *server, int timeout, char *text) {
server->lostrqs = 0;
pthread_mutex_unlock(&server->lock);
pthread_mutex_lock(&server->newrq_mutex);
server->conreset = 1;
server->conreset = reconnect;
pthread_cond_signal(&server->newrq_cond);
pthread_mutex_unlock(&server->newrq_mutex);
if (source) freeaddrinfo(source);
Expand Down Expand Up @@ -322,7 +322,7 @@ void *tlsclientrd(void *arg) {
debug (DBG_WARN, "tlsclientrd: server %s did not respond, closing connection.", server->conf->name);
if (server->dynamiclookuparg)
break;
tlsconnect(server, 0, "tlsclientrd");
tlsconnect(server, 0, 1);
}
if (server->dynamiclookuparg) {
gettimeofday(&now, NULL);
Expand Down

0 comments on commit c17ef97

Please sign in to comment.