italia / cie-middleware-linux

Middleware della CIE (Carta di Identità Elettronica) per Linux
BSD 3-Clause "New" or "Revised" License
39 stars 12 forks source link

Double free for Log #40

Closed alevesely closed 3 months ago

alevesely commented 2 years ago

Eseguendo il test, anche dando 20 come primo input, il programma esce abortendo a causa di un double free. Si tratta di CLog::~CLog() come si vede lanciando il test sotto gdb:

Insert the test number:
20
---------------------------------------------------------
Bye Bye
*************************
*************************
---------------------------------------------------------
free(): double free detected in tcache 2

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
Download failed: Invalid argument.  Continuing without source file ./signal/../sysdeps/unix/sysv/linux/raise.c.
50  ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7bd7537 in __GI_abort () at abort.c:79
#2  0x00007ffff7c30768 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff7d3ee2d "%s\n") at ../sysdeps/posix/libc_fatal.c:155
#3  0x00007ffff7c37a5a in malloc_printerr (str=str@entry=0x7ffff7d41170 "free(): double free detected in tcache 2") at malloc.c:5347
#4  0x00007ffff7c39055 in _int_free (av=0x7ffff7d70b80 <main_arena>, p=0x55555557bc70, have_lock=0) at malloc.c:4201
#5  0x00007ffff79e479f in CLog::~CLog (this=0x7ffff7a66020 <Log>, __in_chrg=<optimized out>) at ../cie-pkcs11/Util/log.cpp:120
#6  0x00007ffff7bf04d7 in __run_exit_handlers (status=0, listp=0x7ffff7d70718 <__exit_funcs>, run_list_atexit=run_list_atexit@entry=true, 
    run_dtors=run_dtors@entry=true) at exit.c:108
#7  0x00007ffff7bf067a in __GI_exit (status=<optimized out>) at exit.c:139
#8  0x00007ffff7bd8d11 in __libc_start_main (main=0x555555558c10 <main(int, char**)>, argc=2, argv=0x7fffffffe078, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe068) at ../csu/libc-start.c:342
#9  0x000055555555632a in _start ()

In effetti, la definizione CLog Log; è stata spostata in defines.h. Non mi è chiaro se gcc tenta di considerarlo un unico oggetto.

Forse dovrebbe essere dichiarato static cosicché ognuno abbia la sua copia di Log, oppure costruirlo come singleton in modo da condividere un'unica copia.

Peraltro, c'è un altro log.cpp in cie_sign_sdk/src/Util, leggermente diverso. C'è una ragione per tenerne due copie?

Già che ci sono, posso chiedere se questo repository viene usato anche per WIN32?

alevesely commented 2 years ago

È sufficiente mettere extern:

Index: cie-pkcs11/LOGGER/Logger.cpp
===================================================================
--- cie-pkcs11/LOGGER/Logger.cpp    (revision 89)
+++ cie-pkcs11/LOGGER/Logger.cpp    (working copy)
@@ -122,7 +122,7 @@
    }

    int log_level = m_Instance->getLogConfig();
-    printf("Lib log level: %d\n", log_level);
+    // printf("Lib log level: %d\n", log_level);

    if (log_level == LOG_STATUS_DISABLED) {
        m_Instance->disableLog();
Index: cie-pkcs11/Util/defines.h
===================================================================
--- cie-pkcs11/Util/defines.h   (revision 89)
+++ cie-pkcs11/Util/defines.h   (working copy)
@@ -54,7 +54,7 @@
 #define logParamBuf(p,l)
 #define logParamBufHide(p,l)

-CLog Log;
+extern CLog Log;

 #define init_func \
    CFuncCallInfo info(__FUNCTION__ ,Log);
    //OutputDebugString(fName);
Index: cie-pkcs11/Util/log.cpp
===================================================================
--- cie-pkcs11/Util/log.cpp (revision 89)
+++ cie-pkcs11/Util/log.cpp (working copy)
@@ -39,6 +39,8 @@
    LM_Module_Thread    // un file per modulo e per thread
 } LogMode = LM_Module;

+// definition
+CLog Log;

 void initLog(const char *moduleName, const char *iniFile,const char *version)
 {
Index: cie_sign_sdk/src/PCSC/APDU.h
===================================================================
--- cie_sign_sdk/src/PCSC/APDU.h    (revision 89)
+++ cie_sign_sdk/src/PCSC/APDU.h    (working copy)
@@ -1,5 +1,6 @@
 #pragma once
 #include <functional>
+#include <cstdint>

 class CToken;
 class APDU
mavoga commented 1 year ago

Questo problema causa anche sistematici crash all'uscita da diverse applicazioni che caricano la libreria libcie-pkcs11.so, fra cui LibreOffice e Okular: ho descritto nel dettaglio il problema su Forum Italia.

Non è quindi una combinazione se il problema è apparso con la release 1.4.2, che includeva proprio delle migliorie alla gestione dei log come descritto nella issue #27 .

aantetomaso commented 3 months ago

Risolta nella 1.4.3.9