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> |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 23 | |
| 24 | using namespace android::util; |
| 25 | |
| 26 | /** |
| 27 | * Write the field to buf based on the wire type, iterator will point to next field. |
| 28 | * If skip is set to true, no data will be written to buf. Return number of bytes written. |
| 29 | */ |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 30 | void |
| 31 | PrivacyBuffer::writeFieldOrSkip(uint32_t fieldTag, bool skip) |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 32 | { |
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(); |
| 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 { |
| 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 | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 74 | status_t |
| 75 | PrivacyBuffer::stripField(const Privacy* parentPolicy, const PrivacySpec& spec) |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 76 | { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 77 | if (!mData.hasNext() || parentPolicy == NULL) return BAD_VALUE; |
| 78 | uint32_t fieldTag = mData.readRawVarint(); |
| 79 | const Privacy* policy = parentPolicy->lookup(read_field_id(fieldTag)); |
| 80 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 81 | if (policy == NULL || !policy->IsMessageType() || !policy->HasChildren()) { |
| 82 | bool skip = !spec.CheckPremission(policy); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 83 | // iterator will point to head of next field |
| 84 | writeFieldOrSkip(fieldTag, skip); |
| 85 | return NO_ERROR; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 86 | } |
| 87 | // 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] | 88 | uint32_t msgSize = mData.readRawVarint(); |
| 89 | EncodedBuffer::Pointer start = mData.rp()->copy(); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 90 | long long token = mProto.start(policy->EncodedFieldId()); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 91 | while (mData.rp()->pos() - start.pos() != msgSize) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 92 | status_t err = stripField(policy, spec); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 93 | if (err != NO_ERROR) return err; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 94 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 95 | mProto.end(token); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 96 | return NO_ERROR; |
| 97 | } |
| 98 | |
| 99 | // ================================================================================ |
| 100 | PrivacyBuffer::PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator& data) |
| 101 | :mPolicy(policy), |
| 102 | mData(data), |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 103 | mProto(), |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 104 | mSize(0) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | PrivacyBuffer::~PrivacyBuffer() |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | status_t |
| 113 | PrivacyBuffer::strip(const PrivacySpec& spec) |
| 114 | { |
| 115 | // optimization when no strip happens |
| 116 | if (mPolicy == NULL || !mPolicy->HasChildren() || spec.RequireAll()) { |
| 117 | if (spec.CheckPremission(mPolicy)) mSize = mData.size(); |
| 118 | return NO_ERROR; |
| 119 | } |
| 120 | while (mData.hasNext()) { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 121 | status_t err = stripField(mPolicy, spec); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 122 | if (err != NO_ERROR) return err; |
| 123 | } |
| 124 | if (mData.bytesRead() != mData.size()) return BAD_VALUE; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 125 | mSize = mProto.size(); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 126 | mData.rp()->rewind(); // rewind the read pointer back to beginning after the strip. |
| 127 | return NO_ERROR; |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | PrivacyBuffer::clear() |
| 132 | { |
| 133 | mSize = 0; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 134 | mProto = ProtoOutputStream(); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | size_t |
| 138 | PrivacyBuffer::size() const { return mSize; } |
| 139 | |
| 140 | status_t |
| 141 | PrivacyBuffer::flush(int fd) |
| 142 | { |
| 143 | status_t err = NO_ERROR; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 144 | EncodedBuffer::iterator iter = size() == mData.size() ? mData : mProto.data(); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 145 | while (iter.readBuffer() != NULL) { |
| 146 | err = write_all(fd, iter.readBuffer(), iter.currentToRead()); |
| 147 | iter.rp()->move(iter.currentToRead()); |
| 148 | if (err != NO_ERROR) return err; |
| 149 | } |
| 150 | return NO_ERROR; |
| 151 | } |