google / gvisor

Application Kernel for Containers
https://gvisor.dev
Apache License 2.0
15.63k stars 1.29k forks source link

tcpip.SlicePayload usage in gonet/Conn.Write is incorrect. #399

Closed hbhasker closed 5 years ago

hbhasker commented 5 years ago

tcpip.SlicePayload implements Get() as follows.

// Get implements Payload. func (s SlicePayload) Get(size int) ([]byte, *Error) { if size > s.Size() { size = s.Size() } return s[:size], nil }

adapters/gonet uses this to wrap a view created from a buffer passed in to the Write() call. If you follow the code eventually this slice is passed when creating a segment. Essentially we are holding a reference to a user-supplied buffer in a segment in the Netstack.

hbhasker commented 5 years ago

Actually on rereading this is fine. It's using NewViewFromBytes which allocates a new buffer underneath and copies b into it. Closing bug.

// NewViewFromBytes allocates a new buffer and copies in the given bytes. func NewViewFromBytes(b []byte) View { return append(View(nil), b...) }