glibc/sysdeps/unix
Adhemerval Zanella fe05e1cb6d posix: Fix improper assert in Linux posix_spawn (BZ#22273)
As noted by Florian Weimer, current Linux posix_spawn implementation
can trigger an assert if the auxiliary process is terminated before
actually setting the err member:

    340   /* Child must set args.err to something non-negative - we rely on
    341      the parent and child sharing VM.  */
    342   args.err = -1;
    [...]
    362   new_pid = CLONE (__spawni_child, STACK (stack, stack_size), stack_size,
    363                    CLONE_VM | CLONE_VFORK | SIGCHLD, &args);
    364
    365   if (new_pid > 0)
    366     {
    367       ec = args.err;
    368       assert (ec >= 0);

Another possible issue is killing the child between setting the err and
actually calling execve.  In this case the process will not ran, but
posix_spawn also will not report any error:

    269
    270   args->err = 0;
    271   args->exec (args->file, args->argv, args->envp);

As suggested by Andreas Schwab, this patch removes the faulty assert
and also handles any signal that happens before fork and execve as the
spawn was successful (and thus relaying the handling to the caller to
figure this out).  Different than Florian, I can not see why using
atomics to set err would help here, essentially the code runs
sequentially (due CLONE_VFORK) and I think it would not be legal the
compiler evaluate ec without checking for new_pid result (thus there
is no need to compiler barrier).

Summarizing the possible scenarios on posix_spawn execution, we
have:

  1. For default case with a success execution, args.err will be 0, pid
     will not be collected and it will be reported to caller.

  2. For default failure case, args.err will be positive and the it will
     be collected by the waitpid.  An error will be reported to the
     caller.

  3. For the unlikely case where the process was terminated and not
     collected by a caller signal handler, it will be reported as succeful
     execution and not be collected by posix_spawn (since args.err will
     be 0). The caller will need to actually handle this case.

  4. For the unlikely case where the process was terminated and collected
     by caller we have 3 other possible scenarios:

     4.1. The auxiliary process was terminated with args.err equal to 0:
	  it will handled as 1. (so it does not matter if we hit the pid
          reuse race since we won't possible collect an unexpected
          process).

     4.2. The auxiliary process was terminated after execve (due a failure
          in calling it) and before setting args.err to -1: it will also
          be handle as 1. but with the issue of not be able to report the
          caller a possible execve failures.

     4.3. The auxiliary process was terminated after args.err is set to -1:
          this is the case where it will be possible to hit the pid reuse
          case where we will need to collected the auxiliary pid but we
          can not be sure if it will be expected one.  I think for this
          case we need to actually change waitpid to use WNOHANG to avoid
          hanging indefinitely on the call and report an error to caller
          since we can't differentiate between a default failure as 2.
          and a possible pid reuse race issue.

Checked on x86_64-linux-gnu.

	* sysdeps/unix/sysv/linux/spawni.c (__spawnix): Handle the case where
	the auxiliary process is terminated by a signal before calling _exit
	or execve.
2017-10-20 16:25:59 -02:00
..
alpha
arm
bsd Hide internal __tcgetattr function [BZ #18822] 2017-10-01 17:48:24 -07:00
i386
inet Remove add-ons mechanism. 2017-10-05 15:58:13 +00:00
mips
powerpc
sh
sysv/linux posix: Fix improper assert in Linux posix_spawn (BZ#22273) 2017-10-20 16:25:59 -02:00
x86_64
clock_gettime.c hurd: Fix exposition of s/gettimeofday through timespec_s/get 2017-09-25 01:55:02 +02:00
clock_nanosleep.c
clock_settime.c hurd: Fix exposition of s/gettimeofday through timespec_s/get 2017-09-25 01:55:02 +02:00
confstr.h
get_child_max.c
getlogin_r.c
getlogin.c
getpagesize.c
grantpt.c
ifreq.c
Implies
make-syscalls.sh Fix make-syscalls.sh VDSO support for GCC 8. 2017-09-26 21:21:01 +00:00
Makefile
s-proto-cancel.S
s-proto.S
setxid.h
sockatmark.c
stime.c
syscall-template.S Remove cancellation support for syscall generation 2017-08-24 14:58:35 -03:00
syscall.S
syscalls.list posix: Consolidate Linux fsync syscall 2017-05-18 18:06:47 -03:00
sysdep.h