Skip to content

Commit 1ac029c

Browse files
dragonfyre13narimiran
authored andcommitted
Fix #19052; [backport:1.6.0] (#19053)
* Fix #19052; [backport:1.6.0] Adds a compile flag to avoid a getrandom syscall, fixing #19052. This is neccesary when the getrandom syscall is missing, as noted in #19052, particularly in kernel versions < 3.17 when getrandom was introduced. Specifically relevant is this is missing from kernel 3.10, which is the supported kernel throughout RHEL 7 and CentOS 7, which is widely used at many organizations. Without this, versions of nim that include sysrand (i.e. versions >= 1.6.0) will not compile without modification, however with this change a compile flag may be used to fall back using /dev/urandom as done with any unknown Posix OS (preferred here as a fallback since it already supplies a cryptographically secure PRNG and existing code deals with entropy pool init, etc). The change is placed behind a compile flag, as discussed in github ticket #19052 (summed up here): * First, I can't seem to catch that a importc such as SYS_getrandom is declared without using it (the declared proc returns true, but compiler throws an undeclared identifier flag when referencing it). * Second, it seemed preferable to be behaviorally explicit vs implicit when considering this is intended to be a cryptographically secure PRNG. * Third, if I intend to compile on a kernel >= 3.17 while running the binary on at least one system < 3.17, I'll want to be able to target this without relying on a compile time determination if the getrandom syscall is available. * Documenting compile flag for -d:nimNoGetRandom and adding changelog entry Related to #19052 and comments in PR #19053. Also created a new changelog file since none currently exists. Co-authored-by: Timothy Alexander <[email protected]> (cherry picked from commit dde5566)
1 parent b18b636 commit 1ac029c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

changelogs/changelog.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# v1.xx.x - yyyy-mm-dd
2+
3+
## Changes affecting backward compatibility
4+
5+
## Standard library additions and changes
6+
7+
### New compile flag (`-d:nimNoGetRandom`) when building `std/sysrand` to remove dependency on linux `getrandom` syscall
8+
9+
This compile flag only affects linux builds and is necessary if either compiling on a linux kernel version < 3.17, or if code built will be executing on kernel < 3.17.
10+
11+
On linux kernels < 3.17 (such as kernel 3.10 in RHEL7 and CentOS7), the `getrandom` syscall was not yet introduced. Without this, the `std/sysrand` module will not build properly, and if code is built on a kernel >= 3.17 without the flag, any usage of the `std/sysrand` module will fail to execute on a kernel < 3.17 (since it attempts to perform a syscall to `getrandom`, which isn't present in the current kernel). A compile flag has been added to force the `std/sysrand` module to use /dev/urandom (available since linux kernel 1.3.30), rather than the `getrandom` syscall. This allows for use of a cryptographically secure PRNG, regardless of kernel support for the `getrandom` syscall.
12+
13+
When building for RHEL7/CentOS7 for example, the entire build process for nim from a source package would then be:
14+
```sh
15+
$ yum install devtoolset-8 # Install GCC version 8 vs the standard 4.8.5 on RHEL7/CentOS7. Alternatively use -d:nimEmulateOverflowChecks. See issue #13692 for details
16+
$ scl enable devtoolset-8 bash # Run bash shell with default toolchain of gcc 8
17+
$ sh build.sh # per unix install instructions
18+
$ bin/nim c koch # per unix install instructions
19+
$ ./koch boot -d:release # per unix install instructions
20+
$ ./koch tools -d:nimNoGetRandom # pass the nimNoGetRandom flag to compile std/sysrand without support for getrandom syscall
21+
```
22+
23+
This is necessary to pass when building nim on kernel versions < 3.17 in particular to avoid an error of "SYS_getrandom undeclared" during the build process for stdlib (sysrand in particular).
24+
25+
## Language changes
26+
27+
28+
## Compiler changes
29+
30+
31+
## Tool changes

lib/std/sysrand.nim

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
## .. _randomFillSync: https://nodejs.org/api/crypto.html#crypto_crypto_randomfillsync_buffer_offset_size
3939
## .. _/dev/urandom: https://en.wikipedia.org/wiki//dev/random
4040
##
41+
## On a Linux target, a call to the `getrandom` syscall can be avoided (e.g.
42+
## for targets running kernel version < 3.17) by passing a compile flag of
43+
## `-d:nimNoGetRandom`. If this flag is passed, sysrand will use `/dev/urandom`
44+
## as with any other POSIX compliant OS.
45+
##
4146

4247
runnableExamples:
4348
doAssert urandom(0).len == 0
@@ -159,7 +164,7 @@ elif defined(windows):
159164

160165
result = randomBytes(addr dest[0], size)
161166

162-
elif defined(linux):
167+
elif defined(linux) and not defined(nimNoGetRandom):
163168
# TODO using let, pending bootstrap >= 1.4.0
164169
var SYS_getrandom {.importc: "SYS_getrandom", header: "<sys/syscall.h>".}: clong
165170
const syscallHeader = """#include <unistd.h>

0 commit comments

Comments
 (0)