Movatterモバイル変換


[0]ホーム

URL:


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

[8]ページ先頭

©2009-2025 Movatter.jp