ensure atomicity when logging pending message 3/3 (#20189)
fix use-after-free
Costa Tsaousis committed
Apr 27, 2025 at 13:52 UTC
de287be351a708d84101fa0d861c1882444ed4e4
1 file changed
+6
-7
src/libnetdata/log/nd_log.c
+6
-7
@@ -346,9 +346,9 @@ static void nd_logger(const char *file, const char *function, const unsigned lon
346
if(nd_log.sources[source].pending_msg && spinlock_trylock(&nd_log.sources[source].limits.spinlock)) {
347
// we have to check again if the pending message is still there
348
349
- bool do_it = false;
349
+ const char *pending_msg = nd_log.sources[source].pending_msg;
350
351
- if(nd_log.sources[source].pending_msg) {
351
+ if(pending_msg) {
352
nd_logger_unset_all_thread_fields();
353
354
thread_log_fields[NDF_TIMESTAMP_REALTIME_USEC].entry = (struct log_stack_entry){
@@ -372,7 +372,7 @@ static void nd_logger(const char *file, const char *function, const unsigned lon
372
thread_log_fields[NDF_MESSAGE].entry = (struct log_stack_entry){
373
.set = true,
374
.type = NDFT_TXT,
375
- .txt = nd_log.sources[source].pending_msg,
375
+ .txt = pending_msg,
376
};
377
378
thread_log_fields[NDF_MESSAGE_ID].entry = (struct log_stack_entry){
@@ -381,18 +381,17 @@ static void nd_logger(const char *file, const char *function, const unsigned lon
381
.uuid = nd_log.sources[source].pending_msgid,
382
};
383
384
- freez((void *)nd_log.sources[source].pending_msg);
384
nd_log.sources[source].pending_msg = NULL;
385
nd_log.sources[source].pending_msgid = NULL;
387
-
388
- do_it = true;
386
}
387
388
spinlock_unlock(&nd_log.sources[source].limits.spinlock);
389
393
- if(do_it)
390
+ if(pending_msg)
391
nd_logger_log_fields(spinlock, fp, false, priority, output, &nd_log.sources[source],
392
thread_log_fields, THREAD_FIELDS_MAX);
393
+
394
+ freez((void *)pending_msg);
395
}
396
397
errno_clear();