ssh-keygen -R changes known_hosts file permissions (mode)

Bug #1966591 reported by Evgeny Morozov
12
This bug affects 1 person
Affects Status Importance Assigned to Milestone
portable OpenSSH
Unknown
Unknown
openssh (Ubuntu)
Fix Released
Undecided
Unassigned
Bionic
Fix Released
Undecided
Sergio Durigan Junior
Focal
Fix Released
Undecided
Sergio Durigan Junior
Impish
Fix Released
Undecided
Unassigned
Jammy
Fix Released
Undecided
Unassigned

Bug Description

[Impact]

When using "ssh-keygen -R" to remove a host from "known_hosts" the command changes permissions on the file. This can cause problems particularly when used on the global "known_hosts" file (/etc/ssh/ssh_known_hosts), because then only root can read it. Programs running non-interactively as non-root users suddenly fail to SSH and it's not immediately obvious why.

[Test Plan]

The problem happens on Bionic and Focal.

$ lxc launch ubuntu-daily:focal openssh-bug1966591
$ lxc shell openssh-bug1966591
# ssh-keyscan github.com > test_known_hosts
# chmod 644 test_known_hosts
# ssh-keygen -R github.com -f test_known_hosts
# stat test_known_hosts
...
Access: (0600/-rw-------) ...
...

[Where problems could occur]

The upstream patch is very simple and it is unlikely that it will cause any regressions. An indirect problem that could occur is that users might expect to see a more strict set of permissions on a "known_hosts" file after using "ssh-keygen -R", but arguably this is not defined behaviour and should not be relied upon. Of course, there is always a (very) small risk of introducing problems when rebuilding packages using newer versions of its dependencies (especially on Bionic, because it's older).

[Original Description]

When I use ssh-keygen -R to remove a host from known_hosts it changes permissions on the file. This causes problems particularly when used on the global known hosts file (/etc/ssh/ssh_known_hosts), because then only root can read it. Programs running non-interactively as non-root users suddenly fail to SSH and it's not immediately obvious why.

To reproduce:

$ ssh-keyscan github.com >test_known_hosts
$ chmod 741 test_known_hosts
$ ssh-keygen -R github.com -f test_known_hosts
$ stat test_known_hosts
...
Access: (0600/-rw-------) ...

Expected behavior: file permissions remain unchanged (mode 0741 in this example).

$ lsb_release -rd
Description: Ubuntu 18.04.6 LTS
Release: 18.04

$ apt-cache policy openssh-client
openssh-client:
  Installed: 1:7.6p1-4ubuntu0.6

Related branches

Revision history for this message
Lena Voytek (lvoytek) wrote (last edit ):

Thank you for taking the time to report this bug while providing a good reproducer.

I was able to reproduce in Focal and Bionic

# ssh-keyscan github.com >test_known_hosts
# ls -la test_known_hosts

-rw-r--r-- 1 root root 656 Mar 28 14:24 test_known_hosts

# ssh-keygen -R github.com -f test_known_hosts

test_known_hosts updated.
Original contents retained as test_known_hosts.old

# ls -la test_known_hosts
-rw------- 1 root root 0 Mar 28 14:25 test_known_hosts

However in Jammy and Impish this is fixed:

# ssh-keyscan github.com >test_known_hosts
# ls -la test_known_hosts

-rw-r--r-- 1 root root 656 Mar 28 14:30 test_known_hosts

# ssh-keygen -R github.com -f test_known_hosts

test_known_hosts updated.
Original contents retained as test_known_hosts.old

# ls -la test_known_hosts
-rw-r--r-- 1 root root 0 Mar 28 14:31 test_known_hosts

With this already being fixed in the newer releases it should just be a matter of finding the relevant commit and adding it to focal and bionic.

Thanks

Changed in openssh (Ubuntu Impish):
status: New → Fix Released
Changed in openssh (Ubuntu Jammy):
status: New → Fix Released
Changed in openssh (Ubuntu Bionic):
status: New → Confirmed
Changed in openssh (Ubuntu Focal):
status: New → Confirmed
tags: added: server-todo
Revision history for this message
Sergio Durigan Junior (sergiodj) wrote :

Thanks for the bug report Evgeny and for the initial investigation, Lena.

The following commit "fixes" the issue:

commit f2d84f1b3fa68d77c99238d4c645d0266fae2a74
Author: <email address hidden> <email address hidden>
AuthorDate: Wed May 13 09:55:57 2020 +0000
Commit: Damien Miller <email address hidden>
CommitDate: Wed May 27 10:09:19 2020 +1000

I say "fixes" because it doesn't do exactly what Evgeny is asking in the bug description; that is, it doesn't make ssh-keygen preserve *all* of the permission bits. Instead, with the commit above applied we see that ssh-keygen in Focal/Bionic start behaving exactly like what we see in Impish/Jammy, and the group/all *read* permissions are preserved, but not the (e.g.) execute permission. We can see that on the output that Lena pasted.

Either way, preserving the "read" permission bits for group/all and dropping everything else is done on purpose, as can be seen on this excerpt (from ssh-keygen.c:do_known_hosts):

...
    fchmod(fd, sb.st_mode & 0644);
...

I have already backported & tested the patch on Bionic, and it works. I will start filing MPs tomorrow.

Changed in openssh (Ubuntu Bionic):
assignee: nobody → Sergio Durigan Junior (sergiodj)
Changed in openssh (Ubuntu Focal):
assignee: nobody → Sergio Durigan Junior (sergiodj)
Changed in openssh (Ubuntu Bionic):
status: Confirmed → Triaged
status: Triaged → In Progress
Changed in openssh (Ubuntu Focal):
status: Confirmed → In Progress
Revision history for this message
Evgeny Morozov (evgeny0) wrote :

Thanks a lot! 0644 are really the important bits, so while the behavior is still "user-surprising" it shouldn't cause any practical problems for us.

description: updated
Revision history for this message
Brian Murray (brian-murray) wrote : Please test proposed package

Hello Evgeny, or anyone else affected,

Accepted openssh into focal-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/openssh/1:8.2p1-4ubuntu0.5 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested, what testing has been performed on the package and change the tag from verification-needed-focal to verification-done-focal. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-focal. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Changed in openssh (Ubuntu Focal):
status: In Progress → Fix Committed
tags: added: verification-needed verification-needed-focal
Changed in openssh (Ubuntu Bionic):
status: In Progress → Fix Committed
tags: added: verification-needed-bionic
Revision history for this message
Brian Murray (brian-murray) wrote :

Hello Evgeny, or anyone else affected,

Accepted openssh into bionic-proposed. The package will build now and be available at https://launchpad.net/ubuntu/+source/openssh/1:7.6p1-4ubuntu0.7 in a few hours, and then in the -proposed repository.

Please help us by testing this new package. See https://wiki.ubuntu.com/Testing/EnableProposed for documentation on how to enable and use -proposed. Your feedback will aid us getting this update out to other Ubuntu users.

If this package fixes the bug for you, please add a comment to this bug, mentioning the version of the package you tested, what testing has been performed on the package and change the tag from verification-needed-bionic to verification-done-bionic. If it does not fix the bug for you, please add a comment stating that, and change the tag to verification-failed-bionic. In either case, without details of your testing we will not be able to proceed.

Further information regarding the verification process can be found at https://wiki.ubuntu.com/QATeam/PerformingSRUVerification . Thank you in advance for helping!

N.B. The updated package will be released to -updates after the bug(s) fixed by this package have been verified and the package has been in -proposed for a minimum of 7 days.

Revision history for this message
Ubuntu SRU Bot (ubuntu-sru-bot) wrote : Autopkgtest regression report (openssh/1:8.2p1-4ubuntu0.5)

All autopkgtests for the newly accepted openssh (1:8.2p1-4ubuntu0.5) for focal have finished running.
The following regressions have been reported in tests triggered by the package:

snapd/2.54.3+20.04.1ubuntu0.2 (ppc64el, s390x, arm64, amd64)
gvfs/1.44.1-1ubuntu1 (ppc64el, arm64)

Please visit the excuses page listed below and investigate the failures, proceeding afterwards as per the StableReleaseUpdates policy regarding autopkgtest regressions [1].

https://people.canonical.com/~ubuntu-archive/proposed-migration/focal/update_excuses.html#openssh

[1] https://wiki.ubuntu.com/StableReleaseUpdates#Autopkgtest_Regressions

Thank you!

Revision history for this message
Ubuntu SRU Bot (ubuntu-sru-bot) wrote : Autopkgtest regression report (openssh/1:7.6p1-4ubuntu0.7)

All autopkgtests for the newly accepted openssh (1:7.6p1-4ubuntu0.7) for bionic have finished running.
The following regressions have been reported in tests triggered by the package:

snapd/2.54.3+18.04.2ubuntu0.2 (arm64, s390x, ppc64el, amd64)

Please visit the excuses page listed below and investigate the failures, proceeding afterwards as per the StableReleaseUpdates policy regarding autopkgtest regressions [1].

https://people.canonical.com/~ubuntu-archive/proposed-migration/bionic/update_excuses.html#openssh

[1] https://wiki.ubuntu.com/StableReleaseUpdates#Autopkgtest_Regressions

Thank you!

Revision history for this message
Evgeny Morozov (evgeny0) wrote :

Tested openssh-client/bionic-proposed,now 1:7.6p1-4ubuntu0.7 and confirmed that group's and others' read permissions are now preserved. Write and execute permissions are still removed, that is, a umask of 0133 seems to be applied.

tags: added: verification-done-bionic
removed: verification-needed-bionic
Revision history for this message
Sergio Durigan Junior (sergiodj) wrote :

Verification for Focal:

First, verifying that the bug manifests with the current package:

# apt policy openssh-client
openssh-client:
  Installed: 1:8.2p1-4ubuntu0.4
  Candidate: 1:8.2p1-4ubuntu0.4
  Version table:
 *** 1:8.2p1-4ubuntu0.4 500
        500 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
        100 /var/lib/dpkg/status
     1:8.2p1-4ubuntu0.2 500
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
     1:8.2p1-4 500
        500 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages
# ssh-keygen -R github.com -f test_known_hosts
# Host github.com found: line 1
# Host github.com found: line 2
# Host github.com found: line 3
test_known_hosts updated.
Original contents retained as test_known_hosts.old
# stat test_known_hosts | grep ^Access
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-04-13 21:54:40.698183232 +0000

Now, enabling -proposed and verifying that the new package fixes the bug:

# apt policy openssh-client
openssh-client:
  Installed: 1:8.2p1-4ubuntu0.5
  Candidate: 1:8.2p1-4ubuntu0.5
  Version table:
 *** 1:8.2p1-4ubuntu0.5 500
        500 http://archive.ubuntu.com/ubuntu focal-proposed/main amd64 Packages
        100 /var/lib/dpkg/status
     1:8.2p1-4ubuntu0.4 500
        500 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
     1:8.2p1-4ubuntu0.2 500
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
     1:8.2p1-4 500
        500 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages
# ssh-keygen -R github.com -f test_known_hosts
# Host github.com found: line 1
# Host github.com found: line 2
# Host github.com found: line 3
test_known_hosts updated.
Original contents retained as test_known_hosts.old
# stat test_known_hosts | grep ^Access
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-04-13 21:57:25.676987718 +0000

The bug has been fixed.

tags: added: verification-done-focal
removed: server-todo verification-needed verification-needed-focal
Revision history for this message
Brian Murray (brian-murray) wrote :

Please investigate the gvfs autopkgtests failures for impish and focal as these are blocking the release of openssh for those releases and subsequently the release for bionic too.

Revision history for this message
Sergio Durigan Junior (sergiodj) wrote :

Sorry about that.

There are not failures on Focal, but there were a bunch of old passes, so I've retriggered them.

I'm investigating what's happening with the Impish one.

Revision history for this message
Sergio Durigan Junior (sergiodj) wrote :

The Impish failure passed with a retrigger, as expected.

Revision history for this message
Sergio Durigan Junior (sergiodj) wrote :

FWIW, I've also retriggered the tests marked as OLD_NEUTRAL. It should take a while until everything runs.

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package openssh - 1:8.2p1-4ubuntu0.5

---------------
openssh (1:8.2p1-4ubuntu0.5) focal; urgency=medium

  * d/p/fix-connect-timeout-overflow.patch: prevent ConnectTimeout overflow.
    (LP: #1903516)

  [ Sergio Durigan Junior ]
  * d/p/lp1966591-upstream-preserve-group-world-read-permission-on-kno.patch:
    Preserve group/world read permissions on known_hosts. (LP: #1966591)

 -- Athos Ribeiro <email address hidden> Wed, 30 Mar 2022 10:03:15 -0300

Changed in openssh (Ubuntu Focal):
status: Fix Committed → Fix Released
Revision history for this message
Brian Murray (brian-murray) wrote : Update Released

The verification of the Stable Release Update for openssh has completed successfully and the package is now being released to -updates. Subsequently, the Ubuntu Stable Release Updates Team is being unsubscribed and will not receive messages about this bug report. In the event that you encounter a regression using the package from -updates please report a new bug using ubuntu-bug and tag the bug report regression-update so we can easily find any regressions.

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package openssh - 1:7.6p1-4ubuntu0.7

---------------
openssh (1:7.6p1-4ubuntu0.7) bionic; urgency=medium

  * d/p/fix-connect-timeout-overflow.patch: prevent ConnectTimeout overflow.
    (LP: #1903516)

  [ Sergio Durigan Junior ]
  * d/p/lp1966591-upstream-preserve-group-world-read-permission-on-kno.patch:
    Preserve group/world read permissions on known_hosts. (LP: #1966591)

 -- Athos Ribeiro <email address hidden> Wed, 30 Mar 2022 10:17:14 -0300

Changed in openssh (Ubuntu Bionic):
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.