This repository was archived by the owner on Mar 7, 2021. It is now read-only.
File tree 5 files changed +40
-0
lines changed
5 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -25,12 +25,16 @@ const INCLUDED_FUNCTIONS: &[&str] = &[
25
25
"_copy_from_user" ,
26
26
"alloc_chrdev_region" ,
27
27
"unregister_chrdev_region" ,
28
+ "wait_for_random_bytes" ,
29
+ "get_random_bytes" ,
30
+ "rng_is_initialized" ,
28
31
] ;
29
32
const INCLUDED_VARS : & [ & str ] = & [
30
33
"EINVAL" ,
31
34
"ENOMEM" ,
32
35
"ESPIPE" ,
33
36
"EFAULT" ,
37
+ "EAGAIN" ,
34
38
"__this_module" ,
35
39
"FS_REQUIRES_DEV" ,
36
40
"FS_BINARY_MOUNTDATA" ,
Original file line number Diff line number Diff line change 1
1
#include <linux/cdev.h>
2
2
#include <linux/fs.h>
3
3
#include <linux/module.h>
4
+ #include <linux/random.h>
4
5
#include <linux/slab.h>
5
6
#include <linux/uaccess.h>
6
7
#include <linux/version.h>
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ impl Error {
10
10
pub const ENOMEM : Self = Error ( -( bindings:: ENOMEM as i32 ) ) ;
11
11
pub const EFAULT : Self = Error ( -( bindings:: EFAULT as i32 ) ) ;
12
12
pub const ESPIPE : Self = Error ( -( bindings:: ESPIPE as i32 ) ) ;
13
+ pub const EAGAIN : Self = Error ( -( bindings:: EAGAIN as i32 ) ) ;
13
14
14
15
pub fn from_kernel_errno ( errno : c_types:: c_int ) -> Error {
15
16
Error ( errno)
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ mod error;
13
13
pub mod file_operations;
14
14
pub mod filesystem;
15
15
pub mod printk;
16
+ pub mod random;
16
17
pub mod sysctl;
17
18
mod types;
18
19
pub mod user_ptr;
Original file line number Diff line number Diff line change
1
+ use core:: convert:: TryInto ;
2
+
3
+ use crate :: { bindings, c_types, error} ;
4
+
5
+ /// Fills `dest` with random bytes generated from the kernel's CSPRNG. Ensures
6
+ /// that the CSPRNG has been seeded before generating any random bytes, and
7
+ /// will block until it's ready.
8
+ pub fn getrandom ( dest : & mut [ u8 ] ) -> error:: KernelResult < ( ) > {
9
+ let res = unsafe { bindings:: wait_for_random_bytes ( ) } ;
10
+ if res != 0 {
11
+ return Err ( error:: Error :: from_kernel_errno ( res) ) ;
12
+ }
13
+
14
+ unsafe {
15
+ // TODO: convert `unwrap` to proper error handling
16
+ bindings:: get_random_bytes (
17
+ dest. as_mut_ptr ( ) as * mut c_types:: c_void ,
18
+ dest. len ( ) . try_into ( ) . unwrap ( ) ,
19
+ ) ;
20
+ }
21
+ Ok ( ( ) )
22
+ }
23
+
24
+ /// Fills `dest` with random bytes generated from the kernel's CSPRNG. If the
25
+ /// CSPRNG is not yet seeded, returns an `Err(EAGAIN)` immediately. Only
26
+ /// available on 4.19 and later kernels.
27
+ #[ cfg( kernel_4_19_0_or_greater) ]
28
+ pub fn getrandom_nonblock ( dest : & mut [ u8 ] ) -> error:: KernelResult < ( ) > {
29
+ if !unsafe { bindings:: rng_is_initialized ( ) } {
30
+ return Err ( error:: Error :: EAGAIN ) ;
31
+ }
32
+ getrandom ( dest)
33
+ }
You can’t perform that action at this time.
0 commit comments