Clean up fuzz helper classes

- Remove OEMCrypto state changes hidden in constructors and destructors.
- Use composition instead of inheritance to structure classes.
- Avoid calling non-trivial destructors for objects with static
  lifetime.

Merged from https://widevine-internal-review.googlesource.com/168497
Merged from https://widevine-internal-review.googlesource.com/171170
Merged from https://widevine-internal-review.googlesource.com/171171
Merged from https://widevine-internal-review.googlesource.com/171870

Change-Id: I20476a7b1132d11f011b8650ec01e3c2dc3fc0e8
This commit is contained in:
Ian Benz
2023-03-20 17:46:15 +00:00
committed by Robert Shih
parent af070601b0
commit 53fe55cb72
30 changed files with 518 additions and 398 deletions

View File

@@ -1,28 +1,29 @@
// Copyright 2020 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine
// License Agreement.
#ifndef OEMCRYPTO_FUZZ_HELPER_H_
#define OEMCRYPTO_FUZZ_HELPER_H_
#include <vector>
#include "FuzzedDataProvider.h"
#include "OEMCryptoCENC.h"
#include "oec_device_features.h"
#include "oemcrypto_corpus_generator_helper.h"
#include "oemcrypto_session_tests_helper.h"
namespace wvoec {
// Forward-declare the libFuzzer's mutator callback. Mark it weak so that
// the program links successfully even outside of --config=asan-fuzzer
// (apparently the only config in which LLVM uses our custom mutator).
extern "C" size_t LLVMFuzzerMutate(uint8_t* Data, size_t Size, size_t MaxSize)
__attribute__((weak));
const size_t KB = 1024;
namespace wvoec {
constexpr size_t KB = 1024;
// Default maximum length of fuzzing output parameters.
const size_t MAX_FUZZ_OUTPUT_LENGTH = 5 * KB;
constexpr size_t MAX_FUZZ_OUTPUT_LENGTH = 5 * KB;
// Fuzzed data region.
struct FuzzedData {
@@ -33,89 +34,115 @@ struct FuzzedData {
// Initial setup to create a valid OEMCrypto state such as initializing crypto
// firmware/hardware, installing golden key box etc. in order to fuzz
// OEMCrypto APIs.
class InitializeFuzz : public SessionUtil {
void InitializeFuzz(SessionUtil& session_util);
class OEMCryptoLicenseAPIFuzz {
public:
InitializeFuzz() {
wvoec::global_features.Initialize();
OEMCrypto_SetSandbox(kTestSandbox, sizeof(kTestSandbox));
OEMCrypto_Initialize();
OEMCrypto_EnterTestMode();
EnsureTestROT();
}
OEMCryptoLicenseAPIFuzz() : license_messages_(&session_) {}
~InitializeFuzz() { OEMCrypto_Terminate(); }
};
void Initialize();
class OEMCryptoLicenseAPIFuzz : public InitializeFuzz {
public:
OEMCryptoLicenseAPIFuzz() : license_messages_(&session_) {
session_.open();
InstallTestDrmKey(&session_);
session_.GenerateNonce();
}
~OEMCryptoLicenseAPIFuzz() { session_.close(); }
LicenseRoundTrip& license_messages() { return license_messages_; }
Session* session() { return &session_; }
uint32_t session_id() { return session_.session_id(); }
void Terminate();
void LoadLicense();
LicenseRoundTrip& license_messages() { return license_messages_; }
const LicenseRoundTrip& license_messages() const { return license_messages_; }
Session& session() { return session_; }
const Session& session() const { return session_; }
private:
SessionUtil session_util_;
Session session_;
LicenseRoundTrip license_messages_;
};
class OEMCryptoProvisioningAPIFuzz : public InitializeFuzz {
class OEMCryptoProvisioningAPIFuzz {
public:
OEMCryptoProvisioningAPIFuzz()
: provisioning_messages_(&session_, encoded_rsa_key_) {
// Opens a session and Generates Nonce.
provisioning_messages_.PrepareSession(keybox_);
}
: provisioning_messages_(&session_, session_util_.encoded_rsa_key_) {}
~OEMCryptoProvisioningAPIFuzz() { session_.close(); }
void Intialize();
void Terminate();
void LoadProvisioning();
ProvisioningRoundTrip& provisioning_messages() {
return provisioning_messages_;
}
Session* session() { return &session_; }
const ProvisioningRoundTrip& provisioning_messages() const {
return provisioning_messages_;
}
Session& session() { return session_; }
const Session& session() const { return session_; }
private:
SessionUtil session_util_;
Session session_;
ProvisioningRoundTrip provisioning_messages_;
};
// Initial setup to create a valid state such as creating session, installing
// golden key box etc. in order to fuzz Load Renewal API.
class OEMCryptoRenewalAPIFuzz : public OEMCryptoLicenseAPIFuzz {
class OEMCryptoRenewalAPIFuzz {
public:
OEMCryptoRenewalAPIFuzz() : renewal_messages_(&license_messages()) {}
OEMCryptoRenewalAPIFuzz()
: renewal_messages_(&license_api_fuzz_.license_messages()) {}
void Intialize() { license_api_fuzz_.Initialize(); }
void Terminate() { license_api_fuzz_.Terminate(); }
LicenseRoundTrip& license_messages() {
return license_api_fuzz_.license_messages();
}
const LicenseRoundTrip& license_messages() const {
return license_api_fuzz_.license_messages();
}
RenewalRoundTrip& renewal_messages() { return renewal_messages_; }
const RenewalRoundTrip& renewal_messages() const { return renewal_messages_; }
private:
OEMCryptoLicenseAPIFuzz license_api_fuzz_;
RenewalRoundTrip renewal_messages_;
};
class LicenseWithUsageEntryFuzz : public InitializeFuzz {
class LicenseWithUsageEntryFuzz {
public:
LicenseWithUsageEntryFuzz() : license_messages_(&session_) {
license_messages_.set_pst("my_pst");
}
void CreateUsageTableHeader();
LicenseRoundTrip& license_messages() { return license_messages_; }
const vector<uint8_t>& encrypted_usage_header() {
return encrypted_usage_header_;
void Initialize() { InitializeFuzz(session_util_); }
void InstallTestDrmKey(Session* session) {
session_util_.InstallTestDrmKey(session);
}
void CreateUsageTableHeader();
void LoadLicense();
LicenseRoundTrip& license_messages() { return license_messages_; }
const LicenseRoundTrip& license_messages() const { return license_messages_; }
const vector<uint8_t>& encrypted_usage_header() const {
return encrypted_usage_header_;
}
private:
SessionUtil session_util_;
vector<uint8_t> encrypted_usage_header_;
LicenseRoundTrip license_messages_;
Session session_;
@@ -146,6 +173,7 @@ std::vector<FuzzedData> SplitFuzzedData(const uint8_t* data, size_t size);
// called to check status of APIs which are called to setup state for fuzzers.
void CheckStatusAndExitFuzzerOnFailure(OEMCryptoResult result,
OEMCryptoResult expected_status);
} // namespace wvoec
#endif // OEMCRYPTO_FUZZ_HELPER_H_