tunnel: bump libwg-go and use newer logging function

We also plug a memory leak. C.CString was calling malloc, and these were
never freed.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-01-27 01:00:49 +01:00
parent b4f54e8958
commit 74ff870cf5
3 changed files with 45 additions and 47 deletions

View File

@ -10,15 +10,12 @@ package main
import "C" import "C"
import ( import (
"bufio" "fmt"
"bytes"
"log"
"math" "math"
"net" "net"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
"strings"
"unsafe" "unsafe"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -30,12 +27,20 @@ import (
type AndroidLogger struct { type AndroidLogger struct {
level C.int level C.int
interfaceName string tag *C.char
} }
func (l AndroidLogger) Write(p []byte) (int, error) { func cstring(s string) *C.char {
C.__android_log_write(l.level, C.CString("WireGuard/GoBackend/"+l.interfaceName), C.CString(string(p))) b, err := unix.BytePtrFromString(s)
return len(p), nil if err != nil {
b := [1]C.char{}
return &b[0]
}
return (*C.char)(unsafe.Pointer(b))
}
func (l AndroidLogger) Printf(format string, args ...interface{}) {
C.__android_log_write(l.level, l.tag, cstring(fmt.Sprintf(format, args...)))
} }
type TunnelHandle struct { type TunnelHandle struct {
@ -55,39 +60,38 @@ func init() {
select { select {
case <-signals: case <-signals:
n := runtime.Stack(buf, true) n := runtime.Stack(buf, true)
if n == len(buf) {
n--
}
buf[n] = 0 buf[n] = 0
C.__android_log_write(C.ANDROID_LOG_ERROR, C.CString("WireGuard/GoBackend/Stacktrace"), (*C.char)(unsafe.Pointer(&buf[0]))) C.__android_log_write(C.ANDROID_LOG_ERROR, cstring("WireGuard/GoBackend/Stacktrace"), (*C.char)(unsafe.Pointer(&buf[0])))
} }
} }
}() }()
} }
//export wgTurnOn //export wgTurnOn
func wgTurnOn(ifnameRef string, tunFd int32, settings string) int32 { func wgTurnOn(interfaceName string, tunFd int32, settings string) int32 {
interfaceName := string([]byte(ifnameRef)) tag := cstring("WireGuard/GoBackend/" + interfaceName)
logger := &device.Logger{ logger := &device.Logger{
Debug: log.New(&AndroidLogger{level: C.ANDROID_LOG_DEBUG, interfaceName: interfaceName}, "", 0), Verbosef: AndroidLogger{level: C.ANDROID_LOG_DEBUG, tag: tag}.Printf,
Info: log.New(&AndroidLogger{level: C.ANDROID_LOG_INFO, interfaceName: interfaceName}, "", 0), Errorf: AndroidLogger{level: C.ANDROID_LOG_ERROR, tag: tag}.Printf,
Error: log.New(&AndroidLogger{level: C.ANDROID_LOG_ERROR, interfaceName: interfaceName}, "", 0),
} }
logger.Debug.Println("Debug log enabled")
tun, name, err := tun.CreateUnmonitoredTUNFromFD(int(tunFd)) tun, name, err := tun.CreateUnmonitoredTUNFromFD(int(tunFd))
if err != nil { if err != nil {
unix.Close(int(tunFd)) unix.Close(int(tunFd))
logger.Error.Println(err) logger.Errorf("CreateUnmonitoredTUNFromFD: %v", err)
return -1 return -1
} }
logger.Info.Println("Attaching to interface", name) logger.Verbosef("Attaching to interface %v", name)
device := device.NewDevice(tun, logger) device := device.NewDevice(tun, logger)
setError := device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings))) err = device.IpcSet(settings)
if setError != nil { if err != nil {
unix.Close(int(tunFd)) unix.Close(int(tunFd))
logger.Error.Println(setError) logger.Errorf("IpcSet: %v", err)
return -1 return -1
} }
device.DisableSomeRoamingForBrokenMobileSemantics() device.DisableSomeRoamingForBrokenMobileSemantics()
@ -96,12 +100,12 @@ func wgTurnOn(ifnameRef string, tunFd int32, settings string) int32 {
uapiFile, err := ipc.UAPIOpen(name) uapiFile, err := ipc.UAPIOpen(name)
if err != nil { if err != nil {
logger.Error.Println(err) logger.Errorf("UAPIOpen: %v", err)
} else { } else {
uapi, err = ipc.UAPIListen(name, uapiFile) uapi, err = ipc.UAPIListen(name, uapiFile)
if err != nil { if err != nil {
uapiFile.Close() uapiFile.Close()
logger.Error.Println(err) logger.Errorf("UAPIListen: %v", err)
} else { } else {
go func() { go func() {
for { for {
@ -116,7 +120,7 @@ func wgTurnOn(ifnameRef string, tunFd int32, settings string) int32 {
} }
device.Up() device.Up()
logger.Info.Println("Device started") logger.Verbosef("Device started")
var i int32 var i int32
for i = 0; i < math.MaxInt32; i++ { for i = 0; i < math.MaxInt32; i++ {
@ -185,14 +189,11 @@ func wgGetConfig(tunnelHandle int32) *C.char {
if !ok { if !ok {
return nil return nil
} }
settings := new(bytes.Buffer) settings, err := handle.device.IpcGet()
writer := bufio.NewWriter(settings)
err := handle.device.IpcGetOperation(writer)
if err != nil { if err != nil {
return nil return nil
} }
writer.Flush() return C.CString(settings)
return C.CString(settings.String())
} }
//export wgVersion //export wgVersion

View File

@ -3,8 +3,7 @@ module golang.zx2c4.com/wireguard/android
go 1.15 go 1.15
require ( require (
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/net v0.0.0-20201216054612-986b41b23924 // indirect golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c
golang.org/x/sys v0.0.0-20201223074533-0d417f636930 golang.zx2c4.com/wireguard v0.0.20201119-0.20210126220548-d669c78c4306
golang.zx2c4.com/wireguard v0.0.20201119-0.20201223215156-09728dc6b340
) )

View File

@ -1,24 +1,22 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY= golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
golang.org/x/net v0.0.0-20201216054612-986b41b23924 h1:QsnDpLLOKwHBBDa8nDws4DYNc/ryVW2vCpxCs09d4PY= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20201216054612-986b41b23924/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210105210732-16f7687f5001 h1:/dSxr6gT0FNI1MO5WLJo8mTmItROeOKTkDn+7OwWBos=
golang.org/x/sys v0.0.0-20201223074533-0d417f636930 h1:vRgIt+nup/B/BwIS0g2oC0haq0iqbV3ZA+u6+0TlNCo= golang.org/x/sys v0.0.0-20210105210732-16f7687f5001/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201223074533-0d417f636930/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.zx2c4.com/wireguard v0.0.20201119-0.20201223215156-09728dc6b340 h1:X6jrf2sUEj3n+q2oB/I3C088vQFKREz2UzgVJ8wENtI= golang.zx2c4.com/wireguard v0.0.20201119-0.20210126220548-d669c78c4306 h1:z7W9tlhU/KwkZWSX6QCfZXBcDpIa1JYANldQ/gFJj44=
golang.zx2c4.com/wireguard v0.0.20201119-0.20201223215156-09728dc6b340/go.mod h1:ITsWNpkFv78VPB7f8MiyuxeEMcHR4jfxHGCJLPP3GHs= golang.zx2c4.com/wireguard v0.0.20201119-0.20210126220548-d669c78c4306/go.mod h1:r0ExowOoGFfDoLDxx+M9SYbNVsoZ0xviLL+K4f2mt+A=