xjasonlyu / tun2socks

tun2socks - powered by gVisor TCP/IP stack
https://github.com/xjasonlyu/tun2socks/wiki
GNU General Public License v3.0
2.85k stars 406 forks source link

[Bug] read more than actual length #118

Closed ssrshz closed 2 years ago

ssrshz commented 2 years ago

https://github.com/xjasonlyu/tun2socks/blob/3926f866138b645948ad69ccfa03d97786df3fa8/core/device/iobased/endpoint.go#L113

according to the context,the data‘s max actual length is n,but here is [offset+n]: n, err := e.rw.Read(data)

xjasonlyu commented 2 years ago

The data slice's total length is offset+mtu

https://github.com/xjasonlyu/tun2socks/blob/3926f866138b645948ad69ccfa03d97786df3fa8/core/device/iobased/endpoint.go#L97

then we read n bytes from Read. also, len(data[offset : offset+n]) == n

ssrshz commented 2 years ago

The data slice's total length is offset+mtu

https://github.com/xjasonlyu/tun2socks/blob/3926f866138b645948ad69ccfa03d97786df3fa8/core/device/iobased/endpoint.go#L97

then we read n bytes from Read. also, len(data[offset : offset+n]) == n

Oh,but i think the n bytes from Read starts with the “offset” bytes,and data[offset : offset+n] exceeds the length of n. Maybe I understand wrong?

xjasonlyu commented 2 years ago

and data[offset : offset+n] exceeds the length of n.

No, it doesn't.

Read returns total length it read, regardless of offset. it overwrites offset bytes first.

ssrshz commented 2 years ago

and data[offset : offset+n] exceeds the length of n.

No, it doesn't.

Read returns total length it read, regardless of offset. it overwrites offset bytes first.

er,i see.tks @xjasonlyu I'm learning tun2socks recently,this is a great project

ssrshz commented 2 years ago

When i debug here, i see all:

image

and data[offset : offset+n] exceeds the length of n.

No, it doesn't. Read returns total length it read, regardless of offset. it overwrites offset bytes first.

er,i see.tks @xjasonlyu I'm learning tun2socks recently,this is a great project

xjasonlyu commented 2 years ago

That's 4 bytes TUN PI header (offset).