Skip to content

Commit fcb4970

Browse files
mlwrussellhancoxpmarkowsky
authored
ES and Logging Interfaces Redesign (#888)
* Initial structure for ES wrappers, enriched types, logging * Basic working ES and logging functionality * Add in oneTBB and thread-safe-lru deps * Added a bunch of enriched types * Auto-mute self when establishing ES client * Basic auth, tamper client. Syslog of all events. Basic compiler tracking. * Update copyright header blobs, convert some tabs to spaces * Auth result cache. Fix getting translocation path. * Added remaining cache methods * Add AuthResultCache to Recorder client. Cache now operates on es_file_t. * Hooked up SNTPrefixTree * Fix CompilerController for RENAME. Fix AllowList logging missing path. * Block loading Santa kext * Added device manager client * Properly log DiskAppear events * Fix build to adopt new adhoc build * Handle clearing cache on UNMOUNT events * Ignore other ES clients if configured * Remove SNTAllowlistInfo. Rename AllowList to Allowlist. Minor cleanup. * Recorder now logs asynchronously. Enricher now returns shared_ptrs. * Added File writer. Added timestamps to BasicStream serializer. * Skip calling stat in SNTFileInfo when path given by ES. * Fix build issue * Address draft PR feedback * santactl integrated, XPC works, fix file writer bug * Integrate syncservice. Start observing some config changes. * Add metrics service wrapper * Add metrics config observers and metrics interval reset. * Start better dependency control. Add Null logger support. * Added more deps * Added more deps * Fix issue where metric service wasn't starting * Add missing variant include * Fix missing parent proc name * Added googletest and new unit test macro * Started expanding AuthResultCacheTest * Properly mock EndpointSecurityAPI * Finished AuthResultCacheTest * bazelrc now builds all C++ as C++17. Added LoggerTest. * Add FileTest. Abstract some File constants to Logger. * Added Empty serializer test * Started work on BasicStringTest. Fixed some BasicString serialization bugs. * Added Unlink BasicString serialization test * Added some more tests. Commonized some test code * Finished BasicStringTest. Converted to XCTest. * Standardize esapi variable naming * Bubble up gTest expect failures to XCTest failures * AuthResultCacheTest now uses XCTest. Added common TestUtils.h * EmptyTest now uses XCTest. * FileTest now uses XCTest * LoggerTest now uses XCTest. Removed santa_unit_gtest bazel macro. * Added ClientTest * Add basic Enricher tests * Add MessageTest. Make more TestUtils. * Rename metrics to Metrics * Add MetricsTest. * Apply template pattern to Serializer * Add SNTDecisionCacheTest. * Add SNTCachedDecisionTest. * Testing with coveralls debug mode * Allow manual CI runs * Remove unused property * Started work on SNTEndpointSecurityClientTest. * WIP SNTEndpointSecurityClientTest, fix test run issue * Added more base ES client tests * Add more base ES client tests * Base ES client tests done. Added serializer utils/tests. Expanded basic string tests. * Add utils test to test suite * Add copy ctor. Add test output to bazel coverage. * Single thread bazel coverage * Updaload coverage file * Updaload coverage file * Old gen cov test * Restructure message handlers to enable better testability * Added enable tests for all ES clients * Made a single MockEndpointSecurityAPI class to share everywhere * Added most of SNTCompilerControllerTest * Cleanup SNTCompilerControllerTest * Started expanding Auth client test * Finished up the Authorizer tests * Move to using enum class for notify/auth instead of bool * WIP for tamper resistance test. ASAN issues. * Add OCMock patch to fix test issue on ARM Macs * Changed patches directory name to external_patches * Update WORKSPACE path * Finished up Tamper Resistance tests * Finished up Recorder tests. * Move SNTExecutionControllerTest to ObjC++ * Initial work to port SNTExecutionControllerTest * Finished porting SNTExecutionControllerTest. * Added SNTExecutionControllerTest to list of unit tests * Ported SNTEndpointSecurityDeviceManager. * Test cleanup, use MockESAPI expectation helpers * Verify SNTEndpointSecurityDeviceManager expectations differently * Test cleanup, omit gTest param list where unused * Log message cleanup * Rename SNTApplicationTest to santad_test.mm * Finished porting santad_test, formerly SNTApplicationTest * Fix SNTEndpointSecurityDeviceManager issues * Pulled in missed fixes. Updated tests. * Renamed lowercase filenames to match rest of codebase * Fix non-static dispatch_once_t, and noisy watching compiler log message * WIP Started process of removing components no longer used * WIP Continued process of removing components no longer used * BUILD file cleanup. Proto warning. Removed unused global * Rename SNTEventProvider to SNTEndpointSecurityEventHandler * Rename SNTEndpointSecurityEventHandler protocol * Remove EnableSysxCache option. Remove --quick flag used during dev. * Ran testing/fix.sh * Addmissing param to fix.sh that was omitting .mm files. * clang-format * Fix linter: find cmd missing .mm ext, git grep exclude patch files. * Use MakeESProcess default params in tests * Move variables to camelCase in objc classes * More case changes * Sanitize strings * Change dispatch queue priorities and standardize daemon queue naming * Exclude patch files in markdown check * Ensure string log messages end with newline * Fix BasicStringTest * Disable clang-format in code producing different results in local/remote versions * Moved to using date ranges in copyright notices as per current guidelines * Update Source/common/SNTConfigurator.h Suggestion adding whitespace in comment to fix clang-format mangling Co-authored-by: Russell Hancox <[email protected]> * Removed santa_panic macro used in one place * Updated comment about ES cachability * Pin oneTBB to specific commit * Address outstanding WORKSPACE 'canonical reproducible form' messages * Use string append instead of ostringstream due to benchmark results * Remove use of freind classes in EnrichedTypes.h * Added SNTKVOManager, removed observers from SNTConfigurator. * Fixed SNTEndpointSecurityRecorderTest class name * Reduce usage of the auto keyword * Each SNTKVOManager instance now adds its own observer * Replaced more auto keywords with real types. * Remove leftover code coverage debugging from ci.yml * Updated comment * Memoize SNTFileInfo sha256. Reduce some cache sizes. * Fix issue checking for translocated paths * Use more performant NSURL creation method * Fix lint issue * Address PR feedback * Use an array literal for kvo objects * Fix some clang tidy and import issues * Replace third party LRU cache with SantaCache for now * Fix clang tidy issues * Address PR feedback * Fix comment typo Co-authored-by: Pete Markowsky <[email protected]> * Added todo for when we adopt macOS 13 Co-authored-by: Russell Hancox <[email protected]> Co-authored-by: Pete Markowsky <[email protected]>
1 parent c9ef723 commit fcb4970

File tree

145 files changed

+9999
-6184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+9999
-6184
lines changed

.bazelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,14 @@ build --apple_generate_dsym --define=apple.propagate_embedded_extra_outputs=yes
33
build --copt=-Werror
44
build --copt=-Wall
55
build --copt=-Wno-error=deprecated-declarations
6+
build --per_file_copt=.*\.mm\$@-std=c++17
7+
build --cxxopt=-std=c++17
8+
9+
build:asan --strip=never
10+
build:asan --copt="-Wno-macro-redefined"
11+
build:asan --copt="-D_FORTIFY_SOURCE=0"
12+
build:asan --copt="-O1"
13+
build:asan --copt="-fno-omit-frame-pointer"
14+
build:asan --copt="-fsanitize=address"
15+
build:asan --copt="-DADDRESS_SANITIZER"
16+
build:asan --linkopt="-fsanitize=address"

.github/workflows/check-markdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@master
1313
- uses: gaurav-nelson/github-action-markdown-link-check@v1
14-
- run: "! git grep -EIn $'[ \t]+$'"
14+
- run: "! git grep -EIn $'[ \t]+$' -- ':(exclude)*.patch'"

.github/workflows/ci.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ jobs:
2020
- name: Run linters
2121
run: ./Testing/lint.sh
2222

23-
2423
build_userspace:
2524
strategy:
2625
fail-fast: false
@@ -55,10 +54,3 @@ jobs:
5554
github-token: ${{ secrets.GITHUB_TOKEN }}
5655
path-to-lcov: ./bazel-out/_coverage/_coverage_report.dat
5756
flag-name: Unit
58-
59-
benchmark:
60-
runs-on: macos-11
61-
steps:
62-
- uses: actions/checkout@v2
63-
- name: Run All Tests
64-
run: ./Testing/benchmark.sh

BUILD

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,3 @@ test_suite(
198198
"//Source/santasyncservice:unit_tests",
199199
],
200200
)
201-
202-
test_suite(
203-
name = "benchmarks",
204-
tests = [
205-
"//Source/santad:SNTApplicationBenchmark",
206-
],
207-
)

Source/common/BUILD

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ objc_library(
8383
],
8484
)
8585

86-
objc_library(
87-
name = "SNTAllowlistInfo",
88-
srcs = ["SNTAllowlistInfo.m"],
89-
hdrs = ["SNTAllowlistInfo.h"],
90-
)
91-
9286
objc_library(
9387
name = "SNTCommonEnums",
9488
hdrs = ["SNTCommonEnums.h"],
@@ -106,6 +100,23 @@ objc_library(
106100
],
107101
)
108102

103+
objc_library(
104+
name = "SNTKVOManager",
105+
srcs = ["SNTKVOManager.mm"],
106+
hdrs = ["SNTKVOManager.h"],
107+
deps = [
108+
":SNTLogging",
109+
],
110+
)
111+
112+
santa_unit_test(
113+
name = "SNTKVOManagerTest",
114+
srcs = ["SNTKVOManagerTest.mm"],
115+
deps = [
116+
":SNTKVOManager",
117+
],
118+
)
119+
109120
objc_library(
110121
name = "SNTDropRootPrivs",
111122
srcs = ["SNTDropRootPrivs.m"],
@@ -117,6 +128,7 @@ objc_library(
117128
srcs = ["SNTFileInfo.m"],
118129
hdrs = ["SNTFileInfo.h"],
119130
deps = [
131+
":SNTLogging",
120132
"@FMDB",
121133
"@MOLCodesignChecker",
122134
],
@@ -298,13 +310,40 @@ santa_unit_test(
298310
deps = [":SNTMetricSet"],
299311
)
300312

313+
santa_unit_test(
314+
name = "SNTCachedDecisionTest",
315+
srcs = ["SNTCachedDecisionTest.mm"],
316+
deps = [
317+
"//Source/common:SNTCachedDecision",
318+
"//Source/common:TestUtils",
319+
"@OCMock",
320+
],
321+
)
322+
301323
test_suite(
302324
name = "unit_tests",
303325
tests = [
326+
":SNTCachedDecisionTest",
304327
":SNTFileInfoTest",
328+
":SNTKVOManagerTest",
305329
":SNTMetricSetTest",
306330
":SNTPrefixTreeTest",
331+
":SNTRuleTest",
307332
":SantaCacheTest",
308333
],
309334
visibility = ["//:santa_package_group"],
310335
)
336+
337+
objc_library(
338+
name = "TestUtils",
339+
testonly = 1,
340+
srcs = ["TestUtils.mm"],
341+
hdrs = ["TestUtils.h"],
342+
sdk_dylibs = [
343+
"bsm",
344+
],
345+
deps = [
346+
"@OCMock",
347+
"@com_google_googletest//:gtest",
348+
],
349+
)

Source/common/SNTAllowlistInfo.m

Lines changed: 0 additions & 32 deletions
This file was deleted.

Source/common/SNTCachedDecision.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Copyright 2015 Google Inc. All rights reserved.
1+
/// Copyright 2015-2022 Google Inc. All rights reserved.
22
///
33
/// Licensed under the Apache License, Version 2.0 (the "License");
44
/// you may not use this file except in compliance with the License.
@@ -12,10 +12,11 @@
1212
/// See the License for the specific language governing permissions and
1313
/// limitations under the License.
1414

15+
#import <EndpointSecurity/EndpointSecurity.h>
1516
#import <Foundation/Foundation.h>
1617

17-
#import "Source/common/SNTCommonEnums.h"
1818
#import "Source/common/SNTCommon.h"
19+
#import "Source/common/SNTCommonEnums.h"
1920

2021
@class MOLCertificate;
2122

@@ -24,6 +25,8 @@
2425
///
2526
@interface SNTCachedDecision : NSObject
2627

28+
- (instancetype)initWithEndpointSecurityFile:(const es_file_t *)esFile;
29+
2730
@property santa_vnode_id_t vnodeId;
2831
@property SNTEventState decision;
2932
@property NSString *decisionExtra;

Source/common/SNTCachedDecision.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Copyright 2015 Google Inc. All rights reserved.
1+
/// Copyright 2015-2022 Google Inc. All rights reserved.
22
///
33
/// Licensed under the Apache License, Version 2.0 (the "License");
44
/// you may not use this file except in compliance with the License.
@@ -15,4 +15,14 @@
1515
#import "Source/common/SNTCachedDecision.h"
1616

1717
@implementation SNTCachedDecision
18+
19+
- (instancetype)initWithEndpointSecurityFile:(const es_file_t *)esFile {
20+
self = [super init];
21+
if (self) {
22+
_vnodeId.fsid = (uint64_t)esFile->stat.st_dev;
23+
_vnodeId.fileid = esFile->stat.st_ino;
24+
}
25+
return self;
26+
}
27+
1828
@end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// Copyright 2022 Google Inc. All rights reserved.
2+
///
3+
/// Licensed under the Apache License, Version 2.0 (the "License");
4+
/// you may not use this file except in compliance with the License.
5+
/// You may obtain a copy of the License at
6+
///
7+
/// http://www.apache.org/licenses/LICENSE-2.0
8+
///
9+
/// Unless required by applicable law or agreed to in writing, software
10+
/// distributed under the License is distributed on an "AS IS" BASIS,
11+
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
/// See the License for the specific language governing permissions and
13+
/// limitations under the License.
14+
15+
#import <XCTest/XCTest.h>
16+
17+
#import "Source/common/SNTCachedDecision.h"
18+
#include "Source/common/TestUtils.h"
19+
20+
@interface SNTCachedDecisionTest : XCTestCase
21+
@end
22+
23+
@implementation SNTCachedDecisionTest
24+
25+
- (void)testSNTCachedDecisionInit {
26+
// Ensure the vnodeId field is properly set from the es_file_t
27+
struct stat sb = MakeStat(1234, 5678);
28+
es_file_t file = MakeESFile("foo", sb);
29+
30+
SNTCachedDecision *cd = [[SNTCachedDecision alloc] initWithEndpointSecurityFile:&file];
31+
32+
XCTAssertEqual(sb.st_ino, cd.vnodeId.fileid);
33+
XCTAssertEqual(sb.st_dev, cd.vnodeId.fsid);
34+
}
35+
36+
@end

Source/common/SNTCommon.h

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Copyright 2015 Google Inc. All rights reserved.
1+
/// Copyright 2015-2022 Google Inc. All rights reserved.
22
///
33
/// Licensed under the Apache License, Version 2.0 (the "License");
44
/// you may not use this file except in compliance with the License.
@@ -27,41 +27,23 @@
2727
#define unlikely(x) __builtin_expect(!!(x), 0)
2828

2929
typedef enum {
30-
ACTION_UNSET = 0,
30+
ACTION_UNSET,
3131

3232
// REQUESTS
33-
ACTION_REQUEST_SHUTDOWN = 10,
34-
ACTION_REQUEST_BINARY = 11,
33+
// If an operation is awaiting a cache decision from a similar operation
34+
// currently being processed, it will poll about every 5 ms for an answer.
35+
ACTION_REQUEST_BINARY,
3536

3637
// RESPONSES
37-
ACTION_RESPOND_ALLOW = 20,
38-
ACTION_RESPOND_DENY = 21,
39-
ACTION_RESPOND_TOOLONG = 22,
40-
ACTION_RESPOND_ACK = 23,
41-
ACTION_RESPOND_ALLOW_COMPILER = 24,
42-
// The following response is stored only in the kernel decision cache.
43-
// It is removed by SNTCompilerController
44-
ACTION_RESPOND_ALLOW_PENDING_TRANSITIVE = 25,
38+
ACTION_RESPOND_ALLOW,
39+
ACTION_RESPOND_DENY,
40+
ACTION_RESPOND_ALLOW_COMPILER,
4541

46-
// NOTIFY
47-
ACTION_NOTIFY_EXEC = 30,
48-
ACTION_NOTIFY_WRITE = 31,
49-
ACTION_NOTIFY_RENAME = 32,
50-
ACTION_NOTIFY_LINK = 33,
51-
ACTION_NOTIFY_EXCHANGE = 34,
52-
ACTION_NOTIFY_DELETE = 35,
53-
ACTION_NOTIFY_WHITELIST = 36,
54-
ACTION_NOTIFY_FORK = 37,
55-
ACTION_NOTIFY_EXIT = 38,
56-
57-
// ERROR
58-
ACTION_ERROR = 99,
5942
} santa_action_t;
6043

6144
#define RESPONSE_VALID(x) \
6245
(x == ACTION_RESPOND_ALLOW || x == ACTION_RESPOND_DENY || \
63-
x == ACTION_RESPOND_ALLOW_COMPILER || \
64-
x == ACTION_RESPOND_ALLOW_PENDING_TRANSITIVE)
46+
x == ACTION_RESPOND_ALLOW_COMPILER)
6547

6648
// Struct to manage vnode IDs
6749
typedef struct santa_vnode_id_t {
@@ -75,28 +57,4 @@ typedef struct santa_vnode_id_t {
7557
#endif
7658
} santa_vnode_id_t;
7759

78-
typedef struct {
79-
santa_action_t action;
80-
santa_vnode_id_t vnode_id;
81-
uid_t uid;
82-
gid_t gid;
83-
pid_t pid;
84-
int pidversion;
85-
pid_t ppid;
86-
char path[MAXPATHLEN];
87-
char newpath[MAXPATHLEN];
88-
char ttypath[MAXPATHLEN];
89-
// For file events, this is the process name.
90-
// For exec requests, this is the parent process name.
91-
// While process names can technically be 4*MAXPATHLEN, that never
92-
// actually happens, so only take MAXPATHLEN and throw away any excess.
93-
char pname[MAXPATHLEN];
94-
95-
// This points to a copy of the original ES message.
96-
void *es_message;
97-
98-
// This points to an NSArray of the process arguments.
99-
void *args_array;
100-
} santa_message_t;
101-
10260
#endif // SANTA__COMMON__COMMON_H

0 commit comments

Comments
 (0)