blob: 3e6f1bd76a0cbf6c3d4ab72c5e3e00a6df6d9c6c [file] [log] [blame]
Sami Kyostila32e0b542018-02-14 08:55:43 +00001/*
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 Kyostila32e0b542018-02-14 08:55:43 +000024#include "perfetto/trace/trusted_packet.pb.h"
25
26namespace perfetto {
27
28// static
Primiano Tucci16d1d632018-02-22 10:16:22 +000029bool PacketStreamValidator::Validate(const Slices& slices) {
Primiano Tucci16d1d632018-02-22 10:16:22 +000030 SlicedProtobufInputStream stream(&slices);
Sami Kyostila32e0b542018-02-14 08:55:43 +000031 size_t size = 0;
Primiano Tucci16d1d632018-02-22 10:16:22 +000032 for (const Slice& slice : slices)
33 size += slice.size;
Sami Kyostila32e0b542018-02-14 08:55:43 +000034
35 protos::TrustedPacket packet;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010036 if (!packet.ParseFromBoundedZeroCopyStream(&stream, static_cast<int>(size)))
Sami Kyostila32e0b542018-02-14 08:55:43 +000037 return false;
Primiano Tucci07e104d2018-04-03 20:45:35 +020038
Sami Kyostila32e0b542018-02-14 08:55:43 +000039 // Only the service is allowed to fill in the trusted uid.
Primiano Tucci07e104d2018-04-03 20:45:35 +020040 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 Tucci5e33cad2018-04-30 14:41:25 +010049 // Only the service is allowed to fill in the TraceStats.
50 if (packet.has_trace_stats())
51 return false;
52
Primiano Tucci9754d0d2018-09-15 12:41:46 +010053 if (!packet.synchronization_marker().empty())
54 return false;
55
Primiano Tucci07e104d2018-04-03 20:45:35 +020056 // 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 Kyostila32e0b542018-02-14 08:55:43 +000063}
64
65} // namespace perfetto