Bug 2591 - ssh-keygen -R is case-sensitive, but should not be
Summary: ssh-keygen -R is case-sensitive, but should not be
Status: CLOSED FIXED
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: ssh-keygen (show other bugs)
Version: -current
Hardware: All All
: P5 minor
Assignee: Damien Miller
URL:
Keywords:
Depends on:
Blocks: V_7_5
  Show dependency treegraph
 
Reported: 2016-06-18 06:41 AEST by Griff Miller II
Modified: 2018-04-06 12:26 AEST (History)
2 users (show)

See Also:


Attachments
Proposed patch for ssh-keygen -R case sensitivity bug (1.65 KB, patch)
2016-06-18 06:48 AEST, Griff Miller II
no flags Details | Diff
lowercase filenames as they are added (875 bytes, patch)
2016-07-08 13:31 AEST, Damien Miller
no flags Details | Diff
A couple more cases (2.19 KB, patch)
2017-03-10 14:49 AEDT, Damien Miller
dtucker: ok+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Griff Miller II 2016-06-18 06:41:00 AEST
Hostnames and domains are case-insensitive, but ssh-keygen -R is not
honoring this.

With openssh-7.2p2 Cygwin/Windows 7 (I've also seen the same behavior on
RHEL/CentOS with 5.3p1 and 6.6.1p1):

% grep -i myhost ~/.ssh/known_hosts # to show myhost is not there yet
% ssh gmiller at Myhost.domain.com date # this will put myhost there if I say
"yes", which I will do. Note mixed case.
The authenticity of host 'myhost.domain.com (1.2.3.4)' can't be established.
RSA key fingerprint is SHA256:kr1BeHAQgtdws3gB1NPpKtVDm9OPJ8Gg1loyiDC1z8Y.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'myhost.domain.com,1.2.3.4' (RSA) to the list
of known hosts.

Fri Apr 15 15:19:54 EDT 2016
% grep -i myhost ~/.ssh/known_hosts # to show that myhost is now in
known_hosts - note it has been smashed to lowercase, which is okay.
myhost.domain.com,1.2.3.4 ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAIEAwBsMvQ0wMfDKDXJT092F3NWjv840AHpzP0MWR+vAK1t+Uu5fjh2Jh93GFtwUH6BHCKntA7ZRTryk8xFGxlXy1NEmBzMkzNEDzWtVKBSTwnyxUZHs81r6DWBmJbsqny+lxYcUIUWMvjHis8ms6fT9G5rfde0hoLQzUSCN+L3cE1k=
% ssh-keygen -R Myhost.domain.com # now try to remove it. Case should not
matter here.
Host Myhost.domain.com not found in /home/millerig/.ssh/known_hosts
% grep -i myhost ~/.ssh/known_hosts # ...but it does. Show that it is
still there.
myhost.domain.com,1.2.3.4 ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAIEAwBsMvQ0wMfDKDXJT092F3NWjv840AHpzP0MWR+vAK1t+Uu5fjh2Jh93GFtwUH6BHCKntA7ZRTryk8xFGxlXy1NEmBzMkzNEDzWtVKBSTwnyxUZHs81r6DWBmJbsqny+lxYcUIUWMvjHis8ms6fT9G5rfde0hoLQzUSCN+L3cE1k=
% ssh-keygen -R myhost.domain.com # this time it will work because we made
sure to use lower case.
# Host myhost.domain.com found: line 14
/home/millerig/.ssh/known_hosts updated.
Original contents retained as /home/millerig/.ssh/known_hosts.old
% grep -i myhost ~/.ssh/known_hosts # show that it's gone
%

Seems like ssh-keygen -R is performing a case-sensitive string compare on
the provided hostname and the hostnames in the known_hosts file. It should
be a case-insensitive compare.

I can fix my scripts so that I convert to lowercase before calling
ssh-keygen -R, but it would be nice if this could be fixed so that others
don't get caught by surprise.

P.S. The same issue exists for the domain portion of the fully-qualified
hostname.

P.P.S. I will upload a patch that I did, with input from Ãngel González.
Comment 1 Griff Miller II 2016-06-18 06:48:51 AEST
Created attachment 2841 [details]
Proposed patch for ssh-keygen -R case sensitivity bug

Ãngel González helped develop this patch.
Comment 2 Damien Miller 2016-07-08 13:31:58 AEST
Created attachment 2847 [details]
lowercase filenames as they are added

I'm wary of changing the semantics for matching, since it's very likely that users are inadvertently depending on this - it has been this way for ~20 years.

I think it would be safer if we lowercase hostnames *as they are added*. This avoids changing semantics for existing hosts but lets new ones be stored in the canonical format.

Does this solve the problem for you?
Comment 3 Griff Miller II 2016-07-11 02:34:12 AEST
Hi, Damien.

I guess I can't think of any way a user would depend on "ssh-keygen -R Myhost" not to remove while depending on "ssh-keygen -R myhost" to remove, unless they were doing something insane like trying to use the success or failure of the removal as a means to determine if they had uppercase letters in their indicated hostname. :) The two commands mean exactly the same thing, and should produce the same result.

By the way, the hostnames are already being lowercased when they are added to known_hosts. So thanks, but no, it does not solve the problem for me.

While it's unlikely that a user would interactively type "ssh-keygen -R THEHOST" they do have to know enough inside baseball to code "ssh-keygen -R ${thehost,,}" (bash example) in scripts instead of "ssh-keygen -R $thehost" .
Comment 4 Damien Miller 2017-03-10 14:49:16 AEDT
Created attachment 2959 [details]
A couple more cases

always lowercase hostnames before hashing them or adding them unhashed to known_hosts
Comment 5 Damien Miller 2017-03-10 15:26:22 AEDT
Patch applied. This will be in OpenSSH 7.5
Comment 6 Damien Miller 2018-04-06 12:26:45 AEST
Close all resolved bugs after release of OpenSSH 7.7.