• Nadav Amit's avatar
    userfaultfd: prevent concurrent API initialization · 22e5fe2a
    Nadav Amit authored
    userfaultfd assumes that the enabled features are set once and never
    changed after UFFDIO_API ioctl succeeded.
    
    However, currently, UFFDIO_API can be called concurrently from two
    different threads, succeed on both threads and leave userfaultfd's
    features in non-deterministic state.  Theoretically, other uffd operations
    (ioctl's and page-faults) can be dispatched while adversely affected by
    such changes of features.
    
    Moreover, the writes to ctx->state and ctx->features are not ordered,
    which can - theoretically, again - let userfaultfd_ioctl() think that
    userfaultfd API completed, while the features are still not initialized.
    
    To avoid races, it is arguably best to get rid of ctx->state.  Since there
    are only 2 states, record the API initialization in ctx->features as the
    uppermost bit and remove ctx->state.
    
    Link: https://lkml.kernel.org/r/20210808020724.1022515-3-namit@vmware.com
    Fixes: 9cd75c3c ("userfaultfd: non-cooperative: add ability to report non-PF events from uffd descriptor")
    Signed-off-by: default avatarNadav Amit <namit@vmware.com>
    Cc: Alexander Viro <viro@zeniv.linux.org.uk>
    Cc: Andrea Arcangeli <aarcange@redhat.com>
    Cc: Axel Rasmussen <axelrasmussen@google.com>
    Cc: Jens Axboe <axboe@kernel.dk>
    Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
    Cc: Peter Xu <peterx@redhat.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    22e5fe2a
userfaultfd.c 54.4 KB