Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: use getrandom(2) for readRandom on Linux #69421

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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