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