diff --git a/src/modules/logger/log_writer_file.cpp b/src/modules/logger/log_writer_file.cpp index 4329e730b945..73f600dc86d2 100644 --- a/src/modules/logger/log_writer_file.cpp +++ b/src/modules/logger/log_writer_file.cpp @@ -85,7 +85,7 @@ LogWriterFile::~LogWriterFile() } #if defined(PX4_CRYPTO) -bool LogWriterFile::init_logfile_encryption(const char *filename) +bool LogWriterFile::init_logfile_encryption(uint8_t **keydata, size_t *keydatasize) { if (_algorithm == CRYPTO_NONE) { _min_blocksize = 1; @@ -129,34 +129,6 @@ bool LogWriterFile::init_logfile_encryption(const char *filename) return false; } - /* Allocate space and get key + nonce */ - uint8_t *key = (uint8_t *)malloc(key_size + nonce_size); - - if (!key || - !rsa_crypto.get_encrypted_key( - _key_idx, - key, - &key_size, - _exchange_key_idx) || - !_crypto.get_nonce( - key + key_size, &nonce_size)) { - PX4_ERR("Can't get & encrypt the key"); - free(key); - rsa_crypto.close(); - return false; - } - - rsa_crypto.close(); - - // Write the encrypted key to the disk - int key_fd = ::open((const char *)filename, O_CREAT | O_WRONLY | O_DIRECT | O_SYNC, PX4_O_MODE_666); - - if (key_fd < 0) { - PX4_ERR("Can't open key file, errno: %d", errno); - free(key); - return false; - } - // write the header to the combined key exchange & cipherdata file struct ulog_key_header_s keyfile_header = { .magic = {'U', 'L', 'o', 'g', 'E', 'n', 'c'}, @@ -168,24 +140,33 @@ bool LogWriterFile::init_logfile_encryption(const char *filename) .initdata_size = (uint16_t)nonce_size }; - size_t hdr_sz = ::write(key_fd, (uint8_t *)&keyfile_header, sizeof(keyfile_header)); - size_t written = 0; + // Store keydata into buffers + *keydatasize = sizeof(keyfile_header) + key_size + nonce_size; + *keydata = (uint8_t *)malloc(*keydatasize); - if (hdr_sz == sizeof(keyfile_header)) { - // Header write succeeded, write the key - written = ::write(key_fd, key, key_size + nonce_size); + if (!*keydata) { + PX4_ERR("Can't allocate keydata"); + rsa_crypto.close(); + return false; } - // Free temporary memory allocations - free(key); - ::close(key_fd); + uint8_t *key = *keydata; + memcpy(key, (uint8_t *)&keyfile_header, sizeof(keyfile_header)); + key += sizeof(keyfile_header); - // Check that writing to the disk succeeded - if (written != key_size + nonce_size) { - PX4_ERR("Writing the encryption key to disk fail"); + if (!rsa_crypto.get_encrypted_key( + _key_idx, + key, + &key_size, + _exchange_key_idx) || + !_crypto.get_nonce( + key + key_size, &nonce_size)) { + PX4_ERR("Can't get & encrypt the key"); + rsa_crypto.close(); return false; } + rsa_crypto.close(); return true; } #endif // PX4_CRYPTO @@ -218,20 +199,32 @@ void LogWriterFile::start_log(LogType type, const char *filename) } #if PX4_CRYPTO - bool enc_init = init_logfile_encryption(filename); + uint8_t *keydata; + size_t keydatasize; + bool enc_init = init_logfile_encryption(&keydata, &keydatasize); if (!enc_init) { PX4_ERR("Failed to start encrypted logging"); _crypto.close(); + if (keydata) { + free(keydata); + } return; } -#endif - + if (_buffers[(int)type].start_log(filename, keydata, keydatasize)) { +#else if (_buffers[(int)type].start_log(filename)) { +#endif PX4_INFO("Opened %s log file: %s", log_type_str(type), filename); notify(); } + +#if PX4_CRYPTO + if (keydata) { + free(keydata); + } +#endif } int LogWriterFile::hardfault_store_filename(const char *log_file) @@ -628,19 +621,32 @@ size_t LogWriterFile::LogFileBuffer::get_read_ptr(void **ptr, bool *is_part) } } -bool LogWriterFile::LogFileBuffer::start_log(const char *filename) -{ #if defined(PX4_CRYPTO) - _fd = ::open(filename, O_APPEND | O_WRONLY, PX4_O_MODE_666); +bool LogWriterFile::LogFileBuffer::start_log(const char *filename, uint8_t *keydata, size_t keydatasize) #else - _fd = ::open(filename, O_CREAT | O_WRONLY, PX4_O_MODE_666); +bool LogWriterFile::LogFileBuffer::start_log(const char *filename) #endif +{ + _fd = ::open(filename, O_CREAT | O_WRONLY, PX4_O_MODE_666); if (_fd < 0) { PX4_ERR("Can't open log file %s, errno: %d", filename, errno); return false; } +#if defined(PX4_CRYPTO) + if (keydata) { + size_t keydata_sz = ::write(_fd, keydata, keydatasize); + // Check that writing to the disk succeeded + if (keydata_sz != keydatasize) { + PX4_ERR("Writing the encryption key to disk fail"); + ::close(_fd); + _fd = -1; + return false; + } + } +#endif + if (_buffer == nullptr) { _buffer = (uint8_t *) px4_cache_aligned_alloc(_buffer_size); diff --git a/src/modules/logger/log_writer_file.h b/src/modules/logger/log_writer_file.h index 4884f4472b2e..3d36384be2ff 100644 --- a/src/modules/logger/log_writer_file.h +++ b/src/modules/logger/log_writer_file.h @@ -170,9 +170,11 @@ class LogWriterFile LogFileBuffer(size_t log_buffer_size, perf_counter_t perf_write, perf_counter_t perf_fsync); ~LogFileBuffer(); - +#if defined(PX4_CRYPTO) + bool start_log(const char *filename, uint8_t *keydata, size_t keydatasize); +#else bool start_log(const char *filename); - +#endif void close_file(); void reset(); @@ -219,7 +221,7 @@ class LogWriterFile pthread_cond_t _cv; pthread_t _thread = 0; #if defined(PX4_CRYPTO) - bool init_logfile_encryption(const char *filename); + bool init_logfile_encryption(uint8_t **keydata, size_t *keydatasize); PX4Crypto _crypto; int _min_blocksize; px4_crypto_algorithm_t _algorithm;