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 | */ |
| 16 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 17 | #define LOG_TAG "incidentd" |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 18 | |
| 19 | #include "PrivacyBuffer.h" |
| 20 | #include "io_util.h" |
| 21 | |
| 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 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 27 | const bool DEBUG = false; |
| 28 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Write the field to buf based on the wire type, iterator will point to next field. |
| 31 | * If skip is set to true, no data will be written to buf. Return number of bytes written. |
| 32 | */ |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 33 | void |
| 34 | PrivacyBuffer::writeFieldOrSkip(uint32_t fieldTag, bool skip) |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 35 | { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 36 | if (DEBUG) ALOGD("%s field %d (wiretype = %d)", skip ? "skip" : "write", |
| 37 | read_field_id(fieldTag), read_wire_type(fieldTag)); |
| 38 | |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 39 | uint8_t wireType = read_wire_type(fieldTag); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 40 | size_t bytesToWrite = 0; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 41 | uint32_t varint = 0; |
| 42 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 43 | switch (wireType) { |
| 44 | case WIRE_TYPE_VARINT: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 45 | varint = mData.readRawVarint(); |
| 46 | if (!skip) { |
| 47 | mProto.writeRawVarint(fieldTag); |
| 48 | mProto.writeRawVarint(varint); |
| 49 | } |
| 50 | return; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 51 | case WIRE_TYPE_FIXED64: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 52 | if (!skip) mProto.writeRawVarint(fieldTag); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 53 | bytesToWrite = 8; |
| 54 | break; |
| 55 | case WIRE_TYPE_LENGTH_DELIMITED: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 56 | bytesToWrite = mData.readRawVarint(); |
| 57 | if(!skip) mProto.writeLengthDelimitedHeader(read_field_id(fieldTag), bytesToWrite); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 58 | break; |
| 59 | case WIRE_TYPE_FIXED32: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 60 | if (!skip) mProto.writeRawVarint(fieldTag); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 61 | bytesToWrite = 4; |
| 62 | break; |
| 63 | } |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 64 | if (DEBUG) ALOGD("%s %d bytes of data", skip ? "skip" : "write", (int)bytesToWrite); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 65 | if (skip) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 66 | mData.rp()->move(bytesToWrite); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 67 | } else { |
| 68 | for (size_t i=0; i<bytesToWrite; i++) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 69 | mProto.writeRawByte(mData.next()); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Strip next field based on its private policy and request spec, then stores data in buf. |
| 76 | * Return NO_ERROR if succeeds, otherwise BAD_VALUE is returned to indicate bad data in FdBuffer. |
| 77 | * |
| 78 | * The iterator must point to the head of a protobuf formatted field for successful operation. |
| 79 | * After exit with NO_ERROR, iterator points to the next protobuf field's head. |
| 80 | */ |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 81 | status_t |
| 82 | PrivacyBuffer::stripField(const Privacy* parentPolicy, const PrivacySpec& spec) |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 83 | { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 84 | if (!mData.hasNext() || parentPolicy == NULL) return BAD_VALUE; |
| 85 | uint32_t fieldTag = mData.readRawVarint(); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 86 | const Privacy* policy = lookup(parentPolicy, read_field_id(fieldTag)); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 87 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 88 | if (policy == NULL || policy->children == NULL) { |
| 89 | if (DEBUG) ALOGD("Not a message field %d: dest(%d)", read_field_id(fieldTag), |
| 90 | policy != NULL ? policy->dest : parentPolicy->dest); |
| 91 | |
| 92 | bool skip = !spec.CheckPremission(policy, parentPolicy->dest); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 93 | // iterator will point to head of next field |
| 94 | writeFieldOrSkip(fieldTag, skip); |
| 95 | return NO_ERROR; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 96 | } |
| 97 | // 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] | 98 | uint32_t msgSize = mData.readRawVarint(); |
| 99 | EncodedBuffer::Pointer start = mData.rp()->copy(); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 100 | long long token = mProto.start(encode_field_id(policy)); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 101 | while (mData.rp()->pos() - start.pos() != msgSize) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 102 | status_t err = stripField(policy, spec); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 103 | if (err != NO_ERROR) return err; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 104 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 105 | mProto.end(token); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 106 | return NO_ERROR; |
| 107 | } |
| 108 | |
| 109 | // ================================================================================ |
| 110 | PrivacyBuffer::PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator& data) |
| 111 | :mPolicy(policy), |
| 112 | mData(data), |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 113 | mProto(), |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 114 | mSize(0) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | PrivacyBuffer::~PrivacyBuffer() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | status_t |
| 123 | PrivacyBuffer::strip(const PrivacySpec& spec) |
| 124 | { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 125 | if (DEBUG) ALOGD("Strip with spec %d", spec.dest); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 126 | // optimization when no strip happens |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 127 | if (mPolicy == NULL || mPolicy->children == NULL || spec.RequireAll()) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 128 | if (spec.CheckPremission(mPolicy)) mSize = mData.size(); |
| 129 | return NO_ERROR; |
| 130 | } |
| 131 | while (mData.hasNext()) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 132 | status_t err = stripField(mPolicy, spec); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 133 | if (err != NO_ERROR) return err; |
| 134 | } |
| 135 | if (mData.bytesRead() != mData.size()) return BAD_VALUE; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 136 | mSize = mProto.size(); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 137 | mData.rp()->rewind(); // rewind the read pointer back to beginning after the strip. |
| 138 | return NO_ERROR; |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | PrivacyBuffer::clear() |
| 143 | { |
| 144 | mSize = 0; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 145 | mProto = ProtoOutputStream(); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | size_t |
| 149 | PrivacyBuffer::size() const { return mSize; } |
| 150 | |
| 151 | status_t |
| 152 | PrivacyBuffer::flush(int fd) |
| 153 | { |
| 154 | status_t err = NO_ERROR; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 155 | EncodedBuffer::iterator iter = size() == mData.size() ? mData : mProto.data(); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 156 | while (iter.readBuffer() != NULL) { |
| 157 | err = write_all(fd, iter.readBuffer(), iter.currentToRead()); |
| 158 | iter.rp()->move(iter.currentToRead()); |
| 159 | if (err != NO_ERROR) return err; |
| 160 | } |
| 161 | return NO_ERROR; |
| 162 | } |