DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (a15ba287ac6f)

VCS Links

Line Code
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/shared_memory.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#ifdef ANDROID
#  include <linux/ashmem.h>
#endif

#include "base/eintr_wrapper.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "mozilla/Atomics.h"
#include "mozilla/UniquePtrExtensions.h"
#include "prenv.h"

namespace base {

SharedMemory::SharedMemory()
    : mapped_file_(-1),
      frozen_file_(-1),
      mapped_size_(0),
      memory_(nullptr),
      read_only_(false),
      freezeable_(false),
      max_size_(0) {}

SharedMemory::SharedMemory(SharedMemory&& other) {
  if (this == &other) {
    return;
  }

  mapped_file_ = other.mapped_file_;
  mapped_size_ = other.mapped_size_;
  frozen_file_ = other.frozen_file_;
  memory_ = other.memory_;
  read_only_ = other.read_only_;
  freezeable_ = other.freezeable_;
  max_size_ = other.max_size_;

  other.mapped_file_ = -1;
  other.mapped_size_ = 0;
  other.frozen_file_ = -1;
  other.memory_ = nullptr;
}

SharedMemory::~SharedMemory() { Close(); }

bool SharedMemory::SetHandle(SharedMemoryHandle handle, bool read_only) {
  DCHECK(mapped_file_ == -1);
  DCHECK(frozen_file_ == -1);

  freezeable_ = false;
  mapped_file_ = handle.fd;
  read_only_ = read_only;
  return true;
}

// static
bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
  return handle.fd >= 0;
}

bool SharedMemory::IsValid() const { return mapped_file_ >= 0; }

// static
SharedMemoryHandle SharedMemory::NULLHandle() { return SharedMemoryHandle(); }

// Workaround for CVE-2018-4435 (https://crbug.com/project-zero/1671);
// can be removed when minimum OS version is at least 10.12.
#ifdef OS_MACOSX
static const char* GetTmpDir() {
  static const char* const kTmpDir = [] {
    const char* tmpdir = PR_GetEnv("TMPDIR");
    if (tmpdir) {
      return tmpdir;
    }
    return "/tmp";
  }();
  return kTmpDir;
}

static int FakeShmOpen(const char* name, int oflag, int mode) {
  CHECK(name[0] == '/');
  std::string path(GetTmpDir());
  path += name;
  return open(path.c_str(), oflag | O_CLOEXEC | O_NOCTTY, mode);
}

static int FakeShmUnlink(const char* name) {
  CHECK(name[0] == '/');
  std::string path(GetTmpDir());
  path += name;
  return unlink(path.c_str());
}

static bool IsShmOpenSecure() {
  static const bool kIsSecure = [] {
    mozilla::UniqueFileHandle rwfd, rofd;
    std::string name;
    CHECK(SharedMemory::AppendPosixShmPrefix(&name, getpid()));
    name += "sectest";
    // The prefix includes the pid and this will be called at most
    // once per process, so no need for a counter.
    rwfd.reset(
        HANDLE_EINTR(shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600)));
    // An adversary could steal the name.  Handle this semi-gracefully.
    DCHECK(rwfd);
    if (!rwfd) {
      return false;
    }
    rofd.reset(shm_open(name.c_str(), O_RDONLY, 0400));
    CHECK(rofd);
    CHECK(shm_unlink(name.c_str()) == 0);
    CHECK(ftruncate(rwfd.get(), 1) == 0);
    rwfd = nullptr;
    void* map = mmap(nullptr, 1, PROT_READ, MAP_SHARED, rofd.get(), 0);
    CHECK(map != MAP_FAILED);
    bool ok = mprotect(map, 1, PROT_READ | PROT_WRITE) != 0;
    munmap(map, 1);
    return ok;
  }();
  return kIsSecure;
}

static int SafeShmOpen(bool freezeable, const char* name, int oflag, int mode) {
  if (!freezeable || IsShmOpenSecure()) {
    return shm_open(name, oflag, mode);
  } else {
    return FakeShmOpen(name, oflag, mode);
  }
}

static int SafeShmUnlink(bool freezeable, const char* name) {
  if (!freezeable || IsShmOpenSecure()) {
    return shm_unlink(name);
  } else {
    return FakeShmUnlink(name);
  }
}

#elif !defined(ANDROID)
static int SafeShmOpen(bool freezeable, const char* name, int oflag, int mode) {
  return shm_open(name, oflag, mode);
}

static int SafeShmUnlink(bool freezeable, const char* name) {
  return shm_unlink(name);
}
#endif

// static
bool SharedMemory::AppendPosixShmPrefix(std::string* str, pid_t pid) {
#if defined(ANDROID)
  return false;
#else
  *str += '/';
#  ifdef OS_LINUX
  // The Snap package environment doesn't provide a private /dev/shm
  // (it's used for communication with services like PulseAudio);
  // instead AppArmor is used to restrict access to it.  Anything with
  // this prefix is allowed:
  static const char* const kSnap = [] {
    auto instanceName = PR_GetEnv("SNAP_INSTANCE_NAME");
    if (instanceName != nullptr) {
      return instanceName;
    }
    // Compatibility for snapd <= 2.35:
    return PR_GetEnv("SNAP_NAME");
  }();

  if (kSnap) {
    StringAppendF(str, "snap.%s.", kSnap);
  }
#  endif  // OS_LINUX
  // Hopefully the "implementation defined" name length limit is long
  // enough for this.
  StringAppendF(str, "org.mozilla.ipc.%d.", static_cast<int>(pid));
  return true;
#endif    // !ANDROID
}

bool SharedMemory::CreateInternal(size_t size, bool freezeable) {
  read_only_ = false;

  DCHECK(size > 0);
  DCHECK(mapped_file_ == -1);
  DCHECK(frozen_file_ == -1);

  mozilla::UniqueFileHandle fd;
  mozilla::UniqueFileHandle frozen_fd;
  bool needs_truncate = true;

#ifdef ANDROID
  // Android has its own shared memory facility:
  fd.reset(open("/" ASHMEM_NAME_DEF, O_RDWR, 0600));
  if (!fd) {
    CHROMIUM_LOG(WARNING) << "failed to open shm: " << strerror(errno);
    return false;
  }
  if (ioctl(fd.get(), ASHMEM_SET_SIZE, size) != 0) {
    CHROMIUM_LOG(WARNING) << "failed to set shm size: " << strerror(errno);
    return false;
  }
  needs_truncate = false;
#else
  // Generic Unix: shm_open + shm_unlink
  do {
    // The names don't need to be unique, but it saves time if they
    // usually are.
    static mozilla::Atomic<size_t> sNameCounter;
    std::string name;
    CHECK(AppendPosixShmPrefix(&name, getpid()));
    StringAppendF(&name, "%zu", sNameCounter++);
    // O_EXCL means the names being predictable shouldn't be a problem.
    fd.reset(HANDLE_EINTR(SafeShmOpen(freezeable, name.c_str(),
                                      O_RDWR | O_CREAT | O_EXCL, 0600)));
    if (fd) {
      if (freezeable) {
        frozen_fd.reset(HANDLE_EINTR(
            SafeShmOpen(freezeable, name.c_str(), O_RDONLY, 0400)));
        if (!frozen_fd) {
          int open_err = errno;
          SafeShmUnlink(freezeable, name.c_str());
          DLOG(FATAL) << "failed to re-open freezeable shm: "
                      << strerror(open_err);
          return false;
        }
      }
      if (SafeShmUnlink(freezeable, name.c_str()) != 0) {
        // This shouldn't happen, but if it does: assume the file is
        // in fact leaked, and bail out now while it's still 0-length.
        DLOG(FATAL) << "failed to unlink shm: " << strerror(errno);
        return false;
      }
    }
  } while (!fd && errno == EEXIST);
#endif

  if (!fd) {
    CHROMIUM_LOG(WARNING) << "failed to open shm: " << strerror(errno);
    return false;
  }

  if (needs_truncate) {
    if (HANDLE_EINTR(ftruncate(fd.get(), static_cast<off_t>(size))) != 0) {
      CHROMIUM_LOG(WARNING) << "failed to set shm size: " << strerror(errno);
      return false;
    }
  }

  mapped_file_ = fd.release();
  frozen_file_ = frozen_fd.release();
  max_size_ = size;
  freezeable_ = freezeable;
  return true;
}

bool SharedMemory::Freeze() {
  DCHECK(!read_only_);
  CHECK(freezeable_);
  Unmap();

#ifdef ANDROID
  if (ioctl(mapped_file_, ASHMEM_SET_PROT_MASK, PROT_READ) != 0) {
    CHROMIUM_LOG(WARNING) << "failed to freeze shm: " << strerror(errno);
    return false;
  }
#else
  DCHECK(frozen_file_ >= 0);
  DCHECK(mapped_file_ >= 0);
  close(mapped_file_);
  mapped_file_ = frozen_file_;
  frozen_file_ = -1;
#endif

  read_only_ = true;
  freezeable_ = false;
  return true;
}

bool SharedMemory::Map(size_t bytes, void* fixed_address) {
  if (mapped_file_ == -1) return false;
  DCHECK(!memory_);

  // Don't use MAP_FIXED when a fixed_address was specified, since that can
  // replace pages that are alread mapped at that address.
  void* mem =
      mmap(fixed_address, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
           MAP_SHARED, mapped_file_, 0);

  if (mem == MAP_FAILED) {
    CHROMIUM_LOG(WARNING) << "Call to mmap failed: " << strerror(errno);
    return false;
  }

  if (fixed_address && mem != fixed_address) {
    bool munmap_succeeded = munmap(mem, bytes) == 0;
    DCHECK(munmap_succeeded) << "Call to munmap failed, errno=" << errno;
    return false;
  }

  memory_ = mem;
  mapped_size_ = bytes;
  return true;
}

bool SharedMemory::Unmap() {
  if (memory_ == NULL) return false;

  munmap(memory_, mapped_size_);
  memory_ = NULL;
  mapped_size_ = 0;
  return true;
}

void* SharedMemory::FindFreeAddressSpace(size_t size) {
  void* memory =
      mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  munmap(memory, size);
  return memory != MAP_FAILED ? memory : NULL;
}

bool SharedMemory::ShareToProcessCommon(ProcessId processId,
                                        SharedMemoryHandle* new_handle,
                                        bool close_self) {
  freezeable_ = false;
  const int new_fd = dup(mapped_file_);
  DCHECK(new_fd >= -1);
  new_handle->fd = new_fd;
  new_handle->auto_close = true;

  if (close_self) Close();

  return true;
}

void SharedMemory::Close(bool unmap_view) {
  if (unmap_view) {
    Unmap();
  }

  if (mapped_file_ >= 0) {
    close(mapped_file_);
    mapped_file_ = -1;
  }
  if (frozen_file_ >= 0) {
    CHROMIUM_LOG(WARNING) << "freezeable shared memory was never frozen";
    close(frozen_file_);
    frozen_file_ = -1;
  }
}

mozilla::UniqueFileHandle SharedMemory::TakeHandle() {
  mozilla::UniqueFileHandle fh(mapped_file_);
  mapped_file_ = -1;
  // Now that the main fd is removed, reset everything else: close the
  // frozen fd if present and unmap the memory if mapped.
  Close();
  return fh;
}

}  // namespace base