Make a function thread safe 73/282873/1
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 12 Oct 2022 22:26:30 +0000 (22:26 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 12 Oct 2022 22:32:55 +0000 (22:32 +0000)
commit9d4307aeb7f9a0f04fb145aea6ae9cf459814046
tree96bd0f4a8fc53f8f5d73dacab0b43e557d72b0aa
parent0e7163a5343abd5cd567bc1981c6b120f7e2981d
Make a function thread safe

The cynara_initialize() is NOT thread safe. The check_privilege() function
can be called from multiple threads at the same time.
To protect critical section, this patch locks and unlocks a mutex.

+------------------------------------------------------------------------------+
| ERROR: AddressSanitizer: heap-use-after-free on address 0xb2937f90 at        |
|       pc 0xb5bc6b83 bp 0xa5bf9f00 sp 0xa5bf9f20                              |
| READ of size 17 at 0xb2937f90 thread T13 (.NET ThreadPool)                   |
|   #0 0xb5bc6b80 in __interceptor_memcpy.part.0 (/usr/lib/libasan.so+0x71b80) |
|   #1 0xb4b28e5e in void std::__cxx11::basic_string<char,                     |
| std::char_traits<char>, std::allocator<char> >::                       |
|       _M_construct<char const*>(char const*, char const*,                    |
| std::forward_iterator_tag) [clone .constprop.0]                        |
|       (/lib/libcynara-commons.so.0+0x430d2e5e)                               |
|   #2 0xb4b3bd86 in Cynara::PluginManager::openPlugin(std::__cxx11::          |
|       basic_string<char, std::char_traits<char>, std::allocator<char> >      |
|       const&) (/lib/libcynara-commons.so.0+0x430e5d86)                       |
|   #3 0xb4b3c16e in Cynara::PluginManager::loadPlugins()                      |
| (/lib/libcynara-commons.so.0+0x430e616e)                               |
|   #4 0xb4ad09fc in Cynara::CapacityCache::CapacityCache(unsigned int)        |
| (/usr/lib/libcynara-client-commons.so.0+0x431b49fc)                    |
|   #5 0xb4b6ac9c in Cynara::Logic::Logic(Cynara::Configuration const&)        |
| (/lib/libcynara-client.so.0+0x431d2c9c)                                |
|   #6 0xb4b6de26 in std::_Function_handler<int (),                            |
| cynara_initialize::{lambda()#1}>::_M_invoke(std::_Any_data const&)     |
| (/lib/libcynara-client.so.0+0x431d5e26)                                |
|   #7 0xb4b3a302 in Cynara::tryCatch(std::function<int ()> const&)            |
| (/lib/libcynara-commons.so.0+0x430e4302)                               |
|   #8 0xb4b6e5da in cynara_initialize (/lib/libcynara-client.so.0+0x431d65da) |
|   #9 0xb36b6a6a in check_privilege                                           |
| (/lib/libcapi-appfw-package-manager.so.0+0x45044a6a)                   |
|   #10 0xb36bae0e in package_info_get_tep_name                                |
|       (/lib/libcapi-appfw-package-manager.so.0+0x45048e0e)                   |
|                                                                              |
| 0xb2937f90 is located 0 bytes inside of 18-byte region                       |
|    [0xb2937f90,0xb2937fa2)                                                   |
| freed by thread T41 here:                                                    |
|    #0 0xb5c1de94 in operator delete(void*) (/usr/lib/libasan.so+0xc8e94)     |
+------------------------------------------------------------------------------+

Change-Id: I831cf7265c8f7fe4591eb3d80975a59389aa113e
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/package_manager_internal.c