mirror of
https://github.com/raspberrypi/linux.git
synced 2025-12-19 16:24:21 +00:00
Mark read-only data actually read-only (simple mprotect), and to be able to test it also implement _nofault accesses. This works by setting up a new "segv_continue" pointer in current, and then when we hit a segfault we change the signal return context so that we continue at that address. The code using this sets it up so that it jumps to a label and then aborts the access that way, returning -EFAULT. It's possible to optimize the ___backtrack_faulted() thing by using asm goto (compiler version dependent) and/or gcc's (not sure if clang has it) &&label extension, but at least in one attempt I made the && caused the compiler to not load -EFAULT into the register in case of jumping to the &&label from the fault handler. So leave it like this for now. Signed-off-by: Johannes Berg <johannes.berg@intel.com> Co-developed-by: Benjamin Berg <benjamin.berg@intel.com> Signed-off-by: Benjamin Berg <benjamin.berg@intel.com> Link: https://patch.msgid.link/20250210160926.420133-2-benjamin@sipsolutions.net Signed-off-by: Johannes Berg <johannes.berg@intel.com>
45 lines
1.3 KiB
C
45 lines
1.3 KiB
C
/*
|
|
* Copyright (C) 2004 Fujitsu Siemens Computers GmbH
|
|
* Author: Bodo Stroesser <bstroesser@fujitsu-siemens.com>
|
|
* Licensed under the GPL
|
|
*/
|
|
|
|
#ifndef __FAULTINFO_X86_64_H
|
|
#define __FAULTINFO_X86_64_H
|
|
|
|
/* this structure contains the full arch-specific faultinfo
|
|
* from the traps.
|
|
* On i386, ptrace_faultinfo unfortunately doesn't provide
|
|
* all the info, since trap_no is missing.
|
|
* All common elements are defined at the same position in
|
|
* both structures, thus making it easy to copy the
|
|
* contents without knowledge about the structure elements.
|
|
*/
|
|
struct faultinfo {
|
|
int error_code; /* in ptrace_faultinfo misleadingly called is_write */
|
|
unsigned long cr2; /* in ptrace_faultinfo called addr */
|
|
int trap_no; /* missing in ptrace_faultinfo */
|
|
};
|
|
|
|
#define FAULT_WRITE(fi) ((fi).error_code & 2)
|
|
#define FAULT_ADDRESS(fi) ((fi).cr2)
|
|
|
|
/* This is Page Fault */
|
|
#define SEGV_IS_FIXABLE(fi) ((fi)->trap_no == 14)
|
|
|
|
#define PTRACE_FULL_FAULTINFO 1
|
|
|
|
#define ___backtrack_faulted(_faulted) \
|
|
asm volatile ( \
|
|
"mov $0, %0\n" \
|
|
"movq $__get_kernel_nofault_faulted_%=,%1\n" \
|
|
"jmp _end_%=\n" \
|
|
"__get_kernel_nofault_faulted_%=:\n" \
|
|
"mov $1, %0;" \
|
|
"_end_%=:" \
|
|
: "=r" (_faulted), \
|
|
"=m" (current->thread.segv_continue) :: \
|
|
)
|
|
|
|
#endif
|