Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | // Copyright (c) 2006, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // macho_id.cc: Functions to gather identifying information from a macho file |
michael@0 | 31 | // |
michael@0 | 32 | // See macho_id.h for documentation |
michael@0 | 33 | // |
michael@0 | 34 | // Author: Dan Waylonis |
michael@0 | 35 | |
michael@0 | 36 | extern "C" { // necessary for Leopard |
michael@0 | 37 | #include <fcntl.h> |
michael@0 | 38 | #include <mach-o/loader.h> |
michael@0 | 39 | #include <mach-o/swap.h> |
michael@0 | 40 | #include <stdio.h> |
michael@0 | 41 | #include <stdlib.h> |
michael@0 | 42 | #include <string.h> |
michael@0 | 43 | #include <sys/time.h> |
michael@0 | 44 | #include <sys/types.h> |
michael@0 | 45 | #include <unistd.h> |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | #include "common/mac/macho_id.h" |
michael@0 | 49 | #include "common/mac/macho_walker.h" |
michael@0 | 50 | #include "common/mac/macho_utilities.h" |
michael@0 | 51 | |
michael@0 | 52 | namespace MacFileUtilities { |
michael@0 | 53 | |
michael@0 | 54 | using google_breakpad::MD5Init; |
michael@0 | 55 | using google_breakpad::MD5Update; |
michael@0 | 56 | using google_breakpad::MD5Final; |
michael@0 | 57 | |
michael@0 | 58 | MachoID::MachoID(const char *path) |
michael@0 | 59 | : memory_(0), |
michael@0 | 60 | memory_size_(0), |
michael@0 | 61 | crc_(0), |
michael@0 | 62 | md5_context_(), |
michael@0 | 63 | update_function_(NULL) { |
michael@0 | 64 | strlcpy(path_, path, sizeof(path_)); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | MachoID::MachoID(const char *path, void *memory, size_t size) |
michael@0 | 68 | : memory_(memory), |
michael@0 | 69 | memory_size_(size), |
michael@0 | 70 | crc_(0), |
michael@0 | 71 | md5_context_(), |
michael@0 | 72 | update_function_(NULL) { |
michael@0 | 73 | strlcpy(path_, path, sizeof(path_)); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | MachoID::~MachoID() { |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // The CRC info is from http://en.wikipedia.org/wiki/Adler-32 |
michael@0 | 80 | // With optimizations from http://www.zlib.net/ |
michael@0 | 81 | |
michael@0 | 82 | // The largest prime smaller than 65536 |
michael@0 | 83 | #define MOD_ADLER 65521 |
michael@0 | 84 | // MAX_BLOCK is the largest n such that 255n(n+1)/2 + (n+1)(MAX_BLOCK-1) <= 2^32-1 |
michael@0 | 85 | #define MAX_BLOCK 5552 |
michael@0 | 86 | |
michael@0 | 87 | void MachoID::UpdateCRC(unsigned char *bytes, size_t size) { |
michael@0 | 88 | // Unrolled loops for summing |
michael@0 | 89 | #define DO1(buf,i) {sum1 += (buf)[i]; sum2 += sum1;} |
michael@0 | 90 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); |
michael@0 | 91 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); |
michael@0 | 92 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); |
michael@0 | 93 | #define DO16(buf) DO8(buf,0); DO8(buf,8); |
michael@0 | 94 | // Split up the crc |
michael@0 | 95 | uint32_t sum1 = crc_ & 0xFFFF; |
michael@0 | 96 | uint32_t sum2 = (crc_ >> 16) & 0xFFFF; |
michael@0 | 97 | |
michael@0 | 98 | // Do large blocks |
michael@0 | 99 | while (size >= MAX_BLOCK) { |
michael@0 | 100 | size -= MAX_BLOCK; |
michael@0 | 101 | int block_count = MAX_BLOCK / 16; |
michael@0 | 102 | do { |
michael@0 | 103 | DO16(bytes); |
michael@0 | 104 | bytes += 16; |
michael@0 | 105 | } while (--block_count); |
michael@0 | 106 | sum1 %= MOD_ADLER; |
michael@0 | 107 | sum2 %= MOD_ADLER; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | // Do remaining bytes |
michael@0 | 111 | if (size) { |
michael@0 | 112 | while (size >= 16) { |
michael@0 | 113 | size -= 16; |
michael@0 | 114 | DO16(bytes); |
michael@0 | 115 | bytes += 16; |
michael@0 | 116 | } |
michael@0 | 117 | while (size--) { |
michael@0 | 118 | sum1 += *bytes++; |
michael@0 | 119 | sum2 += sum1; |
michael@0 | 120 | } |
michael@0 | 121 | sum1 %= MOD_ADLER; |
michael@0 | 122 | sum2 %= MOD_ADLER; |
michael@0 | 123 | crc_ = (sum2 << 16) | sum1; |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | void MachoID::UpdateMD5(unsigned char *bytes, size_t size) { |
michael@0 | 128 | MD5Update(&md5_context_, bytes, size); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | void MachoID::Update(MachoWalker *walker, off_t offset, size_t size) { |
michael@0 | 132 | if (!update_function_ || !size) |
michael@0 | 133 | return; |
michael@0 | 134 | |
michael@0 | 135 | // Read up to 4k bytes at a time |
michael@0 | 136 | unsigned char buffer[4096]; |
michael@0 | 137 | size_t buffer_size; |
michael@0 | 138 | off_t file_offset = offset; |
michael@0 | 139 | while (size > 0) { |
michael@0 | 140 | if (size > sizeof(buffer)) { |
michael@0 | 141 | buffer_size = sizeof(buffer); |
michael@0 | 142 | size -= buffer_size; |
michael@0 | 143 | } else { |
michael@0 | 144 | buffer_size = size; |
michael@0 | 145 | size = 0; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | if (!walker->ReadBytes(buffer, buffer_size, file_offset)) |
michael@0 | 149 | return; |
michael@0 | 150 | |
michael@0 | 151 | (this->*update_function_)(buffer, buffer_size); |
michael@0 | 152 | file_offset += buffer_size; |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | bool MachoID::UUIDCommand(cpu_type_t cpu_type, |
michael@0 | 157 | cpu_subtype_t cpu_subtype, |
michael@0 | 158 | unsigned char bytes[16]) { |
michael@0 | 159 | struct breakpad_uuid_command uuid_cmd; |
michael@0 | 160 | uuid_cmd.cmd = 0; |
michael@0 | 161 | if (!WalkHeader(cpu_type, cpu_subtype, UUIDWalkerCB, &uuid_cmd)) |
michael@0 | 162 | return false; |
michael@0 | 163 | |
michael@0 | 164 | // If we found the command, we'll have initialized the uuid_command |
michael@0 | 165 | // structure |
michael@0 | 166 | if (uuid_cmd.cmd == LC_UUID) { |
michael@0 | 167 | memcpy(bytes, uuid_cmd.uuid, sizeof(uuid_cmd.uuid)); |
michael@0 | 168 | return true; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | return false; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | bool MachoID::IDCommand(cpu_type_t cpu_type, |
michael@0 | 175 | cpu_subtype_t cpu_subtype, |
michael@0 | 176 | unsigned char identifier[16]) { |
michael@0 | 177 | struct dylib_command dylib_cmd; |
michael@0 | 178 | dylib_cmd.cmd = 0; |
michael@0 | 179 | if (!WalkHeader(cpu_type, cpu_subtype, IDWalkerCB, &dylib_cmd)) |
michael@0 | 180 | return false; |
michael@0 | 181 | |
michael@0 | 182 | // If we found the command, we'll have initialized the dylib_command |
michael@0 | 183 | // structure |
michael@0 | 184 | if (dylib_cmd.cmd == LC_ID_DYLIB) { |
michael@0 | 185 | // Take the hashed filename, version, and compatability version bytes |
michael@0 | 186 | // to form the first 12 bytes, pad the rest with zeros |
michael@0 | 187 | |
michael@0 | 188 | // create a crude hash of the filename to generate the first 4 bytes |
michael@0 | 189 | identifier[0] = 0; |
michael@0 | 190 | identifier[1] = 0; |
michael@0 | 191 | identifier[2] = 0; |
michael@0 | 192 | identifier[3] = 0; |
michael@0 | 193 | |
michael@0 | 194 | for (int j = 0, i = (int)strlen(path_)-1; i>=0 && path_[i]!='/'; ++j, --i) { |
michael@0 | 195 | identifier[j%4] += path_[i]; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | identifier[4] = (dylib_cmd.dylib.current_version >> 24) & 0xFF; |
michael@0 | 199 | identifier[5] = (dylib_cmd.dylib.current_version >> 16) & 0xFF; |
michael@0 | 200 | identifier[6] = (dylib_cmd.dylib.current_version >> 8) & 0xFF; |
michael@0 | 201 | identifier[7] = dylib_cmd.dylib.current_version & 0xFF; |
michael@0 | 202 | identifier[8] = (dylib_cmd.dylib.compatibility_version >> 24) & 0xFF; |
michael@0 | 203 | identifier[9] = (dylib_cmd.dylib.compatibility_version >> 16) & 0xFF; |
michael@0 | 204 | identifier[10] = (dylib_cmd.dylib.compatibility_version >> 8) & 0xFF; |
michael@0 | 205 | identifier[11] = dylib_cmd.dylib.compatibility_version & 0xFF; |
michael@0 | 206 | identifier[12] = (cpu_type >> 24) & 0xFF; |
michael@0 | 207 | identifier[13] = (cpu_type >> 16) & 0xFF; |
michael@0 | 208 | identifier[14] = (cpu_type >> 8) & 0xFF; |
michael@0 | 209 | identifier[15] = cpu_type & 0xFF; |
michael@0 | 210 | |
michael@0 | 211 | return true; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | return false; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | uint32_t MachoID::Adler32(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) { |
michael@0 | 218 | update_function_ = &MachoID::UpdateCRC; |
michael@0 | 219 | crc_ = 0; |
michael@0 | 220 | |
michael@0 | 221 | if (!WalkHeader(cpu_type, cpu_subtype, WalkerCB, this)) |
michael@0 | 222 | return 0; |
michael@0 | 223 | |
michael@0 | 224 | return crc_; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | bool MachoID::MD5(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype, unsigned char identifier[16]) { |
michael@0 | 228 | update_function_ = &MachoID::UpdateMD5; |
michael@0 | 229 | |
michael@0 | 230 | MD5Init(&md5_context_); |
michael@0 | 231 | |
michael@0 | 232 | if (!WalkHeader(cpu_type, cpu_subtype, WalkerCB, this)) |
michael@0 | 233 | return false; |
michael@0 | 234 | |
michael@0 | 235 | MD5Final(identifier, &md5_context_); |
michael@0 | 236 | return true; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | bool MachoID::WalkHeader(cpu_type_t cpu_type, |
michael@0 | 240 | cpu_subtype_t cpu_subtype, |
michael@0 | 241 | MachoWalker::LoadCommandCallback callback, |
michael@0 | 242 | void *context) { |
michael@0 | 243 | if (memory_) { |
michael@0 | 244 | MachoWalker walker(memory_, memory_size_, callback, context); |
michael@0 | 245 | return walker.WalkHeader(cpu_type, cpu_subtype); |
michael@0 | 246 | } else { |
michael@0 | 247 | MachoWalker walker(path_, callback, context); |
michael@0 | 248 | return walker.WalkHeader(cpu_type, cpu_subtype); |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | // static |
michael@0 | 253 | bool MachoID::WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, |
michael@0 | 254 | bool swap, void *context) { |
michael@0 | 255 | MachoID *macho_id = (MachoID *)context; |
michael@0 | 256 | |
michael@0 | 257 | if (cmd->cmd == LC_SEGMENT) { |
michael@0 | 258 | struct segment_command seg; |
michael@0 | 259 | |
michael@0 | 260 | if (!walker->ReadBytes(&seg, sizeof(seg), offset)) |
michael@0 | 261 | return false; |
michael@0 | 262 | |
michael@0 | 263 | if (swap) |
michael@0 | 264 | swap_segment_command(&seg, NXHostByteOrder()); |
michael@0 | 265 | |
michael@0 | 266 | struct mach_header_64 header; |
michael@0 | 267 | off_t header_offset; |
michael@0 | 268 | |
michael@0 | 269 | if (!walker->CurrentHeader(&header, &header_offset)) |
michael@0 | 270 | return false; |
michael@0 | 271 | |
michael@0 | 272 | // Process segments that have sections: |
michael@0 | 273 | // (e.g., __TEXT, __DATA, __IMPORT, __OBJC) |
michael@0 | 274 | offset += sizeof(struct segment_command); |
michael@0 | 275 | struct section sec; |
michael@0 | 276 | for (unsigned long i = 0; i < seg.nsects; ++i) { |
michael@0 | 277 | if (!walker->ReadBytes(&sec, sizeof(sec), offset)) |
michael@0 | 278 | return false; |
michael@0 | 279 | |
michael@0 | 280 | if (swap) |
michael@0 | 281 | swap_section(&sec, 1, NXHostByteOrder()); |
michael@0 | 282 | |
michael@0 | 283 | // sections of type S_ZEROFILL are "virtual" and contain no data |
michael@0 | 284 | // in the file itself |
michael@0 | 285 | if ((sec.flags & SECTION_TYPE) != S_ZEROFILL && sec.offset != 0) |
michael@0 | 286 | macho_id->Update(walker, header_offset + sec.offset, sec.size); |
michael@0 | 287 | |
michael@0 | 288 | offset += sizeof(struct section); |
michael@0 | 289 | } |
michael@0 | 290 | } else if (cmd->cmd == LC_SEGMENT_64) { |
michael@0 | 291 | struct segment_command_64 seg64; |
michael@0 | 292 | |
michael@0 | 293 | if (!walker->ReadBytes(&seg64, sizeof(seg64), offset)) |
michael@0 | 294 | return false; |
michael@0 | 295 | |
michael@0 | 296 | if (swap) |
michael@0 | 297 | breakpad_swap_segment_command_64(&seg64, NXHostByteOrder()); |
michael@0 | 298 | |
michael@0 | 299 | struct mach_header_64 header; |
michael@0 | 300 | off_t header_offset; |
michael@0 | 301 | |
michael@0 | 302 | if (!walker->CurrentHeader(&header, &header_offset)) |
michael@0 | 303 | return false; |
michael@0 | 304 | |
michael@0 | 305 | // Process segments that have sections: |
michael@0 | 306 | // (e.g., __TEXT, __DATA, __IMPORT, __OBJC) |
michael@0 | 307 | offset += sizeof(struct segment_command_64); |
michael@0 | 308 | struct section_64 sec64; |
michael@0 | 309 | for (unsigned long i = 0; i < seg64.nsects; ++i) { |
michael@0 | 310 | if (!walker->ReadBytes(&sec64, sizeof(sec64), offset)) |
michael@0 | 311 | return false; |
michael@0 | 312 | |
michael@0 | 313 | if (swap) |
michael@0 | 314 | breakpad_swap_section_64(&sec64, 1, NXHostByteOrder()); |
michael@0 | 315 | |
michael@0 | 316 | // sections of type S_ZEROFILL are "virtual" and contain no data |
michael@0 | 317 | // in the file itself |
michael@0 | 318 | if ((sec64.flags & SECTION_TYPE) != S_ZEROFILL && sec64.offset != 0) |
michael@0 | 319 | macho_id->Update(walker, |
michael@0 | 320 | header_offset + sec64.offset, |
michael@0 | 321 | (size_t)sec64.size); |
michael@0 | 322 | |
michael@0 | 323 | offset += sizeof(struct section_64); |
michael@0 | 324 | } |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | // Continue processing |
michael@0 | 328 | return true; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | // static |
michael@0 | 332 | bool MachoID::UUIDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, |
michael@0 | 333 | bool swap, void *context) { |
michael@0 | 334 | if (cmd->cmd == LC_UUID) { |
michael@0 | 335 | struct breakpad_uuid_command *uuid_cmd = |
michael@0 | 336 | (struct breakpad_uuid_command *)context; |
michael@0 | 337 | |
michael@0 | 338 | if (!walker->ReadBytes(uuid_cmd, sizeof(struct breakpad_uuid_command), |
michael@0 | 339 | offset)) |
michael@0 | 340 | return false; |
michael@0 | 341 | |
michael@0 | 342 | if (swap) |
michael@0 | 343 | breakpad_swap_uuid_command(uuid_cmd, NXHostByteOrder()); |
michael@0 | 344 | |
michael@0 | 345 | return false; |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | // Continue processing |
michael@0 | 349 | return true; |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | // static |
michael@0 | 353 | bool MachoID::IDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, |
michael@0 | 354 | bool swap, void *context) { |
michael@0 | 355 | if (cmd->cmd == LC_ID_DYLIB) { |
michael@0 | 356 | struct dylib_command *dylib_cmd = (struct dylib_command *)context; |
michael@0 | 357 | |
michael@0 | 358 | if (!walker->ReadBytes(dylib_cmd, sizeof(struct dylib_command), offset)) |
michael@0 | 359 | return false; |
michael@0 | 360 | |
michael@0 | 361 | if (swap) |
michael@0 | 362 | swap_dylib_command(dylib_cmd, NXHostByteOrder()); |
michael@0 | 363 | |
michael@0 | 364 | return false; |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | // Continue processing |
michael@0 | 368 | return true; |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | } // namespace MacFileUtilities |