summaryrefslogtreecommitdiff
path: root/src/lib/libtls/tls_config.c
diff options
context:
space:
mode:
authorjsing <>2019-04-01 15:58:02 +0000
committerjsing <>2019-04-01 15:58:02 +0000
commit124072cef0c06581ae5bb8581be095c92b65e802 (patch)
tree968d23aa68db9b19e12bd8f87dac1683253ac622 /src/lib/libtls/tls_config.c
parent9552538f07c2d19c99c3229f037712f6dfa1c550 (diff)
downloadopenbsd-124072cef0c06581ae5bb8581be095c92b65e802.tar.gz
openbsd-124072cef0c06581ae5bb8581be095c92b65e802.tar.bz2
openbsd-124072cef0c06581ae5bb8581be095c92b65e802.zip
Add a mutex to guard reference counting for tls_config.
This makes libtls more friendly for multithreaded use - otherwise we can end up with incorrect refcounts and end up freeing when we should not be (or not freeing when we should be). ok beck@
Diffstat (limited to 'src/lib/libtls/tls_config.c')
-rw-r--r--src/lib/libtls/tls_config.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/lib/libtls/tls_config.c b/src/lib/libtls/tls_config.c
index 19dcc8b0d0..62361e6122 100644
--- a/src/lib/libtls/tls_config.c
+++ b/src/lib/libtls/tls_config.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_config.c,v 1.54 2019/03/27 11:12:10 tedu Exp $ */ 1/* $OpenBSD: tls_config.c,v 1.55 2019/04/01 15:58:02 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -20,6 +20,7 @@
20#include <ctype.h> 20#include <ctype.h>
21#include <errno.h> 21#include <errno.h>
22#include <fcntl.h> 22#include <fcntl.h>
23#include <pthread.h>
23#include <stdlib.h> 24#include <stdlib.h>
24#include <unistd.h> 25#include <unistd.h>
25 26
@@ -96,6 +97,7 @@ tls_config_new_internal(void)
96 if ((config->keypair = tls_keypair_new()) == NULL) 97 if ((config->keypair = tls_keypair_new()) == NULL)
97 goto err; 98 goto err;
98 99
100 config->mutex = PTHREAD_MUTEX_INITIALIZER;
99 config->refcount = 1; 101 config->refcount = 1;
100 config->session_fd = -1; 102 config->session_fd = -1;
101 103
@@ -149,11 +151,16 @@ void
149tls_config_free(struct tls_config *config) 151tls_config_free(struct tls_config *config)
150{ 152{
151 struct tls_keypair *kp, *nkp; 153 struct tls_keypair *kp, *nkp;
154 int refcount;
152 155
153 if (config == NULL) 156 if (config == NULL)
154 return; 157 return;
155 158
156 if (--config->refcount > 0) 159 pthread_mutex_lock(&config->mutex);
160 refcount = --config->refcount;
161 pthread_mutex_unlock(&config->mutex);
162
163 if (refcount > 0)
157 return; 164 return;
158 165
159 for (kp = config->keypair; kp != NULL; kp = nkp) { 166 for (kp = config->keypair; kp != NULL; kp = nkp) {