diff mbox

Fix clone (CLONE_VM) pid/tid reset (BZ#19957)

Message ID 1460750859-5593-1-git-send-email-adhemerval.zanella@linaro.org
State New
Headers show

Commit Message

Adhemerval Zanella April 15, 2016, 8:07 p.m. UTC
As discussed in libc-alpha [1] current clone with CLONE_VM (without
CLONE_THREAD set) will reset the pthread pid/tid fields to -1.  The
issue is since memory is shared between the parent and chield it will
clobber parent's cached pid/tid leading to internal inconsistencies
if the value is not restore.

And even it is restore it may lead to racy condition when between
set/restore a thread might invoke pthread function that validate the
pthread with INVALID_TD_P/INVALID_NOT_TERMINATED_TD_P and thus get
wrong results due inconsistent internal pthread state.

As stated in BZ19957, previously reports of this behaviour was close
with EWONTFIX due the fact usage of clone outside glibc is tricky
since glibc requires consistent internal pthread, while using clone
directly may not provide it. However since now posix_spawn uses
clone(CLONE_VM) to fixes various issues related to previous vfork
usage this issue requires fixing.

The vfork implementation also does something similar, but instead
it negates and restores only the *pid* field and functions that
might access its value know to handle such case (getpid, raise
and pthread ones that uses INVALID_TD_P/INVALID_NOT_TERMINATED_TD_P
macros that check only *tid* field).  Also vfork does not call
__clone directly, instead calling either __NR_vfork or __NR_clone
directly.

So this patch removes this clone behavior by avoiding setting
the pthread pid/tid field for CLONE_VM as well for CLONE_THREAD.
Instead of current approach of:

   int clone(int (*fn)(void *), void *child_stack, int flags, ...)
      [...]
      if (flags & CLONE_THREAD)
        goto do_syscall;
      pid_t new_value;
      if (flags & CLONE_VM)
        new_value = -1;
      else
        new_value = getpid ();
      THREAD_SETMEM (THREAD_SELF, pid, new_value);
      THREAD_SETMEM (THREAD_SELF, tid, new_value);

    do_syscall:
      [...]

The new approach uses:

   int clone(int (*fn)(void *), void *child_stack, int flags, ...)
      [...]
      if (flags & CLONE_THREAD || flags & CLONE_VM)
        goto do_syscall;
      pid_t new_value = getpid ();
      THREAD_SETMEM (THREAD_SELF, pid, new_value);
      THREAD_SETMEM (THREAD_SELF, tid, new_value);

    do_syscall:
      [...]

It also removes the linux tst-getpid2.c test which expects the previous
behavior.

I only fixed the clone.S implementations for i386, x86_64, powerpc,
aarch64, and arm (basically the ones I have a way to actually test it).
If would like to ask for some help on fixing the remaning ones:
sparc (32 and 64-bit), alpha, s390 (32 and 64-bit), microblaze,
sh, ia64, tile, hppa, m68k, nio2, and mips.

Tested on x86_64, i686, x32, powerpc64le, aarch64, and armhf.

[1] https://sourceware.org/ml/libc-alpha/2016-04/msg00307.html
---
 ChangeLog                                         | 13 +++++++++++++
 sysdeps/unix/sysv/linux/Makefile                  |  2 +-
 sysdeps/unix/sysv/linux/aarch64/clone.S           |  8 +++++---
 sysdeps/unix/sysv/linux/arm/clone.S               |  7 +++----
 sysdeps/unix/sysv/linux/i386/clone.S              |  5 +----
 sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S | 12 +++++++-----
 sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S | 10 ++++++----
 sysdeps/unix/sysv/linux/tst-getpid2.c             |  2 --
 sysdeps/unix/sysv/linux/x86_64/clone.S            |  7 ++-----
 9 files changed, 38 insertions(+), 28 deletions(-)
 delete mode 100644 sysdeps/unix/sysv/linux/tst-getpid2.c

-- 
1.9.1
diff mbox

Patch

diff --git a/ChangeLog b/ChangeLog
index 9eafe63..9d094a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@ 
+2016-04-15  Adhemerval Zanella  <adhemerval.zanella@linaro.org>
+
+	* sysdeps/unix/sysv/linux/Makefile [$(subdir) == nptl] (test): Remove
+	tst-getpid2.
+	* sysdeps/unix/sysv/linux/aarch64/clone.S (__clone): Do not change
+	pid/tid fields for CLONE_VM.
+	* sysdeps/unix/sysv/linux/arm/clone.S: Likewise.
+	* sysdeps/unix/sysv/linux/i386/clone.S: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S: Likewise.
+	* sysdeps/unix/sysv/linux/x86_64/clone.S: Likewise.
+	* sysdeps/unix/sysv/linux/tst-getpid2.c: Remove file.
+
 2016-04-15  Mike Frysinger  <vapier@gentoo.org>
 
 	* locale/iso-4217.def: Add SSP and change ZMK to ZMW.
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 9999600..6d52b97 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -194,7 +194,7 @@  CFLAGS-gai.c += -DNEED_NETLINK
 endif
 
 ifeq ($(subdir),nptl)
-tests += tst-setgetname tst-align-clone tst-getpid1 tst-getpid2 \
+tests += tst-setgetname tst-align-clone tst-getpid1 \
 	tst-thread-affinity-pthread tst-thread-affinity-pthread2 \
 	tst-thread-affinity-sched
 endif
diff --git a/sysdeps/unix/sysv/linux/aarch64/clone.S b/sysdeps/unix/sysv/linux/aarch64/clone.S
index 8f3ab40..d95c990 100644
--- a/sysdeps/unix/sysv/linux/aarch64/clone.S
+++ b/sysdeps/unix/sysv/linux/aarch64/clone.S
@@ -72,9 +72,11 @@  thread_start:
 	cfi_undefined (x30)
 	mov	x29, 0
 
-	tbnz	x11, #CLONE_THREAD_BIT, 3f
-	mov	x0, #-1
-	tbnz	x11, #CLONE_VM_BIT, 2f
+	mov	x0, CLONE_VM
+	movk	x0, CLONE_THREAD>>16, lsl 16
+	tst	x0, x11
+	bne	3f
+
 	mov	x8, #SYS_ify(getpid)
 	svc	0x0
 2:
diff --git a/sysdeps/unix/sysv/linux/arm/clone.S b/sysdeps/unix/sysv/linux/arm/clone.S
index c103123..b3a875f 100644
--- a/sysdeps/unix/sysv/linux/arm/clone.S
+++ b/sysdeps/unix/sysv/linux/arm/clone.S
@@ -72,13 +72,12 @@  PSEUDO_END (__clone)
 	.cantunwind
 	tst	ip, #CLONE_THREAD
 	bne	3f
+	tst	ip, #CLONE_VM
+	bne	3f
 	GET_TLS (lr)
 	mov	r1, r0
-	tst	ip, #CLONE_VM
 	ldr	r7, =SYS_ify(getpid)
-	ite	ne
-	movne	r0, #-1
-	swieq	0x0
+	swi	0x0
 	NEGOFF_ADJ_BASE (r1, TID_OFFSET)
 	str	r0, NEGOFF_OFF1 (r1, TID_OFFSET)
 	str	r0, NEGOFF_OFF2 (r1, PID_OFFSET, TID_OFFSET)
diff --git a/sysdeps/unix/sysv/linux/i386/clone.S b/sysdeps/unix/sysv/linux/i386/clone.S
index ef447d1..64f0c9b 100644
--- a/sysdeps/unix/sysv/linux/i386/clone.S
+++ b/sysdeps/unix/sysv/linux/i386/clone.S
@@ -108,7 +108,7 @@  L(thread_start):
 	cfi_undefined (eip);
 	/* Note: %esi is zero.  */
 	movl	%esi,%ebp	/* terminate the stack frame */
-	testl	$CLONE_THREAD, %edi
+	testl	$(CLONE_THREAD | CLONE_VM), %edi
 	je	L(newpid)
 L(haspid):
 	call	*%ebx
@@ -124,9 +124,6 @@  L(here):
 
 	.subsection 2
 L(newpid):
-	testl	$CLONE_VM, %edi
-	movl	$-1, %eax
-	jne	L(nomoregetpid)
 	movl	$SYS_ify(getpid), %eax
 	ENTER_KERNEL
 L(nomoregetpid):
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S b/sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S
index eb973db..ec82ed6 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S
@@ -76,11 +76,13 @@  ENTRY (__clone)
 	crandc	cr1*4+eq,cr1*4+eq,cr0*4+so
 	bne-	cr1,L(parent)		/* The '-' is to minimise the race.  */
 
-	andis.	r0,r28,CLONE_THREAD>>16
-	bne+	r0,L(oldpid)
-	andi.	r0,r28,CLONE_VM
-	li	r3,-1
-	bne-	r0,L(nomoregetpid)
+	/* If CLONE_THREAD of CLONE_VM does not update the pid/tid field.  */
+	lis	r0,CLONE_THREAD>>16
+	ori	r0,r0,CLONE_VM
+	and	r0,r0,r29
+	cmpwi	r0,0
+	bne+	cr0,L(oldpid)
+
 	DO_CALL(SYS_ify(getpid))
 L(nomoregetpid):
 	stw	r3,TID(r2)
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S b/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S
index 959fdb7..a8dfce6 100644
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S
@@ -78,11 +78,13 @@  ENTRY (__clone)
 	crandc	cr1*4+eq,cr1*4+eq,cr0*4+so
 	bne-	cr1,L(parent)		/* The '-' is to minimise the race.  */
 
-	andis.	r0,r29,CLONE_THREAD>>16
+	/* If CLONE_THREAD of CLONE_VM does not update the pid/tid field.  */
+	lis	r0,CLONE_THREAD>>16
+	ori	r0,r0,CLONE_VM
+	and	r0,r0,r29
+	cmpwi	r0,0
 	bne+	cr0,L(oldpid)
-	andi.	r0,r29,CLONE_VM
-	li	r3,-1
-	bne-	cr0,L(nomoregetpid)
+
 	DO_CALL(SYS_ify(getpid))
 L(nomoregetpid):
 	stw	r3,TID(r13)
diff --git a/sysdeps/unix/sysv/linux/tst-getpid2.c b/sysdeps/unix/sysv/linux/tst-getpid2.c
deleted file mode 100644
index fc98cb6..0000000
--- a/sysdeps/unix/sysv/linux/tst-getpid2.c
+++ /dev/null
@@ -1,2 +0,0 @@ 
-#define TEST_CLONE_FLAGS CLONE_VM
-#include "tst-getpid1.c"
diff --git a/sysdeps/unix/sysv/linux/x86_64/clone.S b/sysdeps/unix/sysv/linux/x86_64/clone.S
index 1a50957..d33e35e 100644
--- a/sysdeps/unix/sysv/linux/x86_64/clone.S
+++ b/sysdeps/unix/sysv/linux/x86_64/clone.S
@@ -92,14 +92,11 @@  L(thread_start):
 	   the outermost frame obviously.  */
 	xorl	%ebp, %ebp
 
-	testq	$CLONE_THREAD, %rdi
+	andq	$(CLONE_THREAD | CLONE_VM), %rdi
 	jne	1f
-	testq	$CLONE_VM, %rdi
-	movl	$-1, %eax
-	jne	2f
 	movl	$SYS_ify(getpid), %eax
 	syscall
-2:	movl	%eax, %fs:PID
+	movl	%eax, %fs:PID
 	movl	%eax, %fs:TID
 1: