Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7e948bb7 authored by Hui Peng's avatar Hui Peng Committed by Android (Google) Code Review
Browse files

Merge changes I1709af94,I7dd3a0e6,If9f14d5f

* changes:
  Add regression test for b/254774758
  Add mocking support for now function in AttributionProcessor
  Fix a use-after-free bug in AttributionProcessor::OnWakelockReleased
parents 907d720e bdcf174f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -399,6 +399,7 @@ cc_test {
        ":BluetoothShimTestSources",
        ":BluetoothSecurityUnitTestSources",
        ":BluetoothStorageUnitTestSources",
        ":BluetoothBtaaSources_linux_generic_tests",
    ],
    generated_headers: [
        "BluetoothGeneratedBundlerSchema_h_bfbs",
+7 −0
Original line number Diff line number Diff line
@@ -30,3 +30,10 @@ filegroup {
        "linux_generic/wakelock_processor.cc",
    ],
}

filegroup {
    name: "BluetoothBtaaSources_linux_generic_tests",
    srcs: [
        "linux_generic/attribution_processor_tests.cc",
    ],
}
+13 −0
Original line number Diff line number Diff line
@@ -82,7 +82,20 @@ class AttributionProcessor {
  void Dump(
      std::promise<flatbuffers::Offset<ActivityAttributionData>> promise, flatbuffers::FlatBufferBuilder* fb_builder);

  using ClockType = std::chrono::time_point<std::chrono::system_clock>;
  using NowFunc = ClockType (*)();

  // by default, we use the std::chrono::system_clock::now implementation to
  // get the current timestamp
  AttributionProcessor() : now_func_(std::chrono::system_clock::now) {}
  // in other cases, we may need to use different implementation
  // e.g., for testing purposes
  AttributionProcessor(NowFunc func) : now_func_(func) {}

 private:
  // this function is added for testing support in
  // OnWakelockReleased
  NowFunc now_func_ = std::chrono::system_clock::now;
  bool wakeup_ = false;
  std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> btaa_aggregator_;
  std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> wakelock_duration_aggregator_;
+15 −9
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ void AttributionProcessor::OnWakelockReleased(uint32_t duration_ms) {
    return;
  }

  auto cur_time = std::chrono::system_clock::now();
  auto cur_time = now_func_();
  for (auto& it : wakelock_duration_aggregator_) {
    it.second.wakelock_duration_ms = (uint64_t)duration_ms * it.second.byte_count / total_byte_count;
    if (btaa_aggregator_.find(it.first) == btaa_aggregator_.end()) {
@@ -126,23 +126,29 @@ void AttributionProcessor::OnWakelockReleased(uint32_t duration_ms) {
  }
  // Trim down the transient entries in the aggregator to avoid that it overgrows
  if (btaa_aggregator_.size() > kMapSizeTrimDownAggregationEntry) {
    for (auto& it : btaa_aggregator_) {
    auto it = btaa_aggregator_.begin();
    while (it != btaa_aggregator_.end()) {
      auto elapsed_time_sec =
          std::chrono::duration_cast<std::chrono::seconds>(cur_time - it.second.creation_time).count();
          std::chrono::duration_cast<std::chrono::seconds>(cur_time - it->second.creation_time).count();
      if (elapsed_time_sec > kDurationTransientDeviceActivityEntrySecs &&
          it.second.byte_count < kByteCountTransientDeviceActivityEntry) {
        btaa_aggregator_.erase(it.first);
          it->second.byte_count < kByteCountTransientDeviceActivityEntry) {
        it = btaa_aggregator_.erase(it);
      } else {
        it++;
      }
    }
  }

  if (app_activity_aggregator_.size() > kMapSizeTrimDownAggregationEntry) {
    for (auto& it : app_activity_aggregator_) {
    auto it = app_activity_aggregator_.begin();
    while (it != app_activity_aggregator_.end()) {
      auto elapsed_time_sec =
          std::chrono::duration_cast<std::chrono::seconds>(cur_time - it.second.creation_time).count();
          std::chrono::duration_cast<std::chrono::seconds>(cur_time - it->second.creation_time).count();
      if (elapsed_time_sec > kDurationTransientDeviceActivityEntrySecs &&
          it.second.byte_count < kByteCountTransientDeviceActivityEntry) {
        app_activity_aggregator_.erase(it.first);
          it->second.byte_count < kByteCountTransientDeviceActivityEntry) {
        it = app_activity_aggregator_.erase(it);
      } else {
        it++;
      }
    }
  }
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <base/strings/stringprintf.h>
#include <gtest/gtest.h>
#include <unistd.h>

#include <chrono>
#include <functional>
#include <memory>
#include <vector>

#include "btaa/activity_attribution.h"
#include "btaa/attribution_processor.h"

using bluetooth::hci::Address;
using namespace bluetooth::activity_attribution;
using namespace std::chrono;

// mock for std::chrono::system_clock::now
static AttributionProcessor::ClockType now_ret_val;
static AttributionProcessor::ClockType fake_now() {
  return now_ret_val;
}

class AttributionProcessorTest : public ::testing::Test {
 protected:
  void SetUp() override {
    pAttProc = std::make_unique<AttributionProcessor>(fake_now);
  }
  void TearDown() override {
    pAttProc.reset();
  }

  std::unique_ptr<AttributionProcessor> pAttProc;
};

static void fake_now_set_current() {
  now_ret_val = system_clock::now();
}

static void fake_now_advance_1000sec() {
  now_ret_val += seconds(1000s);
}

TEST_F(AttributionProcessorTest, UAFInOnWakelockReleasedRegressionTest) {
  std::vector<BtaaHciPacket> btaaPackets;
  Address addr;

  fake_now_set_current();

  // setup the condition 1 for triggering erase operation
  // add 220 entries in app_activity_aggregator_
  // and btaa_aggregator_
  for (int i = 0; i < 220; i++) {
    std::string addrStr = base::StringPrintf("21:43:65:87:a9:%02x", i + 10);
    ASSERT_TRUE(Address::FromString(addrStr, addr));
    BtaaHciPacket packet(Activity::ACL, addr, 30 * i);
    btaaPackets.push_back(packet);
    pAttProc->NotifyActivityAttributionInfo(i + 1000, "com.test.app" + std::to_string(i), addrStr);
  }

  pAttProc->OnBtaaPackets(btaaPackets);
  pAttProc->OnWakelockReleased(100);

  // setup the condition 2 for triggering erase operation
  // make elapsed_time_sec > 900s
  fake_now_advance_1000sec();

  pAttProc->OnBtaaPackets(btaaPackets);
  pAttProc->OnWakelockReleased(100);
}