Skip to content

Commit

Permalink
Revert "crypto/rand: add randcrash=0 GODEBUG"
Browse files Browse the repository at this point in the history
A GODEBUG is actually a security risk here: most programs will start to
ignore errors from Read because they can't happen (which is the intended
behavior), but then if a program is run with GODEBUG=randcrash=0 it will
use a partial buffer in case an error occurs, which may be catastrophic.

Note that the proposal was accepted without the GODEBUG, which was only
added later.

This (partially) reverts CL 608435. I kept the tests.

Updates #66821

Change-Id: I3fd20f9cae0d34115133fe935f0cfc7a741a2662
Reviewed-on: https://go-review.googlesource.com/c/go/+/622115
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Carlos Amedee <[email protected]>
Reviewed-by: Roland Shoemaker <[email protected]>
Auto-Submit: Filippo Valsorda <[email protected]>
Reviewed-by: Daniel McCarney <[email protected]>
  • Loading branch information
FiloSottile authored and gopherbot committed Oct 28, 2024
1 parent 7a256ad commit 0138c1a
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 26 deletions.
5 changes: 0 additions & 5 deletions doc/godebug.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,6 @@ For Go 1.24, it now defaults to multipathtcp="2", thus
enabled by default on listerners. Using multipathtcp="0" reverts to the
pre-Go 1.24 behavior.

Go 1.24 changed [`crypto/rand.Read`](/pkg/crypto/rand/#Read) to crash the
program on any error. This setting is controlled by the `randcrash` setting.
For Go 1.24 it defaults to `randcrash=1`.
Using `randcrash=0` reverts to the pre-Go 1.24 behavior.

### Go 1.23

Go 1.23 changed the channels created by package time to be unbuffered
Expand Down
7 changes: 0 additions & 7 deletions src/crypto/rand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package rand

import (
"crypto/internal/boring"
"internal/godebug"
"io"
"os"
"sync"
Expand Down Expand Up @@ -65,8 +64,6 @@ func (r *reader) Read(b []byte) (n int, err error) {
//go:linkname fatal
func fatal(string)

var randcrash = godebug.New("randcrash")

// Read fills b with cryptographically secure random bytes. It never returns an
// error, and always fills b entirely.
//
Expand All @@ -86,10 +83,6 @@ func Read(b []byte) (n int, err error) {
copy(b, bb)
}
if err != nil {
if randcrash.Value() == "0" {
randcrash.IncNonDefault()
return 0, err
}
fatal("crypto/rand: failed to read random data (see https://go.dev/issue/66821): " + err.Error())
panic("unreachable") // To be sure.
}
Expand Down
10 changes: 1 addition & 9 deletions src/crypto/rand/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ func TestReadError(t *testing.T) {
}
testenv.MustHaveExec(t)

// We run this test in a subprocess because it's expected to crash the
// program unless the GODEBUG is set.
// We run this test in a subprocess because it's expected to crash.
if os.Getenv("GO_TEST_READ_ERROR") == "1" {
defer func(r io.Reader) { Reader = r }(Reader)
Reader = readerFunc(func([]byte) (int, error) {
Expand All @@ -221,13 +220,6 @@ func TestReadError(t *testing.T) {
if !bytes.Contains(out, []byte(exp)) {
t.Errorf("subprocess output does not contain %q: %s", exp, out)
}

cmd = testenv.Command(t, os.Args[0], "-test.run=TestReadError")
cmd.Env = append(os.Environ(), "GO_TEST_READ_ERROR=1", "GODEBUG=randcrash=0")
out, err = cmd.CombinedOutput()
if err != nil {
t.Errorf("subprocess failed: %v\n%s", err, out)
}
}

func BenchmarkRead(b *testing.B) {
Expand Down
1 change: 0 additions & 1 deletion src/internal/godebugs/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ var All = []Info{
{Name: "netedns0", Package: "net", Changed: 19, Old: "0"},
{Name: "panicnil", Package: "runtime", Changed: 21, Old: "1"},
{Name: "randautoseed", Package: "math/rand"},
{Name: "randcrash", Package: "crypto/rand", Changed: 24, Old: "0"},
{Name: "randseednop", Package: "math/rand", Changed: 24, Old: "0"},
{Name: "tarinsecurepath", Package: "archive/tar"},
{Name: "tls10server", Package: "crypto/tls", Changed: 22, Old: "1"},
Expand Down
4 changes: 0 additions & 4 deletions src/runtime/metrics/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,6 @@ Below is the full list of supported metrics, ordered lexicographically.
The number of non-default behaviors executed by the math/rand
package due to a non-default GODEBUG=randautoseed=... setting.
/godebug/non-default-behavior/randcrash:events
The number of non-default behaviors executed by the crypto/rand
package due to a non-default GODEBUG=randcrash=... setting.
/godebug/non-default-behavior/randseednop:events
The number of non-default behaviors executed by the math/rand
package due to a non-default GODEBUG=randseednop=... setting.
Expand Down

0 comments on commit 0138c1a

Please sign in to comment.