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