address_is_readable.cc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // base::AddressIsReadable() probes an address to see whether it is readable,
  15. // without faulting.
  16. #include "absl/debugging/internal/address_is_readable.h"
  17. #if !defined(__linux__) || defined(__ANDROID__)
  18. namespace absl {
  19. ABSL_NAMESPACE_BEGIN
  20. namespace debugging_internal {
  21. // On platforms other than Linux, just return true.
  22. bool AddressIsReadable(const void* /* addr */) { return true; }
  23. } // namespace debugging_internal
  24. ABSL_NAMESPACE_END
  25. } // namespace absl
  26. #else // __linux__ && !__ANDROID__
  27. #include <stdint.h>
  28. #include <syscall.h>
  29. #include <unistd.h>
  30. #include "absl/base/internal/errno_saver.h"
  31. #include "absl/base/internal/raw_logging.h"
  32. namespace absl {
  33. ABSL_NAMESPACE_BEGIN
  34. namespace debugging_internal {
  35. // NOTE: be extra careful about adding any interposable function calls here
  36. // (such as open(), read(), etc.). These symbols may be interposed and will get
  37. // invoked in contexts they don't expect.
  38. //
  39. // NOTE: any new system calls here may also require sandbox reconfiguration.
  40. //
  41. bool AddressIsReadable(const void *addr) {
  42. // rt_sigprocmask below checks 8 contiguous bytes. If addr resides in the
  43. // last 7 bytes of a page (unaligned), rt_sigprocmask would additionally
  44. // check the readability of the next page, which is not desired. Align
  45. // address on 8-byte boundary to check only the current page.
  46. const uintptr_t u_addr = reinterpret_cast<uintptr_t>(addr) & ~uintptr_t{7};
  47. addr = reinterpret_cast<const void *>(u_addr);
  48. // rt_sigprocmask below will succeed for this input.
  49. if (addr == nullptr) return false;
  50. absl::base_internal::ErrnoSaver errno_saver;
  51. // Here we probe with some syscall which
  52. // - accepts an 8-byte region of user memory as input
  53. // - tests for EFAULT before other validation
  54. // - has no problematic side-effects
  55. //
  56. // rt_sigprocmask(2) works for this. It copies sizeof(kernel_sigset_t)==8
  57. // bytes from the address into the kernel memory before any validation.
  58. //
  59. // The call can never succeed, since the `how` parameter is not one of
  60. // SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK.
  61. //
  62. // This strategy depends on Linux implementation details,
  63. // so we rely on the test to alert us if it stops working.
  64. //
  65. // Some discarded past approaches:
  66. // - msync() doesn't reject PROT_NONE regions
  67. // - write() on /dev/null doesn't return EFAULT
  68. // - write() on a pipe requires creating it and draining the writes
  69. // - connect() works but is problematic for sandboxes and needs a valid
  70. // file descriptor
  71. //
  72. // This can never succeed (invalid first argument to sigprocmask).
  73. ABSL_RAW_CHECK(syscall(SYS_rt_sigprocmask, ~0, addr, nullptr,
  74. /*sizeof(kernel_sigset_t)*/ 8) == -1,
  75. "unexpected success");
  76. ABSL_RAW_CHECK(errno == EFAULT || errno == EINVAL, "unexpected errno");
  77. return errno != EFAULT;
  78. }
  79. } // namespace debugging_internal
  80. ABSL_NAMESPACE_END
  81. } // namespace absl
  82. #endif // __linux__ && !__ANDROID__