Commit Graph

75 Commits

Author SHA1 Message Date
Patrick Steinhardt
038f0e1b4c global: reset global state on shutdown without threading
When threading is not enabled for libgit2, we keep global state
in a simple static variable. When libgit2 is shut down, we clean
up the global state by freeing the global state's dynamically
allocated memory. When libgit2 is built with threading, we
additionally free the thread-local storage and thus completely
remove the global state. In a non-threaded build, though, we
simply leave the global state as-is, which may result in an error
upon reinitializing libgit2.

Fix the issue by zeroing out the variable on a shutdown, thus
returning it to its initial state.
2016-11-02 08:53:52 +01:00
Patrick Steinhardt
59c6c2860a global: synchronize initialization and shutdown with pthreads
When trying to initialize and tear down global data structures
from different threads at once with `git_libgit2_init` and
`git_libgit2_shutdown`, we race around initializing data. While
we use `pthread_once` to assert that we only initilize data a
single time, we actually reset the `pthread_once_t` on the last
call to `git_libgit2_shutdown`. As resetting this variable is not
synchronized with other threads trying to access it, this is
actually racy when one thread tries to do a complete shutdown of
libgit2 while another thread tries to initialize it.

Fix the issue by creating a mutex which synchronizes `init_once`
and the library shutdown.
2016-11-01 14:30:47 +01:00
Edward Thomson
2381d9e490 mwindow: init mwindow files in git_libgit2_init 2016-08-04 10:30:48 -04:00
Patrick Steinhardt
aab266c932 threads: add platform-independent thread initialization function 2016-06-20 20:07:33 +02:00
Patrick Steinhardt
432af52b37 global: clean up crt only after freeing tls data
The thread local storage is used to hold some global state that
is dynamically allocated and should be freed upon exit. On
Windows, we clean up the C run-time right after execution of
registered shutdown callbacks and before cleaning up the TLS.

When we clean up the CRT, we also cause it to analyze for memory
leaks. As we did not free the TLS yet this will lead to false
positives.

Fix the issue by first freeing the TLS and cleaning up the CRT
only afterwards.
2016-06-07 12:55:17 +02:00
Edward Thomson
14cf05dae8 win32: clean up unused warnings in DllMain 2016-06-01 10:03:52 -05:00
Edward Thomson
ec5a43b60a Merge pull request #3699 from libgit2/cmn/win32-free-tls
win32: free thread-local data on thread exit
2016-03-18 06:37:41 -07:00
Carlos Martin Nieto
a177756b34 win32: free thread-local data on thread exit 2016-03-18 13:05:10 -07:00
Edward Thomson
967e073dca merge driver: correct global initialization 2016-03-17 11:06:00 -04:00
Dirkjan Bussink
fa72d6daf8 Setup better defaults for OpenSSL ciphers
This ensures that when using OpenSSL a safe default set of ciphers
is selected. This is done so that the client communicates securely
and we don't accidentally enable unsafe ciphers like RC4, or even
worse some old export ciphers.

Implements the first part of https://github.com/libgit2/libgit2/issues/3682
2016-03-14 12:07:13 +00:00
Carlos Martín Nieto
22f3d3aa6b ssh: initialize libssh2
We should have been doing this, but it initializes itself upon first
use, which works as long as nobody's doing concurrent network
operations. Initialize it on our init to make sure it's not getting
initialized concurrently.
2016-03-03 22:26:31 +01:00
Carlos Martín Nieto
bf127eec4a global: remove an unused variable 2016-02-19 13:24:41 +01:00
Edward Thomson
8a6d667763 global: make openssl registration like the rest 2016-02-08 16:30:39 -08:00
Edward Thomson
bad2702c43 global: refactor setup and cleanup
Move the common initialization and cleanup methods to reduce
unnecessary duplication.
2016-02-08 15:39:26 -08:00
Carlos Martín Nieto
de870533e0 settings: add a setter for a custom user-agent 2015-11-12 17:18:42 +01:00
Carlos Martín Nieto
f85fc367e0 error: store the error messages in a reusable buffer
Instead of allocating a brand new buffer for each error string we want
to store, we can use a per-thread buffer to store the error string and
re-use the underlying storage. We already use the buffer to format the
string, so this mostly makes that more direct.
2015-07-28 09:31:00 +02:00
joshaber
9830fbba05 Merge branch 'master' into fix-init-ordering 2015-07-22 11:33:18 -04:00
joshaber
cf198fdf2a Increment git__n_inits before doing init_once.
Fixes #3318.
2015-07-22 10:51:38 -04:00
Jeff Hostetler
93b4272895 Include stacktrace summary in memory leak output. 2015-06-29 16:39:11 -04:00
Tim Hentenaar
20f8edb7a5 global: Ensure we free our SSL context. 2015-06-08 09:38:50 +02:00
Carlos Martín Nieto
24e53d2fba Rename GIT_SSL to GIT_OPENSSL
This is what it's meant all along, but now we actually have multiple
implementations, it's clearer to use the name of the library.
2015-04-23 17:39:51 +02:00
Jeff Hostetler
06c985d864 Rename routine to free TLS data 2015-04-18 09:07:48 -04:00
Jeff Hostetler
d3fb7d93a6 Remove DllMain now that TLS data freed by threads 2015-04-17 10:04:01 -04:00
Jeff Hostetler
f5ffb40e2b Also fix leak of TLS data on main thread. 2015-04-17 09:58:09 -04:00
Jeff Hostetler
55c5f756d8 Attempt to fix Windows TLS memory leak. 2015-04-17 09:30:22 -04:00
Edward Thomson
8e851c1e8c libgit2_shutdown: free TLS data (win32)
Free TLS data on thread exit (win32)
2015-03-04 14:48:46 +01:00
Edward Thomson
83fe60fa1b libgit2_shutdown: clear err message on shutdown
Clear the error message on git_libgit2_shutdown for all versions of
the library (no threads and Win32 threads).  Drop the giterr_clear
in clar, as that shouldn't be necessary.
2015-03-04 14:48:46 +01:00
Leo Yang
3a8b69d13d Fix leak of TLS error message in shutdown (ptherad version) 2015-03-04 14:47:57 +01:00
Edward Thomson
73f0278e5d global: include sys/openssl.h for GIT_EXPORT of fn
The openssl setup function needs to be GIT_EXPORT'ed, be sure
to include the `sys/openssl.h` header so that it is appropriately
decorated as an export function.
2014-12-23 16:40:01 -06:00
Carlos Martín Nieto
263b1d6ed9 Make the OpenSSL locking function warnings more severe
Our git_openssl_set_locking() would ideally not exist. Make it clearer
that we provide it as a last resort and you should prefer anything else.
2014-12-12 15:46:57 +01:00
Carlos Martín Nieto
e79fbd9e8a Merge pull request #2743 from ethomson/init_val
init: return the number of initializations
2014-12-05 07:09:08 +01:00
Edward Thomson
6d91dc5351 init: return the number of initializations 2014-12-04 20:42:27 -05:00
Stefan Widgren
2d2cd625ea Add missing else directive
Add missing else directive to fix compiler warning: control reaches
end of non-void function
2014-12-03 21:01:42 +01:00
Ungureanu Marius
d6ecc31140 Plug possible leak in the openssl locks 2014-11-17 14:03:15 +01:00
Carlos Martín Nieto
799e22ea0c Rename git_threads_ to git_libgit2_
This describes their purpose better, as we now initialize ssl and some
other global stuff in there. Calling the init function is not something
which has been optional for a while now.
2014-11-08 23:46:39 +01:00
Edward Thomson
7bb639911b Merge pull request #2676 from libgit2/cmn/threading
Threading and crypto libraries
2014-11-06 10:25:23 -05:00
Jacques Germishuys
bc48bcdcb3 Make the Visual Studio compiler happy 2014-11-03 14:23:13 +02:00
Carlos Martín Nieto
fe6b51ae40 ssl: separate locking init from general init
Extract the lock-setting functions into their own, as we cannot assume
that it's ok for us to set this unconditionally.
2014-11-01 10:58:55 +01:00
Carlos Martín Nieto
e083657771 ssl: clear the OpenSSL locking function
We're freeing the memory which holds the locks so we must make sure that
the locking function doesn't try to use it.
2014-11-01 10:47:22 +01:00
Edward Thomson
50aae0007b global: clean up openssl_locks on shutdown 2014-10-26 22:27:52 -04:00
Carlos Martín Nieto
f0f9737094 ssl: dump the SSL ciphers in favour of TLS
All versions of SSL are considered deprecated now, so let's ask OpenSSl
to only use TLSv1. We still ask it to load those ciphers for
compatibility with servers which want to use an older hello but will use
TLS for encryption.

For good measure we also disable compression, which can be exploitable,
if the OpenSSL version supports it.
2014-10-23 15:56:29 +02:00
Carlos Martín Nieto
fdea219a86 global: free the error message when exiting a thread
When we free the global state at thread termination, we must also free
the error message in order not to leak the string once per thread.
2014-09-14 00:39:07 +02:00
Jacques Germishuys
f59a34d2e6 Only create openssl_locks if thread support is enabled 2014-07-13 16:23:17 +02:00
Carlos Martín Nieto
e6b0ae7a13 ssl: init only once without threads
The OpenSSL library-loading functions do not expect to be called
multiple times. Add a flag in the non-threaded libgit2 init so we only
call once.

This fixes #2446.
2014-06-30 09:29:54 +02:00
Vicent Marti
e93206e0f5 Merge pull request #2421 from libgit2/cmn/init-ssl-once
netops: init OpenSSL once under lock
2014-06-14 12:58:03 +02:00
Carlos Martín Nieto
081e76bac2 ssl: init everything all the time
Bring together all of the OpenSSL initialization to
git_threads_init() so it's together and doesn't need locks.

Moving it here also gives us libssh2 thread safety (when built against
openssl).
2014-06-12 16:58:25 +02:00
Carlos Martín Nieto
8f897b6f2f ssl: init also without threads 2014-06-12 14:50:08 +02:00
Carlos Martín Nieto
cf15ac8aa9 ssl: cargo-cult thread safety
OpenSSL's tests init everything in the main thread, so let's do that.
2014-06-12 14:37:03 +02:00
Carlos Martín Nieto
1d3364ac9d netops: init OpenSSL once under lock
The OpenSSL init functions are not reentrant, which means that running
multiple fetches in parallel can cause us to crash.

Use a mutex to init OpenSSL, and since we're adding this extra checks,
init it only once.
2014-06-11 20:54:42 +02:00
Philip Kelley
fb5917679d Win32: Fix object::cache::threadmania test on x64 2014-06-07 12:51:48 -04:00