fix use of uninitialized memory with application-provided thread stacks
authorRich Felker <dalias@aerifal.cx>
Fri, 22 Aug 2014 18:05:10 +0000 (14:05 -0400)
committerRich Felker <dalias@aerifal.cx>
Fri, 22 Aug 2014 18:05:10 +0000 (14:05 -0400)
the subsequent code in pthread_create and the code which copies TLS
initialization images to the new thread's TLS space assume that the
memory provided to them is zero-initialized, which is true when it's
obtained by pthread_create using mmap. however, when the caller
provides a stack using pthread_attr_setstack, pthread_create cannot
make any assumptions about the contents. simply zero-filling the
relevant memory in this case is the simplest and safest fix.

src/thread/pthread_create.c

index c8c117b9e87adc15a35de7bef68ec5df204e8d41..1601614a32f36e99ca44d8d48e607686e74c4d25 100644 (file)
@@ -3,6 +3,7 @@
 #include "stdio_impl.h"
 #include "libc.h"
 #include <sys/mman.h>
+#include <string.h>
 
 static void dummy_0()
 {
@@ -175,6 +176,7 @@ int pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp
                if (need < size/8 && need < 2048) {
                        tsd = stack - __pthread_tsd_size;
                        stack = tsd - libc.tls_size;
+                       memset(stack, 0, need);
                } else {
                        size = ROUND(need);
                        guard = 0;