Skip to content

Instantly share code, notes, and snippets.

@ChiChou
Last active March 26, 2019 04:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
#import <Foundation/Foundation.h>
#include <sandbox.h>
int sandbox_init_with_parameters(const char* profile,
uint64_t flags,
const char* const parameters[],
char** errorbuf);
#define SANDBOX_PROFILE "/System/Library/Frameworks/WebKit.framework/Versions/A/Resources/com.apple.WebProcess.sb"
#define SANDBOX_NAMED_EXTERNAL 0x0003
void init_sandbox() {
NSString *home = NSHomeDirectory();
NSString *prefs = [home stringByAppendingString:@"Library/Preferences"];
char temporaryDirectory[PATH_MAX];
size_t temporaryDirectoryLen = confstr(_CS_DARWIN_USER_TEMP_DIR, temporaryDirectory, sizeof(temporaryDirectory));
if (!temporaryDirectoryLen) {
printf("failed to retrieve private temporary directory path\n");
exit(-1);
}
setenv("TMPDIR", temporaryDirectory, 1);
char cacheDirectory[PATH_MAX];
size_t cacheDirectoryLen = confstr(_CS_DARWIN_USER_CACHE_DIR, cacheDirectory, PATH_MAX);
if (!cacheDirectoryLen) {
printf("failed to retrieve cache path\n");
exit(-1);
}
#define SAFARI "com.apple.WebKit.WebContent+com.apple.Safari"
strncat(cacheDirectory, SAFARI, PATH_MAX - cacheDirectoryLen);
strncat(temporaryDirectory, SAFARI, PATH_MAX - temporaryDirectoryLen);
const char *sandbox_parameters[] = {
"HOME_DIR",
[home UTF8String],
"WEBKIT2_FRAMEWORK_DIR",
"/System/Library/Frameworks",
"DARWIN_USER_CACHE_DIR",
cacheDirectory,
"DARWIN_USER_TEMP_DIR",
temporaryDirectory,
"HOME_LIBRARY_PREFERENCES_DIR",
[prefs UTF8String],
NULL
};
char *errorBuf;
if (sandbox_init_with_parameters(SANDBOX_PROFILE, SANDBOX_NAMED_EXTERNAL, sandbox_parameters, &errorBuf)) {
printf("Couldn't initialize sandbox profile, error '%s'\n", errorBuf);
}
}
int main(int argc, char *argv[])
{
// comment this out, the call after init_sandbox will fail
NSLog(@"before: %@", CFPreferencesCopyAppValue(CFSTR("TALLogoutSavesState"), CFSTR("com.apple.loginwindow")));
init_sandbox();
NSLog(@"after: %@", CFPreferencesCopyAppValue(CFSTR("TALLogoutSavesState"), CFSTR("com.apple.loginwindow")));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment