Skip to content

Commit ce791aa

Browse files
authored
Fix logging issue for transitive rules added from CLONE events (#827)
Fixes a (unreleased) crash when writing telemetry related to transitive rules generated by CLONE events. Introduced by: #762 Part of SNT-289
1 parent 4e562e7 commit ce791aa

File tree

17 files changed

+49
-20
lines changed

17 files changed

+49
-20
lines changed

Source/santad/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ objc_library(
151151
"//Source/common:SNTFileInfo",
152152
"//Source/common:SNTLogging",
153153
"//Source/common:SNTRule",
154+
"//Source/common:String",
154155
],
155156
)
156157

Source/santad/Logs/EndpointSecurity/Logger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class Logger : public Timer<Logger> {
7979

8080
virtual void Log(std::unique_ptr<santa::EnrichedMessage> msg);
8181

82-
void LogAllowlist(const santa::Message &msg, const std::string_view hash);
82+
void LogAllowlist(const santa::Message &msg, const std::string_view hash,
83+
const std::string_view target_path);
8384

8485
void LogBundleHashingEvents(NSArray<SNTStoredExecutionEvent *> *events);
8586

Source/santad/Logs/EndpointSecurity/Logger.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,10 @@
313313
}
314314
}
315315

316-
void Logger::LogAllowlist(const Message &msg, const std::string_view hash) {
316+
void Logger::LogAllowlist(const Message &msg, const std::string_view hash,
317+
const std::string_view target_path) {
317318
if (ShouldLog(TelemetryEvent::kAllowlist)) {
318-
writer_->Write(serializer_->SerializeAllowlist(msg, hash));
319+
writer_->Write(serializer_->SerializeAllowlist(msg, hash, target_path));
319320
}
320321
}
321322

Source/santad/Logs/EndpointSecurity/LoggerTest.mm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
public:
9797
MOCK_METHOD(std::vector<uint8_t>, SerializeMessage, (const EnrichedClose &msg));
9898

99-
MOCK_METHOD(std::vector<uint8_t>, SerializeAllowlist, (const Message &, const std::string_view));
99+
MOCK_METHOD(std::vector<uint8_t>, SerializeAllowlist,
100+
(const Message &, const std::string_view, const std::string_view));
100101

101102
MOCK_METHOD(std::vector<uint8_t>, SerializeBundleHashingEvent, (SNTStoredExecutionEvent *));
102103
MOCK_METHOD(std::vector<uint8_t>, SerializeDiskAppeared, (NSDictionary *, bool));
@@ -287,13 +288,14 @@ - (void)testLogAllowList {
287288
auto mockWriter = std::make_shared<MockWriter>();
288289
es_message_t msg;
289290
std::string_view hash = "this_is_my_test_hash";
291+
std::string_view target_path = "this_is_my_target_path";
290292

291293
mockESApi->SetExpectationsRetainReleaseMessage();
292-
EXPECT_CALL(*mockSerializer, SerializeAllowlist(testing::_, hash));
294+
EXPECT_CALL(*mockSerializer, SerializeAllowlist(testing::_, hash, target_path));
293295
EXPECT_CALL(*mockWriter, Write);
294296

295297
Logger(nil, nil, TelemetryEvent::kEverything, 1, 1, 1, mockSerializer, mockWriter)
296-
.LogAllowlist(Message(mockESApi, &msg), hash);
298+
.LogAllowlist(Message(mockESApi, &msg), hash, target_path);
297299

298300
XCTBubbleMockVerifyAndClearExpectations(mockESApi.get());
299301
XCTBubbleMockVerifyAndClearExpectations(mockSerializer.get());

Source/santad/Logs/EndpointSecurity/Serializers/BasicString.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class BasicString : public Serializer {
8484
std::optional<santa::EnrichedFile> enriched_event_target, FileAccessPolicyDecision decision,
8585
std::string_view operation_id) override;
8686

87-
std::vector<uint8_t> SerializeAllowlist(const santa::Message &, const std::string_view) override;
87+
std::vector<uint8_t> SerializeAllowlist(const santa::Message &, const std::string_view,
88+
const std::string_view) override;
8889

8990
std::vector<uint8_t> SerializeBundleHashingEvent(SNTStoredExecutionEvent *) override;
9091

Source/santad/Logs/EndpointSecurity/Serializers/BasicString.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,15 +1111,16 @@ static inline void AppendStringToken(std::string &str, std::string key, es_strin
11111111
}
11121112

11131113
std::vector<uint8_t> BasicString::SerializeAllowlist(const Message &msg,
1114-
const std::string_view hash) {
1114+
const std::string_view hash,
1115+
const std::string_view target_path) {
11151116
std::string str = CreateDefaultString();
11161117

11171118
str.append("action=ALLOWLIST|pid=");
11181119
str.append(std::to_string(Pid(msg->process->audit_token)));
11191120
str.append("|pidversion=");
11201121
str.append(std::to_string(Pidversion(msg->process->audit_token)));
11211122
str.append("|path=");
1122-
str.append(FilePath(santa::GetAllowListTargetFile(msg)).Sanitized());
1123+
str.append(SanitizableString(target_path.data(), target_path.size()).Sanitized());
11231124
str.append("|sha256=");
11241125
str.append(hash);
11251126

Source/santad/Logs/EndpointSecurity/Serializers/BasicStringTest.mm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,14 +1295,15 @@ - (void)testSerializeAllowlist {
12951295
auto mockESApi = std::make_shared<MockEndpointSecurityAPI>();
12961296
mockESApi->SetExpectationsRetainReleaseMessage();
12971297

1298-
std::vector<uint8_t> ret = BasicString::Create(mockESApi, nil, false)
1299-
->SerializeAllowlist(Message(mockESApi, &esMsg), "test_hash");
1298+
std::vector<uint8_t> ret =
1299+
BasicString::Create(mockESApi, nil, false)
1300+
->SerializeAllowlist(Message(mockESApi, &esMsg), "test_hash", "target_path");
13001301

13011302
XCTAssertTrue(testing::Mock::VerifyAndClearExpectations(mockESApi.get()),
13021303
"Expected calls were not properly mocked");
13031304

13041305
std::string got(ret.begin(), ret.end());
1305-
std::string want = "action=ALLOWLIST|pid=12|pidversion=34|path=foo"
1306+
std::string want = "action=ALLOWLIST|pid=12|pidversion=34|path=target_path"
13061307
"|sha256=test_hash|machineid=my_id\n";
13071308

13081309
XCTAssertCppStringEqual(got, want);

Source/santad/Logs/EndpointSecurity/Serializers/Empty.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class Empty : public Serializer {
7676
std::optional<santa::EnrichedFile> enriched_event_target, FileAccessPolicyDecision decision,
7777
std::string_view operation_id) override;
7878

79-
std::vector<uint8_t> SerializeAllowlist(const santa::Message &, const std::string_view) override;
79+
std::vector<uint8_t> SerializeAllowlist(const santa::Message &, const std::string_view,
80+
const std::string_view) override;
8081

8182
std::vector<uint8_t> SerializeBundleHashingEvent(SNTStoredExecutionEvent *) override;
8283

Source/santad/Logs/EndpointSecurity/Serializers/Empty.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@
164164
return {};
165165
}
166166

167-
std::vector<uint8_t> Empty::SerializeAllowlist(const Message &msg, const std::string_view hash) {
167+
std::vector<uint8_t> Empty::SerializeAllowlist(const Message &msg, const std::string_view hash,
168+
const std::string_view target_path) {
168169
return {};
169170
}
170171

Source/santad/Logs/EndpointSecurity/Serializers/EmptyTest.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ - (void)testAllSerializersReturnEmptyVector {
4343
XCTAssertEqual(e->SerializeMessage(*(santa::EnrichedUnlink *)&fake).size(), 0);
4444
XCTAssertEqual(e->SerializeMessage(*(santa::EnrichedCSInvalidated *)&fake).size(), 0);
4545

46-
XCTAssertEqual(e->SerializeAllowlist(*(santa::Message *)&fake, "").size(), 0);
46+
XCTAssertEqual(e->SerializeAllowlist(*(santa::Message *)&fake, "", "").size(), 0);
4747
XCTAssertEqual(e->SerializeBundleHashingEvent(nil).size(), 0);
4848
XCTAssertEqual(e->SerializeDiskAppeared(nil, true).size(), 0);
4949
XCTAssertEqual(e->SerializeDiskDisappeared(nil).size(), 0);

0 commit comments

Comments
 (0)