1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#![cfg(target_os = "linux")]
//! # linux-personality
//!
//! This crate is a type safe wrapper around Linux `personality` function.
//!
//! # Examples
//! This sets the personality.
//!
//! ```rust
//! extern crate linux_personality;
//! use linux_personality::personality;
//! use linux_personality::PER_LINUX;
//!
//! let previous_personality = personality(PER_LINUX).unwrap();
//! ```
//!
//! This gets the personality.
//!
//! ```rust
//! extern crate linux_personality;
//! use linux_personality::get_personality;
//!
//! let persona = get_personality().unwrap();
//! ```

extern crate libc;
#[macro_use]
extern crate bitflags;

bitflags! {
    pub flags Personality: u32 {
        /// Have [uname(2)][uname2] report a 2.6.40+ version number rather than
        /// a 3.x version number. Added as a stopgap measure to support broken
        /// applications that could not handle the kernel version- numbering
        /// switch from 2.6.x to 3.x.
        ///
        /// [uname2]: http://man7.org/linux/man-pages/man2/uname.2.html
        const UNAME26 = 0x0020000,

        /// With this flag set, disable address-space-layout randomization.
        const ADDR_NO_RANDOMIZE = 0x0040000,

        /// User-space function pointers to signal handlers point (on certain
        /// architectures) to descriptors.
        const FDPIC_FUNCPTRS = 0x0080000,

        /// Map page 0 as read-only (to support binaries that depend on this
        /// SVr4 behavior).
        const MMAP_PAGE_ZERO = 0x0100000,

        /// With this flag set, provide legacy virtual address space layout.
        const ADDR_COMPAT_LAYOUT = 0x0200000,

        /// With this flag set, `PROT_READ` implies `PROT_EXEC` for
        /// [`mmap(2)`](http://man7.org/linux/man-pages/man2/mmap.2.html).
        const READ_IMPLIES_EXEC = 0x0400000,

        /// Limit the address space to 32 bits.
        const ADDR_LIMIT_32BIT = 0x0800000,

        /// No effects(?).
        const SHORT_INODE = 0x1000000,

        /// No effects(?).
        const WHOLE_SECONDS = 0x2000000,

        /// With this flag set, [`select(2)`][select2],
        /// [`pselect(2)`][pselect2], and [`ppoll(2)`][ppoll2] do not modify the
        /// returned timeout argument when interrupted by a signal handler.
        ///
        /// [select2]: http://man7.org/linux/man-pages/man2/select.2.html
        /// [pselect2]: http://man7.org/linux/man-pages/man2/pselect.2.html
        /// [ppoll2]: http://man7.org/linux/man-pages/man2/ppoll.2.html
        const STICKY_TIMEOUTS = 0x4000000,

        /// With this flag set, use `0xC0000000` as the offset at which to
        /// search a virtual memory chunk on [`mmap(2)`][mmap2]; otherwise use
        /// `0xFFFFE000`.
        ///
        /// [mmap2]: http://man7.org/linux/man-pages/man2/mmap.2.html
        const ADDR_LIMIT_3GB = 0x8000000,

        /// Linux
        const PER_LINUX = 0x0000,

        /// Implies `ADDR_LIMIT_32BIT`.
        const PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT.bits,

        /// Implies `FDPIC_FUNCPTRS`.
        const PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS.bits,

        /// Implies `STICKY_TIMEOUTS` and `MMAP_PAGE_ZERO`; otherwise no
        /// effects.
        const PER_SVR4 = 0x0001 | STICKY_TIMEOUTS.bits | MMAP_PAGE_ZERO.bits,

        /// Implies `STICKY_TIMEOUTS` and `SHORT_INODE`; otherwise no effects.
        const PER_SVR3 = 0x0002 | STICKY_TIMEOUTS.bits | SHORT_INODE.bits,

        /// Implies `STICKY_TIMEOUTS`, `WHOLE_SECONDS`, and `SHORT_INODE`;
        /// otherwise no effects.
        const PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS.bits | WHOLE_SECONDS.bits | SHORT_INODE.bits,

        /// Implies `STICKY_TIMEOUTS` and `WHOLE_SECONDS`; otherwise no
        /// effects.
        const PER_OSR5 = 0x0003 | STICKY_TIMEOUTS.bits | WHOLE_SECONDS.bits,

        /// Implies `STICKY_TIMEOUTS` and `SHORT_INODE`; otherwise no effects.
        const PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS.bits | SHORT_INODE.bits,

        /// Implies STICKY_TIMEOUTS; otherwise no effects.
        const PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS.bits,

        /// BSD. (No effects.)
        const PER_BSD = 0x0006,

        /// Implies `STICKY_TIMEOUTS`. Divert library and dynamic linker
        /// searches to `/usr/gnemul`.  Buggy, largely unmaintained, and almost
        /// entirely unused; support was removed in Linux 2.6.26.
        const PER_SUNOS = 0x0006 | STICKY_TIMEOUTS.bits,

        /// Implies `STICKY_TIMEOUTS` and `SHORT_INODE`; otherwise no effects.
        const PER_XENIX = 0x0007 | STICKY_TIMEOUTS.bits | SHORT_INODE.bits,

        /// [To be documented.]
        const PER_LINUX32 = 0x0008,

        /// Implies ADDR_LIMIT_3GB.
        const PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB.bits,

        /// IRIX 5 32-bit. Never fully functional; support dropped in Linux
        /// 2.6.27. Implies `STICKY_TIMEOUTS`.
        const PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS.bits,

        /// IRIX 6 new 32-bit. Implies `STICKY_TIMEOUTS`; otherwise no effects.
        const PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS.bits,

        /// IRIX 6 64-bit.  Implies `STICKY_TIMEOUTS`; otherwise no effects.
        const PER_IRIX64 = 0x000b | STICKY_TIMEOUTS.bits,

        /// [To be documented.]
        const PER_RISCOS = 0x000c,

        /// Implies `STICKY_TIMEOUTS`; otherwise no effects.
        const PER_SOLARIS = 0x000d | STICKY_TIMEOUTS.bits,

        /// Implies `STICKY_TIMEOUTS` and `MMAP_PAGE_ZERO`; otherwise no effects.
        const PER_UW7 = 0x000e | STICKY_TIMEOUTS.bits | MMAP_PAGE_ZERO.bits,

        /// OSF/1 v4.  On alpha, clear top 32 bits of iov_len in the user's
        /// buffer for compatibility with old versions of OSF/1 where iov_len
        /// was defined as. int.
        const PER_OSF4 = 0x000f,

        /// Support for 32-bit HP/UX.  This support was never complete, and was
        /// dropped so that since Linux 4.0, this value has no effect.
        const PER_HPUX = 0x0010,

        /// [to be documented]
        const PER_MASK = 0x00ff
    }
}

/// Contains FFI function declarations.
pub mod ffi {
    use ::libc;

    extern "system" {
        /// Low level `personality` function.
        pub fn personality(persona: libc::c_ulong) -> libc::c_int;
    }
}

/// Set the process domain execution model.
///
/// # Return value
/// On success returns the Previous `persona` is returned.
/// On failure returns `Err(())` if the kernel was unable to change personality.
pub fn personality(persona: Personality) -> Result<Personality, ()> {
    let previous_persona = unsafe { ffi::personality(persona.bits() as libc::c_ulong) };
    if previous_persona == -1 {
        Err(())
    } else {
        Ok(Personality::from_bits(previous_persona as u32).unwrap())
    }
}

/// This function only gets the current `persona`.
///
/// # Return value
/// On success returns the Previous `persona` is returned.
/// On failure returns `Err(())` if the kernel was unable to retrieve personality.
pub fn get_personality() -> Result<Personality, ()> {
    let persona = unsafe { ffi::personality(0xffffffff as libc::c_ulong) };
    if persona == -1 {
        Err(())
    } else {
        Ok(Personality::from_bits(persona as u32).unwrap())
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_linux_per() {
        use super::personality;
        use super::PER_LINUX;

        personality(PER_LINUX).unwrap();
    }

    #[test]
    fn retrieve_per() {
        use super::get_personality;

        get_personality().unwrap();
    }
}