| 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 | |
| 19 | #include <android/os/IncidentReportArgs.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <strstream> |
| 22 | |
| 23 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace incidentd { |
| 27 | |
| 28 | using namespace android::os; |
| 29 | using std::strstream; |
| 30 | |
| 31 | uint64_t encode_field_id(const Privacy* p) { return (uint64_t)p->type << 32 | p->field_id; } |
| 32 | |
| 33 | string Privacy::toString() const { |
| 34 | if (this == NULL) { |
| 35 | return "Privacy{null}"; |
| 36 | } |
| 37 | strstream os; |
| 38 | os << "Privacy{field_id=" << field_id << " type=" << ((int)type) |
| 39 | << " children=" << ((void*)children) << " policy=" << ((int)policy) << "}"; |
| 40 | return os.str(); |
| 41 | } |
| 42 | |
| 43 | const Privacy* lookup(const Privacy* p, uint32_t fieldId) { |
| 44 | if (p->children == NULL) return NULL; |
| 45 | for (int i = 0; p->children[i] != NULL; i++) { // NULL-terminated. |
| 46 | if (p->children[i]->field_id == fieldId) return p->children[i]; |
| 47 | // Incident section gen tool guarantees field ids in ascending order. |
| 48 | if (p->children[i]->field_id > fieldId) return NULL; |
| 49 | } |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | static bool isAllowed(const uint8_t policy, const uint8_t check) { |
| 54 | switch (check) { |
| 55 | case PRIVACY_POLICY_LOCAL: |
| 56 | return policy == PRIVACY_POLICY_LOCAL; |
| 57 | case PRIVACY_POLICY_EXPLICIT: |
| 58 | case PRIVACY_POLICY_UNSET: |
| 59 | return policy == PRIVACY_POLICY_LOCAL |
| 60 | || policy == PRIVACY_POLICY_EXPLICIT |
| 61 | || policy == PRIVACY_POLICY_UNSET; |
| 62 | case PRIVACY_POLICY_AUTOMATIC: |
| 63 | return true; |
| 64 | default: |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | PrivacySpec::PrivacySpec(uint8_t argPolicy) { |
| 70 | // TODO: Why on earth do we have two definitions of policy. Maybe |
| 71 | // it's not too late to clean this up. |
| 72 | switch (argPolicy) { |
| 73 | case android::os::PRIVACY_POLICY_AUTOMATIC: |
| 74 | case android::os::PRIVACY_POLICY_EXPLICIT: |
| 75 | case android::os::PRIVACY_POLICY_LOCAL: |
| 76 | mPolicy = argPolicy; |
| 77 | break; |
| 78 | default: |
| 79 | mPolicy = android::os::PRIVACY_POLICY_AUTOMATIC; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | bool PrivacySpec::operator<(const PrivacySpec& that) const { |
| 85 | return mPolicy < that.mPolicy; |
| 86 | } |
| 87 | |
| 88 | bool PrivacySpec::CheckPremission(const Privacy* privacy, const uint8_t defaultDest) const { |
| 89 | uint8_t check = privacy != NULL ? privacy->policy : defaultDest; |
| 90 | return isAllowed(mPolicy, check); |
| 91 | } |
| 92 | |
| 93 | bool PrivacySpec::RequireAll() const { |
| 94 | return mPolicy == android::os::PRIVACY_POLICY_LOCAL; |
| 95 | } |
| 96 | |
| 97 | uint8_t cleanup_privacy_policy(uint8_t policy) { |
| 98 | if (policy >= PRIVACY_POLICY_AUTOMATIC) { |
| 99 | return PRIVACY_POLICY_AUTOMATIC; |
| 100 | } |
| 101 | if (policy >= PRIVACY_POLICY_EXPLICIT) { |
| 102 | return PRIVACY_POLICY_EXPLICIT; |
| 103 | } |
| 104 | return PRIVACY_POLICY_LOCAL; |
| 105 | } |
| 106 | |
| 107 | } // namespace incidentd |
| 108 | } // namespace os |
| 109 | } // namespace android |