Skip to content

Consider updating known_hosts writes to behave like OpenSSH - and append-only #835

Open

Description

@bdowling

save_host_keys in client.py does not perform any locking on the host_keys file before overwriting it and it does not do an atomic rename to replace it either.. This can lead to various race conditions if you have multiple instances using and updating this file at the same time.

I would further question why this module should be rewriting this file in entirety at all? Should it instead only be appending new items? openssh for example leaves conflicts up to the user to resolve, which is a safer path generally speaking.

There are a number of issues (e.g. #435, #680) that I see listed due to rewriting, changing order, losing information, dropping keys paramiko can't parse etc that would be avoided if it only appended new where it could.

Activity

  1. bitprophet commented on Oct 28, 2016

    @bitprophet
    Member

    Linking this to #625 which I just looked at, mostly just as both deal with known_hosts.

    You're right that there's no locking or atomic renames, though I do note the file is reread just prior to writes, which heads off many instances of the problem, even though it certainly doesn't eradicate it.

    openssh for example leaves conflicts up to the user to resolve

    I can't recall offhand & don't have time to dig, but: does this mean it happily appends conflicting data and then prompts user about conflicts at load/read time? Either way, we do tend to try and honor OpenSSH's behavior barring reasons not to, so that seems like it might be a sensible change offhand, especially given the other upsides you mention.

    Main downside is it would be backwards incompatible so we'd want to shunt such a change to the 3.0 release. Not an argument against, just something to be aware of.

  2. changed the title [-]save_host_keys does not lock known_hosts for replacement[/-] [+]Consider updating known_hosts writes to behave like OpenSSH - and append-only[/+] on Oct 28, 2016
  3. added this to the milestone on Oct 28, 2016
  4. bdowling commented on Oct 28, 2016

    @bdowling
    Author

    Would you be willing to accept a PR that keeps the current behavior the default, but allows opt in to improved behavior now? With plan to flip the default in the 3.0 release?

  5. bitprophet commented on Nov 1, 2016

    @bitprophet
    Member

    I think I would, yea. I normally like to keep PRs separate but these changes are closely enough related I think you could just update #834 to include that, if you'd prefer. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions