5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-10 08:40:28 +00:00

properly fix the memory errors, it was caused by a function returning and PutBytes-ing a buffer before a worker had a chance to decrypt the buffer, so it would GetBytes the same buffer by dumb luck and then get an illegal overlap

This commit is contained in:
Arceliar 2019-04-26 22:21:31 -05:00
parent 01ea6d3d80
commit 5d323861f0
2 changed files with 4 additions and 13 deletions

View File

@ -199,7 +199,7 @@ func (c *Conn) Read(b []byte) (int, error) {
} }
// Decrypt the packet // Decrypt the packet
bs, isOK := crypto.BoxOpen(&sinfo.sharedSesKey, p.Payload, &p.Nonce) bs, isOK := crypto.BoxOpen(&sinfo.sharedSesKey, p.Payload, &p.Nonce)
defer util.PutBytes(bs) defer util.PutBytes(bs) // FIXME commenting this out leads to illegal buffer reuse, this implies there's a memory error somewhere and that this is just flooding things out of the finite pool of old slices that get reused
// Check if we were unable to decrypt the packet for some reason and // Check if we were unable to decrypt the packet for some reason and
// return an error if we couldn't // return an error if we couldn't
if !isOK { if !isOK {
@ -223,11 +223,7 @@ func (c *Conn) Read(b []byte) (int, error) {
case <-timer.C: case <-timer.C:
return 0, ConnError{errors.New("Timeout"), true, false} return 0, ConnError{errors.New("Timeout"), true, false}
} }
select { // Wait for worker to return <-done // Wait for the worker to finish, failing this can cause memory errors (util.[Get||Put]Bytes stuff)
case <-done:
case <-timer.C:
return 0, ConnError{errors.New("Timeout"), true, false}
}
// Something went wrong in the session worker so abort // Something went wrong in the session worker so abort
if err != nil { if err != nil {
return 0, err return 0, err
@ -270,7 +266,6 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
return 0, errors.New("search failed") return 0, errors.New("search failed")
} }
} }
// defer util.PutBytes(b)
var packet []byte var packet []byte
done := make(chan struct{}) done := make(chan struct{})
workerFunc := func() { workerFunc := func() {
@ -294,11 +289,7 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
case <-timer.C: case <-timer.C:
return 0, ConnError{errors.New("Timeout"), true, false} return 0, ConnError{errors.New("Timeout"), true, false}
} }
select { // Wait for worker to return <-done // Wait for the worker to finish, failing this can cause memory errors (util.[Get||Put]Bytes stuff)
case <-done:
case <-timer.C:
return 0, ConnError{errors.New("Timeout"), true, false}
}
// Give the packet to the router // Give the packet to the router
sinfo.core.router.out(packet) sinfo.core.router.out(packet)
// Finally return the number of bytes we wrote // Finally return the number of bytes we wrote

View File

@ -341,7 +341,7 @@ func (r *router) handleTraffic(packet []byte) {
return return
} }
select { select {
case sinfo.recv <- &p: // FIXME ideally this should be FIFO case sinfo.recv <- &p: // FIXME ideally this should be front drop
default: default:
util.PutBytes(p.Payload) util.PutBytes(p.Payload)
} }