Skip to content

Instantly share code, notes, and snippets.

@rumpl
Last active March 16, 2023 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rumpl/833cb3023388a1b0e8af836bdcdfc75b to your computer and use it in GitHub Desktop.
Save rumpl/833cb3023388a1b0e8af836bdcdfc75b to your computer and use it in GitHub Desktop.

Preparation

First we will create a small program in Rust that will:

  1. print its pid
  2. list all the files and directories in /
use std::{ fs, process };
fn main() {
    println!("{}", process::id());
    
    let paths = fs::read_dir("/").unwrap();

    for path in paths {
        println!("Name: {}", path.unwrap().path().display())
    }
}

We can now statically build this program: RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --target x86_64-unknown-linux-gnu.

Exercice

Let's now create a new cargo project: cargo new mocker (mocker -> mock docker)

Let's build mocker a small docker clone interatively:

  1. Make it so that mocker ./pid will run the binary and show its output.
  2. Make it so that running mocker ./pid shows 1 as its pid
tip When you execute the binary, give it a new pid namespace https://docs.rs/nix/0.26.2/nix/sched/struct.CloneFlags.html#associatedconstant.CLONE_NEWPID use namespaces
  1. Make it so that running mocker ./pid will make pid list files from the current directory
Tip Use chroot: https://docs.rs/nix/0.26.2/nix/unistd/fn.chroot.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment