chromium/base/stack_canary_linux.cc

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "base/stack_canary_linux.h"

#include <dlfcn.h>
#include <stdint.h>
#include <sys/mman.h>

#include "base/bits.h"
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/page_size.h"
#include "base/rand_util.h"
#include "build/build_config.h"

namespace base {

#if defined(LIBC_GLIBC)

#if defined(ARCH_CPU_ARM_FAMILY)
// On ARM, Glibc uses a global variable (exported) called __stack_chk_guard.
extern "C" {
extern uintptr_t __stack_chk_guard;
}
#endif  // defined(ARCH_CPU_ARM_FAMILY)

#if !defined(NDEBUG)
// In debug builds, if we detect stack smashing in old stack frames after
// changing the canary, it's nice to let someone know that it's because the
// canary changed and they should prevent their function from using stack
// canaries.
static bool g_emit_debug_message =;

extern "C" {
GLibcStackChkFailFunction;

// This overrides glibc's version of __stack_chk_fail(), which is called when
// the canary doesn't match.
__attribute__((visibility("default"), noinline, noreturn)) void
__stack_chk_fail() {}
}
#endif  // !defined(NDEBUG)

void NO_STACK_PROTECTOR ResetStackCanaryIfPossible() {}

void SetStackSmashingEmitsDebugMessage() {}

#else  // defined(LIBC_GLIBC)

// We don't know how to reset the canary if not compiling for glibc.
void ResetStackCanaryIfPossible() {}

void SetStackSmashingEmitsDebugMessage() {}

#endif  // defined(LIBC_GLIBC)
}  // namespace base