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