|
| 1 | +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +extern crate logger; |
| 5 | +extern crate sys_util; |
| 6 | + |
| 7 | +use std::io; |
| 8 | +use std::mem; |
| 9 | +use std::ptr::null_mut; |
| 10 | +use std::result::Result; |
| 11 | + |
| 12 | +use libc::{ |
| 13 | + _exit, c_int, c_uint, c_void, sigaction, sigfillset, siginfo_t, sigset_t, SA_SIGINFO, SIGBUS, |
| 14 | + SIGSEGV, |
| 15 | +}; |
| 16 | + |
| 17 | +use logger::LOGGER; |
| 18 | + |
| 19 | +type SiginfoHandler = extern "C" fn(num: c_int, info: *mut siginfo_t, _unused: *mut c_void) -> (); |
| 20 | + |
| 21 | +// `SIGBUS` codes. |
| 22 | +// Hardware memory error consumed on a machine check: action required. |
| 23 | +const BUS_MCEERR_AR: c_int = 4; |
| 24 | +// Hardware memory error detected in process but not consumed: action optional. |
| 25 | +const BUS_MCEERR_AO: c_int = 5; |
| 26 | + |
| 27 | +// `SIGSEGV` codes. |
| 28 | +// Failed address bound checks. |
| 29 | +const SEGV_BNDERR: c_int = 3; |
| 30 | +// Failed protection key checks. |
| 31 | +const SEGV_PKUERR: c_int = 4; |
| 32 | + |
| 33 | +/// Address bounds at time of fault. |
| 34 | +/// See https://elixir.bootlin.com/linux/v4.14/source/include/uapi/asm-generic/siginfo.h |
| 35 | +#[repr(C)] |
| 36 | +#[derive(Copy, Clone)] |
| 37 | +pub(crate) struct _si_addr_bnd_t { |
| 38 | + /// Lower bound. |
| 39 | + pub(crate) _lower: *const c_void, |
| 40 | + /// Upper bound. |
| 41 | + pub(crate) _upper: *const c_void, |
| 42 | +} |
| 43 | + |
| 44 | +/// Representation of the fault stats for different `SIGSEGV` codes. |
| 45 | +/// See https://elixir.bootlin.com/linux/v4.14/source/include/uapi/asm-generic/siginfo.h |
| 46 | +#[repr(C)] |
| 47 | +#[derive(Copy, Clone)] |
| 48 | +pub(crate) union _si_fault_t { |
| 49 | + /// Used when `si_code` == `SEGV_BNDERR`. |
| 50 | + pub(crate) _addr_bnd: _si_addr_bnd_t, |
| 51 | + /// Used when `si_code` == `SEGV_PKUERR`. |
| 52 | + pub(crate) _pkey: c_uint, |
| 53 | +} |
| 54 | + |
| 55 | +/// Stats filled in for `SIGILL`, `SIGFPE`, `SIGSEGV`, `SIGBUS`. |
| 56 | +/// See https://elixir.bootlin.com/linux/v4.14/source/include/uapi/asm-generic/siginfo.h |
| 57 | +#[repr(C)] |
| 58 | +#[derive(Copy, Clone)] |
| 59 | +pub(crate) struct _sigfault_t { |
| 60 | + /// Faulting instruction / memory reference address. |
| 61 | + pub(crate) _si_addr: *const c_void, |
| 62 | + /// Valid LSB of the reported address. |
| 63 | + pub(crate) _si_addr_lsb: i16, |
| 64 | + /// Fault stats. |
| 65 | + pub(crate) _si_stats: _si_fault_t, |
| 66 | +} |
| 67 | + |
| 68 | +/// Stats filled in for `SIGSYS`. |
| 69 | +/// See https://elixir.bootlin.com/linux/v4.14/source/include/uapi/asm-generic/siginfo.h |
| 70 | +#[repr(C)] |
| 71 | +#[derive(Copy, Clone)] |
| 72 | +pub(crate) struct _sigsys_t { |
| 73 | + /// Call address. |
| 74 | + pub(crate) _call_addr: *const c_void, |
| 75 | + /// Offending syscall number. |
| 76 | + pub(crate) _syscall: c_int, |
| 77 | + /// Architecture identifier. |
| 78 | + pub(crate) _arch: c_uint, |
| 79 | +} |
| 80 | + |
| 81 | +/// Union of possible additional stats returned in the `siginfo` struct. |
| 82 | +/// See https://elixir.bootlin.com/linux/v4.14/source/include/uapi/asm-generic/siginfo.h |
| 83 | +#[repr(C)] |
| 84 | +#[derive(Copy, Clone)] |
| 85 | +pub(crate) union _si_fields_t { |
| 86 | + /// Fault info. Filled in for `SIGSEGV`, `SIGBUS`, `SIGILL`, `SIGFPE`. |
| 87 | + pub(crate) _si_sigfault: _sigfault_t, |
| 88 | + /// `SIGSYS` info. Filled in for seccomp faults. |
| 89 | + pub(crate) _si_sigsys: _sigsys_t, |
| 90 | + /// Padding. |
| 91 | + _pad: [c_int; 29], |
| 92 | +} |
| 93 | + |
| 94 | +/// Representation of the `siginfo` struct with its relevant fields. |
| 95 | +/// See https://elixir.bootlin.com/linux/v4.14/source/include/uapi/asm-generic/siginfo.h |
| 96 | +#[repr(C)] |
| 97 | +#[derive(Copy, Clone)] |
| 98 | +pub(crate) struct _si_siginfo_t { |
| 99 | + /// Signal number. |
| 100 | + pub(crate) si_signo: c_int, |
| 101 | + /// If non-zero, errno value associated with the signal. |
| 102 | + pub(crate) si_errno: c_int, |
| 103 | + /// Signal code. |
| 104 | + pub(crate) si_code: c_int, |
| 105 | + /// Additional fields. |
| 106 | + pub(crate) si_fields: _si_fields_t, |
| 107 | +} |
| 108 | + |
| 109 | +/// Sets up the specified handler for the signals. |
| 110 | +/// |
| 111 | +/// # Arguments |
| 112 | +/// |
| 113 | +/// * `signals` - vector of signals to be handled. |
| 114 | +/// * `handler` - signal handler function. |
| 115 | +/// |
| 116 | +pub(crate) fn setup_signal_handler( |
| 117 | + signals: &Vec<c_int>, |
| 118 | + handler: SiginfoHandler, |
| 119 | +) -> Result<(), io::Error> { |
| 120 | + // Safe, because this is a POD struct. |
| 121 | + let mut sigact: sigaction = unsafe { mem::zeroed() }; |
| 122 | + sigact.sa_flags = SA_SIGINFO; |
| 123 | + sigact.sa_sigaction = handler as usize; |
| 124 | + |
| 125 | + // We set all the bits of sa_mask, so all signals are blocked on the current thread while the |
| 126 | + // signal handler is executing. Safe because the parameter is valid and we check the return |
| 127 | + // value. |
| 128 | + if unsafe { sigfillset(&mut sigact.sa_mask as *mut sigset_t) } < 0 { |
| 129 | + return Err(io::Error::last_os_error()); |
| 130 | + } |
| 131 | + |
| 132 | + for signal in signals.iter() { |
| 133 | + // Safe because the parameters are valid and we check the return value. |
| 134 | + if unsafe { sigaction(*signal, &sigact, null_mut()) } < 0 { |
| 135 | + return Err(io::Error::last_os_error()); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + Ok(()) |
| 140 | +} |
| 141 | + |
| 142 | +/// Handles `SIGBUS` and `SIGSEGV`. |
| 143 | +/// |
| 144 | +/// Logs all the available information on the fault that occurred and exits the process. |
| 145 | +/// |
| 146 | +/// # Arguments |
| 147 | +/// |
| 148 | +/// * `num` - signal number. |
| 149 | +/// * `info` - signal information filled in by the kernel. |
| 150 | +/// * `c_void` - signal context. Unused. |
| 151 | +/// |
| 152 | +pub(crate) extern "C" fn sigbus_sigsegv_handler( |
| 153 | + num: c_int, |
| 154 | + info: *mut siginfo_t, |
| 155 | + _unused: *mut c_void, |
| 156 | +) { |
| 157 | + let siginfo = info as *mut _si_siginfo_t; |
| 158 | + // Safe because we dereference a valid value. |
| 159 | + let si_signo = unsafe { (*siginfo).si_signo }; |
| 160 | + let si_code = unsafe { (*siginfo).si_code }; |
| 161 | + |
| 162 | + // Sanity check. The condition should never be true. |
| 163 | + if num != si_signo || (num != SIGBUS && num != SIGSEGV) { |
| 164 | + // Safe because we're terminating the process anyway. |
| 165 | + unsafe { _exit(i32::from(super::FC_EXIT_CODE_UNEXPECTED_ERROR)) }; |
| 166 | + } |
| 167 | + |
| 168 | + // `SIGSEGV` and `SIGBUS` fill in `si_addr` with the address of the fault. |
| 169 | + // http://man7.org/linux/man-pages/man2/sigaction.2.html |
| 170 | + // Safe because we dereference a valid value. |
| 171 | + let si_addr = unsafe { (*siginfo).si_fields._si_sigfault._si_addr as usize }; |
| 172 | + error!( |
| 173 | + "Caught signal {}. Code: {}. Fault address: {:x?}", |
| 174 | + si_signo, si_code, si_addr |
| 175 | + ); |
| 176 | + |
| 177 | + match si_signo { |
| 178 | + SIGBUS => { |
| 179 | + match si_code { |
| 180 | + // `BUS_MCEERR_AO` and `BUS_MCEERR_AR` also fill in `si_addr_lsb`. |
| 181 | + // This field indicates the LSB of the reported address and therefore the extent of |
| 182 | + // the corruption. For example, if a full page was corrupted, `si_addr_lsb` contains |
| 183 | + // `log2(sysconf(_SC_PAGESIZE))`. |
| 184 | + BUS_MCEERR_AO | BUS_MCEERR_AR => { |
| 185 | + // Safe because we dereference a valid value. |
| 186 | + let si_addr_lsb = unsafe { (*siginfo).si_fields._si_sigfault._si_addr_lsb }; |
| 187 | + error!("LSB of the reported address: {:x?}", si_addr_lsb); |
| 188 | + } |
| 189 | + _ => (), |
| 190 | + } |
| 191 | + } |
| 192 | + SIGSEGV => { |
| 193 | + match si_code { |
| 194 | + SEGV_BNDERR => { |
| 195 | + // The `SEGV_BNDERR` suberror of `SIGSEGV` populates `si_lower` and `si_upper`. |
| 196 | + // Safe because we dereference a valid value. |
| 197 | + let addr_bnd = unsafe { (*siginfo).si_fields._si_sigfault._si_stats._addr_bnd }; |
| 198 | + error!( |
| 199 | + "Failed address bound checks. Bounds: lower {:x?} upper {:x?}", |
| 200 | + addr_bnd._lower, addr_bnd._upper |
| 201 | + ); |
| 202 | + } |
| 203 | + SEGV_PKUERR => { |
| 204 | + // The `SEGV_PKUERR` suberror of `SIGSEGV` populates `si_pkey`. |
| 205 | + // Safe because we dereference a valid value. |
| 206 | + let pkey = unsafe { (*siginfo).si_fields._si_sigfault._si_stats._pkey }; |
| 207 | + error!("Failed protection key checks: {}", pkey); |
| 208 | + } |
| 209 | + _ => (), |
| 210 | + } |
| 211 | + } |
| 212 | + _ => (), |
| 213 | + } |
| 214 | + |
| 215 | + // Log the metrics before exiting. |
| 216 | + if let Err(e) = LOGGER.log_metrics() { |
| 217 | + error!("Failed to log metrics while stopping: {}", e); |
| 218 | + } |
| 219 | + |
| 220 | + // Safe because we're terminating the process anyway. We don't actually do anything when |
| 221 | + // running unit tests. |
| 222 | + #[cfg(not(test))] |
| 223 | + unsafe { |
| 224 | + _exit(i32::from(match si_signo { |
| 225 | + SIGBUS => super::FC_EXIT_CODE_SIGBUS, |
| 226 | + SIGSEGV => super::FC_EXIT_CODE_SIGSEGV, |
| 227 | + _ => super::FC_EXIT_CODE_UNEXPECTED_ERROR, |
| 228 | + })) |
| 229 | + }; |
| 230 | +} |
0 commit comments