Skip to content

Commit

Permalink
1) Fixed a problem in AES-MT where it would crash if the stack size f…
Browse files Browse the repository at this point in the history
…or the

   threads isn't large enough. In Linux it's 8MB (which oveersized) and in
   things like Alpine its (128k). Set it to 1MB.
2) Added a routine that will fix issues when it's being built under Alpine. This
   allows for some stack metrics to be gathered. This changed metrics.h metrics.c and
   configure.ac. The change in configure.ac is to automatically check if the application
   is being built under Alpine. This reads /etc/os-release and parses out the OS ID. Right
   now the only test in the case statement is for Alpine but now we have a system if we need
   more stuff like this.
3) minor change to a typedef in the CC20 fastXOR. Should be a little more portable.
  • Loading branch information
rapier1 committed Jul 25, 2023
1 parent 58388ff commit c2ae55c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cipher-chachapoly-libcrypto-mt.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ fastXOR(u_char *dest, const u_char *src, const u_char *keystream, u_int len)

/* XXX: this was __uint128_t but that was causing unaligned load errors.
* this works but we need to explore it more. */
typedef __uint32_t chunk;
typedef uint32_t chunk;
size_t i;
for (i=0; i < (len / sizeof(chunk)); i++)
((chunk *)dest)[i]=((chunk *)src)[i]^((chunk *)keystream)[i];
Expand Down
11 changes: 9 additions & 2 deletions cipher-ctr-mt-functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,17 @@ int aes_mt_start_threads(void *vevp_ctx, const u_char *key,
aes_mt_ctx->ridx = 0;
aes_mt_ctx->struct_id = global_struct_id++;

/* Start threads */
/* Start threads. Make sure we have enough stack space (under alpine)
* and aren't using more than we need (linux). This can be as low as
* 512KB but that's a minimum. 1024KB gives us a little headroom if we
* need it */
#define STACK_SIZE (1024 * 1024)
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, STACK_SIZE);
for (int i = 0; i < cipher_threads; i++) {
pthread_rwlock_wrlock(&aes_mt_ctx->tid_lock);
if (pthread_create(&aes_mt_ctx->tid[i], NULL, thread_loop, aes_mt_ctx) != 0)
if (pthread_create(&aes_mt_ctx->tid[i], &attr, thread_loop, aes_mt_ctx) != 0)
fatal ("AES-CTR MT Could not create thread in %s", __func__);
else {
aes_mt_ctx->id[i] = i;
Expand Down
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,15 @@ AC_CHECK_FUNCS([getpgrp],[
)
])

# Look for OS Type (alpine, fedora, bsd, etc)
# Only need this for alpine at the moment it may be useful in the future
os_type=$(grep '^ID=' /etc/os-release | awk -F "=" '{print $2}')
AC_MSG_CHECKING([OS type: "$os_type"])
case "$os_type" in
alpine) AC_DEFINE([__alpine__], [1], [Alpine Linux])
;;
esac

# Search for OpenSSL
saved_CPPFLAGS="$CPPFLAGS"
saved_LDFLAGS="$LDFLAGS"
Expand Down
5 changes: 3 additions & 2 deletions metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#include "metrics.h"
#include "ssherr.h"
#include <stdlib.h>
#ifdef __linux__
#include <stdio.h>
#if defined(__linux__) && !defined(__alpine__)
#include <linux/version.h>
#endif

Expand All @@ -45,7 +46,7 @@ metrics_write_binn_object(struct tcp_info *data, struct binn_struct *binnobj) {
* on non linux systems we set the kernel version to 0
* which will get us the base set of metrics from netinet/tcp.h
*/
#ifdef __linux__
#if (defined __linux__) && !defined(__alpine__)
binn_object_set_uint32(binnobj, "kernel_version", LINUX_VERSION_CODE);
#else
binn_object_set_uint32(binnobj, "kernel_version", 0);
Expand Down
4 changes: 2 additions & 2 deletions metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
/* linux, freebsd, and netbsd have tcp_info structs.
* I don't know about other systems so we disable this
* functionality for them */
#if defined __linux__ || defined __FreeBSD__ || defined __NetBSD__
#if defined __linux__ || defined __FreeBSD__ || defined __NetBSD__ && !defined(__alpine__)
#define TCP_INFO 1
#if defined __linux__
#if defined __linux__ && !defined(__alpine__)
#include <linux/tcp.h>
#else
#include <netinet/tcp.h>
Expand Down

0 comments on commit c2ae55c

Please sign in to comment.