2026-11-14 host hang: a yams ASan test run (~110 MiB binaries) pushed strawberry.service past MemoryHigh=768M; the kernel throttled the daemon (own memory.pressure full ~72%), so the exec guard stalled launches (589 1-second prevention timeouts in 80 min, even start-swayidle.sh), scans hit p95 67 s and the control socket stopped answering. - Exec guard (assess_exec) no longer copies an image before deciding: a cached scan answers with no read; an image over guard_full_scan_max_bytes, or when the scan-buffer budget is exhausted, is hashed by streaming (1 MiB chunks, new HashUtils::sha256_md5_fd) and decided on hash / IOC / quarantine (degraded; the pipeline still scans it). Only small budgeted images are copied. - Scan-buffer budget (ResourceGovernor::acquire_scan_buffer, RAII ScanBufferLease): in-memory image copies bounded to memory budget / 4 (balanced 192 MiB); the pipeline waits up to 30 s, on-demand scans 60 s; an oversized request is granted only alone, so no deadlock or starvation. - Balanced pipeline_max_image_bytes 128 -> 64 MiB: larger images get hash/IOC checks only (as on the efficient profile). - Response hashing streams instead of copying the whole executable. - Status: resources.cgroup memory_high_mb / memory_high_events / memory_full_pct / throttled, and resources.scan_buffers; doctor check resources.memory (problem when throttled at MemoryHigh, with the fix). Tests: budget grant/refuse/exclusive/wait/timeout/move, PSI full + memory.events parsers, streaming hash = buffer hash across chunks, guard streams a 33 MiB image with no YARA pass and no buffer grant, streams when the budget is exhausted, full-scans + caches when budget is free, doctor throttle check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.1 KiB
C++
121 lines
4.1 KiB
C++
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <filesystem>
|
|
#include "strawberry/hash_utils.hpp"
|
|
#include <iostream>
|
|
#include <cassert>
|
|
#include <fstream>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
void test_sha256_data() {
|
|
const char* test_data = "Hello, World!";
|
|
std::string hash = strawberry::HashUtils::sha256_data(
|
|
reinterpret_cast<const unsigned char*>(test_data),
|
|
strlen(test_data)
|
|
);
|
|
|
|
// Expected SHA256 of "Hello, World!"
|
|
std::string expected = "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f";
|
|
|
|
std::cout << "Test SHA256 data:" << std::endl;
|
|
std::cout << " Input: " << test_data << std::endl;
|
|
std::cout << " Hash: " << hash << std::endl;
|
|
std::cout << " Expected: " << expected << std::endl;
|
|
|
|
assert(hash == expected);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_sha256_file() {
|
|
// Create a temporary test file
|
|
const char* test_file = "/tmp/strawberry_test.txt";
|
|
{
|
|
std::ofstream file(test_file);
|
|
file << "Test content for hashing";
|
|
}
|
|
|
|
std::string hash = strawberry::HashUtils::sha256_file(test_file);
|
|
|
|
std::cout << "Test SHA256 file:" << std::endl;
|
|
std::cout << " File: " << test_file << std::endl;
|
|
std::cout << " Hash: " << hash << std::endl;
|
|
|
|
assert(!hash.empty());
|
|
assert(hash.length() == 64); // SHA256 produces 64 hex characters
|
|
std::cout << " PASSED" << std::endl;
|
|
|
|
// Cleanup
|
|
std::remove(test_file);
|
|
}
|
|
|
|
void test_to_hex() {
|
|
std::vector<unsigned char> data = {0x12, 0x34, 0xAB, 0xCD};
|
|
std::string hex = strawberry::HashUtils::to_hex(data);
|
|
|
|
std::cout << "Test to_hex:" << std::endl;
|
|
std::cout << " Hex: " << hex << std::endl;
|
|
|
|
assert(hex == "1234abcd");
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_sha256_fd_matches_file_hash() {
|
|
std::cout << "Test sha256_fd:" << std::endl;
|
|
const auto path = std::filesystem::temp_directory_path() / "strawberry_sha256_fd_test.bin";
|
|
{
|
|
std::ofstream out(path, std::ios::binary);
|
|
out << "strawberry fd hashing";
|
|
}
|
|
const int fd = ::open(path.c_str(), O_RDONLY);
|
|
assert(fd >= 0);
|
|
const auto from_fd = strawberry::HashUtils::sha256_fd(fd);
|
|
// Reading must not depend on (or move) the descriptor's offset.
|
|
assert(strawberry::HashUtils::sha256_fd(fd) == from_fd);
|
|
::close(fd);
|
|
assert(from_fd == strawberry::HashUtils::sha256_file(path.string()));
|
|
assert(strawberry::HashUtils::sha256_fd(-1).empty());
|
|
std::filesystem::remove(path);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_sha256_md5_fd_streams() {
|
|
std::cout << "Test sha256_md5_fd (streaming, multi-chunk):" << std::endl;
|
|
std::string data(3 * 1024 * 1024 + 123, '\0');
|
|
for (std::size_t i = 0; i < data.size(); ++i) data[i] = static_cast<char>((i * 131) & 0xff);
|
|
const std::string path = (std::filesystem::temp_directory_path() / "sb_hash_stream.bin").string();
|
|
{
|
|
std::ofstream f(path, std::ios::binary);
|
|
f.write(data.data(), static_cast<std::streamsize>(data.size()));
|
|
}
|
|
const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
|
assert(fd >= 0);
|
|
std::string sha, md5;
|
|
assert(strawberry::HashUtils::sha256_md5_fd(fd, sha, md5));
|
|
::close(fd);
|
|
const auto* bytes = reinterpret_cast<const unsigned char*>(data.data());
|
|
assert(sha == strawberry::HashUtils::sha256_data(bytes, data.size()));
|
|
assert(md5 == strawberry::HashUtils::md5_data(bytes, data.size()));
|
|
std::string a, b;
|
|
assert(!strawberry::HashUtils::sha256_md5_fd(-1, a, b) && a.empty() && b.empty());
|
|
std::filesystem::remove(path);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
int main() {
|
|
std::cout << "Running HashUtils tests..." << std::endl << std::endl;
|
|
|
|
try {
|
|
test_sha256_data();
|
|
test_sha256_file();
|
|
test_to_hex();
|
|
test_sha256_fd_matches_file_hash();
|
|
test_sha256_md5_fd_streams();
|
|
|
|
std::cout << std::endl << "All tests passed!" << std::endl;
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "Test failed with exception: " << e.what() << std::endl;
|
|
return 1;
|
|
}
|
|
}
|