Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim:set ts=4 sw=4 sts=4 ci et: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "PoisonIOInterposer.h" |
michael@0 | 8 | #include "mach_override.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/ArrayUtils.h" |
michael@0 | 11 | #include "mozilla/Assertions.h" |
michael@0 | 12 | #include "mozilla/DebugOnly.h" |
michael@0 | 13 | #include "mozilla/IOInterposer.h" |
michael@0 | 14 | #include "mozilla/Mutex.h" |
michael@0 | 15 | #include "mozilla/ProcessedStack.h" |
michael@0 | 16 | #include "mozilla/Scoped.h" |
michael@0 | 17 | #include "mozilla/Telemetry.h" |
michael@0 | 18 | #include "nsPrintfCString.h" |
michael@0 | 19 | #include "nsStackWalk.h" |
michael@0 | 20 | #include "nsTraceRefcnt.h" |
michael@0 | 21 | #include "plstr.h" |
michael@0 | 22 | #include "prio.h" |
michael@0 | 23 | |
michael@0 | 24 | #include <algorithm> |
michael@0 | 25 | #include <vector> |
michael@0 | 26 | |
michael@0 | 27 | #include <sys/param.h> |
michael@0 | 28 | #include <sys/stat.h> |
michael@0 | 29 | #include <sys/socket.h> |
michael@0 | 30 | #include <sys/uio.h> |
michael@0 | 31 | #include <aio.h> |
michael@0 | 32 | #include <dlfcn.h> |
michael@0 | 33 | #include <fcntl.h> |
michael@0 | 34 | |
michael@0 | 35 | namespace { |
michael@0 | 36 | |
michael@0 | 37 | using namespace mozilla; |
michael@0 | 38 | |
michael@0 | 39 | // Bit tracking if poisoned writes are enabled |
michael@0 | 40 | static bool sIsEnabled = false; |
michael@0 | 41 | |
michael@0 | 42 | // Check if writes are dirty before reporting IO |
michael@0 | 43 | static bool sOnlyReportDirtyWrites = false; |
michael@0 | 44 | |
michael@0 | 45 | // Routines for write validation |
michael@0 | 46 | bool IsValidWrite(int fd, const void *wbuf, size_t count); |
michael@0 | 47 | bool IsIPCWrite(int fd, const struct stat &buf); |
michael@0 | 48 | |
michael@0 | 49 | /******************************** IO AutoTimer ********************************/ |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * RAII class for timing the duration of an I/O call and reporting the result |
michael@0 | 53 | * to the IOInterposeObserver API. |
michael@0 | 54 | */ |
michael@0 | 55 | class MacIOAutoObservation : public IOInterposeObserver::Observation |
michael@0 | 56 | { |
michael@0 | 57 | public: |
michael@0 | 58 | MacIOAutoObservation(IOInterposeObserver::Operation aOp, int aFd) |
michael@0 | 59 | : IOInterposeObserver::Observation(aOp, sReference, sIsEnabled && |
michael@0 | 60 | !IsDebugFile(aFd)) |
michael@0 | 61 | , mFd(aFd) |
michael@0 | 62 | , mHasQueriedFilename(false) |
michael@0 | 63 | , mFilename(nullptr) |
michael@0 | 64 | { |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | MacIOAutoObservation(IOInterposeObserver::Operation aOp, int aFd, |
michael@0 | 68 | const void *aBuf, size_t aCount) |
michael@0 | 69 | : IOInterposeObserver::Observation(aOp, sReference, sIsEnabled && |
michael@0 | 70 | !IsDebugFile(aFd) && |
michael@0 | 71 | IsValidWrite(aFd, aBuf, aCount)) |
michael@0 | 72 | , mFd(aFd) |
michael@0 | 73 | , mHasQueriedFilename(false) |
michael@0 | 74 | , mFilename(nullptr) |
michael@0 | 75 | { |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // Custom implementation of IOInterposeObserver::Observation::Filename |
michael@0 | 79 | const char16_t* Filename() MOZ_OVERRIDE; |
michael@0 | 80 | |
michael@0 | 81 | ~MacIOAutoObservation() |
michael@0 | 82 | { |
michael@0 | 83 | Report(); |
michael@0 | 84 | if (mFilename) { |
michael@0 | 85 | NS_Free(mFilename); |
michael@0 | 86 | mFilename = nullptr; |
michael@0 | 87 | } |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | private: |
michael@0 | 91 | int mFd; |
michael@0 | 92 | bool mHasQueriedFilename; |
michael@0 | 93 | char16_t* mFilename; |
michael@0 | 94 | static const char* sReference; |
michael@0 | 95 | }; |
michael@0 | 96 | |
michael@0 | 97 | const char* MacIOAutoObservation::sReference = "PoisonIOInterposer"; |
michael@0 | 98 | |
michael@0 | 99 | // Get filename for this observation |
michael@0 | 100 | const char16_t* MacIOAutoObservation::Filename() |
michael@0 | 101 | { |
michael@0 | 102 | // If mHasQueriedFilename is true, then we already have it |
michael@0 | 103 | if (mHasQueriedFilename) { |
michael@0 | 104 | return mFilename; |
michael@0 | 105 | } |
michael@0 | 106 | char filename[MAXPATHLEN]; |
michael@0 | 107 | if (fcntl(mFd, F_GETPATH, filename) != -1) { |
michael@0 | 108 | mFilename = UTF8ToNewUnicode(nsDependentCString(filename)); |
michael@0 | 109 | } else { |
michael@0 | 110 | mFilename = nullptr; |
michael@0 | 111 | } |
michael@0 | 112 | mHasQueriedFilename = true; |
michael@0 | 113 | |
michael@0 | 114 | // Return filename |
michael@0 | 115 | return mFilename; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | /****************************** Write Validation ******************************/ |
michael@0 | 119 | |
michael@0 | 120 | // We want to detect "actual" writes, not IPC. Some IPC mechanisms are |
michael@0 | 121 | // implemented with file descriptors, so filter them out. |
michael@0 | 122 | bool IsIPCWrite(int fd, const struct stat &buf) { |
michael@0 | 123 | if ((buf.st_mode & S_IFMT) == S_IFIFO) { |
michael@0 | 124 | return true; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | if ((buf.st_mode & S_IFMT) != S_IFSOCK) { |
michael@0 | 128 | return false; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | sockaddr_storage address; |
michael@0 | 132 | socklen_t len = sizeof(address); |
michael@0 | 133 | if (getsockname(fd, (sockaddr*) &address, &len) != 0) { |
michael@0 | 134 | return true; // Ignore the fd if we can't find what it is. |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | return address.ss_family == AF_UNIX; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | // We want to report actual disk IO not things that don't move bits on the disk |
michael@0 | 141 | bool IsValidWrite(int fd, const void *wbuf, size_t count) |
michael@0 | 142 | { |
michael@0 | 143 | // Ignore writes of zero bytes, Firefox does some during shutdown. |
michael@0 | 144 | if (count == 0) { |
michael@0 | 145 | return false; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | { |
michael@0 | 149 | struct stat buf; |
michael@0 | 150 | int rv = fstat(fd, &buf); |
michael@0 | 151 | if (rv != 0) { |
michael@0 | 152 | return true; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | if (IsIPCWrite(fd, buf)) { |
michael@0 | 156 | return false; |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // For writev we pass a nullptr wbuf. We should only get here from |
michael@0 | 161 | // dbm, and it uses write, so assert that we have wbuf. |
michael@0 | 162 | if (!wbuf) { |
michael@0 | 163 | return true; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | // Break, here if we're allowed to report non-dirty writes |
michael@0 | 167 | if(!sOnlyReportDirtyWrites) { |
michael@0 | 168 | return true; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | // As a really bad hack, accept writes that don't change the on disk |
michael@0 | 172 | // content. This is needed because dbm doesn't keep track of dirty bits |
michael@0 | 173 | // and can end up writing the same data to disk twice. Once when the |
michael@0 | 174 | // user (nss) asks it to sync and once when closing the database. |
michael@0 | 175 | ScopedFreePtr<void> wbuf2(malloc(count)); |
michael@0 | 176 | if (!wbuf2) { |
michael@0 | 177 | return true; |
michael@0 | 178 | } |
michael@0 | 179 | off_t pos = lseek(fd, 0, SEEK_CUR); |
michael@0 | 180 | if (pos == -1) { |
michael@0 | 181 | return true; |
michael@0 | 182 | } |
michael@0 | 183 | ssize_t r = read(fd, wbuf2, count); |
michael@0 | 184 | if (r < 0 || (size_t)r != count) { |
michael@0 | 185 | return true; |
michael@0 | 186 | } |
michael@0 | 187 | int cmp = memcmp(wbuf, wbuf2, count); |
michael@0 | 188 | if (cmp != 0) { |
michael@0 | 189 | return true; |
michael@0 | 190 | } |
michael@0 | 191 | off_t pos2 = lseek(fd, pos, SEEK_SET); |
michael@0 | 192 | if (pos2 != pos) { |
michael@0 | 193 | return true; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | // Otherwise this is not a valid write |
michael@0 | 197 | return false; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | /*************************** Function Interception ***************************/ |
michael@0 | 201 | |
michael@0 | 202 | /** Structure for declaration of function override */ |
michael@0 | 203 | struct FuncData { |
michael@0 | 204 | const char *Name; // Name of the function for the ones we use dlsym |
michael@0 | 205 | const void *Wrapper; // The function that we will replace 'Function' with |
michael@0 | 206 | void *Function; // The function that will be replaced with 'Wrapper' |
michael@0 | 207 | void *Buffer; // Will point to the jump buffer that lets us call |
michael@0 | 208 | // 'Function' after it has been replaced. |
michael@0 | 209 | }; |
michael@0 | 210 | |
michael@0 | 211 | // Wrap aio_write. We have not seen it before, so just assert/report it. |
michael@0 | 212 | typedef ssize_t (*aio_write_t)(struct aiocb *aiocbp); |
michael@0 | 213 | ssize_t wrap_aio_write(struct aiocb *aiocbp); |
michael@0 | 214 | FuncData aio_write_data = { 0, (void*) wrap_aio_write, (void*) aio_write }; |
michael@0 | 215 | ssize_t wrap_aio_write(struct aiocb *aiocbp) { |
michael@0 | 216 | MacIOAutoObservation timer(IOInterposeObserver::OpWrite, aiocbp->aio_fildes); |
michael@0 | 217 | |
michael@0 | 218 | aio_write_t old_write = (aio_write_t) aio_write_data.Buffer; |
michael@0 | 219 | return old_write(aiocbp); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | // Wrap pwrite-like functions. |
michael@0 | 223 | // We have not seen them before, so just assert/report it. |
michael@0 | 224 | typedef ssize_t (*pwrite_t)(int fd, const void *buf, size_t nbyte, off_t offset); |
michael@0 | 225 | template<FuncData &foo> |
michael@0 | 226 | ssize_t wrap_pwrite_temp(int fd, const void *buf, size_t nbyte, off_t offset) { |
michael@0 | 227 | MacIOAutoObservation timer(IOInterposeObserver::OpWrite, fd); |
michael@0 | 228 | pwrite_t old_write = (pwrite_t) foo.Buffer; |
michael@0 | 229 | return old_write(fd, buf, nbyte, offset); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | // Define a FuncData for a pwrite-like functions. |
michael@0 | 233 | #define DEFINE_PWRITE_DATA(X, NAME) \ |
michael@0 | 234 | FuncData X ## _data = { NAME, (void*) wrap_pwrite_temp<X ## _data> }; \ |
michael@0 | 235 | |
michael@0 | 236 | // This exists everywhere. |
michael@0 | 237 | DEFINE_PWRITE_DATA(pwrite, "pwrite") |
michael@0 | 238 | // These exist on 32 bit OS X |
michael@0 | 239 | DEFINE_PWRITE_DATA(pwrite_NOCANCEL_UNIX2003, "pwrite$NOCANCEL$UNIX2003"); |
michael@0 | 240 | DEFINE_PWRITE_DATA(pwrite_UNIX2003, "pwrite$UNIX2003"); |
michael@0 | 241 | // This exists on 64 bit OS X |
michael@0 | 242 | DEFINE_PWRITE_DATA(pwrite_NOCANCEL, "pwrite$NOCANCEL"); |
michael@0 | 243 | |
michael@0 | 244 | |
michael@0 | 245 | typedef ssize_t (*writev_t)(int fd, const struct iovec *iov, int iovcnt); |
michael@0 | 246 | template<FuncData &foo> |
michael@0 | 247 | ssize_t wrap_writev_temp(int fd, const struct iovec *iov, int iovcnt) { |
michael@0 | 248 | MacIOAutoObservation timer(IOInterposeObserver::OpWrite, fd, nullptr, iovcnt); |
michael@0 | 249 | writev_t old_write = (writev_t) foo.Buffer; |
michael@0 | 250 | return old_write(fd, iov, iovcnt); |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | // Define a FuncData for a writev-like functions. |
michael@0 | 254 | #define DEFINE_WRITEV_DATA(X, NAME) \ |
michael@0 | 255 | FuncData X ## _data = { NAME, (void*) wrap_writev_temp<X ## _data> }; \ |
michael@0 | 256 | |
michael@0 | 257 | // This exists everywhere. |
michael@0 | 258 | DEFINE_WRITEV_DATA(writev, "writev"); |
michael@0 | 259 | // These exist on 32 bit OS X |
michael@0 | 260 | DEFINE_WRITEV_DATA(writev_NOCANCEL_UNIX2003, "writev$NOCANCEL$UNIX2003"); |
michael@0 | 261 | DEFINE_WRITEV_DATA(writev_UNIX2003, "writev$UNIX2003"); |
michael@0 | 262 | // This exists on 64 bit OS X |
michael@0 | 263 | DEFINE_WRITEV_DATA(writev_NOCANCEL, "writev$NOCANCEL"); |
michael@0 | 264 | |
michael@0 | 265 | typedef ssize_t (*write_t)(int fd, const void *buf, size_t count); |
michael@0 | 266 | template<FuncData &foo> |
michael@0 | 267 | ssize_t wrap_write_temp(int fd, const void *buf, size_t count) { |
michael@0 | 268 | MacIOAutoObservation timer(IOInterposeObserver::OpWrite, fd, buf, count); |
michael@0 | 269 | write_t old_write = (write_t) foo.Buffer; |
michael@0 | 270 | return old_write(fd, buf, count); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | // Define a FuncData for a write-like functions. |
michael@0 | 274 | #define DEFINE_WRITE_DATA(X, NAME) \ |
michael@0 | 275 | FuncData X ## _data = { NAME, (void*) wrap_write_temp<X ## _data> }; \ |
michael@0 | 276 | |
michael@0 | 277 | // This exists everywhere. |
michael@0 | 278 | DEFINE_WRITE_DATA(write, "write"); |
michael@0 | 279 | // These exist on 32 bit OS X |
michael@0 | 280 | DEFINE_WRITE_DATA(write_NOCANCEL_UNIX2003, "write$NOCANCEL$UNIX2003"); |
michael@0 | 281 | DEFINE_WRITE_DATA(write_UNIX2003, "write$UNIX2003"); |
michael@0 | 282 | // This exists on 64 bit OS X |
michael@0 | 283 | DEFINE_WRITE_DATA(write_NOCANCEL, "write$NOCANCEL"); |
michael@0 | 284 | |
michael@0 | 285 | FuncData *Functions[] = { &aio_write_data, |
michael@0 | 286 | |
michael@0 | 287 | &pwrite_data, |
michael@0 | 288 | &pwrite_NOCANCEL_UNIX2003_data, |
michael@0 | 289 | &pwrite_UNIX2003_data, |
michael@0 | 290 | &pwrite_NOCANCEL_data, |
michael@0 | 291 | |
michael@0 | 292 | &write_data, |
michael@0 | 293 | &write_NOCANCEL_UNIX2003_data, |
michael@0 | 294 | &write_UNIX2003_data, |
michael@0 | 295 | &write_NOCANCEL_data, |
michael@0 | 296 | |
michael@0 | 297 | &writev_data, |
michael@0 | 298 | &writev_NOCANCEL_UNIX2003_data, |
michael@0 | 299 | &writev_UNIX2003_data, |
michael@0 | 300 | &writev_NOCANCEL_data}; |
michael@0 | 301 | |
michael@0 | 302 | const int NumFunctions = ArrayLength(Functions); |
michael@0 | 303 | |
michael@0 | 304 | } // anonymous namespace |
michael@0 | 305 | |
michael@0 | 306 | /******************************** IO Poisoning ********************************/ |
michael@0 | 307 | |
michael@0 | 308 | namespace mozilla { |
michael@0 | 309 | |
michael@0 | 310 | void InitPoisonIOInterposer() { |
michael@0 | 311 | // Enable reporting from poisoned write methods |
michael@0 | 312 | sIsEnabled = true; |
michael@0 | 313 | |
michael@0 | 314 | // Make sure we only poison writes once! |
michael@0 | 315 | static bool WritesArePoisoned = false; |
michael@0 | 316 | if (WritesArePoisoned) { |
michael@0 | 317 | return; |
michael@0 | 318 | } |
michael@0 | 319 | WritesArePoisoned = true; |
michael@0 | 320 | |
michael@0 | 321 | // stdout and stderr are OK. |
michael@0 | 322 | MozillaRegisterDebugFD(1); |
michael@0 | 323 | MozillaRegisterDebugFD(2); |
michael@0 | 324 | |
michael@0 | 325 | for (int i = 0; i < NumFunctions; ++i) { |
michael@0 | 326 | FuncData *d = Functions[i]; |
michael@0 | 327 | if (!d->Function) { |
michael@0 | 328 | d->Function = dlsym(RTLD_DEFAULT, d->Name); |
michael@0 | 329 | } |
michael@0 | 330 | if (!d->Function) { |
michael@0 | 331 | continue; |
michael@0 | 332 | } |
michael@0 | 333 | DebugOnly<mach_error_t> t = mach_override_ptr(d->Function, d->Wrapper, |
michael@0 | 334 | &d->Buffer); |
michael@0 | 335 | MOZ_ASSERT(t == err_none); |
michael@0 | 336 | } |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | void OnlyReportDirtyWrites() { |
michael@0 | 340 | sOnlyReportDirtyWrites = true; |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | void ClearPoisonIOInterposer() { |
michael@0 | 344 | // Not sure how or if we can unpoison the functions. Would be nice, but no |
michael@0 | 345 | // worries we won't need to do this anyway. |
michael@0 | 346 | sIsEnabled = false; |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | } // namespace mozilla |