• Gregory Kurz's avatar
    [PATCH] fork() bug invalidates file descriptors · 2dbc5729
    Gregory Kurz authored
    Take a process P1 that spawns a thread T (aka.  a clone with CLONE_FILES). 
    If P1 forks another process P2 (aka.  not a clone) while T is blocked in a
    open() that should return file descriptor FD, then FD will be unusable in
    P2.  This leads to strange behaviors in the context of P2: close(FD)
    returns EBADF, while dup2(a_valid_fd, FD) returns EBUSY and of course FD is
    never returned again by any syscall...
    
    testcase:
    
    #include <errno.h>
    #include <fcntl.h>
    #include <sched.h>
    #include <signal.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <asm/page.h>
    
    #define FIFO "/tmp/bug_fifo"
    #define FD   0
    
    /*
     * This program is meant to show that calling fork() while a clone spawned
     * with CLONE_FILES is blocked in open() makes a fd number unusable in the
     * child.
     *
     *
     *     Parent               Clone                Child
     *        |
     *   clone(CLONE_FILES)-
    2dbc5729
fork.c 33.1 KB