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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use std::{fmt, fs, io, num, path};
use std::io::BufRead;

use super::regex;

use super::ProcessId;

/// Permissions for a mapped region.
#[derive(Clone)]
pub struct Permissions {
    read: bool,
    write: bool,
    execute: bool,
    shared: bool,
}

impl Permissions {
    pub fn read(&self) -> bool {
        self.read
    }
    pub fn write(&self) -> bool {
        self.write
    }
    pub fn execute(&self) -> bool {
        self.execute
    }
    /// True iff `private()` is false.
    pub fn shared(&self) -> bool {
        self.shared
    }
    /// True iff `shared()` is false.
    pub fn private(&self) -> bool {
        !self.shared
    }
}

impl fmt::Debug for Permissions {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let read = if self.read { 'r' } else { '-' };
        let write = if self.write { 'w' } else { '-' };
        let execute = if self.execute { 'x' } else { '-' };
        let shared = if self.shared { 's' } else { 'p' };
        write!(f, "{}{}{}{}", read, write, execute, shared)
    }
}

/// Metadata for a mapped virtual memory region. See the `proc(5)` manpage.
#[derive(Clone, Debug)]
pub struct MemoryRegion {
    /// Start of the address in the process's address space.
    pub start_address: usize,
    pub end_address: usize,
    pub permissions: Permissions,
    /// Offset into the mapped file
    pub offset: usize,
    /// Device major number
    // 12 bits currently
    pub dev_major: u32,
    /// Device minor number
    // 20 bits
    pub dev_minor: u32,
    /// inode, if available
    pub inode: Option<u64>,
    /// Filename, or pseudo-path (e.g. `[stack]`), or `None` for anonymous mappings
    pub pathname: Option<String>,
}

#[derive(Debug)]
pub enum Error {
    ParseFailed,
    IoError(io::Error),
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::IoError(e)
    }
}

impl From<num::ParseIntError> for Error {
    fn from(_: num::ParseIntError) -> Self {
        Error::ParseFailed
    }
}

/// Iterator across VM regions.
pub struct MemoryRegionIter {
    reader: io::BufReader<fs::File>,
    buf: String,
    finished: bool,
}

impl MemoryRegionIter {
    fn parse_buf(&mut self) -> Result<MemoryRegion, Error> {
        lazy_static! {
            static ref RE: regex::Regex = regex::Regex::new(
                "^([0-9a-f]+)-([0-9a-f]+) ([a-z-]{4}) ([0-9a-f]+) (\\d+):(\\d+) (\\d+) +(.*)$")
                .expect("regex is valid");
        }

        let captures = RE.captures(self.buf.trim_right_matches('\n'))
            .ok_or(Error::ParseFailed)?;
        let start_address = usize::from_str_radix(&captures[1], 16)?;
        let end_address = usize::from_str_radix(&captures[2], 16)?;
        let p_bytes = &captures[3].as_bytes();
        let permissions = Permissions {
            read: p_bytes[0] == b'r',
            write: p_bytes[1] == b'w',
            execute: p_bytes[2] == b'x',
            shared: p_bytes[3] == b's',
        };
        let offset = usize::from_str_radix(&captures[4], 16)?;
        let dev_major = u32::from_str_radix(&captures[5], 16)?;
        let dev_minor = u32::from_str_radix(&captures[6], 16)?;
        let inode = u64::from_str_radix(&captures[7], 10)?;
        let inode = if inode == 0 { None } else { Some(inode) };
        let pathname = &captures[8];
        let pathname = if pathname.is_empty() {
            None
        } else {
            Some(String::from(pathname))
        };

        Ok(MemoryRegion {
            start_address,
            end_address,
            permissions,
            offset,
            dev_major,
            dev_minor,
            inode,
            pathname,
        })
    }
}

impl Iterator for MemoryRegionIter {
    type Item = Result<MemoryRegion, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }

        self.buf.clear();

        match self.reader.read_line(&mut self.buf) {
            Ok(bytes_read) => {
                if bytes_read == 0 {
                    self.finished = true;
                    None
                } else {
                    Some(self.parse_buf())
                }
            }
            Err(e) => Some(Err(Error::IoError(e))),
        }
    }
}

/// Load all mappings out of Linux procfs.
///
/// This may fail depending on permissions setup.
pub fn iter_mappings(pid: ProcessId) -> io::Result<MemoryRegionIter> {
    let path = match pid {
        ProcessId::SelfPid => String::from("/proc/self/maps"),
        ProcessId::Num(n) => format!("/proc/{}/maps", n),
    };

    iter_mapping_path(path)
}

fn iter_mapping_path<P: AsRef<path::Path>>(path: P) -> io::Result<MemoryRegionIter> {
    let f = fs::File::open(path)?;
    let reader = io::BufReader::new(f);

    Ok(MemoryRegionIter {
        reader,
        buf: String::new(),
        finished: false,
    })
}

#[cfg(test)]
mod tests {
    extern crate libc;

    use std::path;
    use super::*;

    #[test]
    fn sample_maps_file() {
        let path = path::Path::new("src/test-data/obexd-map.txt");

        let mappings = iter_mapping_path(path)
            .unwrap()
            .map(|r| r.unwrap())
            .collect::<Vec<MemoryRegion>>();

        assert_eq!(80, mappings.len());

        let first = &mappings[0];
        assert_eq!(94858956906496, first.start_address);
        assert_eq!(94858957352960, first.end_address);
        assert!(first.permissions.read());
        assert!(!first.permissions.write());
        assert!(first.permissions.execute());
        assert!(!first.permissions.shared());
        assert!(first.permissions.private());
        assert_eq!(0, first.offset);
        assert_eq!(8, first.dev_major);
        assert_eq!(3, first.dev_minor);
        assert_eq!(Some(58219319), first.inode);
        assert_eq!(
            "/usr/libexec/bluetooth/obexd",
            first.pathname.as_ref().unwrap()
        );

        let anon = &mappings[12];
        assert_eq!(0, anon.offset);
        assert_eq!(0, anon.dev_major);
        assert_eq!(0, anon.dev_minor);
        assert_eq!(None, anon.inode);
        assert_eq!(None, anon.pathname.as_ref());

        // has offset
        let libpthread = &mappings[50];
        assert!(!libpthread.permissions.read());
        assert!(!libpthread.permissions.write());
        assert!(!libpthread.permissions.execute());
        assert!(!libpthread.permissions.shared());
        assert!(libpthread.permissions.private());
        assert_eq!(106496, libpthread.offset);

        // different permissions, has inode
        let gconv = &mappings[71];
        assert!(gconv.permissions.read());
        assert!(!gconv.permissions.write());
        assert!(!gconv.permissions.execute());
        assert!(gconv.permissions.shared());
        assert!(!gconv.permissions.private());
        assert_eq!(3, gconv.dev_minor);
        assert_eq!(Some(55705632), gconv.inode);

        let last = &mappings[79];
        assert_eq!(18446744073699065856, last.start_address);
        assert_eq!(18446744073699069952, last.end_address);
        assert!(last.permissions.read());
        assert!(!last.permissions.write());
        assert!(last.permissions.execute());
        assert!(!last.permissions.shared());
        assert!(last.permissions.private());
        assert_eq!(0, last.offset);
        assert_eq!(0, last.dev_major);
        assert_eq!(0, last.dev_minor);
        assert_eq!(None, last.inode);
        assert_eq!("[vsyscall]", last.pathname.as_ref().unwrap());
    }

    #[test]
    fn read_self() {
        // can find stack
        assert_eq!(
            1,
            iter_mappings(ProcessId::SelfPid)
                .unwrap()
                .filter_map(|r| r.unwrap().pathname.clone())
                .filter(|p| p == "[stack]")
                .count()
        )
    }

    #[test]
    fn read_own_pid() {
        let pid = unsafe { libc::getpid() as u32 };

        // can find stack
        assert_eq!(
            1,
            iter_mappings(ProcessId::Num(pid))
                .unwrap()
                .filter_map(|r| r.unwrap().pathname.clone())
                .filter(|p| p == "[stack]")
                .count()
        )
    }

    #[test]
    fn permissions_debug() {
        let p1 = Permissions {
            read: false,
            write: false,
            execute: false,
            shared: false,
        };

        assert_eq!("---p", format!("{:?}", p1));

        let p2 = Permissions {
            read: true,
            write: true,
            execute: true,
            shared: true,
        };


        assert_eq!("rwxs", format!("{:?}", p2));
    }
}