Compare commits

...

15 Commits
0.8.9 ... 0.9

Author SHA1 Message Date
Russell Hancox
08697d9daf KernelTests: Fix lots-of-executions test 2015-08-05 15:59:41 -04:00
Russell Hancox
8959871988 Rakefile: Clean before dist 2015-08-05 15:59:34 -04:00
Russell Hancox
bb43a04992 SNTFileInfo: Always try to get embedded info.plist before bundle plist 2015-08-05 12:01:05 -04:00
Russell Hancox
5f93dc7991 Project: Stop trying to be smart with logging destinations 2015-08-04 18:13:04 -04:00
Russell Hancox
9be8eb223c KernelTests: Stop blocking ps while tests are running, block ed instead. 2015-08-04 17:13:35 -04:00
Russell Hancox
e8b6c47e0f KernelTests: Remove timeout, chdir to tmp dir before executing, add lots-of-binaries test 2015-08-04 17:13:20 -04:00
Russell Hancox
697d442afb Project: Update Mac OS X -> OS X. 2015-08-04 13:54:55 -04:00
Russell Hancox
5dbd261b5a GUI: Allow selection of all fields and add ppid to end of parent name. 2015-08-04 13:53:47 -04:00
Russell Hancox
9bc94ca658 GUI: Add defaultBlockMessage configuration 2015-08-04 13:52:44 -04:00
Russell Hancox
4404b5f849 santactl/sync: Default to ephemeralSessionConfiguration to avoid caching 2015-08-03 18:03:51 -04:00
Russell Hancox
6a4b73b8a9 santa-driver: Before posting request to santa, ensure it exists in the cache already 2015-08-03 18:02:57 -04:00
Russell Hancox
b6146224b3 santa-driver: Make "cache too large" log info instead of debug 2015-08-03 18:02:34 -04:00
Russell Hancox
e3593c1b0c santad: fclose stderr for santactl sync too 2015-07-22 16:35:25 -04:00
Russell Hancox
90a2f10da6 santactl/rule: Print usage when args are bad, catch missing long arguments.
Fixes #20
2015-07-22 13:48:43 -04:00
Russell Hancox
60bab1c004 Rakefile: Don't miss santad/santactl dSYMs 2015-07-21 15:22:14 -04:00
15 changed files with 198 additions and 118 deletions

View File

@@ -1,7 +1,7 @@
Santa [![Build Status](https://travis-ci.org/google/santa.png?branch=master)](https://travis-ci.org/google/santa)
=====
Santa is a binary whitelisting/blacklisting system for Mac OS X. It consists of
Santa is a binary whitelisting/blacklisting system for OS X. It consists of
a kernel extension that monitors for executions, a userland daemon that makes
execution decisions based on the contents of a SQLite database, a GUI agent that
notifies the user in case of a block decision and a command-line utility for

View File

@@ -1,10 +1,9 @@
require 'timeout'
WORKSPACE = 'Santa.xcworkspace'
DEFAULT_SCHEME = 'All'
OUTPUT_PATH = 'Build'
DIST_PATH = 'Dist'
BINARIES = ['Santa.app', 'santa-driver.kext']
DSYMS = ['Santa.app.dSYM', 'santa-driver.kext.dSYM', 'santad.dSYM', 'santactl.dSYM']
XCPRETTY_DEFAULTS = '-sc'
XCODEBUILD_DEFAULTS = "-workspace #{WORKSPACE} -derivedDataPath #{OUTPUT_PATH} -parallelizeTargets"
@@ -94,6 +93,7 @@ end
task :dist do
desc "Create distribution folder"
Rake::Task['clean'].invoke()
Rake::Task['build:build'].invoke("Release")
FileUtils.rm_rf(DIST_PATH)
@@ -104,9 +104,13 @@ task :dist do
BINARIES.each do |x|
FileUtils.cp_r("#{OUTPUT_PATH}/Products/Release/#{x}", "#{DIST_PATH}/binaries")
FileUtils.cp_r("#{OUTPUT_PATH}/Products/Release/#{x}.dSYM", "#{DIST_PATH}/dsym")
end
DSYMS.each do |x|
FileUtils.cp_r("#{OUTPUT_PATH}/Products/Release/#{x}", "#{DIST_PATH}/dsym")
end
Dir.glob("Conf/*") {|x| File.directory?(x) or FileUtils.cp(x, "#{DIST_PATH}/conf")}
puts "Distribution folder created"
@@ -125,16 +129,16 @@ namespace :tests do
Rake::Task['unload'].invoke()
Rake::Task['install:debug'].invoke()
Rake::Task['load_kext'].invoke
timeout = 30
puts "Running kernel tests with a #{timeout} second timeout"
FileUtils.mkdir_p("/tmp/santa_kerneltests_tmp")
begin
Timeout::timeout(timeout) {
system "sudo #{OUTPUT_PATH}/Products/Debug/KernelTests"
}
rescue Timeout::Error
puts "ERROR: tests ran for longer than #{timeout} seconds and were killed."
puts "\033[?25l\033[12h" # hide cursor
puts "Running kernel tests"
system "cd /tmp/santa_kerneltests_tmp && sudo #{Dir.pwd}/#{OUTPUT_PATH}/Products/Debug/KernelTests"
rescue Exception
puts "\033[?25h\033[12l\n\n" # unhide cursor
FileUtils.rm_rf("/tmp/santa_kerneltests_tmp")
Rake::Task['unload_kext'].execute
end
Rake::Task['unload_kext'].execute
end
end

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6254" systemVersion="14D136" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6254" systemVersion="14E46" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6254"/>
@@ -37,7 +37,7 @@
<rect key="frame" x="18" y="65" width="444" height="60"/>
<textFieldCell key="cell" sendsActionOnEndEditing="YES" alignment="center" id="CcT-ul-1eA">
<font key="font" metaFont="system"/>
<string key="title">Santa is an application whitelisting system for Mac OS X.
<string key="title">Santa is an application whitelisting system for OS X.
There are no user-configurable settings.</string>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6254" systemVersion="14D136" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6254" systemVersion="14E46" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6254"/>
@@ -34,7 +34,7 @@
<constraints>
<constraint firstAttribute="width" constant="450" id="XgJ-EV-tBa"/>
</constraints>
<textFieldCell key="cell" allowsUndo="NO" sendsActionOnEndEditing="YES" alignment="center" title="A message to the user goes here..." allowsEditingTextAttributes="YES" id="5tH-bG-UJA">
<textFieldCell key="cell" selectable="YES" allowsUndo="NO" sendsActionOnEndEditing="YES" alignment="center" title="A message to the user goes here..." allowsEditingTextAttributes="YES" id="5tH-bG-UJA">
<font key="font" metaFont="system"/>
<color key="textColor" red="0.40000000000000002" green="0.40000000000000002" blue="0.40000000000000002" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
@@ -137,7 +137,7 @@
<constraints>
<constraint firstAttribute="width" constant="290" id="on6-pj-m2k"/>
</constraints>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Executing User" id="HRT-Be-ePf">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" sendsActionOnEndEditing="YES" title="Executing User" id="HRT-Be-ePf">
<font key="font" metaFont="system"/>
<color key="textColor" white="0.0" alpha="0.5" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
@@ -218,15 +218,20 @@ DQ
<constraints>
<constraint firstAttribute="width" constant="290" id="h3Y-mO-38F"/>
</constraints>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Parent Name" id="ieo-WK-aDD">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" sendsActionOnEndEditing="YES" title="Parent Name" id="ieo-WK-aDD">
<font key="font" metaFont="system"/>
<color key="textColor" white="0.0" alpha="0.5" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<binding destination="-2" name="value" keyPath="self.event.parentName" id="arL-Mc-4xj">
<binding destination="-2" name="displayPatternValue1" keyPath="self.event.parentName" id="Lce-TO-q9V">
<dictionary key="options">
<string key="NSNullPlaceholder">Unknown</string>
<string key="NSDisplayPattern">%{value1}@ (%{value2}@)</string>
</dictionary>
</binding>
<binding destination="-2" name="displayPatternValue2" keyPath="self.event.ppid" previousBinding="Lce-TO-q9V" id="ofI-kH-F2d">
<dictionary key="options">
<string key="NSDisplayPattern">%{value1}@ (%{value2}@)</string>
</dictionary>
</binding>
</connections>

View File

@@ -133,8 +133,11 @@
if ([self.customMessage length] > 0) {
message = self.customMessage;
} else {
message = @"The following application has been blocked from executing<br />"
@"because its trustworthiness cannot be determined.";
message = [[SNTConfigurator configurator] defaultBlockMessage];
if (!message) {
message = @"The following application has been blocked from executing<br />"
@"because its trustworthiness cannot be determined.";
}
}
NSString *fullHTML = [NSString stringWithFormat:@"%@%@%@", htmlHeader, message, htmlFooter];

View File

@@ -39,7 +39,7 @@ extern NSString * const kDefaultConfigFilePath;
/// The regex of whitelisted paths. Regexes are specified in ICU format.
///
/// The regex flags IXSM can be used, though the s (dotall) and m (multiline) flags are
/// pointless as a path only ever a single line.
/// pointless as a path only ever has a single line.
/// If the regex doesn't begin with ^ to match from the beginning of the line, it will be added.
///
@property(nonatomic) NSRegularExpression *whitelistPathRegex;
@@ -73,6 +73,12 @@ extern NSString * const kDefaultConfigFilePath;
///
@property(readonly, nonatomic) NSString *eventDetailText;
///
/// For any rule that doesn't have a custom message, this setting overrides the message
/// text that is display. If unset, a reasonable default is provided.
///
@property(readonly, nonatomic) NSString *defaultBlockMessage;
#pragma mark - Sync Settings
///

View File

@@ -41,6 +41,7 @@ static NSString * const kLogAllEventsKey = @"LogAllEvents";
static NSString * const kMoreInfoURLKey = @"MoreInfoURL";
static NSString * const kEventDetailURLKey = @"EventDetailURL";
static NSString * const kEventDetailTextKey = @"EventDetailText";
static NSString * const kDefaultBlockMessage = @"DefaultBlockMessage";
static NSString * const kSyncBaseURLKey = @"SyncBaseURL";
static NSString * const kClientAuthCertificateFileKey = @"ClientAuthCertificateFile";
@@ -146,6 +147,10 @@ static NSString * const kMachineIDPlistKeyKey = @"MachineIDKey";
return self.configData[kEventDetailTextKey];
}
- (NSString *)defaultBlockMessage {
return self.configData[kDefaultBlockMessage];
}
- (NSURL *)syncBaseURL {
return [NSURL URLWithString:self.configData[kSyncBaseURLKey]];
}

View File

@@ -223,15 +223,15 @@
if (!self.infoDict) {
self.infoDict = (NSDictionary *)[NSNull null];
if ([self bundle] && [self.bundle infoDictionary]) {
// Binaries with embedded Info.plist aren't in an NSBundle but
// CFBundleCopyInfoDictionaryForURL will return the embedded info dict.
NSURL *url = [NSURL fileURLWithPath:self.path isDirectory:NO];
NSDictionary *infoDict =
(__bridge_transfer NSDictionary *)CFBundleCopyInfoDictionaryForURL((__bridge CFURLRef)url);
if (infoDict){
self.infoDict = infoDict;
} else if ([self bundle] && [self.bundle infoDictionary]) {
self.infoDict = [self.bundle infoDictionary];
} else {
// Binaries with embedded Info.plist aren't in an NSBundle but
// CFBundleCopyInfoDictionaryForURL will return the embedded info dict.
NSURL *url = [NSURL fileURLWithPath:self.path isDirectory:NO];
NSDictionary *infoDict =
(__bridge_transfer NSDictionary *)CFBundleCopyInfoDictionaryForURL((__bridge CFURLRef)url);
if (infoDict) self.infoDict = infoDict;
}
}
return self.infoDict == (NSDictionary *)[NSNull null] ? nil : self.infoDict;

View File

@@ -23,6 +23,7 @@ static int logLevel = LOG_LEVEL_INFO; // default to info
#endif
void logMessage(int level, FILE *destination, NSString *format, ...) {
static BOOL useSyslog = NO;
static NSString *binaryName;
static dispatch_once_t pred;
@@ -33,6 +34,12 @@ void logMessage(int level, FILE *destination, NSString *format, ...) {
if ([[[NSProcessInfo processInfo] arguments] containsObject:@"--debug"]) {
logLevel = LOG_LEVEL_DEBUG;
}
// If requested, redirect output to syslog.
if ([[[NSProcessInfo processInfo] arguments] containsObject:@"--syslog"] ||
[binaryName isEqual:@"santad"]) {
useSyslog = YES;
}
});
if (logLevel < level) return;
@@ -42,10 +49,7 @@ void logMessage(int level, FILE *destination, NSString *format, ...) {
NSString *s = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
// Only prepend timestamp, severity and binary name if stdout is not a TTY
if (isatty(fileno(destination))) {
fprintf(destination, "%s\n", [s UTF8String]);
} else {
if (useSyslog) {
NSString *levelName;
int syslogLevel = LOG_DEBUG;
switch (level) {
@@ -54,13 +58,9 @@ void logMessage(int level, FILE *destination, NSString *format, ...) {
case LOG_LEVEL_INFO: levelName = @"I"; syslogLevel = LOG_INFO; break;
case LOG_LEVEL_DEBUG: levelName = @"D"; syslogLevel = LOG_DEBUG; break;
}
if (fileno(destination) == -1) {
syslog(syslogLevel, "%s\n",
[[NSString stringWithFormat:@"%@ %@: %@", levelName, binaryName, s] UTF8String]);
} else {
fprintf(destination, "%s\n",
[[NSString stringWithFormat:@"%@ %@: %@", levelName, binaryName, s] UTF8String]);
}
syslog(syslogLevel, "%s\n",
[[NSString stringWithFormat:@"%@ %@: %@", levelName, binaryName, s] UTF8String]);
} else {
fprintf(destination, "%s\n", [s UTF8String]);
}
}

View File

@@ -158,7 +158,7 @@ void SantaDecisionManager::AddToCache(
// sufficiently large and a kMaxAllowCacheTimeMilliseconds set
// sufficiently low, this should only ever occur if someone is purposefully
// trying to make the cache grow.
LOGD("Cache too large, flushing.");
LOGI("Cache too large, flushing.");
cached_decisions_->flushCollection();
}
@@ -254,6 +254,9 @@ santa_action_t SantaDecisionManager::GetFromDaemon(
// Wait for the daemon to respond or die.
do {
// Add pending request to cache.
AddToCache(vnode_id_str, ACTION_REQUEST_CHECKBW, 0);
// Send request to daemon...
if (!PostToQueue(message)) {
OSIncrementAtomic(&failed_queue_requests_);
@@ -297,9 +300,6 @@ santa_action_t SantaDecisionManager::FetchDecision(
// If item wasn in cache return it.
if CHECKBW_RESPONSE_VALID(return_action) return return_action;
// Add pending request to cache.
AddToCache(vnode_id_str, ACTION_REQUEST_CHECKBW, 0);
// Get path
char path[MAXPATHLEN];
int name_len = MAXPATHLEN;

View File

@@ -56,6 +56,12 @@ REGISTER_COMMAND_NAME(@"rule")
@" --sha256 {sha256}: hash to add\n");
}
+ (void)printErrorUsageAndExit:(NSString *)error {
printf("%s\n\n", [error UTF8String]);
printf("%s\n", [[self longHelpText] UTF8String]);
exit(1);
}
+ (void)runWithArguments:(NSArray *)arguments daemonConnection:(SNTXPCConnection *)daemonConn {
SNTConfigurator *config = [SNTConfigurator configurator];
@@ -74,8 +80,7 @@ REGISTER_COMMAND_NAME(@"rule")
// add or remove
if (!action) {
printf("Missing action - add or remove?\n");
exit(1);
[self printErrorUsageAndExit:@"Missing action"];
}
int state = RULESTATE_UNKNOWN;
@@ -84,8 +89,7 @@ REGISTER_COMMAND_NAME(@"rule")
} else if ([action compare:@"remove" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
state = RULESTATE_REMOVE;
} else {
printf("Unknown action, expected add or remove.\n");
exit(1);
[self printErrorUsageAndExit:@"Unknown action"];
}
NSString *customMsg = @"";
@@ -103,46 +107,42 @@ REGISTER_COMMAND_NAME(@"rule")
} else if ([argument compare:@"--silent-blacklist" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
state = RULESTATE_SILENT_BLACKLIST;
} else if ([argument compare:@"--message" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
if (++i > ([arguments count])) {
printf("No message specified.\n");
if (++i > arguments.count - 1) {
[self printErrorUsageAndExit:@"No message specified"];
}
customMsg = [arguments objectAtIndex:i];
} else if ([argument compare:@"--path" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
if (++i > ([arguments count])) {
printf("No path specified.\n");
if (++i > arguments.count - 1) {
[self printErrorUsageAndExit:@"No path specified"];
}
filePath = [arguments objectAtIndex:i];
} else if ([argument compare:@"--sha256" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
if (++i > ([arguments count])) {
printf("No SHA-256 specified.\n");
if (++i > arguments.count - 1) {
[self printErrorUsageAndExit:@"No SHA-256 specified"];
}
SHA256 = [arguments objectAtIndex:i];
} else {
printf("Unknown argument %s.\n", [argument UTF8String]);
exit(1);
[self printErrorUsageAndExit:[@"Unknown argument: %@" stringByAppendingString:argument]];
}
}
if (state == RULESTATE_UNKNOWN) {
printf("No state specified.\n");
exit(1);
[self printErrorUsageAndExit:@"No state specified"];
}
if (filePath) {
SNTFileInfo *fileInfo = [[SNTFileInfo alloc] initWithPath:filePath];
if (!fileInfo) {
printf("Not a regular file or executable bundle.\n");
exit(1);
[self printErrorUsageAndExit:@"Provided path is not a regular file or executable bundle"];
}
SHA256 = [fileInfo SHA256];
} else if (SHA256) {
} else {
printf("No SHA-256 or binary specified.\n");
exit(1);
[self printErrorUsageAndExit:@"Either SHA-256 or path to file must be specified"];
}
SNTRule *newRule = [[SNTRule alloc] init];

View File

@@ -31,7 +31,7 @@
}
- (instancetype)init {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
[config setTLSMinimumSupportedProtocol:kTLSProtocol12];
[config setHTTPShouldUsePipelining:YES];
return [self initWithSessionConfiguration:config];

View File

@@ -35,7 +35,7 @@
_driverManager = driverManager;
_syncTimer = [self createSyncTimer];
[self rescheduleSyncSecondsFromNow:600];
[self rescheduleSyncSecondsFromNow:30];
}
return self;
}
@@ -46,22 +46,19 @@
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
dispatch_source_set_event_handler(syncTimerQ, ^{
[self rescheduleSyncSecondsFromNow:600];
[self rescheduleSyncSecondsFromNow:600];
if (![[SNTConfigurator configurator] syncBaseURL]) return;
[[SNTConfigurator configurator] setSyncBackOff:NO];
if (![[SNTConfigurator configurator] syncBaseURL]) return;
[[SNTConfigurator configurator] setSyncBackOff:NO];
pid_t child = fork();
if (child == 0) {
// Ensure we have no privileges
if (!DropRootPrivileges()) {
_exit(1);
if (fork() == 0) {
// Ensure we have no privileges
if (!DropRootPrivileges()) {
_exit(EPERM);
}
_exit(execl(kSantaCtlPath, kSantaCtlPath, "sync", "--syslog", NULL));
}
fclose(stdout);
_exit(execl(kSantaCtlPath, kSantaCtlPath, "sync", NULL));
}
});
dispatch_resume(syncTimerQ);

View File

@@ -269,14 +269,10 @@
}
- (void)initiateEventUploadForSHA256:(NSString *)sha256 {
pid_t child = fork();
if (child == 0) {
fclose(stdout);
fclose(stderr);
if (fork() == 0) {
// Ensure we have no privileges
if (!DropRootPrivileges()) {
_exit(1);
_exit(EPERM);
}
_exit(execl(kSantaCtlPath, kSantaCtlPath, "sync", "singleevent", [sha256 UTF8String], NULL));

View File

@@ -29,23 +29,30 @@
///
#define TSTART(testName) \
printf(" %-50s ", testName);
do { printf(" %-50s ", testName); } while (0)
#define TPASS() \
printf("\x1b[32mPASS\x1b[0m\n");
do { printf("\x1b[32mPASS\x1b[0m\n"); } while (0)
#define TPASSINFO(fmt, ...) \
printf("\x1b[32mPASS\x1b[0m\n " fmt "\n", ##__VA_ARGS__);
do { printf("\x1b[32mPASS\x1b[0m\n " fmt "\n", ##__VA_ARGS__); } while (0)
#define TFAIL() \
printf("\x1b[31mFAIL\x1b[0m\n"); \
exit(1);
do { \
printf("\x1b[31mFAIL\x1b[0m\n"); \
exit(1); \
} while (0)
#define TFAILINFO(fmt, ...) \
printf("\x1b[31mFAIL\x1b[0m\n -> " fmt "\n\nTest failed.\n\n", ##__VA_ARGS__); \
exit(1);
do { \
printf("\x1b[31mFAIL\x1b[0m\n -> " fmt "\n\nTest failed.\n\n", ##__VA_ARGS__); \
exit(1); \
} while (0)
@interface SantaKernelTests : NSObject
@property io_connect_t connection;
@property int timesSeenLs;
@property int timesSeenCat;
@property int timesSeenCp;
@property int testExeIteration;
@property int timesSeenTestExeIteration;
- (void)runTests;
@end
@@ -60,16 +67,15 @@
t.standardInput = nil;
t.standardOutput = nil;
t.standardError = nil;
return t;
}
- (NSString *)sha256ForPath:(NSString *)path {
unsigned char sha256[CC_SHA256_DIGEST_LENGTH];
NSData *psData = [NSData dataWithContentsOfFile:path
options:NSDataReadingMappedIfSafe
error:nil];
CC_SHA256([psData bytes], (unsigned int)[psData length], sha256);
NSData *fData = [NSData dataWithContentsOfFile:path
options:NSDataReadingMappedIfSafe
error:nil];
CC_SHA256([fData bytes], (unsigned int)[fData length], sha256);
char buf[CC_SHA256_DIGEST_LENGTH * 2 + 1];
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
snprintf(buf + (2*i), 4, "%02x", (unsigned char)sha256[i]);
@@ -188,8 +194,15 @@
}
TPASS();
// Fetch the SHA-256 of /bin/ps, as we'll be using that for the cache invalidation test.
NSString *psSHA = [self sha256ForPath:@"/bin/ps"];
// Fetch the SHA-256 of /bin/ed, as we'll be using that for the cache invalidation test.
NSString *edSHA = [self sha256ForPath:@"/bin/ed"];
// Create the RE used for matching testexe's
NSString *cwd = [[NSFileManager defaultManager] currentDirectoryPath];
NSString *pattern = [cwd stringByAppendingPathComponent:@"testexe\\.(\\d+)"];
NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:NULL];
/// Begin listening for events
queueMemory = (IODataQueueMemory *)address;
@@ -198,7 +211,7 @@
dataSize = sizeof(vdata);
kr = IODataQueueDequeue(queueMemory, &vdata, &dataSize);
if (kr == kIOReturnSuccess) {
if ([[self sha256ForPath:@(vdata.path)] isEqual:psSHA]) {
if ([[self sha256ForPath:@(vdata.path)] isEqual:edSHA]) {
[self postToKernelAction:ACTION_RESPOND_CHECKBW_DENY forVnodeID:vdata.vnode_id];
} else if (strncmp("/bin/mv", vdata.path, strlen("/bin/mv")) == 0) {
[self postToKernelAction:ACTION_RESPOND_CHECKBW_DENY forVnodeID:vdata.vnode_id];
@@ -220,6 +233,26 @@
}
TPASSINFO("Received pid, ppid: %d, %d", vdata.pid, vdata.ppid);
} else {
NSString *path = @(vdata.path);
// If current executable is one of our test exe's from handlesLotsOfBinaries,
// check that the number has increased.
NSArray *matches = [re matchesInString:path
options:0
range:NSMakeRange(0, path.length)];
if (matches.count == 1 && [matches[0] numberOfRanges] == 2) {
NSUInteger count = [[path substringWithRange:[matches[0] rangeAtIndex:1]] intValue];
if (count <= self.testExeIteration && count > 0) {
self.timesSeenTestExeIteration++;
if (self.timesSeenTestExeIteration > 2) {
TFAILINFO("Saw same binary several times");
}
} else {
self.timesSeenTestExeIteration = 0;
self.testExeIteration = (int)count;
}
}
// Allow everything not related to our testing.
[self postToKernelAction:ACTION_RESPOND_CHECKBW_ALLOW forVnodeID:vdata.vnode_id];
}
@@ -239,11 +272,11 @@
- (void)receiveAndBlockTests {
TSTART("Blocks denied binaries");
NSTask *ps = [self taskWithPath:@"/bin/ps"];
NSTask *ed = [self taskWithPath:@"/bin/ed"];
@try {
[ps launch];
[ps waitUntilExit];
[ed launch];
[ed waitUntilExit];
TFAIL();
}
@catch (NSException *exception) {
@@ -282,12 +315,12 @@
// Copy the ls binary to a new file
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm copyItemAtPath:@"/bin/pwd" toPath:@"santakerneltests_tmp" error:nil]) {
if (![fm copyItemAtPath:@"/bin/pwd" toPath:@"invalidacachetest_tmp" error:nil]) {
TFAILINFO("Failed to create temp file");
}
// Launch the new file to put it in the cache
NSTask *pwd = [self taskWithPath:@"santakerneltests_tmp"];
NSTask *pwd = [self taskWithPath:@"invalidacachetest_tmp"];
[pwd launch];
[pwd waitUntilExit];
@@ -296,10 +329,10 @@
TFAILINFO("First launch of test binary failed");
}
// Now replace the contents of the test file (which is cached) with the contents of /bin/ps,
// Now replace the contents of the test file (which is cached) with the contents of /bin/ed,
// which is 'blacklisted' by SHA-256 during the tests.
FILE *infile = fopen("/bin/ps", "r");
FILE *outfile = fopen("santakerneltests_tmp", "w");
FILE *infile = fopen("/bin/ed", "r");
FILE *outfile = fopen("invalidacachetest_tmp", "w");
int ch;
while ((ch = fgetc(infile)) != EOF) {
fputc(ch, outfile);
@@ -307,13 +340,13 @@
fclose(infile);
// Now try running the temp file again. If it succeeds, the test failed.
NSTask *ps = [self taskWithPath:@"santakerneltests_tmp"];
NSTask *ed = [self taskWithPath:@"invalidacachetest_tmp"];
@try {
[ps launch];
[ps waitUntilExit];
[ed launch];
[ed waitUntilExit];
TFAILINFO("Launched after write while file open");
[fm removeItemAtPath:@"santakerneltests_tmp" error:nil];
[fm removeItemAtPath:@"invalidacachetest_tmp" error:nil];
} @catch (NSException *exception) {
// This is a pass, but we have more to do.
}
@@ -322,16 +355,16 @@
fclose(outfile);
// And try running the temp file again. If it succeeds, the test failed.
ps = [self taskWithPath:@"santakerneltests_tmp"];
ed = [self taskWithPath:@"invalidacachetest_tmp"];
@try {
[ps launch];
[ps waitUntilExit];
[ed launch];
[ed waitUntilExit];
TFAILINFO("Launched after file closed");
} @catch (NSException *exception) {
TPASS();
} @finally {
[fm removeItemAtPath:@"santakerneltests_tmp" error:nil];
[fm removeItemAtPath:@"invalidacachetest_tmp" error:nil];
}
}
@@ -388,6 +421,36 @@
}
}
/// Tests that the kernel can handle _lots_ of executions.
- (void)handlesLotsOfBinaries {
TSTART("Handles lots of binaries");
const int LIMIT = 12000;
for (int i = 0; i < LIMIT; i++) {
printf("\033[s"); // save cursor position
printf("%d/%i", i+1, LIMIT);
NSString *fname = [@"testexe" stringByAppendingFormat:@".%i", i];
[[NSFileManager defaultManager] copyItemAtPath:@"/bin/hostname" toPath:fname error:NULL];
@try {
NSTask *testexec = [self taskWithPath:fname];
[testexec launch];
[testexec waitUntilExit];
} @catch (NSException *e) {
TFAILINFO("Failed to launch");
}
unlink([fname UTF8String]);
printf("\033[u"); // restore cursor position
}
printf("\033[K\033[u"); // clear line, restore cursor position
TPASS();
}
#pragma mark - Main
- (void)runTests {
@@ -402,7 +465,7 @@
[self performSelectorInBackground:@selector(beginListening) withObject:nil];
// Wait for driver to finish getting ready
sleep(1.0);
sleep(1);
printf("\n-> Functional tests:\033[m\n");
[self receiveAndBlockTests];
@@ -410,6 +473,7 @@
[self invalidatesCacheTests];
[self clearCacheTests];
[self blocksDeniedTracedBinaries];
[self handlesLotsOfBinaries];
printf("\nAll tests passed.\n\n");
}