Просмотр исходного кода

salsa20/salsa: fix out of bounds write

Fixes golang/go#21104

Change-Id: I59054f9e2beed8a0c7efd513eb84795dc0308353
Reviewed-on: https://go-review.googlesource.com/52391
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Wei Congrui 8 лет назад
Родитель
Сommit
358f15eacb
2 измененных файлов с 20 добавлено и 0 удалено
  1. 1 0
      salsa20/salsa/salsa20_amd64.go
  2. 19 0
      salsa20/salsa/salsa_test.go

+ 1 - 0
salsa20/salsa/salsa20_amd64.go

@@ -19,5 +19,6 @@ func XORKeyStream(out, in []byte, counter *[16]byte, key *[32]byte) {
 	if len(in) == 0 {
 		return
 	}
+	_ = out[len(in)-1]
 	salsa2020XORKeyStream(&out[0], &in[0], uint64(len(in)), &counter[0], &key[0])
 }

+ 19 - 0
salsa20/salsa/salsa_test.go

@@ -33,3 +33,22 @@ func TestCore208(t *testing.T) {
 		t.Errorf("expected %x, got %x", out, in)
 	}
 }
+
+func TestOutOfBoundsWrite(t *testing.T) {
+	// encrypted "0123456789"
+	cipherText := []byte{170, 166, 196, 104, 175, 121, 68, 44, 174, 51}
+	var counter [16]byte
+	var key [32]byte
+	want := "abcdefghij"
+	plainText := []byte(want)
+	defer func() {
+		err := recover()
+		if err == nil {
+			t.Error("XORKeyStream expected to panic on len(dst) < len(src), but didn't")
+		}
+		if plainText[3] == '3' {
+			t.Errorf("XORKeyStream did out of bounds write, want %v, got %v", want, string(plainText))
+		}
+	}()
+	XORKeyStream(plainText[:3], cipherText, &counter, &key)
+}