Skip to content
  • John Keeping's avatar
    cache: close race window when unlocking slots · b31e9988
    John Keeping authored and Jason Donenfeld's avatar Jason Donenfeld committed
    
    
    We use POSIX advisory record locks to control access to cache slots, but
    these have an unhelpful behaviour in that they are released when any
    file descriptor referencing the file is closed by this process.
    
    Mostly this is okay, since we know we won't be opening the lock file
    anywhere else, but there is one place that it does matter: when we
    restore stdout we dup2() over a file descriptor referring to the file,
    thus closing that descriptor.
    
    Since we restore stdout before unlocking the slot, this creates a window
    during which the slot content can be overwritten.  The fix is reasonably
    straightforward: simply restore stdout after unlocking the slot, but the
    diff is a bit bigger because this requires us to move the temporary
    stdout FD into struct cache_slot.
    
    Signed-off-by: default avatarJohn Keeping <john@keeping.me.uk>
    Reviewed-by: default avatarChristian Hesse <mail@eworm.de>
    b31e9988