threads/103975: Implicit loading/unloading of libpthread.so may crash user processes

John Baldwin john at baldwin.cx
Thu Oct 5 06:14:54 PDT 2006


On Wednesday 04 October 2006 09:56, KUROSAWA at freebsd.org, Takahiro wrote:
> 
> >Number:         103975
> >Category:       threads
> >Synopsis:       Implicit loading/unloading of libpthread.so may crash user processes
> >Confidential:   no
> >Severity:       serious
> >Priority:       medium
> >Responsible:    freebsd-threads
> >State:          open
> >Quarter:        
> >Keywords:       
> >Date-Required:
> >Class:          sw-bug
> >Submitter-Id:   current-users
> >Arrival-Date:   Wed Oct 04 14:00:50 GMT 2006
> >Closed-Date:
> >Last-Modified:
> >Originator:     KUROSAWA, Takahiro
> >Release:        6.2-PRERELEASE
> >Organization:
> >Environment:
> FreeBSD cube.nodomain.noroot 6.2-PRERELEASE FreeBSD 6.2-PRERELEASE #13: Fri Sep 29 14:34:05 JST 2006     kurosawa at cube.nodomain.noroot:/usr/obj/usr/src/sys/CUBE  i386
> 
> >Description:
> A program (described as ProgA below) gets SIGSEGV if following conditions
> are met:
> - ProgA dlopen()s and dlclose()s a shared object (ShobjB)
> - ProgA doesn't link libpthread.so
> - ShbjB dynamically links libpthread.so
>   (libpthread.so will be loaded when ProgA dlopen()s ShobjB)
> - ProgA calls functions like gethostbyname() that uses __thr_jtable
>   (in src/lib/libc/gen/_pthread_stubs.c) after unloading ShobjB.
> 
> The problem is that function pointers in __thr_jtable still point to functions
> in libpthread.so after libpthread.so is unloaded from ProgA's memory space.

Actually, I wonder if it should be allowed to unload at all.  On 4.x at work
we ran into an issue with the linuxthreads library loading, setting _is_threaded,
then unloading with a malloc() occurring during the destructors resolving a
_spinlock() weak symbol, then after the libraries were completely unloaded, the
next malloc() blew up when _spinlock() pointed off into space.  Hmm, this specific
condition is handled I think since __isthreaded in 6.x libpthread isn't set until
you do pthread_create() which at that point means a symbol is resolved, and the
library won't be unloaded (I think).  Hmm, maybe not since that doesn't guarantee
that libc depends on libpthread (that is what keeps it from being unloaded IIRC).
So, maybe when the library sets __isthreaded it should call one of the libc
functions (like malloc) to force one of the weak symbols to be resolved so it
isn't unloaded.

> To fix the problem, a function that has __attribute__((destructor))
> in libpthread should probably be implemented in order to recover
> the initial state before unloading.

I'm not sure you can recover the state actually, hence why I think maybe we
should make it so that libpthread doesn't unload once it has been loaded.

-- 
John Baldwin


More information about the freebsd-threads mailing list