raz / commit 5b96aba7

commit
5b96aba75d3c31f0ae8d3219a25d454ef9a66da1
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
21ab42f1
Sys.random: stop on an error but EINTR, not spin

patch · log

2 files changed, +28 -4

lib/Sys.linux-x86_64.raz (history) modified +14 -2

@@ -711,14 +711,26 @@
711711 repeat until SYSCALL(230, 1, 1, ADR(t), 0) # -4; (* clock_nanosleep until t, again after EINTR *)
712712 end;
713713
714-(* Fills b with random bytes, from the kernel. *)
714+(* Ends the program: random bytes can't be faked. *)
715+proc noRandom;
716+begin
717+ _ := write(stderr, "Sys.random: the kernel gave no random bytes\n");
718+ _ := SYSCALL(231, 2); (* exit *)
719+end;
720+
721+(* Fills b with random bytes, from the kernel. Random bytes can't be
722+ faked, so if the kernel can't give them (no getrandom, or a filter
723+ refusing it) it says so on standard error and ends the program by
724+ exit with status 2. *)
715725 proc random*(b: []U8);
716726 var n, r: INT;
717727 begin
718728 n := 0;
719729 while n < LEN(b) do (* a signal may cut it short *)
720730 r := SYSCALL(318, ADR(b[n]), LEN(b) - n, 0); (* getrandom *)
721- if r > 0 then INC(n, r); end;
731+ if r > 0 then INC(n, r);
732+ elsif r # errIntr then noRandom();
733+ end;
722734 end;
723735 end;
724736

lib/Sys.raz (history) modified +14 -2

@@ -783,14 +783,26 @@
783783 repeat until C.clock_nanosleep(CAbi.clockMonotonic, CAbi.timerAbstime, ^U8(ADR(t)), nil) # CAbi.eIntr;
784784 end;
785785
786-(* Fills b with random bytes, from the kernel. *)
786+(* Ends the program: random bytes can't be faked. *)
787+proc noRandom;
788+begin
789+ _ := write(stderr, "Sys.random: the kernel gave no random bytes\n");
790+ C._exit(2);
791+end;
792+
793+(* Fills b with random bytes, from the kernel. Random bytes can't be
794+ faked, so if the kernel can't give them (no getrandom, or a filter
795+ refusing it) it says so on standard error and ends the program by
796+ exit with status 2. *)
787797 proc random*(b: []U8);
788798 var n, r: INT;
789799 begin
790800 n := 0;
791801 while n < LEN(b) do (* a signal may cut it short *)
792802 r := C.getrandom(ADR(b[n]), UINT(LEN(b) - n), 0);
793- if r > 0 then INC(n, r); end;
803+ if r > 0 then INC(n, r);
804+ elsif (r >= 0) or (failed() # errIntr) then noRandom(); (* r = 0 can't be, and is no progress *)
805+ end;
794806 end;
795807 end;
796808