Skip to content

Commit

Permalink
Land #961
Browse files Browse the repository at this point in the history
  • Loading branch information
botovq committed Dec 12, 2023
2 parents 2e88c74 + c54bd8b commit f7bed14
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
37 changes: 19 additions & 18 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -767,13 +767,13 @@ endif()

if(NOT HAVE_ASPRINTF)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/bsd-asprintf.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} asprintf)
set(EXTRA_EXPORT ${EXTRA_EXPORT} vasprintf)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_asprintf)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_vasprintf)
endif()

if(NOT HAVE_FREEZERO)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/freezero.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} freezero)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_freezero)
endif()

if(NOT HAVE_GETOPT)
Expand All @@ -799,46 +799,46 @@ endif()

if(NOT HAVE_REALLOCARRAY)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/reallocarray.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} reallocarray)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_reallocarray)
endif()

if(NOT HAVE_RECALLOCARRAY)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/recallocarray.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} recallocarray)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_recallocarray)
endif()

if(NOT HAVE_STRCASECMP)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/strcasecmp.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} strcasecmp)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_strcasecmp)
endif()

if(NOT HAVE_STRLCAT)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/strlcat.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} strlcat)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_strlcat)
endif()

if(NOT HAVE_STRLCPY)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/strlcpy.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} strlcpy)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_strlcpy)
endif()

if(NOT HAVE_STRNDUP)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/strndup.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} strndup)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_strndup)
if(NOT HAVE_STRNLEN)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/strnlen.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} strnlen)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_strnlen)
endif()
endif()

if(NOT HAVE_STRSEP)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/strsep.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} strsep)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_strsep)
endif()

if(NOT HAVE_STRTONUM)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/strtonum.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} strtonum)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_strtonum)
endif()

if(NOT HAVE_SYSLOG_R)
Expand All @@ -857,15 +857,15 @@ if(NOT HAVE_EXPLICIT_BZERO)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/explicit_bzero.c)
set_source_files_properties(compat/explicit_bzero.c PROPERTIES COMPILE_FLAGS -O0)
endif()
set(EXTRA_EXPORT ${EXTRA_EXPORT} explicit_bzero)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_explicit_bzero)
endif()

if(NOT HAVE_ARC4RANDOM_BUF)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/arc4random.c)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/arc4random_uniform.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} arc4random)
set(EXTRA_EXPORT ${EXTRA_EXPORT} arc4random_buf)
set(EXTRA_EXPORT ${EXTRA_EXPORT} arc4random_uniform)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_arc4random)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_arc4random_buf)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_arc4random_uniform)

if(NOT HAVE_GETENTROPY)
if(WIN32)
Expand All @@ -891,12 +891,12 @@ endif()

if(NOT HAVE_TIMINGSAFE_BCMP)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/timingsafe_bcmp.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} timingsafe_bcmp)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_timingsafe_bcmp)
endif()

if(NOT HAVE_TIMINGSAFE_MEMCMP)
set(CRYPTO_SRC ${CRYPTO_SRC} compat/timingsafe_memcmp.c)
set(EXTRA_EXPORT ${EXTRA_EXPORT} timingsafe_memcmp)
set(EXTRA_EXPORT ${EXTRA_EXPORT} libressl_timingsafe_memcmp)
endif()

if(NOT ENABLE_ASM)
Expand All @@ -919,6 +919,7 @@ foreach(SYM IN LISTS CRYPTO_UNEXPORT)
string(REPLACE "${SYM}\n" "" SYMS ${SYMS})
endforeach()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/crypto_p.sym ${SYMS})
# XXX should we still be doing this?

This comment has been minimized.

Copy link
@vszakats

vszakats Dec 13, 2023

Contributor

Just noticed this. My understanding is that we add these symbols to the list of functions we explicitly export from a Windows DLL, because a local app (e.g. openssl.exe), libssl or libtls need to call them when linking libcrypto dynamically. The downside is that someone might assume these are part of the LibreSSL API and call into them from user code.

To avoid giving that impression and making clear the "exported but internal" nature, prefixing them with an underscore might be useful.

For those functions that are only referenced from libcrypto itself, these exports would better be deleted.

This comment has been minimized.

Copy link
@botovq

botovq Dec 13, 2023

Author Contributor

Yes, I agree with all of this.

However, the autoconf builds stopped exporting the compat symbols in 54b31ce and the CMake build should have a matching change.

If we need to maintain prefixes for each lib and each app to avoid collisions, so be it.

This comment has been minimized.

Copy link
@vszakats

vszakats Dec 13, 2023

Contributor

Oh right, so the compat functions are compiled into each app and lib directly. In that case these exports are indeed unnecessary and can be deleted from CMake too.

This comment has been minimized.

Copy link
@botovq

botovq Dec 13, 2023

Author Contributor

Looks like there is more work needed: #971

This comment has been minimized.

Copy link
@vszakats

vszakats Dec 13, 2023

Contributor

It seems CMake is doing a partial job with collecting and linking the compat stubs, while autotools links each of them to every target. It also makes a libcompat static lib out of them.

I'm guessing the compat lib's sources would map to the ones collected in EXTRA_EXPORT.

if(EXTRA_EXPORT)
list(SORT EXTRA_EXPORT)
foreach(SYM IN LISTS EXTRA_EXPORT)
Expand Down
2 changes: 2 additions & 0 deletions include/compat/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

#ifndef HAVE_ASPRINTF
#include <stdarg.h>
#define vasprintf libressl_vasprintf
int vasprintf(char **str, const char *fmt, va_list ap);
#define asprintf libressl_asprintf
int asprintf(char **str, const char *fmt, ...);
#endif

Expand Down
10 changes: 10 additions & 0 deletions include/compat/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,36 @@
#include <stdint.h>

#ifndef HAVE_ARC4RANDOM_BUF
#define arc4random libressl_arc4random
uint32_t arc4random(void);
#define arc4random_buf libressl_arc4random_buf
void arc4random_buf(void *_buf, size_t n);
#define arc4random_uniform libressl_arc4random_uniform
uint32_t arc4random_uniform(uint32_t upper_bound);
#endif

#ifndef HAVE_FREEZERO
#define freezero libressl_freezero
void freezero(void *ptr, size_t sz);
#endif

#ifndef HAVE_GETPROGNAME
#define getprogname libressl_getprogname
const char * getprogname(void);
#endif

#ifndef HAVE_REALLOCARRAY
#define reallocarray libressl_reallocarray
void *reallocarray(void *, size_t, size_t);
#endif

#ifndef HAVE_RECALLOCARRAY
#define recallocarray libressl_recallocarray
void *recallocarray(void *, size_t, size_t, size_t);
#endif

#ifndef HAVE_STRTONUM
#define strtonum libressl_strtonum
long long strtonum(const char *nptr, long long minval,
long long maxval, const char **errstr);
#endif
Expand Down
11 changes: 11 additions & 0 deletions include/compat/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,54 @@
#endif

#ifndef HAVE_STRCASECMP
#define strcasecmp libressl_strcasecmp
int strcasecmp(const char *s1, const char *s2);
#define strncasecmp libressl_strncasecmp
int strncasecmp(const char *s1, const char *s2, size_t len);
#endif

#ifndef HAVE_STRLCPY
#define strlcpy libressl_strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz);
#endif

#ifndef HAVE_STRLCAT
#define strlcat libressl_strlcat
size_t strlcat(char *dst, const char *src, size_t siz);
#endif

#ifndef HAVE_STRNDUP
#define strndup libressl_strndup
char * strndup(const char *str, size_t maxlen);
/* the only user of strnlen is strndup, so only build it if needed */
#ifndef HAVE_STRNLEN
#define strnlen libressl_strnlen
size_t strnlen(const char *str, size_t maxlen);
#endif
#endif

#ifndef HAVE_STRSEP
#define strsep libressl_strsep
char *strsep(char **stringp, const char *delim);
#endif

#ifndef HAVE_EXPLICIT_BZERO
#define explicit_bzero libressl_explicit_bzero
void explicit_bzero(void *, size_t);
#endif

#ifndef HAVE_TIMINGSAFE_BCMP
#define timingsafe_bcmp libressl_timingsafe_bcmp
int timingsafe_bcmp(const void *b1, const void *b2, size_t n);
#endif

#ifndef HAVE_TIMINGSAFE_MEMCMP
#define timingsafe_memcmp libressl_timingsafe_memcmp
int timingsafe_memcmp(const void *b1, const void *b2, size_t len);
#endif

#ifndef HAVE_MEMMEM
#define memmem libressl_memmem
void * memmem(const void *big, size_t big_len, const void *little,
size_t little_len);
#endif
Expand Down
3 changes: 2 additions & 1 deletion m4/check-libc.m4
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AC_HEADER_RESOLV
# Check for general libc functions
AC_CHECK_FUNCS([asprintf freezero memmem])
AC_CHECK_FUNCS([readpassphrase reallocarray recallocarray])
AC_CHECK_FUNCS([strlcat strlcpy strndup strnlen strsep strtonum])
AC_CHECK_FUNCS([strcasecmp strlcat strlcpy strndup strnlen strsep strtonum])
AC_CHECK_FUNCS([timegm _mkgmtime timespecsub])
AC_CHECK_FUNCS([getopt getprogname syslog syslog_r])
AC_CACHE_CHECK([for getpagesize], ac_cv_func_getpagesize, [
Expand All @@ -30,6 +30,7 @@ AM_CONDITIONAL([HAVE_MEMMEM], [test "x$ac_cv_func_memmem" = xyes])
AM_CONDITIONAL([HAVE_READPASSPHRASE], [test "x$ac_cv_func_readpassphrase" = xyes])
AM_CONDITIONAL([HAVE_REALLOCARRAY], [test "x$ac_cv_func_reallocarray" = xyes])
AM_CONDITIONAL([HAVE_RECALLOCARRAY], [test "x$ac_cv_func_recallocarray" = xyes])
AM_CONDITIONAL([HAVE_STRCASECMP], [test "x$ac_cv_func_strcasecmp" = xyes])
AM_CONDITIONAL([HAVE_STRLCAT], [test "x$ac_cv_func_strlcat" = xyes])
AM_CONDITIONAL([HAVE_STRLCPY], [test "x$ac_cv_func_strlcpy" = xyes])
AM_CONDITIONAL([HAVE_STRNDUP], [test "x$ac_cv_func_strndup" = xyes])
Expand Down

0 comments on commit f7bed14

Please sign in to comment.