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 a race condition that could allow same Salt/IV to be reused #201

Merged
merged 1 commit into from
Mar 12, 2021
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
19 changes: 19 additions & 0 deletions internal/bloomring.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (r *BloomRing) Add(b []byte) {
}
r.mutex.Lock()
defer r.mutex.Unlock()
r.add(b)
}

func (r *BloomRing) add(b []byte) {
slot := r.slots[r.slotPosition]
if r.entryCounter > r.slotCapacity {
// Move to next slot and reset
Expand All @@ -64,10 +68,25 @@ func (r *BloomRing) Test(b []byte) bool {
}
r.mutex.RLock()
defer r.mutex.RUnlock()
test := r.test(b)
return test
}

func (r *BloomRing) test(b []byte) bool {
for _, s := range r.slots {
if s.Test(b) {
return true
}
}
return false
}

func (r *BloomRing) Check(b []byte) bool {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.Test(b) {
return true
}
r.Add(b)
return false
}
4 changes: 4 additions & 0 deletions internal/saltfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ func TestSalt(b []byte) bool {
func AddSalt(b []byte) {
getSaltFilterSingleton().Add(b)
}

func CheckSalt(b []byte) bool {
return getSaltFilterSingleton().Test(b)
}
7 changes: 3 additions & 4 deletions shadowaead/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ func Unpack(dst, pkt []byte, ciph Cipher) ([]byte, error) {
return nil, ErrShortPacket
}
salt := pkt[:saltSize]
if internal.TestSalt(salt) {
return nil, ErrRepeatedSalt
}
aead, err := ciph.Decrypter(salt)
if err != nil {
return nil, err
}
internal.AddSalt(salt)
if internal.CheckSalt(salt) {
return nil, ErrRepeatedSalt
}
if len(pkt) < saltSize+aead.Overhead() {
return nil, ErrShortPacket
}
Expand Down
8 changes: 4 additions & 4 deletions shadowaead/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ func (c *streamConn) initReader() error {
if _, err := io.ReadFull(c.Conn, salt); err != nil {
return err
}
if internal.TestSalt(salt) {
return ErrRepeatedSalt
}
aead, err := c.Decrypter(salt)
if err != nil {
return err
}
internal.AddSalt(salt)

if internal.CheckSalt(salt) {
return ErrRepeatedSalt
}

c.r = newReader(c.Conn, aead)
return nil
Expand Down