|
| 1 | +Subject: [PATCH] Revert "[release-branch.go1.21] crypto/rand,runtime: switch RtlGenRandom for ProcessPrng" |
| 2 | +--- |
| 3 | +Index: src/crypto/rand/rand.go |
| 4 | +IDEA additional info: |
| 5 | +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP |
| 6 | +<+>UTF-8 |
| 7 | +=================================================================== |
| 8 | +diff --git a/src/crypto/rand/rand.go b/src/crypto/rand/rand.go |
| 9 | +--- a/src/crypto/rand/rand.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7) |
| 10 | ++++ b/src/crypto/rand/rand.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1) |
| 11 | +@@ -15,7 +15,7 @@ |
| 12 | + // available, /dev/urandom otherwise. |
| 13 | + // On OpenBSD and macOS, Reader uses getentropy(2). |
| 14 | + // On other Unix-like systems, Reader reads from /dev/urandom. |
| 15 | +-// On Windows systems, Reader uses the ProcessPrng API. |
| 16 | ++// On Windows systems, Reader uses the RtlGenRandom API. |
| 17 | + // On JS/Wasm, Reader uses the Web Crypto API. |
| 18 | + // On WASIP1/Wasm, Reader uses random_get from wasi_snapshot_preview1. |
| 19 | + var Reader io.Reader |
| 20 | +Index: src/crypto/rand/rand_windows.go |
| 21 | +IDEA additional info: |
| 22 | +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP |
| 23 | +<+>UTF-8 |
| 24 | +=================================================================== |
| 25 | +diff --git a/src/crypto/rand/rand_windows.go b/src/crypto/rand/rand_windows.go |
| 26 | +--- a/src/crypto/rand/rand_windows.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7) |
| 27 | ++++ b/src/crypto/rand/rand_windows.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1) |
| 28 | +@@ -15,8 +15,11 @@ |
| 29 | + |
| 30 | + type rngReader struct{} |
| 31 | + |
| 32 | +-func (r *rngReader) Read(b []byte) (int, error) { |
| 33 | +- if err := windows.ProcessPrng(b); err != nil { |
| 34 | ++func (r *rngReader) Read(b []byte) (n int, err error) { |
| 35 | ++ // RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at |
| 36 | ++ // most 1<<31-1 bytes at a time so that this works the same on 32-bit |
| 37 | ++ // and 64-bit systems. |
| 38 | ++ if err := batched(windows.RtlGenRandom, 1<<31-1)(b); err != nil { |
| 39 | + return 0, err |
| 40 | + } |
| 41 | + return len(b), nil |
| 42 | +Index: src/internal/syscall/windows/syscall_windows.go |
| 43 | +IDEA additional info: |
| 44 | +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP |
| 45 | +<+>UTF-8 |
| 46 | +=================================================================== |
| 47 | +diff --git a/src/internal/syscall/windows/syscall_windows.go b/src/internal/syscall/windows/syscall_windows.go |
| 48 | +--- a/src/internal/syscall/windows/syscall_windows.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7) |
| 49 | ++++ b/src/internal/syscall/windows/syscall_windows.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1) |
| 50 | +@@ -384,7 +384,7 @@ |
| 51 | + //sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock |
| 52 | + //sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW |
| 53 | + |
| 54 | +-//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng |
| 55 | ++//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036 |
| 56 | + |
| 57 | + //sys RtlLookupFunctionEntry(pc uintptr, baseAddress *uintptr, table *byte) (ret uintptr) = kernel32.RtlLookupFunctionEntry |
| 58 | + //sys RtlVirtualUnwind(handlerType uint32, baseAddress uintptr, pc uintptr, entry uintptr, ctxt uintptr, data *uintptr, frame *uintptr, ctxptrs *byte) (ret uintptr) = kernel32.RtlVirtualUnwind |
| 59 | +Index: src/internal/syscall/windows/zsyscall_windows.go |
| 60 | +IDEA additional info: |
| 61 | +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP |
| 62 | +<+>UTF-8 |
| 63 | +=================================================================== |
| 64 | +diff --git a/src/internal/syscall/windows/zsyscall_windows.go b/src/internal/syscall/windows/zsyscall_windows.go |
| 65 | +--- a/src/internal/syscall/windows/zsyscall_windows.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7) |
| 66 | ++++ b/src/internal/syscall/windows/zsyscall_windows.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1) |
| 67 | +@@ -37,14 +37,13 @@ |
| 68 | + } |
| 69 | + |
| 70 | + var ( |
| 71 | +- modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll")) |
| 72 | +- modbcryptprimitives = syscall.NewLazyDLL(sysdll.Add("bcryptprimitives.dll")) |
| 73 | +- modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll")) |
| 74 | +- modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll")) |
| 75 | +- modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll")) |
| 76 | +- modpsapi = syscall.NewLazyDLL(sysdll.Add("psapi.dll")) |
| 77 | +- moduserenv = syscall.NewLazyDLL(sysdll.Add("userenv.dll")) |
| 78 | +- modws2_32 = syscall.NewLazyDLL(sysdll.Add("ws2_32.dll")) |
| 79 | ++ modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll")) |
| 80 | ++ modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll")) |
| 81 | ++ modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll")) |
| 82 | ++ modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll")) |
| 83 | ++ modpsapi = syscall.NewLazyDLL(sysdll.Add("psapi.dll")) |
| 84 | ++ moduserenv = syscall.NewLazyDLL(sysdll.Add("userenv.dll")) |
| 85 | ++ modws2_32 = syscall.NewLazyDLL(sysdll.Add("ws2_32.dll")) |
| 86 | + |
| 87 | + procAdjustTokenPrivileges = modadvapi32.NewProc("AdjustTokenPrivileges") |
| 88 | + procDuplicateTokenEx = modadvapi32.NewProc("DuplicateTokenEx") |
| 89 | +@@ -53,7 +52,7 @@ |
| 90 | + procOpenThreadToken = modadvapi32.NewProc("OpenThreadToken") |
| 91 | + procRevertToSelf = modadvapi32.NewProc("RevertToSelf") |
| 92 | + procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation") |
| 93 | +- procProcessPrng = modbcryptprimitives.NewProc("ProcessPrng") |
| 94 | ++ procSystemFunction036 = modadvapi32.NewProc("SystemFunction036") |
| 95 | + procGetAdaptersAddresses = modiphlpapi.NewProc("GetAdaptersAddresses") |
| 96 | + procCreateEventW = modkernel32.NewProc("CreateEventW") |
| 97 | + procGetACP = modkernel32.NewProc("GetACP") |
| 98 | +@@ -149,12 +148,12 @@ |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | +-func ProcessPrng(buf []byte) (err error) { |
| 103 | ++func RtlGenRandom(buf []byte) (err error) { |
| 104 | + var _p0 *byte |
| 105 | + if len(buf) > 0 { |
| 106 | + _p0 = &buf[0] |
| 107 | + } |
| 108 | +- r1, _, e1 := syscall.Syscall(procProcessPrng.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0) |
| 109 | ++ r1, _, e1 := syscall.Syscall(procSystemFunction036.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0) |
| 110 | + if r1 == 0 { |
| 111 | + err = errnoErr(e1) |
| 112 | + } |
| 113 | +Index: src/runtime/os_windows.go |
| 114 | +IDEA additional info: |
| 115 | +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP |
| 116 | +<+>UTF-8 |
| 117 | +=================================================================== |
| 118 | +diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go |
| 119 | +--- a/src/runtime/os_windows.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7) |
| 120 | ++++ b/src/runtime/os_windows.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1) |
| 121 | +@@ -127,8 +127,15 @@ |
| 122 | + _AddVectoredContinueHandler, |
| 123 | + _ stdFunction |
| 124 | + |
| 125 | +- // Use ProcessPrng to generate cryptographically random data. |
| 126 | +- _ProcessPrng stdFunction |
| 127 | ++ // Use RtlGenRandom to generate cryptographically random data. |
| 128 | ++ // This approach has been recommended by Microsoft (see issue |
| 129 | ++ // 15589 for details). |
| 130 | ++ // The RtlGenRandom is not listed in advapi32.dll, instead |
| 131 | ++ // RtlGenRandom function can be found by searching for SystemFunction036. |
| 132 | ++ // Also some versions of Mingw cannot link to SystemFunction036 |
| 133 | ++ // when building executable as Cgo. So load SystemFunction036 |
| 134 | ++ // manually during runtime startup. |
| 135 | ++ _RtlGenRandom stdFunction |
| 136 | + |
| 137 | + // Load ntdll.dll manually during startup, otherwise Mingw |
| 138 | + // links wrong printf function to cgo executable (see issue |
| 139 | +@@ -145,12 +152,12 @@ |
| 140 | + ) |
| 141 | + |
| 142 | + var ( |
| 143 | +- bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0} |
| 144 | +- kernel32dll = [...]uint16{'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0} |
| 145 | +- ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0} |
| 146 | +- powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0} |
| 147 | +- winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0} |
| 148 | +- ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0} |
| 149 | ++ advapi32dll = [...]uint16{'a', 'd', 'v', 'a', 'p', 'i', '3', '2', '.', 'd', 'l', 'l', 0} |
| 150 | ++ kernel32dll = [...]uint16{'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0} |
| 151 | ++ ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0} |
| 152 | ++ powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0} |
| 153 | ++ winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0} |
| 154 | ++ ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0} |
| 155 | + ) |
| 156 | + |
| 157 | + // Function to be called by windows CreateThread |
| 158 | +@@ -249,11 +256,11 @@ |
| 159 | + } |
| 160 | + _AddVectoredContinueHandler = windowsFindfunc(k32, []byte("AddVectoredContinueHandler\000")) |
| 161 | + |
| 162 | +- bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:]) |
| 163 | +- if bcryptPrimitives == 0 { |
| 164 | +- throw("bcryptprimitives.dll not found") |
| 165 | ++ a32 := windowsLoadSystemLib(advapi32dll[:]) |
| 166 | ++ if a32 == 0 { |
| 167 | ++ throw("advapi32.dll not found") |
| 168 | + } |
| 169 | +- _ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000")) |
| 170 | ++ _RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000")) |
| 171 | + |
| 172 | + n32 := windowsLoadSystemLib(ntdlldll[:]) |
| 173 | + if n32 == 0 { |
| 174 | +@@ -610,7 +617,7 @@ |
| 175 | + //go:nosplit |
| 176 | + func getRandomData(r []byte) { |
| 177 | + n := 0 |
| 178 | +- if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 { |
| 179 | ++ if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 { |
| 180 | + n = len(r) |
| 181 | + } |
| 182 | + extendRandom(r, n) |
0 commit comments