From a2eded3421f144983c0ccb9e6c0a325fa1ba1f82 Mon Sep 17 00:00:00 2001 From: Shenghou Ma Date: Tue, 5 Apr 2016 23:09:39 -0400 Subject: [PATCH] runtime: get randomness from AT_RANDOM AUXV on linux/arm64 Fixes #15147. Change-Id: Ibfe46c747dea987787a51eb0c95ccd8c5f24f366 Reviewed-on: https://go-review.googlesource.com/21580 Run-TryBot: Minux Ma Reviewed-by: Brad Fitzpatrick --- src/runtime/os_linux_arm64.go | 26 ++++++++++++++++++++++++++ src/runtime/vdso_none.go | 1 + 2 files changed, 27 insertions(+) diff --git a/src/runtime/os_linux_arm64.go b/src/runtime/os_linux_arm64.go index 3f994f128b..57184b0d3a 100644 --- a/src/runtime/os_linux_arm64.go +++ b/src/runtime/os_linux_arm64.go @@ -4,6 +4,11 @@ package runtime +import ( + "runtime/internal/sys" + "unsafe" +) + const ( _AT_NULL = 0 _AT_RANDOM = 25 // introduced in 2.6.29 @@ -11,6 +16,27 @@ const ( var randomNumber uint32 +func sysargs(argc int32, argv **byte) { + // skip over argv, envv to get to auxv + n := argc + 1 + for argv_index(argv, n) != nil { + n++ + } + n++ + auxv := (*[1 << 29]uint64)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize)) + + for i := 0; auxv[i] != _AT_NULL; i += 2 { + switch auxv[i] { + case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data + startupRandomData = (*[16]byte)(unsafe.Pointer(uintptr(auxv[i+1])))[:] + // the pointer provided may not be word aligned, so we must treat it + // as a byte array. + randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 | + uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24 + } + } +} + //go:nosplit func cputicks() int64 { // Currently cputicks() is used in blocking profiler and to seed fastrand1(). diff --git a/src/runtime/vdso_none.go b/src/runtime/vdso_none.go index b4e0a0e349..e14e1a4707 100644 --- a/src/runtime/vdso_none.go +++ b/src/runtime/vdso_none.go @@ -5,6 +5,7 @@ // +build !linux !amd64 // +build !linux !386 // +build !linux !arm +// +build !linux !arm64 package runtime -- 2.48.1