| 1 |
//! Process execution domains |
| 2 |
use crate::errno::Errno; |
| 3 |
use crate::Result; |
| 4 |
|
| 5 |
use libc::{self, c_int, c_ulong}; |
| 6 |
|
| 7 |
libc_bitflags! { |
| 8 |
/// Flags used and returned by [`get()`](fn.get.html) and |
| 9 |
/// [`set()`](fn.set.html). |
| 10 |
pub struct Persona: c_int { |
| 11 |
/// Provide the legacy virtual address space layout. |
| 12 |
ADDR_COMPAT_LAYOUT; |
| 13 |
/// Disable address-space-layout randomization. |
| 14 |
ADDR_NO_RANDOMIZE; |
| 15 |
/// Limit the address space to 32 bits. |
| 16 |
ADDR_LIMIT_32BIT; |
| 17 |
/// Use `0xc0000000` as the offset at which to search a virtual memory |
| 18 |
/// chunk on [`mmap(2)`], otherwise use `0xffffe000`. |
| 19 |
/// |
| 20 |
/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html |
| 21 |
ADDR_LIMIT_3GB; |
| 22 |
/// User-space function pointers to signal handlers point to descriptors. |
| 23 |
#[cfg(not(any(target_env = "musl", target_env = "uclibc", target_env = "ohos")))] |
| 24 |
FDPIC_FUNCPTRS; |
| 25 |
/// Map page 0 as read-only. |
| 26 |
MMAP_PAGE_ZERO; |
| 27 |
/// `PROT_READ` implies `PROT_EXEC` for [`mmap(2)`]. |
| 28 |
/// |
| 29 |
/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html |
| 30 |
READ_IMPLIES_EXEC; |
| 31 |
/// No effects. |
| 32 |
SHORT_INODE; |
| 33 |
/// [`select(2)`], [`pselect(2)`], and [`ppoll(2)`] do not modify the |
| 34 |
/// returned timeout argument when interrupted by a signal handler. |
| 35 |
/// |
| 36 |
/// [`select(2)`]: https://man7.org/linux/man-pages/man2/select.2.html |
| 37 |
/// [`pselect(2)`]: https://man7.org/linux/man-pages/man2/pselect.2.html |
| 38 |
/// [`ppoll(2)`]: https://man7.org/linux/man-pages/man2/ppoll.2.html |
| 39 |
STICKY_TIMEOUTS; |
| 40 |
/// Have [`uname(2)`] report a 2.6.40+ version number rather than a 3.x |
| 41 |
/// version number. |
| 42 |
/// |
| 43 |
/// [`uname(2)`]: https://man7.org/linux/man-pages/man2/uname.2.html |
| 44 |
#[cfg(not(any(target_env = "musl", target_env = "uclibc", target_env = "ohos")))] |
| 45 |
UNAME26; |
| 46 |
/// No effects. |
| 47 |
WHOLE_SECONDS; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/// Retrieve the current process personality. |
| 52 |
/// |
| 53 |
/// Returns a Result containing a Persona instance. |
| 54 |
/// |
| 55 |
/// Example: |
| 56 |
/// |
| 57 |
/// ``` |
| 58 |
/// # use nix::sys::personality::{self, Persona}; |
| 59 |
/// let pers = personality::get().unwrap(); |
| 60 |
/// assert!(!pers.contains(Persona::WHOLE_SECONDS)); |
| 61 |
/// ``` |
| 62 |
pub fn get() -> Result<Persona> { |
| 63 |
let res = unsafe { libc::personality(0xFFFFFFFF) }; |
| 64 |
|
| 65 |
Errno::result(res).map(Persona::from_bits_truncate) |
| 66 |
} |
| 67 |
|
| 68 |
/// Set the current process personality. |
| 69 |
/// |
| 70 |
/// Returns a Result containing the *previous* personality for the |
| 71 |
/// process, as a Persona. |
| 72 |
/// |
| 73 |
/// For more information, see [personality(2)](https://man7.org/linux/man-pages/man2/personality.2.html) |
| 74 |
/// |
| 75 |
/// **NOTE**: This call **replaces** the current personality entirely. |
| 76 |
/// To **update** the personality, first call `get()` and then `set()` |
| 77 |
/// with the modified persona. |
| 78 |
/// |
| 79 |
/// Example: |
| 80 |
/// |
| 81 |
// Disable test on aarch64 until we know why it fails. |
| 82 |
// https://github.com/nix-rust/nix/issues/2060 |
| 83 |
#[cfg_attr(target_arch = "aarch64", doc = " ```no_run")] |
| 84 |
#[cfg_attr(not(target_arch = "aarch64"), doc = " ```")] |
| 85 |
/// # use nix::sys::personality::{self, Persona}; |
| 86 |
/// let mut pers = personality::get().unwrap(); |
| 87 |
/// assert!(!pers.contains(Persona::ADDR_NO_RANDOMIZE)); |
| 88 |
/// personality::set(pers | Persona::ADDR_NO_RANDOMIZE).unwrap(); |
| 89 |
/// ``` |
| 90 |
pub fn set(persona: Persona) -> Result<Persona> { |
| 91 |
let res = unsafe { libc::personality(persona.bits() as c_ulong) }; |
| 92 |
|
| 93 |
Errno::result(res).map(Persona::from_bits_truncate) |
| 94 |
} |