Fix padding extends out of bound again

This commit is contained in:
yuhan6665 2023-03-02 21:42:48 -05:00
parent ccba465590
commit a4790133d2

View File

@ -528,17 +528,17 @@ func ReshapeMultiBuffer(ctx context.Context, buffer buf.MultiBuffer) buf.MultiBu
// XtlsPadding add padding to eliminate length siganature during tls handshake // XtlsPadding add padding to eliminate length siganature during tls handshake
func XtlsPadding(b *buf.Buffer, command byte, userUUID *[]byte, longPadding bool, ctx context.Context) *buf.Buffer { func XtlsPadding(b *buf.Buffer, command byte, userUUID *[]byte, longPadding bool, ctx context.Context) *buf.Buffer {
var contantLen int32 = 0 var contentLen int32 = 0
var paddingLen int32 = 0 var paddingLen int32 = 0
if b != nil { if b != nil {
contantLen = b.Len() contentLen = b.Len()
} }
if contantLen < 900 && longPadding { if contentLen < 900 && longPadding {
l, err := rand.Int(rand.Reader, big.NewInt(500)) l, err := rand.Int(rand.Reader, big.NewInt(500))
if err != nil { if err != nil {
newError("failed to generate padding").Base(err).WriteToLog(session.ExportIDToError(ctx)) newError("failed to generate padding").Base(err).WriteToLog(session.ExportIDToError(ctx))
} }
paddingLen = int32(l.Int64()) + 900 - contantLen paddingLen = int32(l.Int64()) + 900 - contentLen
} else { } else {
l, err := rand.Int(rand.Reader, big.NewInt(256)) l, err := rand.Int(rand.Reader, big.NewInt(256))
if err != nil { if err != nil {
@ -546,21 +546,21 @@ func XtlsPadding(b *buf.Buffer, command byte, userUUID *[]byte, longPadding bool
} }
paddingLen = int32(l.Int64()) paddingLen = int32(l.Int64())
} }
if paddingLen > buf.Size - 21 - contentLen {
paddingLen = buf.Size - 21 - contentLen
}
newbuffer := buf.New() newbuffer := buf.New()
if userUUID != nil { if userUUID != nil {
newbuffer.Write(*userUUID) newbuffer.Write(*userUUID)
} }
newbuffer.Write([]byte{command, byte(contantLen >> 8), byte(contantLen), byte(paddingLen >> 8), byte(paddingLen)}) newbuffer.Write([]byte{command, byte(contentLen >> 8), byte(contentLen), byte(paddingLen >> 8), byte(paddingLen)})
if b != nil { if b != nil {
newbuffer.Write(b.Bytes()) newbuffer.Write(b.Bytes())
b.Release() b.Release()
b = nil b = nil
} }
if paddingLen > buf.Size - newbuffer.Len() {
paddingLen = buf.Size - newbuffer.Len()
}
newbuffer.Extend(paddingLen) newbuffer.Extend(paddingLen)
newError("XtlsPadding ", contantLen, " ", paddingLen, " ", command).WriteToLog(session.ExportIDToError(ctx)) newError("XtlsPadding ", contentLen, " ", paddingLen, " ", command).WriteToLog(session.ExportIDToError(ctx))
return newbuffer return newbuffer
} }