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

Whats the deal with the use std:: in the middle of linux_raw::io::syscalls.rs when target_arch = "aarch64" #323

Closed
AsafFisher opened this issue May 13, 2022 · 6 comments

Comments

@AsafFisher
Copy link
Contributor

I am compiling a no_std code using this library, it works fine with x86_64 but when I set target_arch = "aarch64" it fails with:

error[E0433]: failed to resolve: use of undeclared crate or module `std`
   --> /home/a/.cargo/registry/src/github.com-1ecc6299db9ec823/rustix-0.34.6/src/imp/linux_raw/io/syscalls.rs:560:13
    |
560 |         use std::os::unix::io::AsRawFd;
    |             ^^^ use of undeclared crate or module `std`

#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]

@sunfishcode
Copy link
Member

no_std support in rustix is currently only used for porting std to rustix, which I've only happened to test on x86_64 so far. It looks like you found a bug; the fix is to change std::os::unix::io::AsRawFd to crate::fd::AsRawFd.

I'm also curious; if you have Linux to make syscalls to, why do you need no_std?

@AsafFisher
Copy link
Contributor Author

AsafFisher commented May 13, 2022

Hi! First of all I'll be happy to fix this and some other issues that I've found.

I am making a shellcode debugger (:
I want it to be cross-arch, cross environment and position independent.

Therefore I use this library as it is minimal and does not use libc.

Just in case you want some more details:
stdlib is not an option because the debugger should be robust to kernel mode / user mode
If you try to make a staticshellcode with the libc crate it will use a static lib of libc to link with. It will therefore embed a static GOT (yes the addresses will be already resolved and naturally the binary will contain absolute jumps which is bad for PIE shellcode ) so yah libc crate is not an option

With this library I am able to make a static and PIE(0 absolute calls) shellcode that can perform syscalls on different os's

BTW,
Whats the purpose of #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]

#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]

@AsafFisher
Copy link
Contributor Author

The point here is to optimize the check of the fd's in the user mode instead of performing the system call?
https://github.com/AsafFisher/rustix/blob/5cf42f790a9fad676e3fa912529845467790b0ff/src/imp/linux_raw/io/syscalls.rs#L401

@sunfishcode
Copy link
Member

arch64 and riscv64 don't have a dup2 system call, so on those platforms we need to use dup3.

There's also a subtlety that dup2 and dup3 have different behavior when the old and new file descriptors are the same, so we have some logic to ensure that rustix::io::dup2 has the dup2 behavior even when using the dup3 system call.

@AsafFisher
Copy link
Contributor Author

Yah! I looked it up today and saw it. Coolz

@sunfishcode
Copy link
Member

This is now released in 0.34.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants