• Thomas Gleixner's avatar
    futex: Handle faults correctly for PI futexes · 34b1a1ce
    Thomas Gleixner authored
    fixup_pi_state_owner() tries to ensure that the state of the rtmutex,
    pi_state and the user space value related to the PI futex are consistent
    before returning to user space. In case that the user space value update
    faults and the fault cannot be resolved by faulting the page in via
    fault_in_user_writeable() the function returns with -EFAULT and leaves
    the rtmutex and pi_state owner state inconsistent.
    
    A subsequent futex_unlock_pi() operates on the inconsistent pi_state and
    releases the rtmutex despite not owning it which can corrupt the RB tree of
    the rtmutex and cause a subsequent kernel stack use after free.
    
    It was suggested to loop forever in fixup_pi_state_owner() if the fault
    cannot be resolved, but that results in runaway tasks which is especially
    undesired when the problem happens due to a programming error and not due
    to malice.
    
    As the user space value cannot be fixed up, the proper solution is to make
    the rtmutex and the pi_state consistent so both have the same owner. This
    leaves the user space value out of sync. Any subsequent operation on the
    futex will fail because the 10th rule of PI futexes (pi_state owner and
    user space value are consistent) has been violated.
    
    As a consequence this removes the inept attempts of 'fixing' the situation
    in case that the current task owns the rtmutex when returning with an
    unresolvable fault by unlocking the rtmutex which left pi_state::owner and
    rtmutex::owner out of sync in a different and only slightly less dangerous
    way.
    
    Fixes: 1b7558e4 ("futexes: fix fault handling in futex_lock_pi")
    Reported-by: gzobqq@gmail.com
    Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
    Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
    Cc: stable@vger.kernel.org
    34b1a1ce
futex.c 110 KB