Fix buffer overflow

This commit is contained in:
wwqgtxx 2022-09-21 16:57:35 +08:00 committed by 世界
parent 4e70523360
commit b2828dac5f
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 5 additions and 11 deletions

View file

@ -199,7 +199,7 @@ func (b *Buffer) ReadAtLeastFrom(r io.Reader, min int) (int64, error) {
} }
func (b *Buffer) ReadFullFrom(r io.Reader, size int) (n int, err error) { func (b *Buffer) ReadFullFrom(r io.Reader, size int) (n int, err error) {
if b.IsFull() { if b.end+size > b.Cap() {
return 0, io.ErrShortBuffer return 0, io.ErrShortBuffer
} }
n, err = io.ReadFull(r, b.data[b.end:b.end+size]) n, err = io.ReadFull(r, b.data[b.end:b.end+size])

View file

@ -36,10 +36,11 @@ func (c *ClientConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
if err != nil { if err != nil {
return M.Socksaddr{}, err return M.Socksaddr{}, err
} }
if buffer.FreeLen() < int(length) { _, err = buffer.ReadFullFrom(c, int(length))
return M.Socksaddr{}, io.ErrShortBuffer if err != nil {
return M.Socksaddr{}, err
} }
return destination, common.Error(buffer.ReadFullFrom(c, int(length))) return destination, nil
} }
func (c *ClientConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { func (c *ClientConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {

View file

@ -68,9 +68,6 @@ func (c *ServerConn) loopInput() {
break break
} }
buffer.FullReset() buffer.FullReset()
if int(length) > buffer.FreeLen() {
break
}
_, err = buffer.ReadFullFrom(c.inputReader, int(length)) _, err = buffer.ReadFullFrom(c.inputReader, int(length))
if err != nil { if err != nil {
break break

View file

@ -288,10 +288,6 @@ func ReadPacket(conn net.Conn, buffer *buf.Buffer) (M.Socksaddr, error) {
return M.Socksaddr{}, E.Cause(err, "read chunk length") return M.Socksaddr{}, E.Cause(err, "read chunk length")
} }
if buffer.FreeLen() < int(length) {
return M.Socksaddr{}, io.ErrShortBuffer
}
err = rw.SkipN(conn, 2) err = rw.SkipN(conn, 2)
if err != nil { if err != nil {
return M.Socksaddr{}, E.Cause(err, "skip crlf") return M.Socksaddr{}, E.Cause(err, "skip crlf")