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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#[macro_use]
extern crate nom;

use std::{error, fmt, result};
use std::io::Read;
use std::ops::{Deref, DerefMut};
use libc::pid_t;
use std::fs::File;
use std::path::PathBuf;


pub type Result<T> = result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    InvalidInput,
    IoError,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Error::InvalidInput => write!(f, "Invalid input"),
            Error::IoError => write!(f, "IO Error"),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::InvalidInput => "Incorrect input data for memory mapping",
            Error::IoError => "I/O error",
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        None
    }
}

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

impl <'a>From<Error> for nom::Err<&'a str> {
    fn from(_: Error) -> nom::Err<&'a str> {
        nom::Err::Incomplete(nom::Needed::Unknown)
    }
}

impl <T>From<nom::Err<T>> for Error {
    fn from(_: nom::Err<T>) -> Error {
        Error::InvalidInput
    }
}

/// Represents the privacy of a mapping.
#[derive(PartialEq, Debug)]
pub enum Privacy {
    /// This mapping is shared
    Shared,
    /// This mapping is private (copy on write)
    Private,
}

/// Represents the permissions of for a memory mapping.
#[derive(Debug)]
pub struct Permissions {
    pub readable: bool,
    pub writable: bool,
    pub executable: bool,
    pub privacy: Privacy,
}

impl Permissions {
    fn from_str(input: &str) -> Result<Self> {
        if input.len() != 4 {
            return Err(Error::InvalidInput)
        }
        let input = input.as_bytes();
        let readable = input[0] == b'r';
        let writable = input[1] == b'w';
        let executable = input[2] == b'x';

        let privacy = match input[3] {
            b'p' => Privacy::Private,
            b's' => Privacy::Shared,
            _e => return Err(Error::InvalidInput),
        };

        Ok(Permissions {
            readable: readable,
            writable: writable,
            executable: executable,
            privacy: privacy,
        })
    }
}

/// This enum represents the pathname field of a given process.
/// Usually this is a file that backs up a given mapping.
#[derive(PartialEq, Debug)]
pub enum Path {
    /// A file backs up this mapping
    MappedFile(String),
    /// This mapping is the main thread stack
    Stack,
    /// This mapping is a thread's stack
    ThreadStack(usize),
    /// This mapping is the virtual dynamically linked shared object
    Vdso,
    /// This mapping is the process's heap
    Heap,
    /// This mapping holds variables updated by the kernel
    Vvar,
    /// This region is the vsyscall mapping
    Vsyscall,
}

impl From<&str> for Path {
    fn from(input: &str) -> Self {
        // TODO: add ThreadStack type
        match input {
            "[heap]" => Path::Heap,
            "[stack]" => Path::Stack,
            "[vdso]" => Path::Vdso,
            "[vvar]" => Path::Vvar,
            "[vsyscall]" => Path::Vvar,
            s => Path::MappedFile(s.to_string())
        }
    }
}


/// Holds data for a given memory mapped region.
/// [For more information.](http://man7.org/linux/man-pages/man5/proc.5.html)
#[derive(Debug)]
pub struct Map {
    /// Base of mapped region in process
    pub base: usize,
    /// Ceiling of mapped region in process
    pub ceiling: usize,
    /// Access permissions of memory region
    pub perms: Permissions,
    /// If this mapping is backed by a file, this is the offset into the file.
    pub offset: usize,
    /// Major device number
    pub dev_major: usize,
    /// Minor device number
    pub dev_minor: usize,
    /// The inode on the above device
    pub inode: usize,
    /// If there is no pathname, this mapping was obtained via mmap(2)
    pub pathname: Path,
}

impl Map {
    /// Calculate the size of the mapped region
    pub fn size_of_mapping(&self) -> usize {
        self.ceiling - self.base
    }

    fn from_str(input: &str) -> Result<Map> {
        let res = parse_map(input);

        match res {
            Ok(val) => Ok(val.1),
            Err(_e) => Err(Error::InvalidInput),
        }
    }
}

named!(parse_map<&str, Map>,
    do_parse!(
        base: map_res!(take_until!("-"), |b| usize::from_str_radix(b, 16))      >>
        take!(1)                                                                >>
        ceiling: map_res!(take_until!(" "), |b| usize::from_str_radix(b, 16))   >>
        take!(1)                                                                >>
        perms: map_res!(take_until!(" "), |b| Permissions::from_str(b))         >>
        take!(1)                                                                >>
        offset: map_res!(take_until!(" "), |b| usize::from_str_radix(b, 16))    >>
        take!(1)                                                                >>
        dev_major: map_res!(take_until!(":"), |b| usize::from_str_radix(b, 16)) >>
        take!(1)                                                                >>
        dev_minor: map_res!(take_until!(" "), |b| usize::from_str_radix(b, 16)) >>
        take!(1)                                                                >>
        inode: map_res!(take_until!(" "), |b| usize::from_str_radix(b, 16))     >>
        take!(1)                                                                >>
        pathname: opt!(take_until!("\n"))                                       >>
        (Map {
            base: base,
            ceiling: ceiling,
            perms: perms,
            offset: offset,
            dev_major: dev_major,
            dev_minor: dev_minor,
            inode: inode,
            pathname: pathname.unwrap().trim().into(),
        })
    )
);


/// A collection of memory mapped regions.
#[derive(Debug)]
pub struct Mappings(Vec<Map>);

impl Mappings {
    /// Returns mappings for a given pid
    pub fn from_pid(pid: pid_t) -> Result<Mappings> {
        let path = format!("/proc/{}/maps", pid);
        let mut file = File::open(path)?;
        let mut input = String::new();
        file.read_to_string(&mut input)?;

        let mut res: Vec<Map> = Vec::new();
        let mut iter: Vec<&str> = input.split("\n").collect();
        iter.pop();
        for s in iter {
            let map = Map::from_str(&format!("{}\n", &s))?;
            res.push(map);
        }

        Ok(Mappings(res))
    }

    pub fn from_path(path: &mut PathBuf) -> Result<Mappings> {
        path.push("maps");
        let mut file = File::open(path)?;
        let mut input = String::new();
        file.read_to_string(&mut input)?;

        let mut res: Vec<Map> = Vec::new();
        let mut iter: Vec<&str> = input.split("\n").collect();
        iter.pop();
        for s in iter {
            let map = Map::from_str(&format!("{}\n", &s))?;
            res.push(map);
        }

        Ok(Mappings(res))
    }
}

impl Deref for Mappings {
    type Target = Vec<Map>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Mappings {
    fn deref_mut(&mut self) -> &mut Vec<Map> { &mut self.0 }
}

#[cfg(test)]
mod tests {
    use crate::*;

    //TODO: more tests ie: check none pathname
    #[test]
    fn test_parse_map() {
        let input = "55e8d4153000-55e8d416f000 r-xp 00000000 08:02 9175073                    /bin/dash\n";
        let res = parse_map(input).unwrap().1;
        println!("{:?}", res);
        assert_eq!(res.base, 94458478931968);
        assert_eq!(res.ceiling, 94458479046656);
        assert_eq!(res.offset, 0);
        assert_eq!(res.dev_major, 8);
        assert_eq!(res.dev_minor, 2);
        assert_eq!(res.inode, 152522867);
        assert_eq!(res.pathname, Path::MappedFile("/bin/dash".to_string()));
    }

    #[test]
    fn test_map_path_types() {
        let input = "7fffdb68b000-7fffdb6ac000 rw-p 00000000 00:00 0                          [stack]\n";
        let res = Map::from_str(input).unwrap();
        assert_eq!(res.pathname, Path::Stack);

        let input = "7fffdb7a7000-7fffdb7aa000 r--p 00000000 00:00 0                          [vvar]\n";
        let res = Map::from_str(input).unwrap();
        assert_eq!(res.pathname, Path::Vvar);

        let input = "7fffdb7aa000-7fffdb7ac000 r-xp 00000000 00:00 0                          [vdso]\n";
        let res = Map::from_str(input).unwrap();
        assert_eq!(res.pathname, Path::Vdso);
    }

    #[test]
    fn test_map_from_str_invalid_inputs() {
        // Invalid permissions
        let input = "7fffdb68b000-7fffdb6ac000 rw- 00000000 00:00 0                          [stack]\n";
        let res = Map::from_str(input);
        assert!(res.is_err());

        // Invalid device
        let input = "7fffdb7a7000-7fffdb7aa000 r--p 00000000 0000 0                          [vvar]\n";
        let res = Map::from_str(input);
        assert!(res.is_err());

        // Invalid address format
        let input = "7fffdb7aa0007fffdb7ac000 r-xp 00000000 00:00 0                          [vdso]\n";
        let res = Map::from_str(input);
        assert!(res.is_err());
    }

    #[test]
    fn test_size_of_mapping() {
        let input = "55e8d4153000-55e8d416f000 r-xp 00000000 08:02 9175073                    /bin/dash\n";
        let res = Map::from_str(input).unwrap();
        assert_eq!(res.size_of_mapping(), 114688usize);
    }

    #[test]
    fn test_map_perms() {
        let input = "55e8d4153000-55e8d416f000 r-xp 00000000 08:02 9175073                    /bin/dash\n";
        let res = Map::from_str(input).unwrap();
        println!("{:?}", res);
        assert!(res.perms.readable);
        assert!(!res.perms.writable);
        assert!(res.perms.executable);
        assert_eq!(res.perms.privacy, Privacy::Private);
    }

    /*
    #[bench]
    fn bench_map_from_str(b: &mut Bencher) {
        let input = "55e8d4153000-55e8d416f000 r-xp 00000000 08:02 9175073                    /bin/dash\n";

        b.iter(||
            Map::from_str(input).unwrap()
        )
    }
    */

    #[test]
    fn test_map_from_str() {
        let mut file = File::open("tests/example.txt").unwrap();
        let mut input = String::new();
        file.read_to_string(&mut input).unwrap();

        let mut iter: Vec<&str> = input.split("\n").collect();
        iter.pop();
        for s in iter {
            let map = Map::from_str(&format!("{}\n", &s)).unwrap();
            println!("{:?}", map);
        }
    }

    #[test]
    fn test_maps() {
        use std::process::id;
        let m = Mappings::from_pid(id() as pid_t);
        assert!(m.is_ok());
        println!("{:?}", m);
    }
}