Skip to content

Commit

Permalink
runtime: use getrandom(2) for readRandom on Linux
Browse files Browse the repository at this point in the history
Add Getrandom to internal/runtime/syscall.

Use Getrandom instead of reading /dev/urandom to simplify the code.

Since Go 1.24 needs Linux kernel >= 3.2, and getrandom(2) was
introduced by Linux kernel 3.17, Getrandom could return ENOSYS.
This doesn't matter because the Linux kernel always sets startupRand,
so readRandom is usually not reachable. We also have a time-based
fallback even if readRandom fails.

Updates golang#51087
Fixes golang#68278
  • Loading branch information
apocelipes committed Sep 12, 2024
1 parent e9a500f commit 65fd37e
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/internal/runtime/syscall/defs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ const (
EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3
EFD_CLOEXEC = 0x80000
GRND_NONBLOCK = 0x01
GRND_RANDOM = 0x02
GRND_INSECURE = 0x04
)
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_386.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
SYS_EPOLL_CREATE1 = 329
SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 328
SYS_GETRANDOM = 355

EFD_NONBLOCK = 0x800
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
SYS_EPOLL_CREATE1 = 291
SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 290
SYS_GETRANDOM = 318

EFD_NONBLOCK = 0x800
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
SYS_EPOLL_CREATE1 = 357
SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 356
SYS_GETRANDOM = 384

EFD_NONBLOCK = 0x800
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
SYS_MPROTECT = 226
SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 19
SYS_GETRANDOM = 278

EFD_NONBLOCK = 0x800
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_loong64.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
SYS_MPROTECT = 226
SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 19
SYS_GETRANDOM = 278

EFD_NONBLOCK = 0x800
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_mips64x.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
SYS_EPOLL_CREATE1 = 5285
SYS_EPOLL_PWAIT2 = 5441
SYS_EVENTFD2 = 5284
SYS_GETRANDOM = 5313

EFD_NONBLOCK = 0x80
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_mipsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
SYS_EPOLL_CREATE1 = 4326
SYS_EPOLL_PWAIT2 = 4441
SYS_EVENTFD2 = 4325
SYS_GETRANDOM = 4353

EFD_NONBLOCK = 0x80
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_ppc64x.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
SYS_EPOLL_CREATE1 = 315
SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 314
SYS_GETRANDOM = 359

EFD_NONBLOCK = 0x800
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_riscv64.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
SYS_MPROTECT = 226
SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 19
SYS_GETRANDOM = 278

EFD_NONBLOCK = 0x800
)
Expand Down
1 change: 1 addition & 0 deletions src/internal/runtime/syscall/defs_linux_s390x.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
SYS_EPOLL_CREATE1 = 327
SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 323
SYS_GETRANDOM = 349

EFD_NONBLOCK = 0x800
)
Expand Down
11 changes: 11 additions & 0 deletions src/internal/runtime/syscall/syscall_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ func Eventfd(initval, flags int32) (fd int32, errno uintptr) {
r1, _, e := Syscall6(SYS_EVENTFD2, uintptr(initval), uintptr(flags), 0, 0, 0, 0)
return int32(r1), e
}

func Getrandom(buf []byte, flags int32) (ret int32, errno uintptr) {
var p unsafe.Pointer
if len(buf) > 0 {
p = unsafe.Pointer(&buf[0])
} else {
p = unsafe.Pointer(&_zero)
}
r, _, e := Syscall6(SYS_GETRANDOM, uintptr(p), uintptr(len(buf)), uintptr(flags), 0, 0, 0)
return int32(r), e
}
6 changes: 1 addition & 5 deletions src/runtime/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,8 @@ func osinit() {
osArchInit()
}

var urandom_dev = []byte("/dev/urandom\x00")

func readRandom(r []byte) int {
fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
closefd(fd)
n, _ := syscall.Getrandom(r, 0)
return int(n)
}

Expand Down

0 comments on commit 65fd37e

Please sign in to comment.