Code Monkey home page Code Monkey logo

hash-rs's People

Contributors

asukharev avatar

Watchers

 avatar  avatar

hash-rs's Issues

Unsound `from` with misaligned pointer dereference

The source of unsoundness

hash-rs/src/sha1/mod.rs

Lines 12 to 17 in 96f5aa4

let ptr = p.as_mut_ptr() as *mut u64;
let word = u64::to_be(m.len() as u64 * 8 as u64);
unsafe {
let p = (p.len()/8 - 1) as isize;
*ptr.offset(p) = word;
}

In line 12, the code tried to cast the raw pointer with 1-byte-alignment to the raw pointer with 8-byte-alignment, and this would create a misaligned pointer. In line 16, the code deref the misaligned pointer which is unsound. Similar unsoundness happened in following two places.

hash-rs/src/sha1/mod.rs

Lines 29 to 35 in 96f5aa4

let output_ptr = digest.as_mut_ptr() as *mut u32;
for i in 0..state.len() {
let word = u32::to_be(state[i] as u32);
unsafe {
*output_ptr.offset(i as isize) = word;
}
}

line 29: create misaligned pointer, line 33: dereference

hash-rs/src/sha3/mod.rs

Lines 32 to 38 in 96f5aa4

let output_ptr = digest.as_mut_ptr() as *mut u64;
for i in 0..(OUTPUT_LEN / 8) {
let word = u64::to_le(state[i]);
unsafe {
*output_ptr.offset(i as isize) = word;
}
}

line 32: create misaligned pointer, line 36: dereference

To reproduce the bug

use hash;

fn main() {
    let input = "hello";
    let digest = hash::Digest::sha1(&input.as_bytes());
    println!("{:?}", digest);
}

run with Miri

error: Undefined Behavior: accessing memory with alignment 1, but alignment 8 is required
  --> /{personal path}hash-rs-0.0.2/src/sha1/mod.rs:16:9
   |
16 |         *ptr.offset(p) = word;
   |         ^^^^^^^^^^^^^^^^^^^^^ accessing memory with alignment 1, but alignment 8 is required
   |

Undefined behavior should not happen with safe Rust function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.