Skip to content

Commit 6db39aa

Browse files
authored
Merge pull request #25 from dsprenkels/issue_22
Fallback to old-style entropy count if ioctl returns ENOSYS
2 parents 4e986bb + a697b4c commit 6db39aa

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CFLAGS := -g -O2 -m64 -std=gnu99 \
1+
CFLAGS ?= -g -O2 -std=gnu99 \
22
-Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual -Wformat \
33
-Wformat-security -Werror=format-security -Wstrict-prototypes \
44
-D_FORTIFY_SOURCE=2 -fPIC -fno-strict-overflow

randombytes.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,17 @@ static int randombytes_linux_wait_for_entropy(int device)
138138

139139
/* If the device has enough entropy already, we will want to return early */
140140
retcode = randombytes_linux_read_entropy_ioctl(device, &entropy);
141-
if (retcode != 0 && errno == ENOTTY) {
142-
/* The ioctl call on /dev/urandom has failed due to a ENOTTY (i.e.
143-
* unsupported action). We will fall back to reading from
144-
* `/proc/sys/kernel/random/entropy_avail`. This is obviously less
145-
* ideal, but at this point it seems we have no better option. */
141+
// printf("errno: %d (%s)\n", errno, strerror(errno));
142+
if (retcode != 0 && (errno == ENOTTY || errno == ENOSYS)) {
143+
// The ioctl call on /dev/urandom has failed due to a
144+
// - ENOTTY (unsupported action), or
145+
// - ENOSYS (invalid ioctl; this happens on MIPS, see #22).
146+
//
147+
// We will fall back to reading from
148+
// `/proc/sys/kernel/random/entropy_avail`. This less ideal,
149+
// because it allocates a file descriptor, and it may not work
150+
// in a chroot. But at this point it seems we have no better
151+
// options left.
146152
strategy = PROC;
147153
// Open the entropy count file
148154
proc_file = fopen("/proc/sys/kernel/random/entropy_avail", "r");

randombytes_test.c

+15
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ static void test_issue_17(void) {
8787
assert(memcmp(buf1, buf2, sizeof(buf1)) != 0);
8888
}
8989

90+
static void test_issue_22(void) {
91+
uint8_t buf1[20] = {}, buf2[sizeof(buf1)] = {};
92+
const int ret1 = randombytes(buf1, sizeof(buf1));
93+
const int ret2 = randombytes(buf2, sizeof(buf2));
94+
assert(ret1 == 0);
95+
assert(ret2 == 0);
96+
assert(memcmp(buf1, buf2, sizeof(buf1)) != 0);
97+
}
98+
9099
// ======== Mock OS functions to simulate uncommon behavior ========
91100

92101
#if defined(__linux__) && defined(SYS_getrandom)
@@ -112,6 +121,10 @@ int __wrap_ioctl(int fd, int code, int* ret) {
112121
errno = ENOTTY;
113122
return -1;
114123
}
124+
if (current_test == test_issue_17) {
125+
errno = ENOSYS;
126+
return -1;
127+
}
115128
return __real_ioctl(fd, code, ret);
116129
}
117130
#endif /* defined(__linux__) && !defined(SYS_getrandom) */
@@ -133,8 +146,10 @@ int main(void) {
133146
#endif /* defined(__linux__) && defined(SYS_getrandom) */
134147
#if defined(__linux__) && !defined(SYS_getrandom)
135148
RUN_TEST(test_issue_17)
149+
RUN_TEST(test_issue_22)
136150
#else
137151
SKIP_TEST(test_issue_17)
152+
SKIP_TEST(test_issue_22)
138153
#endif /* defined(__linux__) && !defined(SYS_getrandom) */
139154
return 0;
140155
}

0 commit comments

Comments
 (0)