Skip to content

Commit cefa00f

Browse files
committed
logger: fix file open issue if crypto algorithm is disabled
move init_logfile_encryption() call after the buffer start_log() call to have log file already open while storing the header and key data to the beginning of the file.
1 parent 208db4c commit cefa00f

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

src/modules/logger/log_writer_file.cpp

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ LogWriterFile::~LogWriterFile()
8585
}
8686

8787
#if defined(PX4_CRYPTO)
88-
bool LogWriterFile::init_logfile_encryption(const char *filename)
88+
bool LogWriterFile::init_logfile_encryption(const LogType type)
8989
{
9090
if (_algorithm == CRYPTO_NONE) {
9191
_min_blocksize = 1;
@@ -148,16 +148,16 @@ bool LogWriterFile::init_logfile_encryption(const char *filename)
148148

149149
rsa_crypto.close();
150150

151-
// Write the encrypted key to the disk
152-
int key_fd = ::open((const char *)filename, O_CREAT | O_WRONLY | O_DIRECT | O_SYNC, PX4_O_MODE_666);
151+
// Write the encrypted key to the beginning of the opened log file
152+
int key_fd = _buffers[(int)type].fd();
153153

154154
if (key_fd < 0) {
155-
PX4_ERR("Can't open key file, errno: %d", errno);
155+
PX4_ERR("Log file not open for storing the key, errno: %d", errno);
156156
free(key);
157157
return false;
158158
}
159159

160-
// write the header to the combined key exchange & cipherdata file
160+
// write header and key to the beginning of the log file
161161
struct ulog_key_header_s keyfile_header = {
162162
.magic = {'U', 'L', 'o', 'g', 'E', 'n', 'c'},
163163
.hdr_ver = 1,
@@ -168,20 +168,14 @@ bool LogWriterFile::init_logfile_encryption(const char *filename)
168168
.initdata_size = (uint16_t)nonce_size
169169
};
170170

171-
size_t hdr_sz = ::write(key_fd, (uint8_t *)&keyfile_header, sizeof(keyfile_header));
172-
size_t written = 0;
173-
174-
if (hdr_sz == sizeof(keyfile_header)) {
175-
// Header write succeeded, write the key
176-
written = ::write(key_fd, key, key_size + nonce_size);
177-
}
171+
size_t written = ::write(key_fd, (uint8_t *)&keyfile_header, sizeof(keyfile_header));
172+
written += ::write(key_fd, key, key_size + nonce_size);
178173

179174
// Free temporary memory allocations
180175
free(key);
181-
::close(key_fd);
182176

183177
// Check that writing to the disk succeeded
184-
if (written != key_size + nonce_size) {
178+
if (written != sizeof(keyfile_header) + key_size + nonce_size) {
185179
PX4_ERR("Writing the encryption key to disk fail");
186180
return false;
187181
}
@@ -217,18 +211,21 @@ void LogWriterFile::start_log(LogType type, const char *filename)
217211
}
218212
}
219213

220-
#if PX4_CRYPTO
221-
bool enc_init = init_logfile_encryption(filename);
214+
if (_buffers[(int)type].start_log(filename)) {
222215

223-
if (!enc_init) {
224-
PX4_ERR("Failed to start encrypted logging");
225-
_crypto.close();
226-
return;
227-
}
216+
#if PX4_CRYPTO
217+
bool enc_init = init_logfile_encryption(type);
218+
219+
if (!enc_init) {
220+
PX4_ERR("Failed to start encrypted logging");
221+
_crypto.close();
222+
_buffers[(int)type]._should_run = false;
223+
_buffers[(int)type].close_file();
224+
return;
225+
}
228226

229227
#endif
230228

231-
if (_buffers[(int)type].start_log(filename)) {
232229
PX4_INFO("Opened %s log file: %s", log_type_str(type), filename);
233230
notify();
234231
}
@@ -630,11 +627,7 @@ size_t LogWriterFile::LogFileBuffer::get_read_ptr(void **ptr, bool *is_part)
630627

631628
bool LogWriterFile::LogFileBuffer::start_log(const char *filename)
632629
{
633-
#if defined(PX4_CRYPTO)
634-
_fd = ::open(filename, O_APPEND | O_WRONLY, PX4_O_MODE_666);
635-
#else
636630
_fd = ::open(filename, O_CREAT | O_WRONLY, PX4_O_MODE_666);
637-
#endif
638631

639632
if (_fd < 0) {
640633
PX4_ERR("Can't open log file %s, errno: %d", filename, errno);

src/modules/logger/log_writer_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class LogWriterFile
219219
pthread_cond_t _cv;
220220
pthread_t _thread = 0;
221221
#if defined(PX4_CRYPTO)
222-
bool init_logfile_encryption(const char *filename);
222+
bool init_logfile_encryption(const LogType type);
223223
PX4Crypto _crypto;
224224
int _min_blocksize;
225225
px4_crypto_algorithm_t _algorithm;

0 commit comments

Comments
 (0)