Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsafe math rand usage #2926

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/router/strategy_random.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package router
import (
"context"

"google.golang.org/protobuf/runtime/protoiface"

core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/observatory"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/dice"
"github.com/v2fly/v2ray-core/v5/features"
"github.com/v2fly/v2ray-core/v5/features/extension"
"google.golang.org/protobuf/runtime/protoiface"
)

// RandomStrategy represents a random balancing strategy
Expand Down
16 changes: 16 additions & 0 deletions common/dice/dice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
package dice

import (
crand "crypto/rand"
"io"
"math/big"
"math/rand"
"time"

"github.com/v2fly/v2ray-core/v5/common"
)

// Roll returns a non-negative number between 0 (inclusive) and n (exclusive).
Expand All @@ -15,6 +20,17 @@ func Roll(n int) int {
return rand.Intn(n)
}

// RollWith returns a non-negative number between 0 (inclusive) and n (exclusive).
// Use random as the random source, if read fails, it panics.
func RollWith(n int, random io.Reader) int {
if n == 1 {
return 0
}
mrand, err := crand.Int(random, big.NewInt(int64(n)))
common.Must(err)
return int(mrand.Int64())
}

// Roll returns a non-negative number between 0 (inclusive) and n (exclusive).
func RollDeterministic(n int, seed int64) int {
if n == 1 {
Expand Down
2 changes: 1 addition & 1 deletion infra/conf/v4/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (c *TrojanServerConfig) Build() (proto.Message, error) {
return nil, newError(`Trojan fallbacks: "path" must be empty or start with "/"`)
}
if fb.Type == "" && fb.Dest != "" {
if fb.Dest == "serve-ws-none" {
if fb.Dest == "serve-ws-none" { // nolint:gocritic
fb.Type = "serve"
} else if filepath.IsAbs(fb.Dest) || fb.Dest[0] == '@' {
fb.Type = "unix"
Expand Down
2 changes: 1 addition & 1 deletion infra/conf/v4/vless.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *VLessInboundConfig) Build() (proto.Message, error) {
return nil, newError(`VLESS fallbacks: "path" must be empty or start with "/"`)
}
if fb.Type == "" && fb.Dest != "" {
if fb.Dest == "serve-ws-none" {
if fb.Dest == "serve-ws-none" { // nolint:gocritic
fb.Type = "serve"
} else if filepath.IsAbs(fb.Dest) || fb.Dest[0] == '@' {
fb.Type = "unix"
Expand Down
7 changes: 3 additions & 4 deletions proxy/shadowsocks2022/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
cryptoRand "crypto/rand"
"encoding/binary"
"io"
"math/rand"
"time"

"github.com/v2fly/v2ray-core/v5/common"

"github.com/lunixbochs/struc"

"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/crypto"
"github.com/v2fly/v2ray-core/v5/common/dice"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
)
Expand Down Expand Up @@ -62,7 +61,7 @@ func (t *TCPRequest) EncodeTCPRequestHeader(effectivePsk []byte,
paddingLength := TCPMinPaddingLength
if initialPayload == nil {
initialPayload = []byte{}
paddingLength += 1 + rand.Intn(TCPMaxPaddingLength) // TODO INSECURE RANDOM USED
paddingLength += 1 + dice.RollWith(TCPMaxPaddingLength, cryptoRand.Reader)
}

variableLengthHeader := &TCPRequestHeader3VariableLength{
Expand Down
2 changes: 1 addition & 1 deletion proxy/vmess/encoding/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
common.Must(buffer.WriteByte(c.responseHeader))
common.Must(buffer.WriteByte(byte(header.Option)))

paddingLen := dice.Roll(16)
paddingLen := dice.RollWith(16, rand.Reader)
security := byte(paddingLen<<4) | byte(header.Security)
common.Must2(buffer.Write([]byte{security, byte(0), byte(header.Command)}))

Expand Down
Loading