From 7ecdf44e06bfc399671e1b11d5b57c6655946f27 Mon Sep 17 00:00:00 2001 From: Dean Ruina Date: Sat, 7 Nov 2026 14:50:30 +0200 Subject: [PATCH 1/2] ssh: quote remaining peer-controlled strings in error messages CL 798420 switched peer-controlled strings in error messages to %q, but a few sites still print them unquoted: - Waitmsg.String (and so ExitError.Error) prints the signal name and message from the server's exit-signal request. - parsePubKey prints the algorithm name read from the key blob. - The PublicKey.Verify implementations print the signature format. A malicious peer can use these to put control characters into a client's terminal or logs. Quote them with %q like the other messages. Signal, Msg and Lang still return the raw values. Updates golang/go#80301 Change-Id: I9d8afa7853d7568a1974cf3a7e5aa16aa9a875e7 --- ssh/keys.go | 12 ++++++------ ssh/keys_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ ssh/mldsa.go | 2 +- ssh/mldsa_test.go | 1 + ssh/session.go | 7 ++++--- ssh/session_test.go | 42 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 10 deletions(-) diff --git a/ssh/keys.go b/ssh/keys.go index 012c8f119e..bfcba7ecdc 100644 --- a/ssh/keys.go +++ b/ssh/keys.go @@ -100,7 +100,7 @@ func parsePubKey(in []byte, algo string) (pubKey PublicKey, rest []byte, err err algo, keyFormat) } - return nil, nil, fmt.Errorf("ssh: unknown key algorithm: %v", algo) + return nil, nil, fmt.Errorf("ssh: unknown key algorithm: %q", algo) } // trimSpace removes leading and trailing ASCII space and tab. @@ -547,7 +547,7 @@ func (r *rsaPublicKey) Marshal() []byte { func (r *rsaPublicKey) Verify(data []byte, sig *Signature) error { supportedAlgos := algorithmsForKeyFormat(r.Type()) if !slices.Contains(supportedAlgos, sig.Format) { - return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, r.Type()) + return fmt.Errorf("ssh: signature type %q for key type %q", sig.Format, r.Type()) } hash, err := hashFunc(sig.Format) if err != nil { @@ -659,7 +659,7 @@ func (k ed25519PublicKey) Marshal() []byte { func (k ed25519PublicKey) Verify(b []byte, sig *Signature) error { if sig.Format != k.Type() { - return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, k.Type()) + return fmt.Errorf("ssh: signature type %q for key type %q", sig.Format, k.Type()) } if l := len(k); l != ed25519.PublicKeySize { return fmt.Errorf("ssh: invalid size %d for Ed25519 public key", l) @@ -738,7 +738,7 @@ func (k *ecdsaPublicKey) Marshal() []byte { func (k *ecdsaPublicKey) Verify(data []byte, sig *Signature) error { if sig.Format != k.Type() { - return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, k.Type()) + return fmt.Errorf("ssh: signature type %q for key type %q", sig.Format, k.Type()) } hash, err := hashFunc(sig.Format) if err != nil { @@ -859,7 +859,7 @@ func (k *skECDSAPublicKey) Marshal() []byte { func (k *skECDSAPublicKey) Verify(data []byte, sig *Signature) error { if sig.Format != k.Type() { - return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, k.Type()) + return fmt.Errorf("ssh: signature type %q for key type %q", sig.Format, k.Type()) } hash, err := hashFunc(sig.Format) if err != nil { @@ -970,7 +970,7 @@ func (k *skEd25519PublicKey) Marshal() []byte { func (k *skEd25519PublicKey) Verify(data []byte, sig *Signature) error { if sig.Format != k.Type() { - return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, k.Type()) + return fmt.Errorf("ssh: signature type %q for key type %q", sig.Format, k.Type()) } if l := len(k.PublicKey); l != ed25519.PublicKeySize { return fmt.Errorf("invalid size %d for Ed25519 public key", l) diff --git a/ssh/keys_test.go b/ssh/keys_test.go index f9a33a6bd0..20e9886fd4 100644 --- a/ssh/keys_test.go +++ b/ssh/keys_test.go @@ -71,6 +71,53 @@ func TestParsePublicKeyWithSigningAlgoAsKeyFormat(t *testing.T) { } } +func TestParsePublicKeyUnknownAlgoSanitized(t *testing.T) { + in := Marshal(struct{ Algo string }{"evil\x1b[31m\r\n"}) + _, err := ParsePublicKey(in) + if err == nil { + t.Fatal("parsing a public key with an unknown algorithm succeeded unexpectedly") + } + if want := `ssh: unknown key algorithm: "evil\x1b[31m\r\n"`; err.Error() != want { + t.Errorf("got %q, want %q", err, want) + } +} + +func TestVerifySignatureFormatSanitized(t *testing.T) { + ecKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + edPub, _, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + keys := map[string]PublicKey{ + "sk-ecdsa": &skECDSAPublicKey{PublicKey: ecKey.PublicKey}, + "sk-ed25519": &skEd25519PublicKey{PublicKey: edPub}, + "cert": testSigners["cert"].PublicKey(), + } + for name, k := range testPublicKeys { + keys[name] = k + } + + const format = "evil\x1b[31m\r\n" + for name, k := range keys { + t.Run(name, func(t *testing.T) { + err := k.Verify([]byte("data"), &Signature{Format: format}) + if err == nil { + t.Fatal("Verify succeeded unexpectedly") + } + got := err.Error() + if !strings.Contains(got, `"evil\x1b[31m\r\n"`) { + t.Errorf("got %q, want it to contain the quoted signature format", got) + } + if bad := "\n\r\x1b"; strings.ContainsAny(got, bad) { + t.Errorf("got %q, want none of %q", got, bad) + } + }) + } +} + func TestUnsupportedCurves(t *testing.T) { raw, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader) if err != nil { diff --git a/ssh/mldsa.go b/ssh/mldsa.go index 09176b652d..7acb07fe36 100644 --- a/ssh/mldsa.go +++ b/ssh/mldsa.go @@ -105,7 +105,7 @@ func (k *mldsaPublicKey) Marshal() []byte { func (k *mldsaPublicKey) Verify(b []byte, sig *Signature) error { if sig.Format != k.Type() { - return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, k.Type()) + return fmt.Errorf("ssh: signature type %q for key type %q", sig.Format, k.Type()) } // nil options select pure ML-DSA with an empty context, as required by // [SSH-MLDSA], Sections 6 and 8. diff --git a/ssh/mldsa_test.go b/ssh/mldsa_test.go index 5f275a7b7a..d00aa602bd 100644 --- a/ssh/mldsa_test.go +++ b/ssh/mldsa_test.go @@ -170,6 +170,7 @@ func TestMLDSAVerifyRejectsBadSignatures(t *testing.T) { {"wrong format", &Signature{Format: KeyAlgoED25519, Blob: sig.Blob}, "signature type"}, {"other parameter set format", &Signature{Format: KeyAlgoMLDSA44, Blob: sig.Blob}, "signature type"}, {"signature from another parameter set", smallerSig, "signature type"}, + {"control characters in format", &Signature{Format: "evil\x1b[31m\r\n", Blob: sig.Blob}, `"evil\x1b[31m\r\n"`}, {"truncated signature", &Signature{Format: algo, Blob: shortSig}, "did not verify"}, {"extended signature", &Signature{Format: algo, Blob: longSig}, "did not verify"}, {"empty signature", &Signature{Format: algo, Blob: nil}, "did not verify"}, diff --git a/ssh/session.go b/ssh/session.go index ac62955788..c56824bc0d 100644 --- a/ssh/session.go +++ b/ssh/session.go @@ -438,7 +438,8 @@ func (s *Session) wait(reqs <-chan *Request) error { return err } - // Must sanitize strings? + // The strings are kept verbatim for the accessors and + // quoted when formatted by Waitmsg.String. wm.signal = sigval.Signal wm.msg = sigval.Error wm.lang = sigval.Lang @@ -641,10 +642,10 @@ func (w Waitmsg) Lang() string { func (w Waitmsg) String() string { str := fmt.Sprintf("Process exited with status %v", w.status) if w.signal != "" { - str += fmt.Sprintf(" from signal %v", w.signal) + str += fmt.Sprintf(" from signal %q", w.signal) } if w.msg != "" { - str += fmt.Sprintf(". Reason was: %v", w.msg) + str += fmt.Sprintf(". Reason was: %q", w.msg) } return str } diff --git a/ssh/session_test.go b/ssh/session_test.go index 3c6bf13318..df82871aa7 100644 --- a/ssh/session_test.go +++ b/ssh/session_test.go @@ -13,6 +13,7 @@ import ( "io" "math/rand" "net" + "strings" "sync" "testing" @@ -314,6 +315,47 @@ func TestUnknownExitSignal(t *testing.T) { } } +func TestExitSignalSanitizesError(t *testing.T) { + const ( + signal = "TERM\x1b[2J" + errmsg = "bye\r\n\x1b]0;title\a" + ) + conn := dial(func(ch Channel, in <-chan *Request, t *testing.T) { + defer ch.Close() + shell := newServerShell(ch, in, "> ") + readLine(shell, t) + sig := exitSignalMsg{Signal: signal, Errmsg: errmsg} + if _, err := ch.SendRequest("exit-signal", false, Marshal(&sig)); err != nil { + t.Errorf("unable to send signal: %v", err) + } + }, t) + defer conn.Close() + session, err := conn.NewSession() + if err != nil { + t.Fatalf("Unable to request new session: %v", err) + } + defer session.Close() + if err := session.Shell(); err != nil { + t.Fatalf("Unable to execute command: %v", err) + } + err = session.Wait() + e, ok := err.(*ExitError) + if !ok { + t.Fatalf("expected *ExitError but got %T", err) + } + if e.Signal() != signal || e.Msg() != errmsg { + t.Errorf("got signal %q and msg %q, want %q and %q", e.Signal(), e.Msg(), signal, errmsg) + } + got := e.Error() + want := `Process exited with status 128 from signal "TERM\x1b[2J". Reason was: "bye\r\n\x1b]0;title\a"` + if got != want { + t.Errorf("got %q, want %q", got, want) + } + if bad := "\n\r\a\x1b"; strings.ContainsAny(got, bad) { + t.Errorf("got %q, want none of %q", got, bad) + } +} + func TestExitWithoutStatusOrSignal(t *testing.T) { conn := dial(exitWithoutSignalOrStatus, t) defer conn.Close() From 1ca8dafd4450b1011588b6e2c7a5bc2314c2f20b Mon Sep 17 00:00:00 2001 From: Dean Ruina Date: Mon, 9 Nov 2026 18:35:08 +0000 Subject: [PATCH 2/2] ssh: address review: quote requested service, drop comment Quote the service name in the server's error for a service requested before authenticating, and remove the comment in Session.wait. Updates golang/go#80301 Change-Id: Ib90ee2ff4ffb31addaa3595acf7f9ee73614e6a6 --- ssh/server.go | 2 +- ssh/server_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ ssh/session.go | 2 -- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ssh/server.go b/ssh/server.go index 411fd27bce..3eced0e75e 100644 --- a/ssh/server.go +++ b/ssh/server.go @@ -437,7 +437,7 @@ func (s *connection) serverHandshake(config *ServerConfig) (*Permissions, error) return nil, err } if serviceRequest.Service != serviceUserAuth { - return nil, errors.New("ssh: requested service '" + serviceRequest.Service + "' before authenticating") + return nil, fmt.Errorf("ssh: requested service %q before authenticating", serviceRequest.Service) } serviceAccept := serviceAcceptMsg{ Service: serviceUserAuth, diff --git a/ssh/server_test.go b/ssh/server_test.go index 86d0325187..a59465ed82 100644 --- a/ssh/server_test.go +++ b/ssh/server_test.go @@ -1344,3 +1344,47 @@ func TestServerAuthSKUserPresence(t *testing.T) { } }) } + +func TestServerQuotesRequestedService(t *testing.T) { + c1, c2, err := netPipe() + if err != nil { + t.Fatalf("netPipe: %v", err) + } + defer c1.Close() + defer c2.Close() + + serverConf := &ServerConfig{NoClientAuth: true} + serverConf.AddHostKey(testSigners["ecdsa"]) + errc := make(chan error, 1) + go func() { + _, _, _, err := NewServerConn(c1, serverConf) + errc <- err + }() + + clientConf := &ClientConfig{HostKeyCallback: InsecureIgnoreHostKey()} + clientConf.SetDefaults() + v := []byte(packageVersion) + if _, err := c2.Write(append(v, '\r', '\n')); err != nil { + t.Fatalf("Write: %v", err) + } + serverVersion, err := readVersion(c2) + if err != nil { + t.Fatalf("readVersion: %v", err) + } + tr := newClientTransport(newTransport(c2, clientConf.Rand, true), v, serverVersion, clientConf, "addr", c2.RemoteAddr()) + defer tr.Close() + if err := tr.waitSession(); err != nil { + t.Fatalf("waitSession: %v", err) + } + if err := tr.writePacket(Marshal(&serviceRequestMsg{"evil\x1b[2J"})); err != nil { + t.Fatalf("writePacket: %v", err) + } + + err = <-errc + if err == nil { + t.Fatal("NewServerConn succeeded, want error") + } + if strings.ContainsRune(err.Error(), '\x1b') || !strings.Contains(err.Error(), `"evil\x1b[2J"`) { + t.Errorf("error = %q, want the requested service quoted", err) + } +} diff --git a/ssh/session.go b/ssh/session.go index c56824bc0d..8670e28bed 100644 --- a/ssh/session.go +++ b/ssh/session.go @@ -438,8 +438,6 @@ func (s *Session) wait(reqs <-chan *Request) error { return err } - // The strings are kept verbatim for the accessors and - // quoted when formatted by Waitmsg.String. wm.signal = sigval.Signal wm.msg = sigval.Error wm.lang = sigval.Lang