Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A wrong implement for sys_getrandom's flags #140

Open
ken4647 opened this issue Sep 24, 2024 · 0 comments
Open

A wrong implement for sys_getrandom's flags #140

ken4647 opened this issue Sep 24, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@ken4647
Copy link
Contributor

ken4647 commented Sep 24, 2024

In api/ruxos_posix_api/src/imp/getrandom.rs, it is implemented as:

/// Fills the buffer pointed to by buf with up to buflen random bytes.
pub unsafe extern "C" fn sys_getrandom(buf: *mut c_void, buflen: size_t, flags: c_int) -> ssize_t {
    info!(
        "sys_getrandom <= buf: {:?}, buflen: {}, flags: {}",
        buf, buflen, flags
    );
    syscall_body!(sys_getrandom, {
        if buf.is_null() {
            return Err(LinuxError::EFAULT);
        }

        // BUG: flags are implemented wrongly, flags should be checks bit by bit
        match flags as _ {
            crate::ctypes::GRND_NONBLOCK => {}
            crate::ctypes::GRND_RANDOM => {}
            _ => return Err(LinuxError::EINVAL),
        }
        // fill the buffer 8 bytes at a time first, then fill the remaining bytes
        let buflen_mod = buflen % (core::mem::size_of::<i64>() / core::mem::size_of::<u8>());
        let buflen_div = buflen / (core::mem::size_of::<i64>() / core::mem::size_of::<u8>());
        for i in 0..buflen_div {
            *((buf as *mut u8 as *mut i64).add(i)) = sys_random() as i64;
        }
        for i in 0..buflen_mod {
            *((buf as *mut u8).add(buflen - buflen_mod + i)) = sys_rand() as u8;
        }
        Ok(buflen as ssize_t)
    })
}

Obviously, the flag is checked as:

        match flags as _ {
            crate::ctypes::GRND_NONBLOCK => {}
            crate::ctypes::GRND_RANDOM => {}
            _ => return Err(LinuxError::EINVAL),
        }

This will make errors like below happens:

[1727161820.746064 0:1 ruxos_posix_api::imp::getrandom:152] sys_getrandom <= buf: 0xffff8000009d5ef0, buflen: 32, flags: 0
[1727161820.746318 0:1 ruxos_posix_api::imp::getrandom:156] sys_getrandom => Err(EINVAL)
@ken4647 ken4647 added the bug Something isn't working label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant