1
0
mirror of https://github.com/mariadb-corporation/libmarias3.git synced 2025-04-18 16:24:01 +03:00

detect libcurl linked openssl version a run time

dynamically resolve openssl symbols to avoid openssl run-time
dependency - libmarias3 does not use openssl and should not depend on it.
libcurl might.

this partially reverts commits 90c7698 and b9ffda8
This commit is contained in:
Sergei Golubchik 2020-05-03 10:02:58 +02:00
parent 8727cf74fd
commit 9a2f4194b6
5 changed files with 58 additions and 214 deletions

View File

@ -77,7 +77,6 @@ AX_ENDIAN
AX_HEX_VERSION([LIBMARIAS3],[$VERSION])
AC_SUBST([RPM_RELEASE],[1])
AX_CURL_OPENSSL_UNSAFE
AX_HARDEN_COMPILER_FLAGS
AC_DEFINE_UNQUOTED([C_COMPILER_VENDOR],["$ax_cv_c_compiler_vendor"],[Compiler vendor])

View File

@ -1,63 +0,0 @@
# SYNOPSIS
#
# AX_CURL_OPENSSL_UNSAFE
#
# DESCRIPTION
#
# Checks to see if Curl is using a thread safe version of OpenSSL
#
# LICENSE
#
# Copyright (c) 2019 Andrew Hutchings <andrew.hutchings@mariadb.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 1
AC_DEFUN([AX_CURL_OPENSSL_UNSAFE],
[AC_PREREQ([2.63])dnl
AC_CACHE_CHECK([whether curl uses an OpenSSL which is not thread safe],
[ax_cv_curl_openssl_unsafe],
[AX_SAVE_FLAGS
LIBS="-lcurl $LIBS"
AC_LANG_PUSH([C])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>]],
[[curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
if (data->ssl_version)
{
if (strncmp(data->ssl_version, "OpenSSL", 7) != 0)
{
return EXIT_FAILURE;
}
if (data->ssl_version[8] == '0')
{
return EXIT_SUCCESS;
}
if ((data->ssl_version[8] == '1') && (data->ssl_version[10] == '0'))
{
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
else
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;]])],
[ax_cv_curl_openssl_unsafe=yes],
[ax_cv_curl_openssl_unsafe=no],
[ax_cv_curl_openssl_unsafe=no])
AC_LANG_POP
AX_RESTORE_FLAGS])
AC_MSG_CHECKING([checking for curl_openssl_unsafe])
AC_MSG_RESULT(["$ax_cv_curl_openssl_unsafe"])
AS_IF([test "x$ax_cv_curl_openssl_unsafe" = xyes],
[AC_DEFINE([HAVE_CURL_OPENSSL_UNSAFE],[1],[define if curl's OpenSSL is not thread safe])])
])

View File

@ -1,52 +0,0 @@
# ===========================================================================
# https://www.gnu.org/software/autoconf-archive/ax_restore_flags.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_RESTORE_FLAGS([namespace])
#
# DESCRIPTION
#
# Restore common compilation flags from temporary variables.
#
# Compilation flags includes: CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS, LIBS,
# OBJCFLAGS.
#
# By default these flags are restored to a global (empty) namespace, but
# user could restore from specific NAMESPACE by using
# AX_RESTORE_FLAGS(NAMESPACE) macro.
#
# Typical usage is like:
#
# AX_SAVE_FLAGS(mypackage)
# CPPFLAGS="-Imypackagespath ${CPPFLAGS}"
# dnl ... do some detection ...
# AX_RESTORE_FLAGS(mypackage)
#
# LICENSE
#
# Copyright (c) 2009 Filippo Giunchedi <filippo@esaurito.net>
# Copyright (c) 2011 The Board of Trustees of the Leland Stanford Junior University
# Copyright (c) 2011 Russ Allbery <rra@stanford.edu>
# Copyright (c) 2013 Bastien ROUCARIES <roucaries.bastien+autoconf@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 7
# save one flag in name space
AC_DEFUN([_AX_RESTORE_ONE_FLAG],[dnl
AS_VAR_PUSHDEF([_ax_restore_flag_var], [$2[]_$1[]_ax_save_flags])
AS_VAR_COPY($2[],_ax_restore_flag_var)
AS_VAR_POPDEF([_ax_restore_flag_var])
])
AC_DEFUN([AX_RESTORE_FLAGS], [dnl
m4_foreach([FLAG], dnl
[_AX_SAVE_FLAGS_LIST()], dnl
[_AX_RESTORE_ONE_FLAG([$1],FLAG)])
])

View File

@ -1,71 +0,0 @@
# ===========================================================================
# https://www.gnu.org/software/autoconf-archive/ax_save_flags.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_SAVE_FLAGS([NAMESPACE])
#
# DESCRIPTION
#
# Save common compilation flags into temporary variables.
#
# Compilation flags includes: CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS, LIBS,
# OBJCFLAGS.
#
# By default these flags are saved to a global (empty) namespace, but user
# could specify a specific NAMESPACE to AX_SAVE_FLAGS macro and latter
# restore it by using AX_RESTORE_FLAGS(NAMESPACE).
#
# AX_SAVE_FLAGS(mypackage)
# CPPFLAGS="-Imypackagespath ${CPPFLAGS}"
# dnl .. do some detection ...
# AX_RESTORE_FLAGS(mypackage)
#
# LICENSE
#
# Copyright (c) 2009 Filippo Giunchedi <filippo@esaurito.net>
# Copyright (c) 2011 The Board of Trustees of the Leland Stanford Junior University
# Copyright (c) 2011 Russ Allbery <rra@stanford.edu>
# Copyright (c) 2013 Bastien ROUCARIES <roucaries.bastien+autoconf@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 8
# list of flag to save
AC_DEFUN([_AX_SAVE_FLAGS_LIST],[dnl
[CCASFLAGS],dnl
[CFLAGS],dnl
[CPPFLAGS],dnl
[CXXFLAGS],dnl
[ERLCFLAGS],dnl
[FCFLAGS],dnl
[FCLIBS],dnl
[FFLAGS],dnl
[FLIBS],dnl
[GCJFLAGS],dnl
[JAVACFLAGS],dnl
[LDFLAGS],dnl
[LIBS],dnl
[OBJCFLAGS],dnl
[OBJCXXFLAGS],dnl
[UPCFLAGS],dnl
[VALAFLAGS]dnl
])
# save one flag in name space
AC_DEFUN([_AX_SAVE_ONE_FLAG],[
AS_VAR_PUSHDEF([_ax_save_flag_var], [$2[]_$1[]_ax_save_flags])
AS_VAR_COPY(_ax_save_flag_var, $2[])
AS_VAR_POPDEF([_ax_save_flag_var])
])
AC_DEFUN([AX_SAVE_FLAGS],[dnl
m4_foreach([FLAG], dnl
[_AX_SAVE_FLAGS_LIST()], dnl
[_AX_SAVE_ONE_FLAG([$1],FLAG)])
])

View File

@ -31,11 +31,14 @@ ms3_calloc_callback ms3_ccalloc = (ms3_calloc_callback)calloc;
/* Thread locking code for OpenSSL < 1.1.0 */
#ifdef HAVE_CURL_OPENSSL_UNSAFE
#include <openssl/crypto.h>
#include <dlfcn.h>
static pthread_mutex_t *mutex_buf = NULL;
#define CRYPTO_LOCK 1
static void (*openssl_set_id_callback)(unsigned long (*func)(void));
static void (*openssl_set_locking_callback)(void (*func)(int mode,int type, const char *file,int line));
static int (*openssl_num_locks)(void);
static void __attribute__((unused)) locking_function(int mode, int n, const char *file, int line)
static void locking_function(int mode, int n, const char *file, int line)
{
(void) file;
(void) line;
@ -45,11 +48,39 @@ static void __attribute__((unused)) locking_function(int mode, int n, const char
pthread_mutex_unlock(&(mutex_buf[n]));
}
static int curl_needs_openssl_locking()
{
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
if (data->ssl_version == NULL)
{
return 0;
}
if (strncmp(data->ssl_version, "OpenSSL", 7) != 0)
{
return 0;
}
if (data->ssl_version[8] == '0')
{
return 1;
}
if ((data->ssl_version[8] == '1') && (data->ssl_version[10] == '0'))
{
openssl_set_id_callback = dlsym(RTLD_DEFAULT, "CRYPTO_set_id_callback");
openssl_set_locking_callback = dlsym(RTLD_DEFAULT, "CRYPTO_set_locking_callback");
openssl_num_locks = dlsym(RTLD_DEFAULT, "CRYPTO_num_locks");
return openssl_set_id_callback != NULL &&
openssl_set_locking_callback != NULL &&
openssl_num_locks != NULL;
}
return 0;
}
static unsigned long __attribute__((unused)) id_function(void)
{
return ((unsigned long)pthread_self());
}
#endif
uint8_t ms3_library_init_malloc(ms3_malloc_callback m,
ms3_free_callback f, ms3_realloc_callback r,
@ -66,17 +97,18 @@ uint8_t ms3_library_init_malloc(ms3_malloc_callback m,
ms3_cstrdup = s;
ms3_ccalloc = c;
#ifdef HAVE_CURL_OPENSSL_UNSAFE
int i;
mutex_buf = ms3_cmalloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
if(mutex_buf)
if (curl_needs_openssl_locking())
{
for(i = 0; i < CRYPTO_num_locks(); i++)
pthread_mutex_init(&(mutex_buf[i]), NULL);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_locking_callback(locking_function);
int i;
mutex_buf = ms3_cmalloc(openssl_num_locks() * sizeof(pthread_mutex_t));
if(mutex_buf)
{
for(i = 0; i < openssl_num_locks(); i++)
pthread_mutex_init(&(mutex_buf[i]), NULL);
openssl_set_id_callback(id_function);
openssl_set_locking_callback(locking_function);
}
}
#endif
if (curl_global_init_mem(CURL_GLOBAL_DEFAULT, m, f, r, s, c))
{
@ -88,34 +120,33 @@ uint8_t ms3_library_init_malloc(ms3_malloc_callback m,
void ms3_library_init(void)
{
#ifdef HAVE_CURL_OPENSSL_UNSAFE
int i;
mutex_buf = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
if(mutex_buf)
if (curl_needs_openssl_locking())
{
for(i = 0; i < CRYPTO_num_locks(); i++)
pthread_mutex_init(&(mutex_buf[i]), NULL);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_locking_callback(locking_function);
int i;
mutex_buf = malloc(openssl_num_locks() * sizeof(pthread_mutex_t));
if(mutex_buf)
{
for(i = 0; i < openssl_num_locks(); i++)
pthread_mutex_init(&(mutex_buf[i]), NULL);
openssl_set_id_callback(id_function);
openssl_set_locking_callback(locking_function);
}
}
#endif
curl_global_init(CURL_GLOBAL_DEFAULT);
}
void ms3_library_deinit(void)
{
#ifdef HAVE_CURL_OPENSSL_UNSAFE
int i;
if (mutex_buf)
{
CRYPTO_set_id_callback(NULL);
CRYPTO_set_locking_callback(NULL);
for(i = 0; i < CRYPTO_num_locks(); i++)
openssl_set_id_callback(NULL);
openssl_set_locking_callback(NULL);
for(i = 0; i < openssl_num_locks(); i++)
pthread_mutex_destroy(&(mutex_buf[i]));
ms3_cfree(mutex_buf);
mutex_buf = NULL;
}
#endif
curl_global_cleanup();
}