Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 1 | // Copyright 2020, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Keystore error provides convenience methods and types for Keystore error handling. |
| 16 | //! Clients of Keystore expect one of two error codes, i.e., a Keystore ResponseCode as |
| 17 | //! defined by the Keystore AIDL interface, or a Keymint ErrorCode as defined by |
| 18 | //! the Keymint HAL specification. |
| 19 | //! This crate provides `Error` which can wrap both. It is to be used |
| 20 | //! internally by Keystore to diagnose error conditions that need to be reported to |
| 21 | //! the client. To report the error condition to the client the Keystore AIDL |
| 22 | //! interface defines a wire type `Result` which is distinctly different from Rust's |
| 23 | //! `enum Result<T,E>`. |
| 24 | //! |
| 25 | //! This crate provides the convenience method `map_or_log_err` to convert `anyhow::Error` |
| 26 | //! into this wire type. In addition to handling the conversion of `Error` |
| 27 | //! to the `Result` wire type it handles any other error by mapping it to |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 28 | //! `ResponseCode::SYSTEM_ERROR` and logs any error condition. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 29 | //! |
| 30 | //! Keystore functions should use `anyhow::Result` to return error conditions, and |
| 31 | //! context should be added every time an error is forwarded. |
| 32 | |
| 33 | use std::cmp::PartialEq; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 34 | |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 35 | pub use android_hardware_security_keymint::aidl::android::hardware::security::keymint::ErrorCode::ErrorCode; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 36 | pub use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 37 | |
Janis Danisevskis | ce99543 | 2020-07-21 12:22:34 -0700 | [diff] [blame] | 38 | use keystore2_selinux as selinux; |
| 39 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 40 | use android_system_keystore2::binder::{ |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 41 | ExceptionCode, Result as BinderResult, Status as BinderStatus, StatusCode, |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 42 | }; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 43 | |
| 44 | /// This is the main Keystore error type. It wraps the Keystore `ResponseCode` generated |
| 45 | /// from AIDL in the `Rc` variant and Keymint `ErrorCode` in the Km variant. |
| 46 | #[derive(Debug, thiserror::Error, PartialEq)] |
| 47 | pub enum Error { |
| 48 | /// Wraps a Keystore `ResponseCode` as defined by the Keystore AIDL interface specification. |
| 49 | #[error("Error::Rc({0:?})")] |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 50 | Rc(ResponseCode), |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 51 | /// Wraps a Keymint `ErrorCode` as defined by the Keymint AIDL interface specification. |
| 52 | #[error("Error::Km({0:?})")] |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 53 | Km(ErrorCode), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 54 | /// Wraps a Binder exception code other than a service specific exception. |
| 55 | #[error("Binder exception code {0:?}, {1:?}")] |
| 56 | Binder(ExceptionCode, i32), |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 57 | /// Wraps a Binder status code. |
| 58 | #[error("Binder transaction error {0:?}")] |
| 59 | BinderTransaction(StatusCode), |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame^] | 60 | /// Wraps a Remote Provisioning ErrorCode as defined by the IRemotelyProvisionedComponent |
| 61 | /// AIDL interface spec. |
| 62 | #[error("Error::Rp({0:?})")] |
| 63 | Rp(ErrorCode), |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | impl Error { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 67 | /// Short hand for `Error::Rc(ResponseCode::SYSTEM_ERROR)` |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 68 | pub fn sys() -> Self { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 69 | Error::Rc(ResponseCode::SYSTEM_ERROR) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 72 | /// Short hand for `Error::Rc(ResponseCode::PERMISSION_DENIED` |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 73 | pub fn perm() -> Self { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 74 | Error::Rc(ResponseCode::PERMISSION_DENIED) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 78 | /// Helper function to map the binder status we get from calls into KeyMint |
| 79 | /// to a Keystore Error. We don't create an anyhow error here to make |
| 80 | /// it easier to evaluate KeyMint errors, which we must do in some cases, e.g., |
| 81 | /// when diagnosing authentication requirements, update requirements, and running |
| 82 | /// out of operation slots. |
| 83 | pub fn map_km_error<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 84 | r.map_err(|s| { |
| 85 | match s.exception_code() { |
| 86 | ExceptionCode::SERVICE_SPECIFIC => { |
| 87 | let se = s.service_specific_error(); |
| 88 | if se < 0 { |
| 89 | // Negative service specific errors are KM error codes. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 90 | Error::Km(ErrorCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 91 | } else { |
| 92 | // Non negative error codes cannot be KM error codes. |
| 93 | // So we create an `Error::Binder` variant to preserve |
| 94 | // the service specific error code for logging. |
| 95 | // `map_or_log_err` will map this on a system error, |
| 96 | // but not before logging the details to logcat. |
| 97 | Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se) |
| 98 | } |
| 99 | } |
| 100 | // We create `Error::Binder` to preserve the exception code |
| 101 | // for logging. |
| 102 | // `map_or_log_err` will map this on a system error. |
| 103 | e_code => Error::Binder(e_code, 0), |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame^] | 108 | /// Helper function to map the binder status we get from calls into a RemotelyProvisionedComponent |
| 109 | /// to a Keystore Error. We don't create an anyhow error here to make |
| 110 | /// it easier to evaluate service specific errors. |
| 111 | pub fn map_rem_prov_error<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 112 | r.map_err(|s| match s.exception_code() { |
| 113 | ExceptionCode::SERVICE_SPECIFIC => Error::Rp(ErrorCode(s.service_specific_error())), |
| 114 | e_code => Error::Binder(e_code, 0), |
| 115 | }) |
| 116 | } |
| 117 | |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 118 | /// This function is similar to map_km_error only that we don't expect |
| 119 | /// any KeyMint error codes, we simply preserve the exception code and optional |
| 120 | /// service specific exception. |
| 121 | pub fn map_binder_status<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 122 | r.map_err(|s| match s.exception_code() { |
| 123 | ExceptionCode::SERVICE_SPECIFIC => { |
| 124 | let se = s.service_specific_error(); |
| 125 | Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se) |
| 126 | } |
| 127 | ExceptionCode::TRANSACTION_FAILED => { |
| 128 | let e = s.transaction_error(); |
| 129 | Error::BinderTransaction(e) |
| 130 | } |
| 131 | e_code => Error::Binder(e_code, 0), |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | /// This function maps a status code onto a Keystore Error. |
| 136 | pub fn map_binder_status_code<T>(r: Result<T, StatusCode>) -> Result<T, Error> { |
| 137 | r.map_err(Error::BinderTransaction) |
| 138 | } |
| 139 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 140 | /// This function should be used by Keystore service calls to translate error conditions |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 141 | /// into service specific exceptions. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 142 | /// |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 143 | /// All error conditions get logged by this function. |
| 144 | /// |
| 145 | /// All `Error::Rc(x)` and `Error::Km(x)` variants get mapped onto a service specific error |
| 146 | /// code of x. This is possible because KeyMint `ErrorCode` errors are always negative and |
| 147 | /// `ResponseCode` codes are always positive. |
| 148 | /// `selinux::Error::PermissionDenied` is mapped on `ResponseCode::PERMISSION_DENIED`. |
| 149 | /// |
| 150 | /// All non `Error` error conditions and the Error::Binder variant get mapped onto |
| 151 | /// ResponseCode::SYSTEM_ERROR`. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 152 | /// |
| 153 | /// `handle_ok` will be called if `result` is `Ok(value)` where `value` will be passed |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 154 | /// as argument to `handle_ok`. `handle_ok` must generate a `BinderResult<T>`, but it |
| 155 | /// typically returns Ok(value). |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 156 | /// |
| 157 | /// # Examples |
| 158 | /// |
| 159 | /// ``` |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 160 | /// fn loadKey() -> anyhow::Result<Vec<u8>> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 161 | /// if (good_but_auth_required) { |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 162 | /// Ok(vec!['k', 'e', 'y']) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 163 | /// } else { |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 164 | /// Err(anyhow!(Error::Rc(ResponseCode::KEY_NOT_FOUND))) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 165 | /// } |
| 166 | /// } |
| 167 | /// |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 168 | /// map_or_log_err(loadKey(), Ok) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 169 | /// ``` |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 170 | pub fn map_or_log_err<T, U, F>(result: anyhow::Result<U>, handle_ok: F) -> BinderResult<T> |
| 171 | where |
| 172 | F: FnOnce(U) -> BinderResult<T>, |
| 173 | { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 174 | result.map_or_else( |
| 175 | |e| { |
| 176 | log::error!("{:?}", e); |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 177 | let root_cause = e.root_cause(); |
| 178 | let rc = match root_cause.downcast_ref::<Error>() { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 179 | Some(Error::Rc(rcode)) => rcode.0, |
| 180 | Some(Error::Km(ec)) => ec.0, |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame^] | 181 | Some(Error::Rp(_)) => ResponseCode::SYSTEM_ERROR.0, |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 182 | // If an Error::Binder reaches this stage we report a system error. |
| 183 | // The exception code and possible service specific error will be |
| 184 | // printed in the error log above. |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 185 | Some(Error::Binder(_, _)) | Some(Error::BinderTransaction(_)) => { |
| 186 | ResponseCode::SYSTEM_ERROR.0 |
| 187 | } |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 188 | None => match root_cause.downcast_ref::<selinux::Error>() { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 189 | Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0, |
| 190 | _ => ResponseCode::SYSTEM_ERROR.0, |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 191 | }, |
| 192 | }; |
| 193 | Err(BinderStatus::new_service_specific_error(rc, None)) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 194 | }, |
| 195 | handle_ok, |
| 196 | ) |
| 197 | } |
| 198 | |
| 199 | #[cfg(test)] |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 200 | pub mod tests { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 201 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 202 | use super::*; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 203 | use android_system_keystore2::binder::{ |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 204 | ExceptionCode, Result as BinderResult, Status as BinderStatus, |
| 205 | }; |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 206 | use anyhow::{anyhow, Context}; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 207 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 208 | fn nested_nested_rc(rc: ResponseCode) -> anyhow::Result<()> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 209 | Err(anyhow!(Error::Rc(rc))).context("nested nested rc") |
| 210 | } |
| 211 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 212 | fn nested_rc(rc: ResponseCode) -> anyhow::Result<()> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 213 | nested_nested_rc(rc).context("nested rc") |
| 214 | } |
| 215 | |
| 216 | fn nested_nested_ec(ec: ErrorCode) -> anyhow::Result<()> { |
| 217 | Err(anyhow!(Error::Km(ec))).context("nested nested ec") |
| 218 | } |
| 219 | |
| 220 | fn nested_ec(ec: ErrorCode) -> anyhow::Result<()> { |
| 221 | nested_nested_ec(ec).context("nested ec") |
| 222 | } |
| 223 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 224 | fn nested_nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 225 | Ok(rc) |
| 226 | } |
| 227 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 228 | fn nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 229 | nested_nested_ok(rc).context("nested ok") |
| 230 | } |
| 231 | |
Janis Danisevskis | ce99543 | 2020-07-21 12:22:34 -0700 | [diff] [blame] | 232 | fn nested_nested_selinux_perm() -> anyhow::Result<()> { |
| 233 | Err(anyhow!(selinux::Error::perm())).context("nested nexted selinux permission denied") |
| 234 | } |
| 235 | |
| 236 | fn nested_selinux_perm() -> anyhow::Result<()> { |
| 237 | nested_nested_selinux_perm().context("nested selinux permission denied") |
| 238 | } |
| 239 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 240 | #[derive(Debug, thiserror::Error)] |
| 241 | enum TestError { |
| 242 | #[error("TestError::Fail")] |
| 243 | Fail = 0, |
| 244 | } |
| 245 | |
| 246 | fn nested_nested_other_error() -> anyhow::Result<()> { |
| 247 | Err(anyhow!(TestError::Fail)).context("nested nested other error") |
| 248 | } |
| 249 | |
| 250 | fn nested_other_error() -> anyhow::Result<()> { |
| 251 | nested_nested_other_error().context("nested other error") |
| 252 | } |
| 253 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 254 | fn binder_sse_error(sse: i32) -> BinderResult<()> { |
| 255 | Err(BinderStatus::new_service_specific_error(sse, None)) |
| 256 | } |
| 257 | |
| 258 | fn binder_exception(ex: ExceptionCode) -> BinderResult<()> { |
| 259 | Err(BinderStatus::new_exception(ex, None)) |
| 260 | } |
| 261 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 262 | #[test] |
| 263 | fn keystore_error_test() -> anyhow::Result<(), String> { |
| 264 | android_logger::init_once( |
| 265 | android_logger::Config::default() |
| 266 | .with_tag("keystore_error_tests") |
| 267 | .with_min_level(log::Level::Debug), |
| 268 | ); |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 269 | // All Error::Rc(x) get mapped on a service specific error |
| 270 | // code of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 271 | for rc in ResponseCode::LOCKED.0..ResponseCode::BACKEND_BUSY.0 { |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 272 | assert_eq!( |
| 273 | Result::<(), i32>::Err(rc), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 274 | map_or_log_err(nested_rc(ResponseCode(rc)), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 275 | .map_err(|s| s.service_specific_error()) |
| 276 | ); |
| 277 | } |
| 278 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 279 | // All Keystore Error::Km(x) get mapped on a service |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 280 | // specific error of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 281 | for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 { |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 282 | assert_eq!( |
| 283 | Result::<(), i32>::Err(ec), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 284 | map_or_log_err(nested_ec(ErrorCode(ec)), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 285 | .map_err(|s| s.service_specific_error()) |
| 286 | ); |
| 287 | } |
| 288 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 289 | // All Keymint errors x received through a Binder Result get mapped on |
| 290 | // a service specific error of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 291 | for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 { |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 292 | assert_eq!( |
| 293 | Result::<(), i32>::Err(ec), |
| 294 | map_or_log_err( |
| 295 | map_km_error(binder_sse_error(ec)) |
| 296 | .with_context(|| format!("Km error code: {}.", ec)), |
| 297 | |_| Err(BinderStatus::ok()) |
| 298 | ) |
| 299 | .map_err(|s| s.service_specific_error()) |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | // map_km_error creates an Error::Binder variant storing |
| 304 | // ExceptionCode::SERVICE_SPECIFIC and the given |
| 305 | // service specific error. |
| 306 | let sse = map_km_error(binder_sse_error(1)); |
| 307 | assert_eq!(Err(Error::Binder(ExceptionCode::SERVICE_SPECIFIC, 1)), sse); |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 308 | // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR. |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 309 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 310 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 311 | map_or_log_err(sse.context("Non negative service specific error."), |_| Err( |
| 312 | BinderStatus::ok() |
| 313 | )) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 314 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 315 | ); |
| 316 | |
| 317 | // map_km_error creates a Error::Binder variant storing the given exception code. |
| 318 | let binder_exception = map_km_error(binder_exception(ExceptionCode::TRANSACTION_FAILED)); |
| 319 | assert_eq!(Err(Error::Binder(ExceptionCode::TRANSACTION_FAILED, 0)), binder_exception); |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 320 | // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR. |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 321 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 322 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 323 | map_or_log_err(binder_exception.context("Binder Exception."), |_| Err( |
| 324 | BinderStatus::ok() |
| 325 | )) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 326 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 327 | ); |
| 328 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 329 | // selinux::Error::Perm() needs to be mapped to ResponseCode::PERMISSION_DENIED |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 330 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 331 | Result::<(), ResponseCode>::Err(ResponseCode::PERMISSION_DENIED), |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 332 | map_or_log_err(nested_selinux_perm(), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 333 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 334 | ); |
| 335 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 336 | // All other errors get mapped on System Error. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 337 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 338 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 339 | map_or_log_err(nested_other_error(), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 340 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 341 | ); |
| 342 | |
| 343 | // Result::Ok variants get passed to the ok handler. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 344 | assert_eq!(Ok(ResponseCode::LOCKED), map_or_log_err(nested_ok(ResponseCode::LOCKED), Ok)); |
| 345 | assert_eq!( |
| 346 | Ok(ResponseCode::SYSTEM_ERROR), |
| 347 | map_or_log_err(nested_ok(ResponseCode::SYSTEM_ERROR), Ok) |
| 348 | ); |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 349 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 350 | Ok(()) |
| 351 | } |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 352 | |
| 353 | //Helper function to test whether error cases are handled as expected. |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 354 | pub fn check_result_contains_error_string<T>( |
| 355 | result: anyhow::Result<T>, |
| 356 | expected_error_string: &str, |
| 357 | ) { |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 358 | let error_str = format!( |
| 359 | "{:#?}", |
| 360 | result.err().unwrap_or_else(|| panic!("Expected the error: {}", expected_error_string)) |
| 361 | ); |
| 362 | assert!( |
| 363 | error_str.contains(expected_error_string), |
| 364 | "The string \"{}\" should contain \"{}\"", |
| 365 | error_str, |
| 366 | expected_error_string |
| 367 | ); |
| 368 | } |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 369 | } // mod tests |