How can I use the function `std::os::unix::fs::chroot` if I'm not a root user?

I'm trying to build a program in my Linux, the following is the content of main.rs:

fn main() {
    std::os::unix::fs::chroot(".").unwrap();
    println!("Hello, world!");
}

But I'm not the root user, so I got the error:

called `Result::unwrap()` on an `Err` value: Os { code: 1, kind: PermissionDenied, message: "Operation not permitted" }

How can Imake the user inside which I'm running the program has the permission to use the chroot function? Is there a way I can do something like this (assumming the username is brian): sudo brian capacity --add-CAP_SYS_CHROOT?

No, not really. See eg. this answer for the rationale.

3 Likes