Golang在Linux上的tls conn Read中出现panic吗?

huangapple go评论91阅读模式
英文:

Golang panic in tls conn Read - only on linux?

问题

我正在使用golang的crypto/tls来处理自定义的基于行的消息协议。

这种方法在Windows上运行良好:

var fullBuffer string

for {
    // 如果未连接,则尝试重新连接
    if this.conn == nil {
        if this.IsSecure() {
            this.conn, err = tls.Dial("tcp", this.GetHostOnly(), nil)
        } else {
            this.conn, err = net.Dial("tcp", this.GetHostOnly())
        }

        if err == nil {
            // 记录日志并继续
        }
    }

    // 从套接字读取到本地缓冲区(阻塞)
    if this.conn != nil {
        readBuff := make([]byte, 4096)
        nbytes, err = this.conn.Read(readBuff)
        if nbytes > 0 {
            fullBuffer += string(readBuff[0:nbytes])
        }
    }
}

非常简单直接 - 在win64上运行良好。

但是当我尝试在Linux上运行它(debian 8 - i386和amd64 - golang 1.5本机和从Windows交叉编译的1.6)时,我遇到以下恐慌:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x4ec8b4]

goroutine 8 [running]:
panic(0x8237780, 0x18522030)
    C:/Go/src/runtime/panic.go:464 +0x326
crypto/tls.(*Conn).Handshake(0x0, 0x0, 0x0)
    C:/Go/src/crypto/tls/conn.go:1023 +0x198
crypto/tls.(*Conn).Read(0x0, 0x18597000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
    C:/Go/src/crypto/tls/conn.go:922 +0x5e
mylib.(*MyConnection).worker(0x18512480)
    C:/gopath/src/mylib/mylib.go:342 +0x200

Read调用失败,因为它在TLS握手时传递了nil。

这里出了什么问题?

而且,为什么问题只出现在Linux上?

英文:

I'm using golang crypto/tls to process a custom line-oriented message protocol.

This approach works fine on windows:

var fullBuffer string

for {

	// If we're not connected, attempt reconnect
	if this.conn == nil {

		if this.IsSecure() {
			this.conn, err = tls.Dial("tcp", this.GetHostOnly(), nil)
		} else {
			this.conn, err = net.Dial("tcp", this.GetHostOnly())
		}

		if err == nil {
			// log and continue
		}
	}

	// Read from socket into our local buffer (blocking)
	if this.conn != nil {
		readBuff := make([]byte, 4096)
		nbytes, err = this.conn.Read(readBuff)
		if nbytes > 0 {
			fullBuffer += string(readBuff[0:nbytes])
		}
	}

Pretty straightforward - and it works fine on win64.

But when i try to run it on Linux (debian 8 - both i386 and amd64 - both golang 1.5 native and 1.6 crosscompiled from windows) i get the following panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x4ec8b4]

goroutine 8 [running]:
panic(0x8237780, 0x18522030)
    C:/Go/src/runtime/panic.go:464 +0x326
crypto/tls.(*Conn).Handshake(0x0, 0x0, 0x0)
    C:/Go/src/crypto/tls/conn.go:1023 +0x198
crypto/tls.(*Conn).Read(0x0, 0x18597000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
    C:/Go/src/crypto/tls/conn.go:922 +0x5e
mylib.(*MyConnection).worker(0x18512480)
    C:/gopath/src/mylib/mylib.go:342 +0x200

The Read call is failing because it's somehow passing nil to the TLS handshake.

What's going wrong here?

And, why is the problem isolated to linux?

答案1

得分: 0

好的,以下是翻译好的内容:

好的,Linux构建出现了“x509: certificate signed by unknown authority”错误。

但是:

  • 证书是有效的

    • crypto/x509/root_unix.go/etc/ssl/certs中查找,该目录是可读的
    • 但是openssl s_client-showcerts -verify上挂起,指向我的操作系统网络配置存在问题
  • 由于某种原因,无论错误如何,下面的if块都会被执行

    • 但是我将其存储为net.Conn而不是指针,所以它不能为nil

问题已解决,对于噪音表示抱歉。希望这个调试故事能帮助其他人解决类似问题。

英文:

OK, linux builds are producing the error x509: certificate signed by unknown authority.

But

  • the certificate is valid

    • crypto/x509/root_unix.go looks in /etc/ssl/certs which is world-readable
    • but openssl s_client is hanging on -showcerts -verify, pointing to some issue with my OS network configuration
  • for some reason the if block below is being entered regardless of the error

    • but i was storing it as a net.Conn instead of a pointer, so it's not nil-able.

Calling this solved, sorry for the noise. Hopefully this debugging story helps someone else in the future

huangapple
  • 本文由 发表于 2016年4月3日 08:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/36380233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定