Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ssh/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions ssh/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ssh/mldsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions ssh/mldsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion ssh/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions ssh/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 2 additions & 3 deletions ssh/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ func (s *Session) wait(reqs <-chan *Request) error {
return err
}

// Must sanitize strings?
wm.signal = sigval.Signal
wm.msg = sigval.Error
wm.lang = sigval.Lang
Expand Down Expand Up @@ -641,10 +640,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
}
42 changes: 42 additions & 0 deletions ssh/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"math/rand"
"net"
"strings"
"sync"
"testing"

Expand Down Expand Up @@ -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()
Expand Down
Loading