summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/mozannotation_server/src/process_reader/linux.rs
blob: db6dbd3df2ae19ecda15ccc628394fa490b0f7f9 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use mozannotation_client::MozAnnotationNote;
use std::{
    cmp::min,
    fs::File,
    io::{BufRead, BufReader, Error},
    mem::{size_of, MaybeUninit},
    ptr::null_mut,
    slice,
};

use crate::{
    errors::{FindAnnotationsAddressError, PtraceError, ReadError, RetrievalError},
    ProcessHandle,
};

use super::ProcessReader;

use goblin::elf::{
    self,
    program_header::{PF_R, PT_NOTE},
    Elf, ProgramHeader,
};
use libc::{
    c_int, c_long, c_void, pid_t, ptrace, waitpid, EINTR, PTRACE_ATTACH, PTRACE_DETACH,
    PTRACE_PEEKDATA, __WALL,
};
use memoffset::offset_of;
use mozannotation_client::ANNOTATION_TYPE;

impl ProcessReader {
    pub fn new(process: ProcessHandle) -> Result<ProcessReader, RetrievalError> {
        let pid: pid_t = process;

        ptrace_attach(pid)?;

        let mut status: i32 = 0;

        loop {
            let res = unsafe { waitpid(pid, &mut status as *mut _, __WALL) };
            if res < 0 {
                match get_errno() {
                    EINTR => continue,
                    _ => {
                        ptrace_detach(pid)?;
                        return Err(RetrievalError::WaitPidError);
                    }
                }
            } else {
                break;
            }
        }

        Ok(ProcessReader { process: pid })
    }

    pub fn find_annotations(&self) -> Result<usize, FindAnnotationsAddressError> {
        let maps_file = File::open(format!("/proc/{}/maps", self.process))?;

        BufReader::new(maps_file)
            .lines()
            .flatten()
            .find_map(|line| self.find_annotations_in_module(&line).ok())
            .ok_or(FindAnnotationsAddressError::NotFound)
    }

    fn find_annotations_in_module(&self, line: &str) -> Result<usize, FindAnnotationsAddressError> {
        parse_proc_maps_line(line).and_then(|module_address| {
            let header_bytes = self.copy_array(module_address, size_of::<elf::Header>())?;
            let elf_header = Elf::parse_header(&header_bytes)?;

            let program_header_bytes = self.copy_array(
                module_address + (elf_header.e_phoff as usize),
                (elf_header.e_phnum as usize) * (elf_header.e_phentsize as usize),
            )?;

            let mut elf = Elf::lazy_parse(elf_header)?;
            let context = goblin::container::Ctx {
                container: elf.header.container()?,
                le: elf.header.endianness()?,
            };

            elf.program_headers = ProgramHeader::parse(
                &program_header_bytes,
                0,
                elf_header.e_phnum as usize,
                context,
            )?;

            self.find_mozannotation_note(module_address, &elf)
                .ok_or(FindAnnotationsAddressError::ProgramHeaderNotFound)
        })
    }

    // Looks through the program headers for the note contained in the
    // mozannotation_client crate. If the note is found return the address of the
    // note's desc field as well as its contents.
    fn find_mozannotation_note(&self, module_address: usize, elf: &Elf) -> Option<usize> {
        for program_header in elf.program_headers.iter() {
            // We're looking for a note in the program headers, it needs to be
            // readable and it needs to be at least as large as the
            // MozAnnotationNote structure.
            if (program_header.p_type == PT_NOTE)
                && ((program_header.p_flags & PF_R) != 0
                    && (program_header.p_memsz as usize >= size_of::<MozAnnotationNote>()))
            {
                // Iterate over the notes
                let notes_address = module_address + program_header.p_offset as usize;
                let mut notes_offset = 0;
                let notes_size = program_header.p_memsz as usize;
                while notes_offset < notes_size {
                    let note_address = notes_address + notes_offset;
                    if let Ok(note) = self.copy_object::<goblin::elf::note::Nhdr32>(note_address) {
                        if note.n_type == ANNOTATION_TYPE {
                            if let Ok(note) = self.copy_object::<MozAnnotationNote>(note_address) {
                                let desc = note.desc;
                                let ehdr = (-note.ehdr) as usize;
                                let offset = desc + ehdr
                                    - (offset_of!(MozAnnotationNote, ehdr)
                                        - offset_of!(MozAnnotationNote, desc));

                                return usize::checked_add(module_address, offset);
                            }
                        }

                        notes_offset += size_of::<goblin::elf::note::Nhdr32>()
                            + (note.n_descsz as usize)
                            + (note.n_namesz as usize);
                    } else {
                        break;
                    }
                }
            }
        }

        None
    }

    pub fn copy_object_shallow<T>(&self, src: usize) -> Result<MaybeUninit<T>, ReadError> {
        let data = self.copy_array(src, size_of::<T>())?;
        let mut object = MaybeUninit::<T>::uninit();
        let uninitialized_object = uninit_as_bytes_mut(&mut object);

        for (index, &value) in data.iter().enumerate() {
            uninitialized_object[index].write(value);
        }

        Ok(object)
    }

    pub fn copy_object<T>(&self, src: usize) -> Result<T, ReadError> {
        self.copy_object_shallow(src)
            .map(|object| unsafe { object.assume_init() })
    }

    pub fn copy_array<T>(&self, src: usize, num: usize) -> Result<Vec<T>, ReadError> {
        let mut array = Vec::<MaybeUninit<T>>::with_capacity(num);
        let num_bytes = num * size_of::<T>();
        let mut array_buffer = array.as_mut_ptr() as *mut u8;
        let mut index = 0;

        while index < num_bytes {
            let word = ptrace_read(self.process, src + index)?;
            let len = min(size_of::<c_long>(), num_bytes - index);
            let word_as_bytes = word.to_ne_bytes();
            for &byte in word_as_bytes.iter().take(len) {
                unsafe {
                    array_buffer.write(byte);
                    array_buffer = array_buffer.add(1);
                }
            }

            index += size_of::<c_long>();
        }

        unsafe {
            array.set_len(num);
            Ok(std::mem::transmute(array))
        }
    }
}

impl Drop for ProcessReader {
    fn drop(&mut self) {
        let _ignored = ptrace_detach(self.process);
    }
}

fn parse_proc_maps_line(line: &str) -> Result<usize, FindAnnotationsAddressError> {
    let mut splits = line.trim().splitn(6, ' ');
    let address_str = splits
        .next()
        .ok_or(FindAnnotationsAddressError::ProcMapsParseError)?;
    let _perms_str = splits
        .next()
        .ok_or(FindAnnotationsAddressError::ProcMapsParseError)?;
    let _offset_str = splits
        .next()
        .ok_or(FindAnnotationsAddressError::ProcMapsParseError)?;
    let _dev_str = splits
        .next()
        .ok_or(FindAnnotationsAddressError::ProcMapsParseError)?;
    let _inode_str = splits
        .next()
        .ok_or(FindAnnotationsAddressError::ProcMapsParseError)?;
    let _path_str = splits
        .next()
        .ok_or(FindAnnotationsAddressError::ProcMapsParseError)?;

    let address = get_proc_maps_address(address_str)?;

    Ok(address)
}

fn get_proc_maps_address(addresses: &str) -> Result<usize, FindAnnotationsAddressError> {
    let begin = addresses
        .split('-')
        .next()
        .ok_or(FindAnnotationsAddressError::ProcMapsParseError)?;
    usize::from_str_radix(begin, 16).map_err(FindAnnotationsAddressError::from)
}

fn uninit_as_bytes_mut<T>(elem: &mut MaybeUninit<T>) -> &mut [MaybeUninit<u8>] {
    // SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
    unsafe { slice::from_raw_parts_mut(elem.as_mut_ptr() as *mut MaybeUninit<u8>, size_of::<T>()) }
}

/***********************************************************************
 ***** libc helpers                                                *****
 ***********************************************************************/

fn get_errno() -> c_int {
    #[cfg(target_os = "linux")]
    unsafe {
        *libc::__errno_location()
    }
    #[cfg(target_os = "android")]
    unsafe {
        *libc::__errno()
    }
}

fn clear_errno() {
    #[cfg(target_os = "linux")]
    unsafe {
        *libc::__errno_location() = 0;
    }
    #[cfg(target_os = "android")]
    unsafe {
        *libc::__errno() = 0;
    }
}

#[derive(Clone, Copy)]
enum PTraceOperation {
    Attach,
    Detach,
    PeekData,
}

#[cfg(target_os = "linux")]
type PTraceOperationNative = libc::c_uint;
#[cfg(target_os = "android")]
type PTraceOperationNative = c_int;

impl From<PTraceOperation> for PTraceOperationNative {
    fn from(val: PTraceOperation) -> Self {
        match val {
            PTraceOperation::Attach => PTRACE_ATTACH,
            PTraceOperation::Detach => PTRACE_DETACH,
            PTraceOperation::PeekData => PTRACE_PEEKDATA,
        }
    }
}

fn ptrace_attach(pid: pid_t) -> Result<(), PtraceError> {
    ptrace_helper(pid, PTraceOperation::Attach, 0).map(|_r| ())
}

fn ptrace_detach(pid: pid_t) -> Result<(), PtraceError> {
    ptrace_helper(pid, PTraceOperation::Detach, 0).map(|_r| ())
}

fn ptrace_read(pid: libc::pid_t, addr: usize) -> Result<c_long, PtraceError> {
    ptrace_helper(pid, PTraceOperation::PeekData, addr)
}

fn ptrace_helper(pid: pid_t, op: PTraceOperation, addr: usize) -> Result<c_long, PtraceError> {
    clear_errno();
    let result = unsafe { ptrace(op.into(), pid, addr, null_mut::<c_void>()) };

    if result == -1 {
        let errno = get_errno();
        if errno != 0 {
            let error = match op {
                PTraceOperation::Attach => PtraceError::TraceError(Error::from_raw_os_error(errno)),
                PTraceOperation::Detach => PtraceError::TraceError(Error::from_raw_os_error(errno)),
                PTraceOperation::PeekData => {
                    PtraceError::ReadError(Error::from_raw_os_error(errno))
                }
            };
            Err(error)
        } else {
            Ok(result)
        }
    } else {
        Ok(result)
    }
}