summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/lib/libtls/tls.c4
-rw-r--r--src/lib/libtls/tls_config.c11
-rw-r--r--src/lib/libtls/tls_internal.h5
3 files changed, 16 insertions, 4 deletions
diff --git a/src/lib/libtls/tls.c b/src/lib/libtls/tls.c
index bf1d9da81e..46ed8180d1 100644
--- a/src/lib/libtls/tls.c
+++ b/src/lib/libtls/tls.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls.c,v 1.82 2018/11/29 14:24:23 tedu Exp $ */ 1/* $OpenBSD: tls.c,v 1.83 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 *
@@ -256,7 +256,9 @@ tls_configure(struct tls *ctx, struct tls_config *config)
256 if (config == NULL) 256 if (config == NULL)
257 config = tls_config_default; 257 config = tls_config_default;
258 258
259 pthread_mutex_lock(&config->mutex);
259 config->refcount++; 260 config->refcount++;
261 pthread_mutex_unlock(&config->mutex);
260 262
261 tls_config_free(ctx->config); 263 tls_config_free(ctx->config);
262 264
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) {
diff --git a/src/lib/libtls/tls_internal.h b/src/lib/libtls/tls_internal.h
index e1a858d4de..3842439d58 100644
--- a/src/lib/libtls/tls_internal.h
+++ b/src/lib/libtls/tls_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_internal.h,v 1.73 2018/11/06 20:34:54 jsing Exp $ */ 1/* $OpenBSD: tls_internal.h,v 1.74 2019/04/01 15:58:02 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> 3 * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org>
4 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 4 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
@@ -19,6 +19,8 @@
19#ifndef HEADER_TLS_INTERNAL_H 19#ifndef HEADER_TLS_INTERNAL_H
20#define HEADER_TLS_INTERNAL_H 20#define HEADER_TLS_INTERNAL_H
21 21
22#include <pthread.h>
23
22#include <arpa/inet.h> 24#include <arpa/inet.h>
23#include <netinet/in.h> 25#include <netinet/in.h>
24 26
@@ -75,6 +77,7 @@ struct tls_ticket_key {
75struct tls_config { 77struct tls_config {
76 struct tls_error error; 78 struct tls_error error;
77 79
80 pthread_mutex_t mutex;
78 int refcount; 81 int refcount;
79 82
80 char *alpn; 83 char *alpn;