Skip to content

Commit ebfc697

Browse files
committed
Abuse __cxa_thread_atexit_impl to free TLS pointers.
1 parent cf367db commit ebfc697

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

crypt-gensalt-static.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ crypt_gensalt (const char *prefix, unsigned long count,
3636
obuf = malloc (CRYPT_GENSALT_OUTPUT_SIZE);
3737
if (!obuf)
3838
return 0;
39+
#if HAVE_GLIBC_CXA_THREAD_ATEXIT_IMPL
40+
__cxa_thread_atexit_impl (free, obuf, obuf);
41+
#endif
3942
}
4043

4144
return crypt_gensalt_rn (prefix, count, rbytes, nrbytes,

crypt-port.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ typedef union
123123
/* ARRAY_SIZE is used in tests. */
124124
#define ARRAY_SIZE(a_) (sizeof (a_) / sizeof ((a_)[0]))
125125

126+
/* Thread local storage needs a way to be destructed, when the thread
127+
is terminated. Using a weak defined function from glibc >= 2.18
128+
is the only way we currently know about. */
129+
#if defined __GLIBC__ && ((__GLIBC__ == 2 && \
130+
defined __GLIBC_MINOR__ && __GLIBC_MINOR__ >= 18) || \
131+
__GLIBC__ >= 3)
132+
#define HAVE_GLIBC_CXA_THREAD_ATEXIT_IMPL 1
133+
extern int __cxa_thread_atexit_impl (void (*func) (void *), void *arg,
134+
void *d);
135+
#else
136+
#define HAVE_GLIBC_CXA_THREAD_ATEXIT_IMPL 0
137+
#endif
138+
126139
/* Provide a guaranteed way to erase sensitive memory at the best we
127140
can, given the possibilities of the system. */
128141
#if defined HAVE_MEMSET_S

crypt-static.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ crypt (const char *key, const char *setting)
4949
return 0;
5050
#endif
5151
}
52+
#if HAVE_GLIBC_CXA_THREAD_ATEXIT_IMPL
53+
__cxa_thread_atexit_impl (free, nr_crypt_ctx, nr_crypt_ctx);
54+
#endif
5255
}
5356

5457
return crypt_r (key, setting, nr_crypt_ctx);

0 commit comments

Comments
 (0)