Skip to content

nil pointer panic when StartTLS fails #58

@trestletech

Description

@trestletech

Thanks for creating and sharing this library!

I'm reproducibly getting a nil pointer panic when StartTLS fails. I can reproduce using a free account on jumpcloud.com and trying to use StartTLS on port 686 (the TLS-enabled port). Of course, this isn't expected to work, but hopefully we can avoid a panic.

conn, err := ldap.Dial("tcp", "ldap.jumpcloud.com:686")
if err != nil {
  log.Fatal("Error opening connection")
  return;
}
conn.StartTLS(&tls.Config{InsecureSkipVerify: ldapConfig.ServerTLSInsecure, ServerName: serverName})

The last line there will panic and the stack trace takes us through:
https://github.com/go-ldap/ldap/blob/v2.2.1/conn.go#L182

ldap/error.go

Line 102 in 07a7330

if len(packet.Children) >= 2 {

where we get a nil pointer. It seems that packet or Children must be nil.

The panic does not occur when conn.Debug is set to true. When Debugging is enabled, we get the following output (our own logging intermingled with this package's).

2016/04/11 19:18:11 /connect/src/connect/auth/providers/ldap/client.go:129: (ldap) Dialing unencrypted LDAP connection: ldap.jumpcloud.com:636
2016/04/11 19:18:11 /connect/src/connect/auth/providers/ldap/client.go:139: (ldap) Using StartTLS on LDAP connection: ldap.jumpcloud.com:636
LDAP Request: (Universal, Constructed, Sequence and Sequence of) Len=29 "<nil>"
 MessageID: (Universal, Primitive, Integer) Len=1 "1"
 Start TLS: (Application, Constructed, 0x17) Len=24 "<nil>"
  TLS Extended Command: (Context, Primitive, 0x00) Len=22 "1.3.6.1.4.1.1466.20037"
2016/04/11 19:18:11 flags&startTLS = 1
2016/04/11 19:18:11 1: waiting for response
2016/04/11 19:18:11 Sending message 1
2016/04/11 19:18:11 reader error: unexpected EOF
2016/04/11 19:18:11 Sending quit message and waiting for confirmation
2016/04/11 19:18:11 Shutting down - quit message received
2016/04/11 19:18:11 Closing channel for MessageID 1
2016/04/11 19:18:11 1: got response 0x0
2016/04/11 19:18:11 Closing network connection
2016/04/11 19:18:11 /connect/src/connect/auth/providers/ldap/client.go:143: (ldap) Failed to connect with TLS: LDAP Result Code 203 "": ldap: cannot process packet to add descriptions
2016/04/11 19:18:11 /connect/src/connect/auth/providers/ldap/ldap.go:180: (ldap) Unable to verify credentials: LDAP Result Code 203 "": ldap: cannot process packet to add descriptions

I'll have a PR opened with one solution for this shortly which will either defer a recover in the getLDAPResultCode function or check packet.Children for nil before checking length. But open to other approaches if anyone has any ideas.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions