__thread and fork() is a big no-no
It is not about caching tid, it is about ALL __thread variables not being cleared across fork. In this case the parent used a setfscreatecon, forked, then the child tried to use setfscreatecon! and found the cached version. But in kernel the child did not have a Sid set. Failure. __thread variables and fork() == bad stuff.
-----Original Message-----
From: Stephen Smalley [[email protected]]
Received: Tuesday, 15 Jan 2013, 4:13pm
To: Eric Paris [[email protected]]
CC: [email protected]
Subject: Re: __thread and fork() is a big no-no
Quoted 56 lines · Stephen Smalley
Quoted 55 lines · Eric Paris
Looking at the set*createcon cache patch we realized it wasn't working right. Quickly putting together a test program we found that:
static
__threadpid_ttid = 0;int main(void)
{ pid_t child; tid = syscall(__NR_gettid); printf("parent=%d\n", tid); child = fork(); if (child == 0) printf("child=%d\n", tid); return 0; }We expected to get results like:
parent=6500
child=0Instead you find result like:
parent=6500
child=6500Thread local storage is NOT initialized across
fork(). It's always been known that threads andfork()don't play well together:
http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-themBut we weren't threading the parent or the child, so we expected it to work. Instead we've had to add a call to
pthread_atfork()which explicitly resets tid at fork. Basically just:static void clear(void)
{ tid = 0; }int main(void)
{
...int rc; rc = pthread_atfork(NULL, NULL, clear); if (rc) return rc;...
}We actually call this
ptherad_atfork()inside theinit_procattr()function which is only called once. I'm wondering if we suffer from the same problems with__threadvariables andfork()in the library callers elsewhere in the code. I'll take a look.
Quoted 6 lines
Quoted 2 lines
Just letting folks know something interesting we found yesterday, in case they find it interesting...
Hmm...seems like caching things like
gettid()might be frowned upon, if this is any guide,
http://marc.info/?t=108644931500001&r=1&w=2
BTW, we used to just use /proc/self/attr there, and could conceivably go back to using that. Then you don't need the tid. The tradeoff is whether you care to support set*con for threads other than the thread group leader in multi-threaded applications.
Quoted 1 line · Eric Paris
It is not about caching tid, it is about ALL
__threadvariables not being cleared across fork.
Using pthread_atfork() is a way to ensure it *is* cleared.
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.