Skip to content

Commit d24e7ce

Browse files
committed
Abuse __cxa_thread_atexit_impl to free TLS pointers.
1 parent 904ab56 commit d24e7ce

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
@@ -122,6 +122,19 @@ typedef union
122122
/* ARRAY_SIZE is used in tests. */
123123
#define ARRAY_SIZE(a_) (sizeof (a_) / sizeof ((a_)[0]))
124124

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

crypt-static.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ crypt (const char *key, const char *setting)
5151
return 0;
5252
#endif
5353
}
54+
#if HAVE_GLIBC_CXA_THREAD_ATEXIT_IMPL
55+
__cxa_thread_atexit_impl (free, nr_crypt_ctx, nr_crypt_ctx);
56+
#endif
5457
}
5558

5659
return crypt_r (key, setting, nr_crypt_ctx);

0 commit comments

Comments
 (0)