Skip to content

Commit 5687323

Browse files
authored
Merge pull request #2994 from euantorano/fix/1626-os-getRandomBytesDevURandom
Check if /dev/urandom is a character device
2 parents 57830e4 + 08251fb commit 5687323

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

std/os.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ fn getRandomBytesDevURandom(buf: []u8) !void {
133133
const fd = try openC(c"/dev/urandom", O_RDONLY | O_CLOEXEC, 0);
134134
defer close(fd);
135135

136+
const st = try fstat(fd);
137+
if (!S_ISCHR(st.mode)) {
138+
return error.NoDevice;
139+
}
140+
136141
const stream = &std.fs.File.openHandle(fd).inStream().stream;
137142
stream.readNoEof(buf) catch return error.Unexpected;
138143
}

std/os/bits/darwin.zig

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,3 +1116,62 @@ pub const stack_t = extern struct {
11161116
ss_size: isize,
11171117
ss_flags: i32,
11181118
};
1119+
1120+
pub const S_IFMT = 0o170000;
1121+
1122+
pub const S_IFIFO = 0o010000;
1123+
pub const S_IFCHR = 0o020000;
1124+
pub const S_IFDIR = 0o040000;
1125+
pub const S_IFBLK = 0o060000;
1126+
pub const S_IFREG = 0o100000;
1127+
pub const S_IFLNK = 0o120000;
1128+
pub const S_IFSOCK = 0o140000;
1129+
pub const S_IFWHT = 0o160000;
1130+
1131+
pub const S_ISUID = 0o4000;
1132+
pub const S_ISGID = 0o2000;
1133+
pub const S_ISVTX = 0o1000;
1134+
pub const S_IRWXU = 0o700;
1135+
pub const S_IRUSR = 0o400;
1136+
pub const S_IWUSR = 0o200;
1137+
pub const S_IXUSR = 0o100;
1138+
pub const S_IRWXG = 0o070;
1139+
pub const S_IRGRP = 0o040;
1140+
pub const S_IWGRP = 0o020;
1141+
pub const S_IXGRP = 0o010;
1142+
pub const S_IRWXO = 0o007;
1143+
pub const S_IROTH = 0o004;
1144+
pub const S_IWOTH = 0o002;
1145+
pub const S_IXOTH = 0o001;
1146+
1147+
pub fn S_ISFIFO(m: u32) bool {
1148+
return m & S_IFMT == S_IFIFO;
1149+
}
1150+
1151+
pub fn S_ISCHR(m: u32) bool {
1152+
return m & S_IFMT == S_IFCHR;
1153+
}
1154+
1155+
pub fn S_ISDIR(m: u32) bool {
1156+
return m & S_IFMT == S_IFDIR;
1157+
}
1158+
1159+
pub fn S_ISBLK(m: u32) bool {
1160+
return m & S_IFMT == S_IFBLK;
1161+
}
1162+
1163+
pub fn S_ISREG(m: u32) bool {
1164+
return m & S_IFMT == S_IFREG;
1165+
}
1166+
1167+
pub fn S_ISLNK(m: u32) bool {
1168+
return m & S_IFMT == S_IFLNK;
1169+
}
1170+
1171+
pub fn S_ISSOCK(m: u32) bool {
1172+
return m & S_IFMT == S_IFSOCK;
1173+
}
1174+
1175+
pub fn S_IWHT(m: u32) bool {
1176+
return m & S_IFMT == S_IFWHT;
1177+
}

std/os/bits/freebsd.zig

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,62 @@ pub const stack_t = extern struct {
876876
ss_size: isize,
877877
ss_flags: i32,
878878
};
879+
880+
pub const S_IFMT = 0o170000;
881+
882+
pub const S_IFIFO = 0o010000;
883+
pub const S_IFCHR = 0o020000;
884+
pub const S_IFDIR = 0o040000;
885+
pub const S_IFBLK = 0o060000;
886+
pub const S_IFREG = 0o100000;
887+
pub const S_IFLNK = 0o120000;
888+
pub const S_IFSOCK = 0o140000;
889+
pub const S_IFWHT = 0o160000;
890+
891+
pub const S_ISUID = 0o4000;
892+
pub const S_ISGID = 0o2000;
893+
pub const S_ISVTX = 0o1000;
894+
pub const S_IRWXU = 0o700;
895+
pub const S_IRUSR = 0o400;
896+
pub const S_IWUSR = 0o200;
897+
pub const S_IXUSR = 0o100;
898+
pub const S_IRWXG = 0o070;
899+
pub const S_IRGRP = 0o040;
900+
pub const S_IWGRP = 0o020;
901+
pub const S_IXGRP = 0o010;
902+
pub const S_IRWXO = 0o007;
903+
pub const S_IROTH = 0o004;
904+
pub const S_IWOTH = 0o002;
905+
pub const S_IXOTH = 0o001;
906+
907+
pub fn S_ISFIFO(m: u32) bool {
908+
return m & S_IFMT == S_IFIFO;
909+
}
910+
911+
pub fn S_ISCHR(m: u32) bool {
912+
return m & S_IFMT == S_IFCHR;
913+
}
914+
915+
pub fn S_ISDIR(m: u32) bool {
916+
return m & S_IFMT == S_IFDIR;
917+
}
918+
919+
pub fn S_ISBLK(m: u32) bool {
920+
return m & S_IFMT == S_IFBLK;
921+
}
922+
923+
pub fn S_ISREG(m: u32) bool {
924+
return m & S_IFMT == S_IFREG;
925+
}
926+
927+
pub fn S_ISLNK(m: u32) bool {
928+
return m & S_IFMT == S_IFLNK;
929+
}
930+
931+
pub fn S_ISSOCK(m: u32) bool {
932+
return m & S_IFMT == S_IFSOCK;
933+
}
934+
935+
pub fn S_IWHT(m: u32) bool {
936+
return m & S_IFMT == S_IFWHT;
937+
}

std/os/darwin.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub const is_the_target = switch (builtin.os) {
55
else => false,
66
};
77
pub usingnamespace std.c;
8+
pub usingnamespace @import("bits.zig");

std/os/freebsd.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ const std = @import("../std.zig");
22
const builtin = @import("builtin");
33
pub const is_the_target = builtin.os == .freebsd;
44
pub usingnamespace std.c;
5+
pub usingnamespace @import("bits.zig");

0 commit comments

Comments
 (0)