Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "src/tracing/core/packet_stream_validator.h" |
| 18 | |
| 19 | #include <inttypes.h> |
| 20 | #include <stddef.h> |
| 21 | |
| 22 | #include "perfetto/base/logging.h" |
| 23 | #include "perfetto/protozero/proto_utils.h" |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 24 | #include "perfetto/trace/trusted_packet.pb.h" |
| 25 | |
| 26 | namespace perfetto { |
| 27 | |
| 28 | // static |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 29 | bool PacketStreamValidator::Validate(const Slices& slices) { |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 30 | SlicedProtobufInputStream stream(&slices); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 31 | size_t size = 0; |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 32 | for (const Slice& slice : slices) |
| 33 | size += slice.size; |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 34 | |
| 35 | protos::TrustedPacket packet; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 36 | if (!packet.ParseFromBoundedZeroCopyStream(&stream, static_cast<int>(size))) |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 37 | return false; |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 38 | |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 39 | // Only the service is allowed to fill in the trusted uid. |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 40 | if (packet.optional_trusted_uid_case() != |
| 41 | protos::TrustedPacket::OPTIONAL_TRUSTED_UID_NOT_SET) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | // Only the service is allowed to fill in the TraceConfig. |
| 46 | if (packet.has_trace_config()) |
| 47 | return false; |
| 48 | |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 49 | // Only the service is allowed to fill in the TraceStats. |
| 50 | if (packet.has_trace_stats()) |
| 51 | return false; |
| 52 | |
Primiano Tucci | 9754d0d | 2018-09-15 12:41:46 +0100 | [diff] [blame^] | 53 | if (!packet.synchronization_marker().empty()) |
| 54 | return false; |
| 55 | |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 56 | // We are deliberately not checking for clock_snapshot for the moment. It's |
| 57 | // unclear if we want to allow producers to snapshot their clocks. Ideally we |
| 58 | // want a security model where producers can only snapshot their own clocks |
| 59 | // and not system ones. However, right now, there isn't a compelling need to |
| 60 | // be so prescriptive. |
| 61 | |
| 62 | return true; |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | } // namespace perfetto |