// YARA rule partitioning: the splitter, the condition facts and the file classification all read // rule text and file bytes the host did not write (feeds fetched by strawberry-update, operator // rules, scanned files), so the invariants the partitioning relies on must hold for arbitrary bytes. #include "strawberry/rule_partition.hpp" #include #include #include #include #include namespace { using namespace strawberry::rules; bool is_identifier(const std::string& s) { if (s.empty()) return false; for (std::size_t i = 0; i < s.size(); ++i) { const unsigned char c = static_cast(s[i]); const bool ok = i == 0 ? (std::isalpha(c) != 0 || c == '_') : (std::isalnum(c) != 0 || c == '_'); if (!ok) return false; } return true; } // A successful split is a partition of the source into an ordered, non-overlapping list of rules: // nothing dropped, nothing counted twice, each rule ending at its closing brace and its condition // being the tail of its own text. void check_split(std::string_view source) { const auto r = split_rules(source); if (r.ok && !r.error.empty()) std::abort(); if (!r.ok && r.error.empty()) std::abort(); std::size_t covered = r.preamble.size(); std::size_t previous_end = 0; for (const auto& rule : r.rules) { if (rule.begin >= rule.end || rule.end > source.size() || rule.begin < previous_end) std::abort(); if (source[rule.end - 1] != '}') std::abort(); previous_end = rule.end; if (!is_identifier(rule.name)) std::abort(); const std::string_view text = source.substr(rule.begin, rule.end - rule.begin); if (text.size() < rule.condition.size() + 1) std::abort(); if (text.compare(text.size() - rule.condition.size() - 1, rule.condition.size() + 1, rule.condition + "}") != 0) { std::abort(); } covered += rule.end - rule.begin; } if (r.ok && covered != source.size()) std::abort(); } // Condition facts may only narrow: a class requirement excludes exactly the other class group and a // filesize bound excludes every bucket whose low end is at or above it. Anything else keeps the rule. void check_condition(std::string_view condition) { for (const auto& id : condition_identifiers(condition)) { if (!is_identifier(id)) std::abort(); } const auto facts = condition_facts(condition); for (std::size_t i = 0; i < kPartitions; ++i) { const auto key = PartitionKey::from_index(i); if (key.index() != i) std::abort(); const bool excluded_here = excluded(facts, key); if (facts.required_class) { const bool needs_pe = *facts.required_class == FileClass::pe; const bool other_group = (key.cls == ClassGroup::pe) != needs_pe; if (other_group && !excluded_here) std::abort(); } if (facts.filesize_lt && *facts.filesize_lt <= kBucketLow[key.bucket] && !excluded_here) std::abort(); } } // The file's partition must agree with its magic and with its size: the bucket low end never exceeds // the file size and the next bucket's low end is never reached. void check_partition(const uint8_t* data, std::size_t size) { const std::size_t head_len = size < 8 ? size : 8; const auto cls = file_class(data, head_len); if (size >= 2 && data[0] == 'M' && data[1] == 'Z' && cls != FileClass::pe) std::abort(); if (size >= 4 && data[0] == 0x7f && data[1] == 'E' && data[2] == 'L' && data[3] == 'F' && cls != FileClass::elf) { std::abort(); } uint64_t file_size = size; for (std::size_t i = 0; i < head_len; ++i) file_size = (file_size << 8) | data[i]; const auto key = partition_for(data, head_len, file_size); if (key.index() >= kPartitions) std::abort(); if ((cls == FileClass::pe) != (key.cls == ClassGroup::pe)) std::abort(); if (kBucketLow[key.bucket] > file_size) std::abort(); if (key.bucket + 1 < kSizeBuckets && file_size >= kBucketLow[key.bucket + 1]) std::abort(); } } // namespace extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { if (data == nullptr || size == 0) return 0; const std::string_view source(reinterpret_cast(data), size); check_split(source); // A truncated download looks like a suffix of a good file: a rule cut mid-body must fail cleanly. if (size > 2) check_split(source.substr(size / 2)); check_condition(source); check_partition(data, size); return 0; }