-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathinit.c
112 lines (99 loc) · 3.62 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <unistd.h>
#include <mach-o/dyld.h>
#include <string.h>
#include <pthread.h>
#include "common.h"
#include "logging.h"
int g_asepsis_disabled = 0;
pthread_mutex_t g_asepsis_mutex;
pthread_mutexattr_t g_asepsis_mutex_attr;
aslclient g_asepsis_asl = NULL;
aslmsg g_asepsis_log_msg = NULL;
uint32_t g_asepsis_executable_path_length = 1024;
char g_asepsis_executable_path[1024] = "!";
void asepsis_setup_safe(void);
void asepsis_setup_logging(void);
// here is a hardcoded list of white-listed processes - asepsis will disable itself for others
const char* g_asepsis_whitelist[] = {
"/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder",
"/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdwrite",
NULL
};
static int asepsis_process_is_whitelisted(const char* executable) {
const char** path = g_asepsis_whitelist;
while (*path) {
// test if path is a prefix of executable
if (strncmp(*path, executable, strlen(*path))==0) {
return 1;
}
path++;
}
return 0;
}
// create a new ASL log
void asepsis_setup_logging(void) {
static int asepsis_logging_initialized = 0;
if (asepsis_logging_initialized) {
return;
}
asepsis_logging_initialized = 1;
g_asepsis_asl = asl_open("Asepsis", "dylib", 0);
g_asepsis_log_msg = asl_new(ASL_TYPE_MSG);
asl_set(g_asepsis_log_msg, ASL_KEY_SENDER, "Asepsis");
}
static void asepsis_setup_executable_path() {
static int asepsis_executable_path_initialized = 0;
if (asepsis_executable_path_initialized) {
return;
}
asepsis_executable_path_initialized = 1;
if (_NSGetExecutablePath(g_asepsis_executable_path, &g_asepsis_executable_path_length)) {
// failed? ok, mockup some dummy value here
strcpy(g_asepsis_executable_path, "?");
g_asepsis_executable_path_length = 1;
#if defined (DEBUG)
asepsis_setup_logging();
DLOG("unable to retrieve executable path %s", "");
#endif
}
}
// this is called first time Asepsis is going to do some action
static void asepsis_setup(void) {
// existence of special file forces Asepsis disabling
if (access(DISABLED_TWEAK_PATH, F_OK)==0) {
#if defined (DEBUG) // bail-out should be light-weight in non-debug mode
asepsis_setup_executable_path();
asepsis_setup_logging();
DLOG("disabled by %s", DISABLED_TWEAK_PATH);
#endif
g_asepsis_disabled = 1;
return; // minimize further interference
}
// retrieve host executable path
asepsis_setup_executable_path();
// disable if hosting executable is not white-listed
if (!asepsis_process_is_whitelisted(g_asepsis_executable_path)) {
#if defined (DEBUG) // bail-out should be light-weight in non-debug mode
asepsis_setup_logging();
DLOG("disabled because not on the white-list %s", "");
#endif
g_asepsis_disabled = 1;
return; // minimize further interference
}
}
// thread-safe version of asepsis_setup
// called first time when someone calls interposed functionality related to .DS_Store or when someone wants to log something
void asepsis_setup_safe(void) {
static int already_initialized = 0;
if (already_initialized) {
return; // no-op
}
already_initialized = 1;
// init a recursive mutex
pthread_mutexattr_init(&g_asepsis_mutex_attr);
pthread_mutexattr_settype(&g_asepsis_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&g_asepsis_mutex, &g_asepsis_mutex_attr);
pthread_mutex_lock(&g_asepsis_mutex);
asepsis_setup();
pthread_mutex_unlock(&g_asepsis_mutex);
}