1 |
efrain |
1 |
// Copyright 2019 Google
|
|
|
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 |
#include "Crashlytics/Crashlytics/Helpers/FIRCLSUtility.h"
|
|
|
16 |
|
|
|
17 |
#include <mach/mach.h>
|
|
|
18 |
|
|
|
19 |
#include <dlfcn.h>
|
|
|
20 |
|
|
|
21 |
#include "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h"
|
|
|
22 |
#include "Crashlytics/Crashlytics/Helpers/FIRCLSFeatures.h"
|
|
|
23 |
#include "Crashlytics/Crashlytics/Helpers/FIRCLSFile.h"
|
|
|
24 |
|
|
|
25 |
#import "Crashlytics/Shared/FIRCLSByteUtility.h"
|
|
|
26 |
#import "Crashlytics/Shared/FIRCLSUUID.h"
|
|
|
27 |
|
|
|
28 |
#import <CommonCrypto/CommonHMAC.h>
|
|
|
29 |
|
|
|
30 |
void FIRCLSLookupFunctionPointer(void* ptr, void (^block)(const char* name, const char* lib)) {
|
|
|
31 |
Dl_info info;
|
|
|
32 |
|
|
|
33 |
if (dladdr(ptr, &info) == 0) {
|
|
|
34 |
block(NULL, NULL);
|
|
|
35 |
return;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
const char* name = "unknown";
|
|
|
39 |
const char* lib = "unknown";
|
|
|
40 |
|
|
|
41 |
if (info.dli_sname) {
|
|
|
42 |
name = info.dli_sname;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if (info.dli_fname) {
|
|
|
46 |
lib = info.dli_fname;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
block(name, lib);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
uint8_t FIRCLSNybbleFromChar(char c) {
|
|
|
53 |
if (c >= '0' && c <= '9') {
|
|
|
54 |
return c - '0';
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
if (c >= 'a' && c <= 'f') {
|
|
|
58 |
return c - 'a' + 10;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
if (c >= 'A' && c <= 'F') {
|
|
|
62 |
return c - 'A' + 10;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
return FIRCLSInvalidCharNybble;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
bool FIRCLSReadMemory(vm_address_t src, void* dest, size_t len) {
|
|
|
69 |
if (!FIRCLSIsValidPointer(src)) {
|
|
|
70 |
return false;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
vm_size_t readSize = len;
|
|
|
74 |
|
|
|
75 |
return vm_read_overwrite(mach_task_self(), src, len, (pointer_t)dest, &readSize) == KERN_SUCCESS;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
bool FIRCLSReadString(vm_address_t src, char** dest, size_t maxlen) {
|
|
|
79 |
char c;
|
|
|
80 |
vm_address_t address;
|
|
|
81 |
|
|
|
82 |
if (!dest) {
|
|
|
83 |
return false;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
// Walk the entire string. Not certain this is perfect...
|
|
|
87 |
for (address = src; address < src + maxlen; ++address) {
|
|
|
88 |
if (!FIRCLSReadMemory(address, &c, 1)) {
|
|
|
89 |
return false;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if (c == 0) {
|
|
|
93 |
break;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
*dest = (char*)src;
|
|
|
98 |
|
|
|
99 |
return true;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
const char* FIRCLSDupString(const char* string) {
|
|
|
103 |
#if CLS_MEMORY_PROTECTION_ENABLED
|
|
|
104 |
char* buffer;
|
|
|
105 |
size_t length;
|
|
|
106 |
|
|
|
107 |
if (!string) {
|
|
|
108 |
return NULL;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
length = strlen(string);
|
|
|
112 |
buffer = FIRCLSAllocatorSafeAllocate(_firclsContext.allocator, length + 1, CLS_READONLY);
|
|
|
113 |
|
|
|
114 |
memcpy(buffer, string, length);
|
|
|
115 |
|
|
|
116 |
buffer[length] = 0; // null-terminate
|
|
|
117 |
|
|
|
118 |
return buffer;
|
|
|
119 |
#else
|
|
|
120 |
return strdup(string);
|
|
|
121 |
#endif
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
void FIRCLSDispatchAfter(float timeInSeconds, dispatch_queue_t queue, dispatch_block_t block) {
|
|
|
125 |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeInSeconds * NSEC_PER_SEC)), queue,
|
|
|
126 |
block);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
bool FIRCLSUnlinkIfExists(const char* path) {
|
|
|
130 |
if (unlink(path) != 0) {
|
|
|
131 |
if (errno != ENOENT) {
|
|
|
132 |
return false;
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
return true;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
NSString* FIRCLSNormalizeUUID(NSString* value) {
|
|
|
140 |
return [[value stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
NSString* FIRCLSGenerateNormalizedUUID(void) {
|
|
|
144 |
return FIRCLSNormalizeUUID(FIRCLSGenerateUUID());
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
// Redacts a UUID wrapped in parenthesis from a char* using strchr, which is async safe.
|
|
|
148 |
// Ex.
|
|
|
149 |
// "foo (bar) (45D62CC2-CFB5-4E33-AB61-B0684627F1B6) baz"
|
|
|
150 |
// becomes
|
|
|
151 |
// "foo (bar) (********-****-****-****-************) baz"
|
|
|
152 |
void FIRCLSRedactUUID(char* value) {
|
|
|
153 |
if (value == NULL) {
|
|
|
154 |
return;
|
|
|
155 |
}
|
|
|
156 |
char* openParen = value;
|
|
|
157 |
// find the index of the first paren
|
|
|
158 |
while ((openParen = strchr(openParen, '(')) != NULL) {
|
|
|
159 |
// find index of the matching close paren
|
|
|
160 |
const char* closeParen = strchr(openParen, ')');
|
|
|
161 |
if (closeParen == NULL) {
|
|
|
162 |
break;
|
|
|
163 |
}
|
|
|
164 |
// if the distance between them is 37, traverse the characters
|
|
|
165 |
// and replace anything that is not a '-' with '*'
|
|
|
166 |
if (closeParen - openParen == 37) {
|
|
|
167 |
for (int i = 1; i < 37; ++i) {
|
|
|
168 |
if (*(openParen + i) != '-') {
|
|
|
169 |
*(openParen + i) = '*';
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
break;
|
|
|
173 |
}
|
|
|
174 |
openParen++;
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
NSString* FIRCLSNSDataToNSString(NSData* data) {
|
|
|
179 |
NSString* string;
|
|
|
180 |
char* buffer;
|
|
|
181 |
size_t size;
|
|
|
182 |
NSUInteger length;
|
|
|
183 |
|
|
|
184 |
// we need 2 hex char for every byte of data, plus one more spot for a
|
|
|
185 |
// null terminator
|
|
|
186 |
length = [data length];
|
|
|
187 |
size = (length * 2) + 1;
|
|
|
188 |
buffer = malloc(sizeof(char) * size);
|
|
|
189 |
|
|
|
190 |
if (!buffer) {
|
|
|
191 |
FIRCLSErrorLog(@"Unable to malloc in FIRCLSNSDataToNSString");
|
|
|
192 |
return nil;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
FIRCLSSafeHexToString([data bytes], length, buffer);
|
|
|
196 |
|
|
|
197 |
string = [NSString stringWithUTF8String:buffer];
|
|
|
198 |
|
|
|
199 |
free(buffer);
|
|
|
200 |
|
|
|
201 |
return string;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
void FIRCLSAddOperationAfter(float timeInSeconds, NSOperationQueue* queue, void (^block)(void)) {
|
|
|
205 |
dispatch_queue_t afterQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
|
|
206 |
FIRCLSDispatchAfter(timeInSeconds, afterQueue, ^{
|
|
|
207 |
[queue addOperationWithBlock:block];
|
|
|
208 |
});
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
#if DEBUG
|
|
|
212 |
void FIRCLSPrintAUUID(const uint8_t* value) {
|
|
|
213 |
CFUUIDRef uuid = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *(CFUUIDBytes*)value);
|
|
|
214 |
|
|
|
215 |
NSString* string = CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));
|
|
|
216 |
|
|
|
217 |
CFRelease(uuid);
|
|
|
218 |
|
|
|
219 |
FIRCLSDebugLog(@"%@", [[string stringByReplacingOccurrencesOfString:@"-"
|
|
|
220 |
withString:@""] lowercaseString]);
|
|
|
221 |
}
|
|
|
222 |
#endif
|