Skip to content

Commit ce35c67

Browse files
authored
netbsd: fix potential panic (#519)
The code was assuming that syscall returns either -1 or provided length. Fix code to account for potential bad return results.
1 parent 1c029c8 commit ce35c67

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

.github/workflows/nopanic.yaml

+5-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ jobs:
7474
toolchain: stable
7575
components: rust-src
7676
targets: aarch64-unknown-linux-gnu,x86_64-unknown-netbsd,x86_64-unknown-freebsd,x86_64-pc-solaris
77+
# TODO: use pre-compiled cross after a new (post-0.2.5) release
7778
- name: Install cross
7879
run: cargo install cross --git https://github.com/cross-rs/cross
7980

@@ -89,10 +90,10 @@ jobs:
8990
- name: Check (getrandom.rs)
9091
run: ret=$(grep panic target/x86_64-unknown-freebsd/release/libgetrandom_wrapper.so; echo $?); [ $ret -eq 1 ]
9192

92-
# - name: Build (netbsd.rs)
93-
# run: cross build --release --target=x86_64-unknown-netbsd
94-
# - name: Check (netbsd.rs)
95-
# run: ret=$(grep panic target/x86_64-unknown-netbsd/release/libgetrandom_wrapper.so; echo $?); [ $ret -eq 1 ]
93+
- name: Build (netbsd.rs)
94+
run: cross build --release --target=x86_64-unknown-netbsd
95+
- name: Check (netbsd.rs)
96+
run: ret=$(grep panic target/x86_64-unknown-netbsd/release/libgetrandom_wrapper.so; echo $?); [ $ret -eq 1 ]
9697

9798
# - name: Build (solaris.rs)
9899
# run: cross build --release --target=x86_64-pc-solaris

src/netbsd.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ unsafe extern "C" fn polyfill_using_kern_arand(
2525
// NetBSD will only return up to 256 bytes at a time, and
2626
// older NetBSD kernels will fail on longer buffers.
2727
let mut len = cmp::min(buflen, 256);
28+
let expected_ret = libc::c_int::try_from(len).expect("len is bounded by 256");
2829

2930
let ret = unsafe { libc::sysctl(MIB.as_ptr(), MIB_LEN, buf, &mut len, ptr::null(), 0) };
30-
if ret == -1 {
31+
32+
if ret == expected_ret {
33+
libc::ssize_t::try_from(ret).expect("len is bounded by 256")
34+
} else if ret == -1 {
3135
-1
3236
} else {
33-
libc::ssize_t::try_from(len).expect("len is bounded by 256")
37+
// Zero return result will be converted into `Error::UNEXPECTED` by `sys_fill_exact`
38+
0
3439
}
3540
}
3641

0 commit comments

Comments
 (0)