Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Privacy.h" |
| 18 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 19 | #include <android/os/IncidentReportArgs.h> |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 21 | #include <strstream> |
| 22 | |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 23 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace incidentd { |
| 27 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 28 | using namespace android::os; |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 29 | using std::strstream; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 30 | |
Yao Chen | 35be60e | 2019-04-25 11:52:10 -0700 | [diff] [blame] | 31 | static const bool kEncryptionEnabled = false; |
| 32 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 33 | uint64_t encode_field_id(const Privacy* p) { return (uint64_t)p->type << 32 | p->field_id; } |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 34 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 35 | string Privacy::toString() const { |
| 36 | if (this == NULL) { |
| 37 | return "Privacy{null}"; |
| 38 | } |
| 39 | strstream os; |
| 40 | os << "Privacy{field_id=" << field_id << " type=" << ((int)type) |
| 41 | << " children=" << ((void*)children) << " policy=" << ((int)policy) << "}"; |
| 42 | return os.str(); |
| 43 | } |
| 44 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 45 | const Privacy* lookup(const Privacy* p, uint32_t fieldId) { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 46 | if (p->children == NULL) return NULL; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 47 | for (int i = 0; p->children[i] != NULL; i++) { // NULL-terminated. |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 48 | if (p->children[i]->field_id == fieldId) return p->children[i]; |
| 49 | // Incident section gen tool guarantees field ids in ascending order. |
| 50 | if (p->children[i]->field_id > fieldId) return NULL; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 51 | } |
| 52 | return NULL; |
| 53 | } |
| 54 | |
Yao Chen | 35be60e | 2019-04-25 11:52:10 -0700 | [diff] [blame] | 55 | bool sectionEncryption(int section_id) { |
| 56 | return kEncryptionEnabled ? (section_id == 3025) /*restricted image section*/ : false; |
| 57 | } |
Yao Chen | 43706b4 | 2019-04-21 14:34:30 -0700 | [diff] [blame] | 58 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 59 | static bool isAllowed(const uint8_t policy, const uint8_t check) { |
| 60 | switch (check) { |
| 61 | case PRIVACY_POLICY_LOCAL: |
| 62 | return policy == PRIVACY_POLICY_LOCAL; |
| 63 | case PRIVACY_POLICY_EXPLICIT: |
| 64 | case PRIVACY_POLICY_UNSET: |
| 65 | return policy == PRIVACY_POLICY_LOCAL |
| 66 | || policy == PRIVACY_POLICY_EXPLICIT |
| 67 | || policy == PRIVACY_POLICY_UNSET; |
| 68 | case PRIVACY_POLICY_AUTOMATIC: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 69 | return true; |
| 70 | default: |
| 71 | return false; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 75 | PrivacySpec::PrivacySpec(uint8_t argPolicy) { |
| 76 | // TODO: Why on earth do we have two definitions of policy. Maybe |
| 77 | // it's not too late to clean this up. |
| 78 | switch (argPolicy) { |
| 79 | case android::os::PRIVACY_POLICY_AUTOMATIC: |
| 80 | case android::os::PRIVACY_POLICY_EXPLICIT: |
| 81 | case android::os::PRIVACY_POLICY_LOCAL: |
| 82 | mPolicy = argPolicy; |
| 83 | break; |
| 84 | default: |
| 85 | mPolicy = android::os::PRIVACY_POLICY_AUTOMATIC; |
| 86 | break; |
| 87 | } |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 90 | bool PrivacySpec::operator<(const PrivacySpec& that) const { |
| 91 | return mPolicy < that.mPolicy; |
| 92 | } |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 93 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 94 | bool PrivacySpec::CheckPremission(const Privacy* privacy, const uint8_t defaultDest) const { |
| 95 | uint8_t check = privacy != NULL ? privacy->policy : defaultDest; |
| 96 | return isAllowed(mPolicy, check); |
| 97 | } |
| 98 | |
| 99 | bool PrivacySpec::RequireAll() const { |
| 100 | return mPolicy == android::os::PRIVACY_POLICY_LOCAL; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 101 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 102 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 103 | uint8_t cleanup_privacy_policy(uint8_t policy) { |
| 104 | if (policy >= PRIVACY_POLICY_AUTOMATIC) { |
| 105 | return PRIVACY_POLICY_AUTOMATIC; |
| 106 | } |
| 107 | if (policy >= PRIVACY_POLICY_EXPLICIT) { |
| 108 | return PRIVACY_POLICY_EXPLICIT; |
| 109 | } |
| 110 | return PRIVACY_POLICY_LOCAL; |
| 111 | } |
| 112 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 113 | } // namespace incidentd |
| 114 | } // namespace os |
| 115 | } // namespace android |