shared_memory_security_policy.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/memory/shared_memory_security_policy.h"
  5. #include <algorithm>
  6. #include <atomic>
  7. #include <limits>
  8. #include "base/bits.h"
  9. #include "base/memory/page_size.h"
  10. #include "base/numerics/checked_math.h"
  11. #include "build/build_config.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace base {
  14. namespace {
  15. // Note: pointers are 32 bits on all architectures in NaCl. See
  16. // https://bugs.chromium.org/p/nativeclient/issues/detail?id=1162
  17. #if defined(ARCH_CPU_32_BITS) || BUILDFLAG(IS_NACL)
  18. // No effective limit on 32-bit, since there simply isn't enough address space
  19. // for ASLR to be particularly effective.
  20. constexpr size_t kTotalMappedSizeLimit = std::numeric_limits<size_t>::max();
  21. #elif defined(ARCH_CPU_64_BITS)
  22. // 32 GB of mappings ought to be enough for anybody.
  23. constexpr size_t kTotalMappedSizeLimit = 32ULL * 1024 * 1024 * 1024;
  24. #endif
  25. static std::atomic_size_t total_mapped_size_;
  26. absl::optional<size_t> AlignWithPageSize(size_t size) {
  27. #if BUILDFLAG(IS_WIN)
  28. // TODO(crbug.com/210609): Matches alignment requirements defined in
  29. // platform_shared_memory_region_win.cc:PlatformSharedMemoryRegion::Create.
  30. // Remove this when NaCl is gone.
  31. static const size_t kSectionSize = 65536;
  32. const size_t page_size = std::max(kSectionSize, GetPageSize());
  33. #else
  34. const size_t page_size = GetPageSize();
  35. #endif // BUILDFLAG(IS_WIN)
  36. size_t rounded_size = bits::AlignUp(size, page_size);
  37. // Fail on overflow.
  38. if (rounded_size < size)
  39. return absl::nullopt;
  40. return rounded_size;
  41. }
  42. } // namespace
  43. // static
  44. bool SharedMemorySecurityPolicy::AcquireReservationForMapping(size_t size) {
  45. size_t previous_mapped_size =
  46. total_mapped_size_.load(std::memory_order_relaxed);
  47. size_t total_mapped_size;
  48. absl::optional<size_t> page_aligned_size = AlignWithPageSize(size);
  49. if (!page_aligned_size)
  50. return false;
  51. // Relaxed memory ordering is all that's needed since all atomicity is all
  52. // that's required. If the value is stale, compare_exchange_weak() will fail
  53. // and the loop will retry the operation with an updated total mapped size.
  54. do {
  55. if (!CheckAdd(previous_mapped_size, *page_aligned_size)
  56. .AssignIfValid(&total_mapped_size)) {
  57. return false;
  58. }
  59. if (total_mapped_size >= kTotalMappedSizeLimit)
  60. return false;
  61. } while (!total_mapped_size_.compare_exchange_weak(
  62. previous_mapped_size, total_mapped_size, std::memory_order_relaxed,
  63. std::memory_order_relaxed));
  64. return true;
  65. }
  66. // static
  67. void SharedMemorySecurityPolicy::ReleaseReservationForMapping(size_t size) {
  68. // Note #1: relaxed memory ordering is sufficient since atomicity is all
  69. // that's required.
  70. // Note #2: |size| should never overflow when aligned to page size, since
  71. // this should only be called if AcquireReservationForMapping() returned true.
  72. absl::optional<size_t> page_aligned_size = AlignWithPageSize(size);
  73. total_mapped_size_.fetch_sub(*page_aligned_size, std::memory_order_relaxed);
  74. }
  75. } // namespace base