Skip to content

Commit 1b77d23

Browse files
committed
ssh/knownhosts: reject lines with multiple or unknown markers
Previously, parseLine would accept lines with multiple markers (e.g., "@cert-authority @Revoked ...") or unknown markers (e.g., "@unknown ..."). In these cases, the second or unknown marker was incorrectly parsed as the hostname. This often resulted in confusing downstream errors (like "illegal base64 data") because field positions were shifted. OpenSSH's hostfile.c strictly enforces a limit of one marker per line and validates that markers must be exactly "@cert-authority" or "@Revoked". This change adds a check to ensure that the parsed hostname does not start with '@'. This effectively catches both multiple markers (where the second marker becomes the host) and unknown markers (which are not consumed as markers and thus become the host), enforcing strict compliance with the OpenSSH format. This issue was found during a security audit by NCC Group Cryptography Services, sponsored by Teleport, and was assessed and is being fixed as a non-security bug. Change-Id: I13bb04d1f2610483ad9c6d1020e5100b6feded90 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/782428 Reviewed-by: David Chase <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Reviewed-by: Mark Freeman <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent 3872a2b commit 1b77d23

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎ssh/knownhosts/knownhosts.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ func parseLine(line []byte) (marker, host string, key ssh.PublicKey, err error)
188188
}
189189

190190
host, line = nextWord(line)
191+
// If the extracted 'host' starts with '@', it means we either encountered
192+
// a second marker (e.g., "@cert-authority @revoked") or an unknown marker
193+
// (e.g., "@unknown"). Both are invalid.
194+
if len(host) > 0 && host[0] == '@' {
195+
return "", "", nil, fmt.Errorf("knownhosts: unexpected marker: %q", host)
196+
}
191197
if len(line) == 0 {
192198
return "", "", nil, errors.New("knownhosts: missing host pattern")
193199
}

‎ssh/knownhosts/knownhosts_test.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,41 @@ func TestKeyTypeMismatch(t *testing.T) {
462462
t.Fatalf("got error %q, want to contain %q", err.Error(), expectedErr)
463463
}
464464
}
465+
466+
func TestMultipleMarkers(t *testing.T) {
467+
lineDouble := fmt.Sprintf("@cert-authority @revoked server.org %s %s", edKey.Type(), base64.StdEncoding.EncodeToString(edKey.Marshal()))
468+
db := newHostKeyDB()
469+
err := db.Read(bytes.NewBufferString(lineDouble), "testdb")
470+
if err == nil {
471+
t.Fatal("Read succeeded on line with multiple markers, expected error")
472+
}
473+
expectedError := "unexpected marker"
474+
if !strings.Contains(err.Error(), expectedError) {
475+
t.Errorf("got error %q, want it to contain %q", err.Error(), expectedError)
476+
}
477+
478+
lineUnknown := fmt.Sprintf("@unknown-marker server.org %s %s", edKey.Type(), base64.StdEncoding.EncodeToString(edKey.Marshal()))
479+
err = db.Read(bytes.NewBufferString(lineUnknown), "testdb")
480+
if err == nil {
481+
t.Fatal("Read succeeded on line with unknown marker, expected error")
482+
}
483+
if !strings.Contains(err.Error(), expectedError) {
484+
t.Errorf("got error %q, want it to contain %q", err.Error(), expectedError)
485+
}
486+
}
487+
488+
func TestUnknownMarker(t *testing.T) {
489+
line := fmt.Sprintf("@unknown-marker server.org %s %s", edKey.Type(), base64.StdEncoding.EncodeToString(edKey.Marshal()))
490+
491+
db := newHostKeyDB()
492+
err := db.Read(bytes.NewBufferString(line), "testdb")
493+
494+
if err == nil {
495+
t.Fatal("Read succeeded on line with unknown marker, expected error")
496+
}
497+
498+
expectedError := "unexpected marker"
499+
if !strings.Contains(err.Error(), expectedError) {
500+
t.Errorf("got error %q, want it to contain %q", err.Error(), expectedError)
501+
}
502+
}

0 commit comments

Comments
 (0)