Home | History | Annotate | Download | only in core
      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"
     24 #include "perfetto/trace/trusted_packet.pb.h"
     25 
     26 namespace perfetto {
     27 
     28 // static
     29 bool PacketStreamValidator::Validate(const Slices& slices) {
     30   SlicedProtobufInputStream stream(&slices);
     31   size_t size = 0;
     32   for (const Slice& slice : slices)
     33     size += slice.size;
     34 
     35   protos::TrustedPacket packet;
     36   if (!packet.ParseFromBoundedZeroCopyStream(&stream, static_cast<int>(size)))
     37     return false;
     38 
     39   // Only the service is allowed to fill in the trusted uid.
     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 
     49   // Only the service is allowed to fill in the TraceStats.
     50   if (packet.has_trace_stats())
     51     return false;
     52 
     53   // We are deliberately not checking for clock_snapshot for the moment. It's
     54   // unclear if we want to allow producers to snapshot their clocks. Ideally we
     55   // want a security model where producers can only snapshot their own clocks
     56   // and not system ones. However, right now, there isn't a compelling need to
     57   // be so prescriptive.
     58 
     59   return true;
     60 }
     61 
     62 }  // namespace perfetto
     63