diff options
Diffstat (limited to 'libs/libsodium/src/sodium/utils.c')
-rw-r--r-- | libs/libsodium/src/sodium/utils.c | 63 |
1 files changed, 48 insertions, 15 deletions
diff --git a/libs/libsodium/src/sodium/utils.c b/libs/libsodium/src/sodium/utils.c index b14b5151e7..2227bb8a63 100644 --- a/libs/libsodium/src/sodium/utils.c +++ b/libs/libsodium/src/sodium/utils.c @@ -4,16 +4,23 @@ #include <assert.h>
#include <errno.h>
#include <limits.h>
-#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#if defined(HAVE_RAISE) && !defined(__wasm__)
+# include <signal.h>
+#endif
+
#ifdef HAVE_SYS_MMAN_H
# include <sys/mman.h>
#endif
+#ifdef HAVE_SYS_PARAM_H
+# include <sys/param.h>
+#endif
+
#ifdef _WIN32
# include <windows.h>
# include <wincrypt.h>
@@ -43,7 +50,10 @@ void *alloca (size_t); #endif
#include "core.h"
+#include "crypto_generichash.h"
+#include "crypto_stream.h"
#include "randombytes.h"
+#include "private/common.h"
#include "utils.h"
#ifndef ENOSYS
@@ -59,7 +69,11 @@ void *alloca (size_t); #define GARBAGE_VALUE 0xdb
#ifndef MAP_NOCORE
-# define MAP_NOCORE 0
+# ifdef MAP_CONCEAL
+# define MAP_NOCORE MAP_CONCEAL
+# else
+# define MAP_NOCORE 0
+# endif
#endif
#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
# define MAP_ANON MAP_ANONYMOUS
@@ -68,6 +82,7 @@ void *alloca (size_t); defined(HAVE_POSIX_MEMALIGN)
# define HAVE_ALIGNED_MALLOC
#endif
+
#if defined(HAVE_MPROTECT) && \
!(defined(PROT_NONE) && defined(PROT_READ) && defined(PROT_WRITE))
# undef HAVE_MPROTECT
@@ -81,7 +96,15 @@ void *alloca (size_t); # define MADV_DONTDUMP MADV_NOCORE
#endif
-static size_t page_size;
+#ifndef DEFAULT_PAGE_SIZE
+# ifdef PAGE_SIZE
+# define DEFAULT_PAGE_SIZE PAGE_SIZE
+# else
+# define DEFAULT_PAGE_SIZE 0x10000
+# endif
+#endif
+
+static size_t page_size = DEFAULT_PAGE_SIZE;
static unsigned char canary[CANARY_SIZE];
/* LCOV_EXCL_START */
@@ -100,9 +123,9 @@ _sodium_dummy_symbol_to_prevent_memzero_lto(void *const pnt, /* LCOV_EXCL_STOP */
void
-sodium_memzero(void *const pnt, const size_t len)
+sodium_memzero(void * const pnt, const size_t len)
{
-#ifdef _WIN32
+#if defined(_WIN32) && !defined(__CRT_INLINE)
SecureZeroMemory(pnt, len);
#elif defined(HAVE_MEMSET_S)
if (len > 0U && memset_s(pnt, (rsize_t) len, 0, (rsize_t) len) != 0) {
@@ -110,11 +133,15 @@ sodium_memzero(void *const pnt, const size_t len) }
#elif defined(HAVE_EXPLICIT_BZERO)
explicit_bzero(pnt, len);
+#elif defined(HAVE_MEMSET_EXPLICIT)
+ memset_explicit(pnt, 0, len);
#elif defined(HAVE_EXPLICIT_MEMSET)
explicit_memset(pnt, 0, len);
#elif HAVE_WEAK_SYMBOLS
- memset(pnt, 0, len);
- _sodium_dummy_symbol_to_prevent_memzero_lto(pnt, len);
+ if (len > 0U) {
+ memset(pnt, 0, len);
+ _sodium_dummy_symbol_to_prevent_memzero_lto(pnt, len);
+ }
# ifdef HAVE_INLINE_ASM
__asm__ __volatile__ ("" : : "r"(pnt) : "memory");
# endif
@@ -221,8 +248,8 @@ sodium_compare(const unsigned char *b1_, const unsigned char *b2_, size_t len) i--;
x1 = b1[i];
x2 = b2[i];
- gt |= ((x2 - x1) >> 8) & eq;
- eq &= ((x2 ^ x1) - 1) >> 8;
+ gt |= (((unsigned int) x2 - (unsigned int) x1) >> 8) & eq;
+ eq &= (((unsigned int) (x2 ^ x1)) - 1) >> 8;
}
return (int) (gt + gt + eq) - 1;
}
@@ -382,7 +409,7 @@ int _sodium_alloc_init(void)
{
#ifdef HAVE_ALIGNED_MALLOC
-# if defined(_SC_PAGESIZE)
+# if defined(_SC_PAGESIZE) && defined(HAVE_SYSCONF)
long page_size_ = sysconf(_SC_PAGESIZE);
if (page_size_ > 0L) {
page_size = (size_t) page_size_;
@@ -391,12 +418,14 @@ _sodium_alloc_init(void) SYSTEM_INFO si;
GetSystemInfo(&si);
page_size = (size_t) si.dwPageSize;
+# elif !defined(PAGE_SIZE)
+# warning Unknown page size
# endif
if (page_size < CANARY_SIZE || page_size < sizeof(size_t)) {
sodium_misuse(); /* LCOV_EXCL_LINE */
}
#endif
- randombytes_buf(canary, sizeof canary);
+ randombytes_buf(canary, CANARY_SIZE);
return 0;
}
@@ -481,10 +510,14 @@ _mprotect_readwrite(void *ptr, size_t size) __attribute__((noreturn)) static void
_out_of_bounds(void)
{
-# ifdef SIGSEGV
+# if defined(HAVE_RAISE) && !defined(__wasm__)
+# ifdef SIGPROT
+ raise(SIGPROT);
+# elif defined(SIGSEGV)
raise(SIGSEGV);
-# elif defined(SIGKILL)
+# elif defined(SIGKILL)
raise(SIGKILL);
+# endif
# endif
abort(); /* not something we want any higher-level API to catch */
} /* LCOV_EXCL_LINE */
@@ -589,7 +622,7 @@ _sodium_malloc(const size_t size) memcpy(unprotected_ptr + unprotected_size, canary, sizeof canary);
# endif
_mprotect_noaccess(unprotected_ptr + unprotected_size, page_size);
- sodium_mlock(unprotected_ptr, unprotected_size);
+ (void) sodium_mlock(unprotected_ptr, unprotected_size); /* not a hard error in the context of sodium_malloc() */
canary_ptr =
unprotected_ptr + _page_round(size_with_canary) - size_with_canary;
user_ptr = canary_ptr + sizeof canary;
@@ -659,7 +692,7 @@ sodium_free(void *ptr) _out_of_bounds();
}
# endif
- sodium_munlock(unprotected_ptr, unprotected_size);
+ (void) sodium_munlock(unprotected_ptr, unprotected_size);
_free_aligned(base_ptr, total_size);
}
#endif /* HAVE_ALIGNED_MALLOC */
|