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

TinyInst for Linux #1316

Closed
tokatoka opened this issue Jun 15, 2023 · 6 comments
Closed

TinyInst for Linux #1316

tokatoka opened this issue Jun 15, 2023 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@tokatoka
Copy link
Member

they support linux now

@tokatoka tokatoka added enhancement New feature or request good first issue Good for newcomers labels Jun 15, 2023
@domenukk
Copy link
Member

Do we even support MacOS? I thought it tinyinst libafl was Windows only?

@tokatoka
Copy link
Member Author

tokatoka commented Jul 4, 2023

tinyinst-bindings itself works we even have CI.
for libafl-tinyinst i don't know

@am009
Copy link
Contributor

am009 commented Mar 17, 2024

I just started working on this.

At first, I used the UnixShMemProvider, but I found that it uses shmget/shmat, while the harness uses shm_open. So I switched to MmapShMemProvider.

    #[cfg(target_vendor = "apple")]
    let mut shmem_provider = UnixShMemProvider::new().unwrap();

    #[cfg(target_os = "linux")]
    let mut shmem_provider = MmapShMemProvider::new().unwrap();

However, the fuzzer tinyinst_simple's harness (fuzzers/tinyinst_simple/test/test.cpp) still fails at shm_open. After debugging, I found that the fd is used as the shared memory id, instead of the shared memory filename:

  • First, the fuzzer calls shm_open("/libafl_1344706_2656399188", ...), and it returns a fd (for example, 3)
  • Then, the ShMemId is set to the fd (after converting to string, "3"), and is passed as the harness's argument ("test.exe -m 3").
    Ok(Self {
    filename_path: Some(filename_path),
    map: map as *mut u8,
    map_size,
    shm_fd,
    id: ShMemId::from_string(&format!("{shm_fd}")),
    })
  • Then the harness maps the shared memory by calling shm_open("3", ...), and the call failed with "Error in shm_open: No such file or directory". It should pass the file name to shm_open (shm_open("/libafl_1344706_2656399188", ...))

On Windows, the Win32ShMemProvider sets the ShMemId to the shared memory filename (format!("libafl_{}", uuid.simple())).

Is this the expected behavior on MacOS? I don't have a MacOS device to test it. Or the child process inherits the file descriptor of the shared memory and it should skip the shm_open and just call the mmap?

By the way, I'm not sure but I feel like keeping the "test.exe" as the executable name even on Linux. First, it uses less conditional compilation#[cfg(target_os = "linux")]. And second, there is a folder called test that prevents me from creating a file with the same name.

@tokatoka
Copy link
Member Author

Is this the expected behavior on MacOS?

No it is not supposed to work on MacOS

am009 added a commit to am009/LibAFL-log that referenced this issue Mar 19, 2024
Fix the id of MmapShMem to path instead of the fd number.
am009 added a commit to am009/LibAFL-log that referenced this issue Mar 19, 2024
Fix the id of MmapShMem to the shared memory path instead of the fd number.
am009 added a commit to am009/LibAFL-log that referenced this issue Mar 19, 2024
Fix the id of MmapShMem to the shared memory path instead of the fd number.
@am009
Copy link
Contributor

am009 commented Mar 19, 2024

MmapShMem has both fd and the path passed to shm_open, but there is a disagreement on which should be set as ShMem.id():

  • When the MmapShMem is wrapped in ShMemService (especially on MacOS, StdShMemService is aliased to ShMemService<MmapShMemProvider>), the ShMemService sends the shmem fd using socket. So it assumes the id is fd number, and tries to parse the id to get the fd. This is also why some MacOS tests failed.
  • When the MmapShMem is used directly (in this case), the path passed to shm_open is expected to be the id, so that the harness can use shm_open to get the memory.

In my opinion, ShMem.id() represents something that can be used to fetch the shared memory, so these two cases are both reasonable.

I think we can add another type (for example, NamedMmapShMem) that has identical behavior as MmapShMem, but its id is set to the shm_open path instead of the fd number. (also add its provider NamedMmapShMemProvider).

am009 added a commit to am009/LibAFL-log that referenced this issue Mar 19, 2024
Fix a documentation error for `MmapShMemProvider`.
@am009
Copy link
Contributor

am009 commented Mar 19, 2024

I didn't realize that we could still use UnixShMemProvider and shmat instead of shm_open on Linux. I have updated the PR.

domenukk pushed a commit that referenced this issue Mar 19, 2024
* Make fuzzers/tinyinst_simple support Linux (#1316)

Fix a documentation error for `MmapShMemProvider`.

* Support shmem for `fuzzers/tinyinst_simple` on Linux.

Format code.

* Fix CI to install cxxbridge-cmd.

* Add `CARGO_TARGET_DIR` in makefile to fix CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants