'OPENSSL Rand DRBG Andorid crashed when is used many times: A/libc: Fatal signal 7 (SIGBUS)

I'm trying the use the OpenSSL Rand library with DRBG in an android app, this library is implemented in native code of NDK. At starting, the OpenSSL works fine but in many times the app crashed and don't show any throw message. Here is the only error message that showwing:

A/libc: Fatal signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0xb1aedca6f2d64adf in tid 22101 (RenderThread), pid 22075 (android.example)

My code is the follow:

libnative.cpp

#include <jni.h>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <openssl/rand.h>
#include <openssl/rand_drbg.h>
#include <android/log.h>
#include <future>

#ifndef TAG
#define TAG "OpenSslApi"
#endif

#ifndef DEFAULT_VALUE_ERROR
#define DEFAULT_VALUE_ERROR 0
#endif

void thread_handler(union sigval sv) {}

extern "C"
JNIEXPORT void JNICALL
Java_com_android_random_OpenSslApi_initDrbgRandom(
  JNIEnv * env,
  jclass clazz) {
  RAND_DRBG * randDrbgInstance = RAND_DRBG_new(NID_aes_256_ctr, RAND_DRBG_FLAG_CTR_NO_DF, nullptr);
  RAND_DRBG_instantiate(randDrbgInstance, nullptr, 0);
  RAND_DRBG_set_reseed_time_interval(randDrbgInstance, 0);
  RAND_DRBG_set_reseed_interval(randDrbgInstance, 0);
}

std::pair < jint * , jint > generateRandomIntDrbg(jint * secureRandom, jint sizeKey) {
  jint myStatus = RAND_DRBG_bytes(
    RAND_DRBG_get0_public(),
    (unsigned char * ) secureRandom,
    sizeKey * sizeof(int)
  );
  return std::make_pair(secureRandom, myStatus);
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_android_random_OpenSslApi_intDrbgGenerateSecureRandom(
  JNIEnv * env,
  jclass clazz,
  jintArray empty_array,
  jint size_key) {

  struct sigevent sev {};
  timer_t timerid;
  memset( & sev, 0, sizeof(sev));
  sev.sigev_notify = SIGEV_THREAD;
  sev.sigev_notify_function = & thread_handler;
  sev.sigev_value.sival_ptr = & timerid;
  timer_create(CLOCK_MONOTONIC, & sev, & timerid);

  JavaVM * javaVm = nullptr;
  env -> GetJavaVM( & javaVm);
  javaVm -> AttachCurrentThread( & env, (void ** ) & env);

  jintArray array = env -> NewIntArray(size_key);
  if (array == nullptr) {
    return DEFAULT_VALUE_ERROR;
  }
  jint * secureRandomIntArray = env -> GetIntArrayElements(array, nullptr);
  if (secureRandomIntArray == nullptr) {
    return DEFAULT_VALUE_ERROR;
  }
  std::future < std::pair < jint * , jint >> futureIntRandom = std::async (generateRandomIntDrbg, secureRandomIntArray, size_key);
  std::pair < jint * , jint > result = futureIntRandom.get();
  jint * resultSecureRandom = std::get < jint * > (result);
  if (resultSecureRandom == nullptr) {
    return DEFAULT_VALUE_ERROR;
  }
  memcpy(secureRandomIntArray, empty_array, size_key);
  env -> ReleaseIntArrayElements(empty_array, secureRandomIntArray, 0);
  return std::get < jint > (result);
}

OpenSslApi.java

static {
      System.loadLibrary("libnative");
}

public OpenSslApi() {
      initDrbgRandom();
}

public static native void initDrbgRandom();

public static native int intDrbgGenerateSecureRandom(
            int[] emptyArray,
            final int sizeKey
    );

Thanks for either suggestions about the solution of this error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source