2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-22 01:51:51 +00:00

criu: fix log_keep_err signal deadlock

When using pr_err in signal handler, locking is used
in an unsafe manner. If another signal happens while holding the
lock, deadlock can happen.

To fix this, we can introduce mutex_trylock similar to
pthread_mutex_trylock that returns immediately. Due to the fact
that lock is used only for writing first_err, this change garantees
that deadlock cannot happen.

Fixes: #358

Signed-off-by: Ivan Pravdin <ipravdin.official@gmail.com>
This commit is contained in:
Ivan Pravdin 2025-03-22 19:31:02 -04:00 committed by Andrei Vagin
parent 25f7185202
commit 2a428d20ce
2 changed files with 11 additions and 4 deletions

View File

@ -132,11 +132,12 @@ static void log_note_err(char *msg)
* anyway, so it doesn't make much sense to try hard
* and optimize this out.
*/
mutex_lock(&first_err->l);
if (mutex_trylock(&first_err->l)) {
if (first_err->s[0] == '\0')
__strlcpy(first_err->s, msg, sizeof(first_err->s));
mutex_unlock(&first_err->l);
}
}
}
char *log_first_err(void)

View File

@ -2,6 +2,7 @@
#define __CR_COMMON_LOCK_H__
#include <stdint.h>
#include <stdbool.h>
#include <linux/futex.h>
#include <sys/time.h>
#include <limits.h>
@ -162,6 +163,11 @@ static inline void mutex_lock(mutex_t *m)
}
}
static inline bool mutex_trylock(mutex_t *m)
{
return atomic_inc_return(&m->raw) == 1;
}
static inline void mutex_unlock(mutex_t *m)
{
uint32_t c = 0;