Skip to content

Commit 5c34a2d

Browse files
committed
fix race condition and potential crash in curl usage; release 1.4.4.1
in oauth2_url_encode / oauth2_url_decode see OpenIDC/mod_oauth2#27; thanks @rtitle Signed-off-by: Hans Zandbelt <hans.zandbelt@zmartzone.eu>
1 parent dd59540 commit 5c34a2d

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ reporting bugs, providing fixes, suggesting useful features or other:
1010
Alexander Bokovoy <https://github.com/abbra>
1111
Niebardzo <https://github.com/niebardzo>
1212
Mikael Broadfoot <https://github.com/broadfootmi>
13+
Robert Title <https://github.com/rtitle>

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
03/03/2022
2+
- fix race condition and potential crash in curl usage in oauth2_url_decode
3+
see zmartzone/mod_oauth2#27; thanks @rtitle
4+
- release 1.4.4.1
5+
16
12/23/2021
27
- allow deprecated declarations to build with OpenSSL 3.0; see #31
38
- release 1.4.4

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AC_INIT([liboauth2],[1.4.4],[hans.zandbelt@zmartzone.eu])
1+
AC_INIT([liboauth2],[1.4.4.1],[hans.zandbelt@zmartzone.eu])
22

33
AM_INIT_AUTOMAKE([foreign no-define subdir-objects])
44
AC_CONFIG_MACRO_DIR([m4])

src/cache.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,11 @@ static const char *_oauth_cache_get_enc_key(oauth2_log_t *log,
273273
cache->enc_key = oauth2_strdup(passphrase);
274274
} else {
275275
// if (oauth2_jose_hash_bytes(log,
276-
//passphrase_hash_algo, (const unsigned char *)passphrase,
277-
// strlen(passphrase),
278-
//&cache->enc_key, &enc_key_len) == false) {
276+
// passphrase_hash_algo,
277+
// (const unsigned char *)passphrase,
278+
// strlen(passphrase),
279+
//&cache->enc_key,
280+
//&enc_key_len) == false) {
279281
if (oauth2_jose_hash2s(log, passphrase_hash_algo, passphrase,
280282
&cache->enc_key) == false) {
281283
oauth2_error(

src/util.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <ctype.h>
2525
#include <string.h>
2626

27+
#include "oauth2/ipc.h"
2728
#include "oauth2/log.h"
2829
#include "oauth2/mem.h"
2930
#include "oauth2/util.h"
@@ -40,16 +41,20 @@
4041
#include "cfg_int.h"
4142

4243
static CURL *_s_curl = NULL;
43-
static oauth2_uint_t _curl_refcount = 0;
44+
static oauth2_ipc_mutex_t *_curl_mutex = NULL;
4445

4546
oauth2_log_t *oauth2_init(oauth2_log_level_t level, oauth2_log_sink_t *sink)
4647
{
48+
oauth2_log_t *log = NULL;
4749
ERR_load_crypto_strings();
4850
OpenSSL_add_all_algorithms();
4951
// TODO: align flags/call with memory initialization in mem.c
5052
// possibly providing alloc funcs as part of init?
5153
curl_global_init(CURL_GLOBAL_ALL);
52-
return oauth2_log_init(level, sink);
54+
log = oauth2_log_init(level, sink);
55+
_curl_mutex = oauth2_ipc_mutex_init(log);
56+
oauth2_ipc_mutex_post_config(log, _curl_mutex);
57+
return log;
5358
}
5459

5560
void oauth2_shutdown(oauth2_log_t *log)
@@ -61,6 +66,10 @@ void oauth2_shutdown(oauth2_log_t *log)
6166
curl_easy_cleanup(_s_curl);
6267
_s_curl = NULL;
6368
}
69+
if (_curl_mutex != NULL) {
70+
oauth2_ipc_mutex_free(log, _curl_mutex);
71+
_curl_mutex = NULL;
72+
}
6473
curl_global_cleanup();
6574
EVP_cleanup();
6675
ERR_free_strings();
@@ -243,23 +252,19 @@ int oauth2_strnenvcmp(const char *a, const char *b, int len)
243252

244253
static CURL *oauth2_curl_init(oauth2_log_t *log)
245254
{
255+
oauth2_ipc_mutex_lock(log, _curl_mutex);
246256
if (_s_curl == NULL) {
247257
_s_curl = curl_easy_init();
248258
if (_s_curl == NULL) {
249259
oauth2_error(log, "curl_easy_init() error");
250260
}
251261
}
252-
_curl_refcount++;
253262
return _s_curl;
254263
}
255264

256-
static void oauth2_curl_free(CURL *curl)
265+
static void oauth2_curl_free(oauth2_log_t *log, CURL *curl)
257266
{
258-
_curl_refcount--;
259-
if ((_curl_refcount == 0) && (_s_curl)) {
260-
curl_easy_cleanup(_s_curl);
261-
_s_curl = NULL;
262-
}
267+
oauth2_ipc_mutex_unlock(log, _curl_mutex);
263268
}
264269

265270
char *oauth2_url_encode(oauth2_log_t *log, const char *src)
@@ -290,7 +295,7 @@ char *oauth2_url_encode(oauth2_log_t *log, const char *src)
290295
if (rc)
291296
curl_free(rc);
292297
if (curl)
293-
oauth2_curl_free(curl);
298+
oauth2_curl_free(log, curl);
294299

295300
oauth2_debug(log, "leave: %s", dst);
296301

@@ -340,7 +345,7 @@ char *oauth2_url_decode(oauth2_log_t *log, const char *src)
340345
if (replaced)
341346
oauth2_mem_free(replaced);
342347
if (curl)
343-
oauth2_curl_free(curl);
348+
oauth2_curl_free(log, curl);
344349

345350
oauth2_debug(log, "leave: %s", dst);
346351

0 commit comments

Comments
 (0)