4

I'm attempting to make a buffer of memory executable, then execute it in Rust. I've gotten all the way until I need to cast the raw executable bytes as code/instructions. You can see a working example in C below.

Extra details:

  • Rust 1.34
  • Linux
  • CC 8.2.1
unsigned char code[] = {
0x55,                           //    push   %rbp
0x48, 0x89, 0xe5,               //    mov    %rsp,%rbp
0xb8, 0x37, 0x00, 0x00, 0x00,   //    mov    $0x37,%eax
0xc9,                           //    leaveq
0xc3                            //    retq
};


void reflect(const unsigned char *code) {
  void *buf;

  /* copy code to executable buffer */    
  buf = mmap(0, sizeof(code), PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANON,-1,0);
  memcpy(buf, code, sizeof(code));

  ((void (*) (void))buf)();
}
extern crate mmap;

use mmap::{MapOption, MemoryMap};

unsafe fn reflect(instructions: &[u8]) {
    let map = MemoryMap::new(
        instructions.len(),
        &[
            MapOption::MapAddr(0 as *mut u8),
            MapOption::MapOffset(0),
            MapOption::MapFd(-1),
            MapOption::MapReadable,
            MapOption::MapWritable,
            MapOption::MapExecutable,
            MapOption::MapNonStandardFlags(libc::MAP_ANON),
            MapOption::MapNonStandardFlags(libc::MAP_PRIVATE),
        ],
    )
    .unwrap();

    std::ptr::copy(instructions.as_ptr(), map.data(), instructions.len());
    // How to cast into extern "C" fn() ?
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Skarlett
  • 762
  • 1
  • 10
  • 22
  • 1
    It may be easier to generate a dynamic library and then link that in. – tadman Apr 25 '19 at 19:26
  • Yeah that maybe my approach if no one can figure this out. – Skarlett Apr 25 '19 at 19:30
  • Not sure how you allocate an executable memory page in Rust, but [examples in C++](https://stackoverflow.com/questions/40936534/how-to-alloc-a-executable-memory-buffer) show it's an operating-system specific thing. If Rust had this feature it'd probably require surrounding it in `super_duper_unsafe { ... }` – tadman Apr 25 '19 at 20:09

1 Answers1

9

Use mem::transmute to cast a raw pointer to a function pointer type.

use std::mem;

let func: unsafe extern "C" fn() = mem::transmute(map.data());
func();
Francis Gagné
  • 60,274
  • 7
  • 180
  • 155