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_walker.cc: Iterate over the load commands in a mach-o file |
michael@0 | 31 | // |
michael@0 | 32 | // See macho_walker.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 <assert.h> |
michael@0 | 38 | #include <fcntl.h> |
michael@0 | 39 | #include <mach-o/arch.h> |
michael@0 | 40 | #include <mach-o/loader.h> |
michael@0 | 41 | #include <mach-o/swap.h> |
michael@0 | 42 | #include <string.h> |
michael@0 | 43 | #include <unistd.h> |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | #include "common/mac/byteswap.h" |
michael@0 | 47 | #include "common/mac/macho_walker.h" |
michael@0 | 48 | #include "common/mac/macho_utilities.h" |
michael@0 | 49 | |
michael@0 | 50 | namespace MacFileUtilities { |
michael@0 | 51 | |
michael@0 | 52 | MachoWalker::MachoWalker(const char *path, LoadCommandCallback callback, |
michael@0 | 53 | void *context) |
michael@0 | 54 | : file_(0), |
michael@0 | 55 | memory_(NULL), |
michael@0 | 56 | memory_size_(0), |
michael@0 | 57 | callback_(callback), |
michael@0 | 58 | callback_context_(context), |
michael@0 | 59 | current_header_(NULL), |
michael@0 | 60 | current_header_size_(0), |
michael@0 | 61 | current_header_offset_(0) { |
michael@0 | 62 | file_ = open(path, O_RDONLY); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | MachoWalker::MachoWalker(void *memory, size_t size, |
michael@0 | 66 | LoadCommandCallback callback, void *context) |
michael@0 | 67 | : file_(0), |
michael@0 | 68 | memory_(memory), |
michael@0 | 69 | memory_size_(size), |
michael@0 | 70 | callback_(callback), |
michael@0 | 71 | callback_context_(context), |
michael@0 | 72 | current_header_(NULL), |
michael@0 | 73 | current_header_size_(0), |
michael@0 | 74 | current_header_offset_(0) { |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | MachoWalker::~MachoWalker() { |
michael@0 | 78 | if (file_ != -1) |
michael@0 | 79 | close(file_); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | bool MachoWalker::WalkHeader(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) { |
michael@0 | 83 | cpu_type_t valid_cpu_type = cpu_type; |
michael@0 | 84 | cpu_subtype_t valid_cpu_subtype = cpu_subtype; |
michael@0 | 85 | // if |cpu_type| is 0, use the native cpu type. |
michael@0 | 86 | if (cpu_type == 0) { |
michael@0 | 87 | const NXArchInfo *arch = NXGetLocalArchInfo(); |
michael@0 | 88 | assert(arch); |
michael@0 | 89 | valid_cpu_type = arch->cputype; |
michael@0 | 90 | valid_cpu_subtype = CPU_SUBTYPE_MULTIPLE; |
michael@0 | 91 | } |
michael@0 | 92 | off_t offset; |
michael@0 | 93 | if (FindHeader(valid_cpu_type, valid_cpu_subtype, offset)) { |
michael@0 | 94 | if (cpu_type & CPU_ARCH_ABI64) |
michael@0 | 95 | return WalkHeader64AtOffset(offset); |
michael@0 | 96 | |
michael@0 | 97 | return WalkHeaderAtOffset(offset); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | return false; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | bool MachoWalker::ReadBytes(void *buffer, size_t size, off_t offset) { |
michael@0 | 104 | if (memory_) { |
michael@0 | 105 | if (offset < 0) |
michael@0 | 106 | return false; |
michael@0 | 107 | bool result = true; |
michael@0 | 108 | if (offset + size > memory_size_) { |
michael@0 | 109 | if (static_cast<size_t>(offset) >= memory_size_) |
michael@0 | 110 | return false; |
michael@0 | 111 | size = memory_size_ - static_cast<size_t>(offset); |
michael@0 | 112 | result = false; |
michael@0 | 113 | } |
michael@0 | 114 | memcpy(buffer, static_cast<char *>(memory_) + offset, size); |
michael@0 | 115 | return result; |
michael@0 | 116 | } else { |
michael@0 | 117 | return pread(file_, buffer, size, offset) == (ssize_t)size; |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | bool MachoWalker::CurrentHeader(struct mach_header_64 *header, off_t *offset) { |
michael@0 | 122 | if (current_header_) { |
michael@0 | 123 | memcpy(header, current_header_, sizeof(mach_header_64)); |
michael@0 | 124 | *offset = current_header_offset_; |
michael@0 | 125 | return true; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | return false; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | bool MachoWalker::FindHeader(cpu_type_t cpu_type, |
michael@0 | 132 | cpu_subtype_t cpu_subtype, |
michael@0 | 133 | off_t &offset) { |
michael@0 | 134 | // Read the magic bytes that's common amongst all mach-o files |
michael@0 | 135 | uint32_t magic; |
michael@0 | 136 | if (!ReadBytes(&magic, sizeof(magic), 0)) |
michael@0 | 137 | return false; |
michael@0 | 138 | |
michael@0 | 139 | offset = sizeof(magic); |
michael@0 | 140 | |
michael@0 | 141 | // Figure out what type of file we've got |
michael@0 | 142 | bool is_fat = false; |
michael@0 | 143 | if (magic == FAT_MAGIC || magic == FAT_CIGAM) { |
michael@0 | 144 | is_fat = true; |
michael@0 | 145 | } |
michael@0 | 146 | else if (magic != MH_MAGIC && magic != MH_CIGAM && magic != MH_MAGIC_64 && |
michael@0 | 147 | magic != MH_CIGAM_64) { |
michael@0 | 148 | return false; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | if (!is_fat) { |
michael@0 | 152 | // If we don't have a fat header, check if the cpu type matches the single |
michael@0 | 153 | // header |
michael@0 | 154 | struct mach_header header; |
michael@0 | 155 | if (!ReadBytes(&header, sizeof(header), 0)) |
michael@0 | 156 | return false; |
michael@0 | 157 | |
michael@0 | 158 | if (magic == MH_CIGAM || magic == MH_CIGAM_64) |
michael@0 | 159 | swap_mach_header(&header, NXHostByteOrder()); |
michael@0 | 160 | |
michael@0 | 161 | if (cpu_type != header.cputype || |
michael@0 | 162 | (cpu_subtype != CPU_SUBTYPE_MULTIPLE && |
michael@0 | 163 | cpu_subtype != header.cpusubtype)) { |
michael@0 | 164 | return false; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | offset = 0; |
michael@0 | 168 | return true; |
michael@0 | 169 | } else { |
michael@0 | 170 | // Read the fat header and find an appropriate architecture |
michael@0 | 171 | offset = 0; |
michael@0 | 172 | struct fat_header fat; |
michael@0 | 173 | if (!ReadBytes(&fat, sizeof(fat), offset)) |
michael@0 | 174 | return false; |
michael@0 | 175 | |
michael@0 | 176 | if (NXHostByteOrder() != NX_BigEndian) |
michael@0 | 177 | swap_fat_header(&fat, NXHostByteOrder()); |
michael@0 | 178 | |
michael@0 | 179 | offset += sizeof(fat); |
michael@0 | 180 | |
michael@0 | 181 | // Search each architecture for the desired one |
michael@0 | 182 | struct fat_arch arch; |
michael@0 | 183 | for (uint32_t i = 0; i < fat.nfat_arch; ++i) { |
michael@0 | 184 | if (!ReadBytes(&arch, sizeof(arch), offset)) |
michael@0 | 185 | return false; |
michael@0 | 186 | |
michael@0 | 187 | if (NXHostByteOrder() != NX_BigEndian) |
michael@0 | 188 | swap_fat_arch(&arch, 1, NXHostByteOrder()); |
michael@0 | 189 | |
michael@0 | 190 | if (arch.cputype == cpu_type && |
michael@0 | 191 | (cpu_subtype == CPU_SUBTYPE_MULTIPLE || |
michael@0 | 192 | arch.cpusubtype == cpu_subtype)) { |
michael@0 | 193 | offset = arch.offset; |
michael@0 | 194 | return true; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | offset += sizeof(arch); |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | return false; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | bool MachoWalker::WalkHeaderAtOffset(off_t offset) { |
michael@0 | 205 | struct mach_header header; |
michael@0 | 206 | if (!ReadBytes(&header, sizeof(header), offset)) |
michael@0 | 207 | return false; |
michael@0 | 208 | |
michael@0 | 209 | bool swap = (header.magic == MH_CIGAM); |
michael@0 | 210 | if (swap) |
michael@0 | 211 | swap_mach_header(&header, NXHostByteOrder()); |
michael@0 | 212 | |
michael@0 | 213 | // Copy the data into the mach_header_64 structure. Since the 32-bit and |
michael@0 | 214 | // 64-bit only differ in the last field (reserved), this is safe to do. |
michael@0 | 215 | struct mach_header_64 header64; |
michael@0 | 216 | memcpy((void *)&header64, (const void *)&header, sizeof(header)); |
michael@0 | 217 | header64.reserved = 0; |
michael@0 | 218 | |
michael@0 | 219 | current_header_ = &header64; |
michael@0 | 220 | current_header_size_ = sizeof(header); // 32-bit, not 64-bit |
michael@0 | 221 | current_header_offset_ = offset; |
michael@0 | 222 | offset += current_header_size_; |
michael@0 | 223 | bool result = WalkHeaderCore(offset, header.ncmds, swap); |
michael@0 | 224 | current_header_ = NULL; |
michael@0 | 225 | current_header_size_ = 0; |
michael@0 | 226 | current_header_offset_ = 0; |
michael@0 | 227 | return result; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | bool MachoWalker::WalkHeader64AtOffset(off_t offset) { |
michael@0 | 231 | struct mach_header_64 header; |
michael@0 | 232 | if (!ReadBytes(&header, sizeof(header), offset)) |
michael@0 | 233 | return false; |
michael@0 | 234 | |
michael@0 | 235 | bool swap = (header.magic == MH_CIGAM_64); |
michael@0 | 236 | if (swap) |
michael@0 | 237 | breakpad_swap_mach_header_64(&header, NXHostByteOrder()); |
michael@0 | 238 | |
michael@0 | 239 | current_header_ = &header; |
michael@0 | 240 | current_header_size_ = sizeof(header); |
michael@0 | 241 | current_header_offset_ = offset; |
michael@0 | 242 | offset += current_header_size_; |
michael@0 | 243 | bool result = WalkHeaderCore(offset, header.ncmds, swap); |
michael@0 | 244 | current_header_ = NULL; |
michael@0 | 245 | current_header_size_ = 0; |
michael@0 | 246 | current_header_offset_ = 0; |
michael@0 | 247 | return result; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | bool MachoWalker::WalkHeaderCore(off_t offset, uint32_t number_of_commands, |
michael@0 | 251 | bool swap) { |
michael@0 | 252 | for (uint32_t i = 0; i < number_of_commands; ++i) { |
michael@0 | 253 | struct load_command cmd; |
michael@0 | 254 | if (!ReadBytes(&cmd, sizeof(cmd), offset)) |
michael@0 | 255 | return false; |
michael@0 | 256 | |
michael@0 | 257 | if (swap) |
michael@0 | 258 | swap_load_command(&cmd, NXHostByteOrder()); |
michael@0 | 259 | |
michael@0 | 260 | // Call the user callback |
michael@0 | 261 | if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_)) |
michael@0 | 262 | break; |
michael@0 | 263 | |
michael@0 | 264 | offset += cmd.cmdsize; |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | return true; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | } // namespace MacFileUtilities |