Yi Jin | c23fad2 | 2017-09-15 17:24:59 -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 | */ |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 16 | #define DEBUG false |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 17 | #include "Log.h" |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 18 | |
| 19 | #include "PrivacyBuffer.h" |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 20 | #include "incidentd_util.h" |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 21 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 22 | #include <android-base/file.h> |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 23 | #include <android/util/protobuf.h> |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 24 | #include <cutils/log.h> |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 25 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace os { |
| 28 | namespace incidentd { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Write the field to buf based on the wire type, iterator will point to next field. |
| 32 | * If skip is set to true, no data will be written to buf. Return number of bytes written. |
| 33 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 34 | void PrivacyBuffer::writeFieldOrSkip(uint32_t fieldTag, bool skip) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 35 | uint8_t wireType = read_wire_type(fieldTag); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 36 | size_t bytesToWrite = 0; |
Yi Jin | ad3e6e5 | 2018-04-03 15:10:34 -0700 | [diff] [blame] | 37 | uint64_t varint = 0; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 38 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 39 | switch (wireType) { |
| 40 | case WIRE_TYPE_VARINT: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 41 | varint = mData.readRawVarint(); |
| 42 | if (!skip) { |
| 43 | mProto.writeRawVarint(fieldTag); |
| 44 | mProto.writeRawVarint(varint); |
| 45 | } |
| 46 | return; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 47 | case WIRE_TYPE_FIXED64: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 48 | if (!skip) mProto.writeRawVarint(fieldTag); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 49 | bytesToWrite = 8; |
| 50 | break; |
| 51 | case WIRE_TYPE_LENGTH_DELIMITED: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 52 | bytesToWrite = mData.readRawVarint(); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 53 | if (!skip) mProto.writeLengthDelimitedHeader(read_field_id(fieldTag), bytesToWrite); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 54 | break; |
| 55 | case WIRE_TYPE_FIXED32: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 56 | if (!skip) mProto.writeRawVarint(fieldTag); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 57 | bytesToWrite = 4; |
| 58 | break; |
| 59 | } |
| 60 | if (skip) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 61 | mData.rp()->move(bytesToWrite); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 62 | } else { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 63 | for (size_t i = 0; i < bytesToWrite; i++) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 64 | mProto.writeRawByte(mData.next()); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Strip next field based on its private policy and request spec, then stores data in buf. |
| 71 | * Return NO_ERROR if succeeds, otherwise BAD_VALUE is returned to indicate bad data in FdBuffer. |
| 72 | * |
| 73 | * The iterator must point to the head of a protobuf formatted field for successful operation. |
| 74 | * After exit with NO_ERROR, iterator points to the next protobuf field's head. |
| 75 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 76 | status_t PrivacyBuffer::stripField(const Privacy* parentPolicy, const PrivacySpec& spec, |
| 77 | int depth /* use as a counter for this recusive method. */) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 78 | if (!mData.hasNext() || parentPolicy == NULL) return BAD_VALUE; |
| 79 | uint32_t fieldTag = mData.readRawVarint(); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 80 | uint32_t fieldId = read_field_id(fieldTag); |
| 81 | const Privacy* policy = lookup(parentPolicy, fieldId); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 82 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 83 | VLOG("[Depth %2d]Try to strip id %d, wiretype %d", depth, fieldId, read_wire_type(fieldTag)); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 84 | if (policy == NULL || policy->children == NULL) { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 85 | bool skip = !spec.CheckPremission(policy, parentPolicy->dest); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 86 | // iterator will point to head of next field |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 87 | size_t currentAt = mData.rp()->pos(); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 88 | writeFieldOrSkip(fieldTag, skip); |
Yi Jin | 8cb370f | 2018-04-23 13:03:14 -0700 | [diff] [blame] | 89 | VLOG("[Depth %2d]Field %d %ss %zu bytes", depth, fieldId, skip ? "skip" : "write", |
| 90 | get_varint_size(fieldTag) + mData.rp()->pos() - currentAt); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 91 | return NO_ERROR; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 92 | } |
| 93 | // current field is message type and its sub-fields have extra privacy policies |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 94 | uint32_t msgSize = mData.readRawVarint(); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 95 | size_t start = mData.rp()->pos(); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 96 | uint64_t token = mProto.start(encode_field_id(policy)); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 97 | while (mData.rp()->pos() - start != msgSize) { |
| 98 | status_t err = stripField(policy, spec, depth + 1); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 99 | if (err != NO_ERROR) return err; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 100 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 101 | mProto.end(token); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 102 | return NO_ERROR; |
| 103 | } |
| 104 | |
| 105 | // ================================================================================ |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 106 | PrivacyBuffer::PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator data) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 107 | : mPolicy(policy), mData(data), mProto(), mSize(0) {} |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 108 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 109 | PrivacyBuffer::~PrivacyBuffer() {} |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 110 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 111 | status_t PrivacyBuffer::strip(const PrivacySpec& spec) { |
| 112 | VLOG("Strip with spec %d", spec.dest); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 113 | // optimization when no strip happens |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 114 | if (mPolicy == NULL || mPolicy->children == NULL || spec.RequireAll()) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 115 | if (spec.CheckPremission(mPolicy)) mSize = mData.size(); |
| 116 | return NO_ERROR; |
| 117 | } |
| 118 | while (mData.hasNext()) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 119 | status_t err = stripField(mPolicy, spec, 0); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 120 | if (err != NO_ERROR) return err; |
| 121 | } |
| 122 | if (mData.bytesRead() != mData.size()) return BAD_VALUE; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 123 | mSize = mProto.size(); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 124 | mData.rp()->rewind(); // rewind the read pointer back to beginning after the strip. |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 125 | return NO_ERROR; |
| 126 | } |
| 127 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 128 | void PrivacyBuffer::clear() { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 129 | mSize = 0; |
Yi Jin | 7f9e63b | 2018-02-02 16:25:11 -0800 | [diff] [blame] | 130 | mProto.clear(); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 133 | size_t PrivacyBuffer::size() const { return mSize; } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 134 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 135 | status_t PrivacyBuffer::flush(int fd) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 136 | status_t err = NO_ERROR; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 137 | EncodedBuffer::iterator iter = size() == mData.size() ? mData : mProto.data(); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 138 | while (iter.readBuffer() != NULL) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 139 | err = WriteFully(fd, iter.readBuffer(), iter.currentToRead()) ? NO_ERROR : -errno; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 140 | iter.rp()->move(iter.currentToRead()); |
| 141 | if (err != NO_ERROR) return err; |
| 142 | } |
| 143 | return NO_ERROR; |
| 144 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 145 | |
| 146 | } // namespace incidentd |
| 147 | } // namespace os |
| 148 | } // namespace android |