stack_canary_linux.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2021 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/stack_canary_linux.h"
  5. #include <dlfcn.h>
  6. #include <stdint.h>
  7. #include <sys/mman.h>
  8. #include "base/bits.h"
  9. #include "base/check_op.h"
  10. #include "base/compiler_specific.h"
  11. #include "base/logging.h"
  12. #include "base/memory/page_size.h"
  13. #include "base/rand_util.h"
  14. #include "build/build_config.h"
  15. namespace base {
  16. #if defined(LIBC_GLIBC)
  17. #if defined(ARCH_CPU_ARM_FAMILY)
  18. // On ARM, Glibc uses a global variable (exported) called __stack_chk_guard.
  19. extern "C" {
  20. extern uintptr_t __stack_chk_guard;
  21. }
  22. #endif // defined(ARCH_CPU_ARM_FAMILY)
  23. #if !defined(NDEBUG)
  24. // In debug builds, if we detect stack smashing in old stack frames after
  25. // changing the canary, it's nice to let someone know that it's because the
  26. // canary changed and they should prevent their function from using stack
  27. // canaries.
  28. static bool g_emit_debug_message = false;
  29. extern "C" {
  30. typedef __attribute__((noreturn)) void(GLibcStackChkFailFunction)();
  31. // This overrides glibc's version of __stack_chk_fail(), which is called when
  32. // the canary doesn't match.
  33. __attribute__((visibility("default"), noinline, noreturn)) void
  34. __stack_chk_fail() {
  35. if (g_emit_debug_message) {
  36. RAW_LOG(
  37. FATAL,
  38. "Stack smashing detected. The canary was changed during runtime "
  39. "(see crbug.com/1206626). You may need to mark your function with "
  40. "the no_stack_protector attribute, or just exit() before stack "
  41. "smashing occurs. You can also disable this canary-changing feature "
  42. "by adding --change-stack-guard-on-fork=disable to the command line.");
  43. }
  44. // Call the real __stack_chk_fail().
  45. // Note that dlsym may not be safe to perform since this is called during
  46. // corruption, but this code purposely only runs in debug builds and in the
  47. // normal case might provide better debug information.
  48. GLibcStackChkFailFunction* glibc_stack_chk_fail =
  49. reinterpret_cast<GLibcStackChkFailFunction*>(
  50. dlsym(RTLD_NEXT, "__stack_chk_fail"));
  51. (*glibc_stack_chk_fail)();
  52. }
  53. }
  54. #endif // !defined(NDEBUG)
  55. void NO_STACK_PROTECTOR ResetStackCanaryIfPossible() {
  56. uintptr_t canary;
  57. base::RandBytes(&canary, sizeof(canary));
  58. // First byte should be the null byte for string functions.
  59. canary &= ~static_cast<uintptr_t>(0xff);
  60. // The x86/x64 offsets should work for musl too.
  61. #if defined(ARCH_CPU_X86_64)
  62. asm volatile("movq %q0,%%fs:%P1" : : "er"(canary), "i"(0x28));
  63. #elif defined(ARCH_CPU_X86)
  64. asm volatile("movl %0,%%gs:%P1" : : "ir"(canary), "i"(0x14));
  65. #elif defined(ARCH_CPU_ARM_FAMILY)
  66. // ARM's stack canary is held on a relro page. So, we'll need to make the page
  67. // writable, change the stack canary, and then make the page ro again.
  68. // We want to be single-threaded when changing page permissions, since it's
  69. // reasonable for other threads to assume that page permissions for global
  70. // variables don't change.
  71. size_t page_size = base::GetPageSize();
  72. uintptr_t __stack_chk_guard_page = base::bits::AlignDown(
  73. reinterpret_cast<uintptr_t>(&__stack_chk_guard), page_size);
  74. PCHECK(0 == mprotect(reinterpret_cast<void*>(__stack_chk_guard_page),
  75. page_size, PROT_READ | PROT_WRITE));
  76. __stack_chk_guard = canary;
  77. PCHECK(0 == mprotect(reinterpret_cast<void*>(__stack_chk_guard_page),
  78. page_size, PROT_READ));
  79. #endif
  80. }
  81. void SetStackSmashingEmitsDebugMessage() {
  82. #if !defined(NDEBUG)
  83. g_emit_debug_message = true;
  84. #endif // !defined(NDEBUG)
  85. }
  86. #else // defined(LIBC_GLIBC)
  87. // We don't know how to reset the canary if not compiling for glibc.
  88. void ResetStackCanaryIfPossible() {}
  89. void SetStackSmashingEmitsDebugMessage() {}
  90. #endif // defined(LIBC_GLIBC)
  91. } // namespace base