address_is_readable.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "y_absl/debugging/internal/address_is_readable.h"
  17. #if !defined(__linux__) || defined(__ANDROID__)
  18. namespace y_absl {
  19. Y_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. Y_ABSL_NAMESPACE_END
  25. } // namespace y_absl
  26. #else // __linux__ && !__ANDROID__
  27. #include <stdint.h>
  28. #include <syscall.h>
  29. #include <unistd.h>
  30. #include "y_absl/base/internal/errno_saver.h"
  31. #include "y_absl/base/internal/raw_logging.h"
  32. namespace y_absl {
  33. Y_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. // Align address on 8-byte boundary. On aarch64, checking last
  43. // byte before inaccessible page returned unexpected EFAULT.
  44. const uintptr_t u_addr = reinterpret_cast<uintptr_t>(addr) & ~uintptr_t{7};
  45. addr = reinterpret_cast<const void *>(u_addr);
  46. // rt_sigprocmask below will succeed for this input.
  47. if (addr == nullptr) return false;
  48. y_absl::base_internal::ErrnoSaver errno_saver;
  49. // Here we probe with some syscall which
  50. // - accepts an 8-byte region of user memory as input
  51. // - tests for EFAULT before other validation
  52. // - has no problematic side-effects
  53. //
  54. // rt_sigprocmask(2) works for this. It copies sizeof(kernel_sigset_t)==8
  55. // bytes from the address into the kernel memory before any validation.
  56. //
  57. // The call can never succeed, since the `how` parameter is not one of
  58. // SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK.
  59. //
  60. // This strategy depends on Linux implementation details,
  61. // so we rely on the test to alert us if it stops working.
  62. //
  63. // Some discarded past approaches:
  64. // - msync() doesn't reject PROT_NONE regions
  65. // - write() on /dev/null doesn't return EFAULT
  66. // - write() on a pipe requires creating it and draining the writes
  67. // - connect() works but is problematic for sandboxes and needs a valid
  68. // file descriptor
  69. //
  70. // This can never succeed (invalid first argument to sigprocmask).
  71. Y_ABSL_RAW_CHECK(syscall(SYS_rt_sigprocmask, ~0, addr, nullptr,
  72. /*sizeof(kernel_sigset_t)*/ 8) == -1,
  73. "unexpected success");
  74. Y_ABSL_RAW_CHECK(errno == EFAULT || errno == EINVAL, "unexpected errno");
  75. return errno != EFAULT;
  76. }
  77. } // namespace debugging_internal
  78. Y_ABSL_NAMESPACE_END
  79. } // namespace y_absl
  80. #endif // __linux__ && !__ANDROID__