Skip to content

Commit dcc9bdf

Browse files
randall77gopherbot
authored andcommitted
[release-branch.go1.20] crypto/subtle: don't cast to *uintptr when word size is 0
Casting to a *uintptr is not ok if there isn't at least 8 bytes of data backing that pointer (on 64-bit archs). So although we end up making a slice of 0 length with that pointer, the cast itself doesn't know that. Instead, bail early if the result is going to be 0 length. Fixes #59336 Change-Id: Id3c0e09d341d838835c0382cccfb0f71dc3dc7e6 Reviewed-on: https://go-review.googlesource.com/c/go/+/480575 Run-TryBot: Keith Randall <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> (cherry picked from commit 297cf6dd31bd99fc4ccda320aa3d4faf290ab278) Reviewed-on: https://go-review.googlesource.com/c/go/+/481238 Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Michael Knyszek <[email protected]> Run-TryBot: Michael Knyszek <[email protected]>
1 parent 5c7c20e commit dcc9bdf

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/crypto/subtle/xor_generic.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ func aligned(dst, x, y *byte) bool {
4646
// words returns a []uintptr pointing at the same data as x,
4747
// with any trailing partial word removed.
4848
func words(x []byte) []uintptr {
49-
return unsafe.Slice((*uintptr)(unsafe.Pointer(&x[0])), uintptr(len(x))/wordSize)
49+
n := uintptr(len(x)) / wordSize
50+
if n == 0 {
51+
// Avoid creating a *uintptr that refers to data smaller than a uintptr;
52+
// see issue 59334.
53+
return nil
54+
}
55+
return unsafe.Slice((*uintptr)(unsafe.Pointer(&x[0])), n)
5056
}
5157

5258
func xorLoop[T byte | uintptr](dst, x, y []T) {

test/fixedbugs/issue59334.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run -tags=purego -gcflags=all=-d=checkptr
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import "crypto/subtle"
10+
11+
func main() {
12+
dst := make([]byte, 5)
13+
src := make([]byte, 5)
14+
for _, n := range []int{1024, 2048} { // just to make the size non-constant
15+
b := make([]byte, n)
16+
subtle.XORBytes(dst, src, b[n-5:])
17+
}
18+
}

0 commit comments

Comments
 (0)