Add Safefetch double-fetch protection
Subsystems (13)
cover Add Safefetch double-fetch protection
SafeFetch is a patch that provides a caching mechanism to user syscalls to prevent time-of-check to time-of-use bugs in the kernel. SafeFetch was originally created by Victor Duta, Mitchel Josephus Aloserij, and Cristiano Giuffrida. Their original research publication and appendix can be found here [1]. Their patch for v5.11 can be found here [2]. The testing scripts can be found here [3].
This patchset is not currently intended for production but rather for use in testing.
I have been forward porting this patchset for about one year now, from v5.11 to v6.16-rc6. I branched and merged in my own fork the kernel to keep track of my progress [4].
I have been able to test the patchset with the same CVE used by the original paper authors for each new version I ported the patchset to confirm its functionality. I have not tested the performance of the patchset in different versions in order to verify the original claims of minimal overheads. I have done limited testing with different compiler versions to confirm it can compile across clang and gcc versions. I have not tested recommended kernel configuration variations.
I would love some help testing this patchset more rigorously in addition to cleaning up any checkpatch warnings or errors that I was unsure of. Specifically I was hesitant to try to fix macro warnings as some seemed wrong to me in my limited knowledge.
I also need help fixing up or formatting the dmesg output to be more consistent with other messages.
[1] https://www.usenix.org/conference/usenixsecurity24/presentation/duta
[2] https://github.com/vusec/safefetch
[3] https://github.com/vusec/safefetch-ae
[4] https://github.com/gatlinnewhouse/linux
Gatlin Newhouse (17):
Add SafeFetch double-fetch protection to the kernel
x86: syscall: support caching in do_syscall_64()
x86: asm: support caching in do_get_user_call()
sched: add protection to task_struct
uaccess: add non-caching copy_from_user functions
futex: add get_user_no_dfcache() functions
gup: add non-caching get_user call to fault_in_readable()
init: add caching startup and initialization to start_kernel()
exit: add destruction of SafeFetch caches and debug info to do_exit()
iov_iter: add SafeFetch pinning call to copy_from_user_iter()
kernel: add SafeFetch cache handling to dup_task_struct()
bug: add SafeFetch statistics tracking to __report_bug() calls
softirq: add SafeFetch statistics to irq_enter_rc() and irq_exit()
makefile: add SafeFetch support to makefiles
kconfig: debug: add SafeFetch to debug kconfig
x86: enable SafeFetch on x86_64 builds
vfs: ioctl: add logging to ioctl_file_dedupe_range() for testing
Makefile | 3 +- arch/x86/Kconfig | 5 +- arch/x86/entry/syscall_64.c | 76 + arch/x86/include/asm/uaccess.h | 211 ++- arch/x86/include/asm/uaccess_64.h | 54 + fs/ioctl.c | 6 + include/linux/dfcache_measuring.h | 72 + include/linux/mem_range.h | 302 ++++ include/linux/region_allocator.h | 188 +++ include/linux/safefetch.h | 222 +++ include/linux/safefetch_static_keys.h | 22 + include/linux/sched.h | 11 + include/linux/uaccess.h | 30 + init/Kconfig | 2 +- init/init_task.c | 11 + init/main.c | 7 + kernel/exit.c | 16 + kernel/fork.c | 17 + kernel/futex/core.c | 5 + kernel/futex/futex.h | 4 + kernel/futex/pi.c | 5 + kernel/futex/requeue.c | 5 +- kernel/futex/waitwake.c | 4 + kernel/softirq.c | 8 + lib/Kconfig.debug | 1 + lib/Kconfig.safefetch | 36 + lib/bug.c | 10 + lib/iov_iter.c | 12 + mm/Makefile | 1 + mm/gup.c | 4 + mm/safefetch/Makefile | 11 + mm/safefetch/mem_range.c | 1882 +++++++++++++++++++++++++ mm/safefetch/page_cache.c | 129 ++ mm/safefetch/page_cache.h | 141 ++ mm/safefetch/region_allocator.c | 584 ++++++++ mm/safefetch/safefetch.c | 487 +++++++ mm/safefetch/safefetch_debug.c | 110 ++ mm/safefetch/safefetch_debug.h | 86 ++ mm/safefetch/safefetch_static_keys.c | 299 ++++ scripts/Makefile.lib | 4 + scripts/Makefile.safefetch | 10 + 41 files changed, 5077 insertions(+), 16 deletions(-) create mode 100644 include/linux/dfcache_measuring.h create mode 100644 include/linux/mem_range.h create mode 100644 include/linux/region_allocator.h create mode 100644 include/linux/safefetch.h create mode 100644 include/linux/safefetch_static_keys.h create mode 100644 lib/Kconfig.safefetch create mode 100644 mm/safefetch/Makefile create mode 100644 mm/safefetch/mem_range.c create mode 100644 mm/safefetch/page_cache.c create mode 100644 mm/safefetch/page_cache.h create mode 100644 mm/safefetch/region_allocator.c create mode 100644 mm/safefetch/safefetch.c create mode 100644 mm/safefetch/safefetch_debug.c create mode 100644 mm/safefetch/safefetch_debug.h create mode 100644 mm/safefetch/safefetch_static_keys.c create mode 100644 scripts/Makefile.safefetch
Signature · 1 line
2.25.1
RFC 1/17 Add SafeFetch double-fetch protection to the kernel
SafeFetch[1] protects the kernel from double-fetch vulnerabilities. Double-fetch bugs enable time-of-check to time-of-use (TOCTTOU) attack vectors. Scott Bauer found and patched a doube-fetch in dedupe ioctl[2] of which the proof of concept (POC) was used to test SafeFetch[3][4][5], see also the appendix PDF in [1].
SafeFetch accomplishes this protection by caching user data in syscall specific caches to replay them when fetched again within the same syscall [1].
SafeFetch is not currently intended for production use but rather for finding bugs. Although the original authors presented enabling SafeFetch had "marginal memory overheads and geometric performance overheads consistently below 5% across various OS benchmarks" [1].
SafeFetch was originally created by Victor Duta, Mitchel Josephus Alonserij, and Cristiano Giuffrida.
I have forward ported, and tested it with Scott Bauer's POC [4], from v5.11 to v6.16-rc5. I have not yet tested it across various performance suites to verify the persistence of the overhead claims from the paper in the forward porting process. I have also not yet tested it across all recommended configuration variants, or with some compiler versions.
[1] https://www.usenix.org/conference/usenixsecurity24/presentation/duta
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=10eec60ce79187686e052092e5383c99b4420a20
[3] https://www.openwall.com/lists/oss-security/2016/07/31/6
[4] https://github.com/wpengfei/CVE-2016-6516-exploit/tree/master/Scott%20Bauer
[5] https://github.com/vusec/safefetch-ae/
--- include/linux/dfcache_measuring.h | 72 + include/linux/mem_range.h | 302 ++++ include/linux/region_allocator.h | 188 +++ include/linux/safefetch.h | 222 +++ include/linux/safefetch_static_keys.h | 22 + lib/Kconfig.safefetch | 36 + mm/safefetch/Makefile | 11 + mm/safefetch/mem_range.c | 1882 +++++++++++++++++++++++++ mm/safefetch/page_cache.c | 129 ++ mm/safefetch/page_cache.h | 141 ++ mm/safefetch/region_allocator.c | 584 ++++++++ mm/safefetch/safefetch.c | 487 +++++++ mm/safefetch/safefetch_debug.c | 110 ++ mm/safefetch/safefetch_debug.h | 86 ++ mm/safefetch/safefetch_static_keys.c | 299 ++++ scripts/Makefile.safefetch | 10 + 16 files changed, 4581 insertions(+) create mode 100644 include/linux/dfcache_measuring.h create mode 100644 include/linux/mem_range.h create mode 100644 include/linux/region_allocator.h create mode 100644 include/linux/safefetch.h create mode 100644 include/linux/safefetch_static_keys.h create mode 100644 lib/Kconfig.safefetch create mode 100644 mm/safefetch/Makefile create mode 100644 mm/safefetch/mem_range.c create mode 100644 mm/safefetch/page_cache.c create mode 100644 mm/safefetch/page_cache.h create mode 100644 mm/safefetch/region_allocator.c create mode 100644 mm/safefetch/safefetch.c create mode 100644 mm/safefetch/safefetch_debug.c create mode 100644 mm/safefetch/safefetch_debug.h create mode 100644 mm/safefetch/safefetch_static_keys.c create mode 100644 scripts/Makefile.safefetch
include/linux/dfcache_measuring.h new file +72 -0
@@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* source for TSC measurement code: +* https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf +*/ + +#define MEASURE_BEFORE (cycles_high, cycles_low) \ + asm volatile( \ + "CPUID\n\t" \ + "RDTSC\n\t" \ + "mov %%edx, %0\n\t" \ + "mov %%eax, %1\n\t" \ + : "=r" (cycles_high), "=r" (cycles_low) \ + :: "%rax", "%rbx", "%rcx", "%rdx"); + +#define MEASURE_AFTER (cycles_high, cycles_low) \ + asm volatile( \ + "RDTSCP\n\t" \ + "mov %%edx, %0\n\t" \ + "mov %%eax, %1\n\t" \ + "CPUID\n\t" \ + : "=r" (cycles_high), "=r" (cycles_low) \ + :: "%rax", "%rbx", "%rcx", "%rdx"); + +#define MAKESTRING2 (x) #x +#define MAKESTRING (x) MAKESTRING2(x) + +static int64_t make_int64 (uint32_t high,uint32_t low) +{ +return (((int64_t ) high) <<32 ) | (int64_t ) low; +} + + + +#define MEASURE_FUNC_AND_COUNT (code_to_measure, out_buffer, index) { \ + uint32_t cycles_low_before, cycles_high_before; \ + uint32_t cycles_low_after, cycles_high_after; \ + if (out_buffer) { \ + MEASURE_BEFORE(cycles_high_before, cycles_low_before); \ + do { \ + code_to_measure \ + } while (0); \ + MEASURE_AFTER(cycles_high_after, cycles_low_after); \ + \ + if (index < SAFEFETCH_MEASURE_MAX) { \ + out_buffer[index++] = make_int64(cycles_high_after, cycles_low_after) \ + - make_int64(cycles_high_before, cycles_low_before) - rdmsr_ovr; \ + } \ + } else { \ + code_to_measure \ + } \ +} + + + +#define MEASURE_FUNC (code_to_measure, out_buffer, index) { \ + uint32_t cycles_low_before, cycles_high_before; \ + uint32_t cycles_low_after, cycles_high_after; \ + if (out_buffer) { \ + MEASURE_BEFORE(cycles_high_before, cycles_low_before); \ + do { \ + code_to_measure \ + } while (0); \ + MEASURE_AFTER(cycles_high_after, cycles_low_after); \ + \ + if (index < SAFEFETCH_MEASURE_MAX) { \ + out_buffer[index] = make_int64(cycles_high_after, cycles_low_after) \ + - make_int64(cycles_high_before, cycles_low_before) - rdmsr_ovr; \ + } \ + } else { \ + code_to_measure \ + } \ +}
include/linux/mem_range.h new file +302 -0
302 lines not on this page · raw message
include/linux/region_allocator.h new file +188 -0
188 lines not on this page · raw message
include/linux/safefetch.h new file +222 -0
222 lines not on this page · raw message
include/linux/safefetch_static_keys.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __SAFEFETCH_STATIC_KEYS_H__ +#define __SAFEFETCH_STATIC_KEYS_H__ + +#ifdef SAFEFETCH_STATIC_KEYS +DECLARE_STATIC_KEY_FALSE (safefetch_copy_from_user_key); +DECLARE_STATIC_KEY_FALSE (safefetch_hooks_key); +DECLARE_STATIC_KEY_FALSE (safefetch_adaptive_key); +DECLARE_STATIC_KEY_FALSE (safefetch_rbtree_key); + +#define IF_SAFEFETCH_STATIC_BRANCH_LIKELY_WRAPPER (key) if (static_branch_likely(&key)) +#define IF_SAFEFETCH_STATIC_BRANCH_UNLIKELY_WRAPPER (key) if (static_branch_unlikely(&key)) + +void init_safefetch_skey_layer (void ); + +#else /* SAFEFETCH_STATIC_KEYS */ + +#define IF_SAFEFETCH_STATIC_BRANCH_LIKELY_WRAPPER (key) +#define IF_SAFEFETCH_STATIC_BRANCH_UNLIKELY_WRAPPER (key) +#endif + +#endif
lib/Kconfig.safefetch new file +36 -0
@@ -0,0 +1,36 @@ +config HAVE_ARCH_SAFEFETCH + bool + +if HAVE_ARCH_SAFEFETCH + +config SAFEFETCH + bool "Safefetch : double fetch monitoring for copy_from_user/get_user calls" + select SAFEFETCH_STATIC_KEYS + default n + help + SAFEFETCH is a strategy for eliminating double-fetch bugs by + caching user data in exchange for a small performance cost. + SAFEFETCH creates per-syscall caches to persist user data + and replay on subsequent calls. + +config SAFEFETCH_STATIC_KEYS + bool "Safefetch : static keys safefetch" + depends on SAFEFETCH + default y + help + Instrument SAFEFETCH protections through hooks which can be + disabled through static keys, hooks are nop instructions + otherwise. Enabling this option allows userspace to configure + the protection scheme and enable/disable protections. + +config SAFEFETCH_DEBUG + bool "Safefetch : double fetch debugging layer" + default n + help + Debugging messages for SAFEFETCH detected double fetches in the + kernel ring buffer. Used for testing the patchset. If + SAFEFETCH_DEBUG_COLLECT_VULNERABILITIES is enabled, then + report vulnerabilities. Other SAFEFETCH_DEBUG_* options will + enable additional reporting. + +endif
mm/safefetch/Makefile new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-y := page_cache.o mem_range.o region_allocator.o safefetch.o +obj-$(CONFIG_SAFEFETCH_DEBUG) += safefetch_debug.o +obj-$(CONFIG_SAFEFETCH_STATIC_KEYS) += safefetch_static_keys.o +CFLAGS_REMOVE_page_cache.o=-Werror +CFLAGS_REMOVE_mem_range.o=-Werror +CFLAGS_REMOVE_region_allocator.o=-Werror +CFLAGS_REMOVE_safefetch.o=-Werror +KASAN_SANITIZE := n +KCOV_INSTRUMENT := n +UBSAN_SANITIZE := n
mm/safefetch/mem_range.c new file +1882 -0
1,882 lines not on this page · raw message
mm/safefetch/page_cache.c new file +129 -0
129 lines not on this page · raw message
mm/safefetch/page_cache.h new file +141 -0
135 lines not on this page · raw message
mm/safefetch/region_allocator.c new file +584 -0
584 lines not on this page · raw message
mm/safefetch/safefetch.c new file +487 -0
487 lines not on this page · raw message
mm/safefetch/safefetch_debug.c new file +110 -0
110 lines not on this page · raw message
mm/safefetch/safefetch_debug.h new file +86 -0
86 lines not on this page · raw message
mm/safefetch/safefetch_static_keys.c new file +299 -0
299 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.25.1
Part of the diff is not shown here. View the raw message.
RFC 2/17 x86: syscall: support caching in do_syscall_64()
In reply to Gatlin NewhouseInclude safefetch and support caching strategy in syscalls to protect against time of check to time of use bugs.
--- arch/x86/entry/syscall_64.c | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+)
arch/x86/entry/syscall_64.c +76 -0
@@ -20,6 +20,30 @@#undef __SYSCALL_NORETURN#define __SYSCALL_NORETURN __SYSCALL +#ifdef CONFIG_SAFEFETCH +#include <linux/safefetch.h> +#include <linux/region_allocator.h> +#include <linux/mem_range.h> +#include <linux/safefetch_static_keys.h> +#ifdef SAFEFETCH_WHITELISTING +#warning "Using DFCACHER whitelisting" +static noinline void should_whitelist(unsigned long syscall_nr) +{ +switch (syscall_nr) { +case __NR_futex: +case __NR_execve: +case __NR_writev: +case __NR_pwritev2: +case __NR_pwrite64: +case __NR_write: + current->df_prot_struct_head.is_whitelisted =1 ; +return ; + } + current->df_prot_struct_head.is_whitelisted =0 ; +} +#endif +#endif +/* * The sys_call_table[] is no longer used for system calls, but * kernel/trace/trace_syscalls.c still wants to know the system
@@ -87,8 +111,46 @@ static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr) __visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr) {add_random_kstack_offset (); +// If interrupts using current execute prior to the next syscall +// then we will enter the syscall with the mem_range initialized +// we could chose to clean this info (shrink_region) or simply +// trust that the interrupt doesn't fetch something nasty and just +// operate the next syscall on the interrupt state (happens for +// sigaction calls mostly during IPI's that save the signal frame +// prior to executing a sigaction call). Or simply clear state +// on irq end (might slow down irqs so avoid this). +#if defined(CONFIG_SAFEFETCH ) +IF_SAFEFETCH_STATIC_BRANCH_UNLIKELY_WRAPPER (safefetch_hooks_key) { +if (unlikely (SAFEFETCH_MEM_RANGE_INIT_FLAG )) { +// An IPI probably sent us a signal and the signal +// enabled the defense in interrupt context. Reset +// dfcache interrupt state. +#ifndef SAFEFETCH_DEBUG +// If in debug mode, we actually reset the range in +// df_debug_syscall_entry. +SAFEFETCH_RESET_MEM_RANGE (); +#endif +shrink_region (DF_CUR_STORAGE_REGION_ALLOCATOR ); +shrink_region (DF_CUR_METADATA_REGION_ALLOCATOR ); + } + } +#endif + +#ifdef SAFEFETCH_MEASURE_DEFENSE +// We only use this for measuring so execute this without the static key +// else we get into nasty scenarios if we miss this initialization step. +df_init_measure_structs (current); +#endif nr =syscall_enter_from_user_mode (regs, nr); +#if defined(CONFIG_SAFEFETCH ) && defined(SAFEFETCH_WHITELISTING ) +should_whitelist (nr); +#endif +#if defined(CONFIG_SAFEFETCH ) && defined(SAFEFETCH_DEBUG ) +IF_SAFEFETCH_STATIC_BRANCH_UNLIKELY_WRAPPER (safefetch_hooks_key) { +df_debug_syscall_entry (nr, regs); + } +#endif instrumentation_begin ();if (!do_syscall_x64 (regs, nr) && !do_syscall_x32 (regs, nr) && nr !=-1 ) {
@@ -99,6 +161,20 @@ __visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr)instrumentation_end ();syscall_exit_to_user_mode (regs); +#ifdef CONFIG_SAFEFETCH +// Note, we might have rseq regions executing in syscall_exit_to_user_mode +// and irqs so delay resetting region after this. +IF_SAFEFETCH_STATIC_BRANCH_UNLIKELY_WRAPPER (safefetch_hooks_key) { +#ifdef SAFEFETCH_DEBUG +df_debug_syscall_exit (); +#endif +#ifdef SAFEFETCH_MEASURE_DEFENSE +df_destroy_measure_structs (); +#endif +reset_regions (); + } +#endif +/* * Check that the register state is valid for using SYSRET to exit * to userspace. Otherwise use the slower but fully capable IRET
Signature · 1 line
2.25.1
RFC 3/17 x86: asm: support caching in do_get_user_call()
In reply to Gatlin NewhouseAdds various caching functions with different sizes alongside a macro to select the smallest possible caching function to enable caching of user calls to protect against time-of-check to time-of-use bugs.
--- arch/x86/include/asm/uaccess.h | 211 ++++++++++++++++++++++++++++-- arch/x86/include/asm/uaccess_64.h | 54 ++++++++ 2 files changed, 254 insertions(+), 11 deletions(-)
arch/x86/include/asm/uaccess.h +200 -11
@@ -73,27 +73,215 @@ extern int __get_user_bad(void);* Clang/LLVM cares about the size of the register, but still wants * the base register for something that ends up being a pair. */ + +#ifdef CONFIG_SAFEFETCH +#include <linux/safefetch.h> +#include <linux/safefetch_static_keys.h> + +extern int df_get_user1 (unsigned long long user_src,unsigned char user_val, +unsigned long long kern_dst); +extern int df_get_user2 (unsigned long long user_src,unsigned short user_val, +unsigned long long kern_dst); +extern int df_get_user4 (unsigned long long user_src,unsigned int user_val, +unsigned long long kern_dst); +extern int df_get_user8 (unsigned long long user_src,unsigned long user_val, +unsigned long long kern_dst); +extern int df_get_useru8 (unsigned long long user_src,unsigned long user_val, +unsigned long long kern_dst); + +// This macro returns the smallest possible get_user function based on value x +#define __dfgetuserfunc (x) \ + __dfgetuserfuncfits(x, char, df_get_user1, \ + __dfgetuserfuncfits(x, short, df_get_user2, \ + __dfgetuserfuncfits(x, int, df_get_user4, \ + __dfgetuserfuncfits(x, long, df_get_user8, \ + df_get_useru8)))) + +// This macro will deduce the best double fetch get_user protection function, +// based on the register content +#define __dfgetuserfuncfits (x, type, func, not) \ + __builtin_choose_expr(sizeof(x) <= sizeof(type), func, not) + + +//#define GET_USER_CALL_CHECK(x) (likely(!x) && !IS_WHITELISTED(current)) +#define GET_USER_CALL_CHECK (x) likely(!x) + + +// fn = get_user function name template +// x = destination +// ptr = source +#define do_get_user_call (fn, x, ptr) \ +({ \ +/* __ret_gu = the return value from the copy from user function */ \ +int __ret_gu; \ +/* register = compiler hint to store it into a register instead of RAM +* __inttype = func that gets the smallest variable type that fits the source +* __val_gu = intermediate storage of user obtained variable +* Obtain a register with a size equal to *ptr and store the user data pointer inside it +*/ \ + register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \ +/* Sparse integrity check, checks is a ptr is in fact a pointer to user space */ \ +__chk_user_ptr (ptr); \ +/* asm := assembly instruction +* volatile := no optimizations +* +* Assembler template: +* "call" := issue a call assembly instruction +* "__" #fn "_%P4" := stringbuilder that creates the right __get_user_X function name +* based on size of ptr +* %P4 := Take fourth variable value as literal string +* +* Output operands: +* "=a" (__ret_gu) := overwrite (=) the address register (a) __ret_gu +* "=r" (__val_gu) := overwrite (=) the general register (r) __val_gu +* ASM_CALL_CONSTRAINT := Constraint that forces the right execution order of inline asm +* +* Input operands: +* "0" (ptr) := first argument, the user space source address +* "i" (sizeof(*(ptr))) := second argument, the size of user space data that must be copied +* +* This function calls one of the __get_user_X functions based on the size of the ptr data +* This copies the data from user space into the temporary variable __val_gu +* The result of this operation is stored in the variable __ret_gu +*/ \ + asm volatile("call __" #fn "_%P4" \ + : "=a" (__ret_gu), "=r" (__val_gu), \ + ASM_CALL_CONSTRAINT \ + : "0" (ptr), "i" (sizeof(*(ptr)))); \ + instrument_get_user(__val_gu); \ +/* Casts the variable inside __val_gu to the correct type and stores it inside +* the kernel destination 'x' +*/ \ + (x) = (__force __typeof__(*(ptr))) __val_gu; \ +IF_SAFEFETCH_STATIC_BRANCH_UNLIKELY_WRAPPER (safefetch_copy_from_user_key) { \ +if (GET_USER_CALL_CHECK (__ret_gu)) { \ + __ret_gu =__dfgetuserfunc (*(ptr))((unsigned long long )(ptr), __val_gu, (unsigned long long )(&x)) ; \ + } \ + } \ +/* Integrity check that expects a 0 as value for __ret_gu (call successful) */ \ +__builtin_expect (__ret_gu,0 ); \ +}) + +// fn = get_user function name template +// x = destination +// ptr = source +#define do_get_user_call_no_dfcache (fn, x, ptr) \ +({ \ +/* __ret_gu = the return value from the copy from user function */ \ +int __ret_gu; \ +/* register = compiler hint to store it into a register instead of RAM \ +* __inttype = func that gets the smallest variable type that fits the source \ +* __val_gu = intermediate storage of user obtained variable \ +* Obtain a register with a size equal to *ptr and store the user data pointer inside it \ +*/ \ + register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \ +/* Sparse integrity check, checks is a ptr is in fact a pointer to user space */ \ +__chk_user_ptr (ptr); \ +/* asm := assembly instruction +* volatile := no optimizations +* +* Assembler template: +* "call" := issue a call assembly instruction +* "__" #fn "_%P4" := stringbuilder that creates the right __get_user_X function name +* based on size of ptr +* %P4 := Take fourth variable value as literal string +* +* Output operands: +* "=a" (__ret_gu) := overwrite (=) the address register (a) __ret_gu +* "=r" (__val_gu) := overwrite (=) the general register (r) __val_gu +* ASM_CALL_CONSTRAINT := Constraint that forces the right execution order of inline asm +* +* Input operands: +* "0" (ptr) := first argument, the user space source address +* "i" (sizeof(*(ptr))) := second argument, the size of user space data that must be copied +* +* This function calls one of the __get_user_X functions based on the size of the ptr data +* This copies the data from user space into the temporary variable __val_gu +* The result of this operation is stored in the variable __ret_gu +*/ \ + asm volatile("call __" #fn "_%P4" \ + : "=a" (__ret_gu), "=r" (__val_gu), \ + ASM_CALL_CONSTRAINT \ + : "0" (ptr), "i" (sizeof(*(ptr)))); \ +/* Casts the variable inside __val_gu to the correct type and stores it inside +* the kernel destination 'x' +*/ \ +instrument_get_user (__val_gu); \ + (x) = (__force __typeof__(*(ptr))) __val_gu; \ +/* Integrity check that expects a 0 as value for __ret_gu (call successful) */ \ +__builtin_expect (__ret_gu,0 ); \ +}) + +#define get_user_no_dfcache (x, ptr) ({ might_fault(); do_get_user_call_no_dfcache(get_user, x, ptr); }) + +#define __get_user_no_dfcache (x, ptr) do_get_user_call_no_dfcache(get_user_nocheck, x, ptr) + +#define unsafe_op_wrap (op, err) do { if (unlikely(op)) goto err; } while (0) +#define unsafe_get_user_no_dfcache (x, p, e) unsafe_op_wrap(__get_user_no_dfcache(x, p), e) + +#else + + +// fn = get_user function name template +// x = destination +// ptr = source #define do_get_user_call (fn,x,ptr) \ ({ \ +/* __ret_gu = the return value from the copy from user function */ \int __ret_gu; \ +/* register = compiler hint to store it into a register instead of RAM \ +* __inttype = func that gets the smallest variable type that fits the source \ +* __val_gu = intermediate storage of user obtained variable \ +* Obtain a register with a size equal to *ptr and store the user data pointer inside it \ +*/ \ register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \ +/* Sparse integrity check, checks is a ptr is in fact a pointer to user space */ \__chk_user_ptr (ptr); \ +/* asm := assembly instruction +* volatile := no optimizations +* +* Assembler template: +* "call" := issue a call assembly instruction +* "__" #fn "_%c[size]" := stringbuilder that creates the right __get_user_X function name +* based on size of ptr +* %c[size] := Take fourth variable value as literal string with +* named argument size per 8c860ed +* +* Output operands: +* "=a" (__ret_gu) := overwrite (=) the address register (a) __ret_gu +* "=r" (__val_gu) := overwrite (=) the general register (r) __val_gu +* ASM_CALL_CONSTRAINT := Constraint that forces the right execution order of inline asm +* +* Input operands: +* "0" (ptr) := first argument, the user space source address +* "i" (sizeof(*(ptr))) := second argument, the size of user space data that must be copied +* +* This function calls one of the __get_user_X functions based on the size of the ptr data +* This copies the data from user space into the temporary variable __val_gu +* The result of this operation is stored in the variable __ret_gu +*/ \ asm volatile("call __" #fn "_%c[size]" \ : "=a" (__ret_gu), "=r" (__val_gu), \ ASM_CALL_CONSTRAINT \ : "0" (ptr), [size] "i" (sizeof(*(ptr)))); \ instrument_get_user(__val_gu); \ +/* Casts the variable inside __val_gu to the correct type and stores it inside +* the kernel destination 'x' +*/ \ (x) = (__force __typeof__(*(ptr))) __val_gu; \ +/* Integrity check that expects a 0 as value for __ret_gu (call successful) */ \__builtin_expect (__ret_gu,0 ); \ }) -/** +#endif + +/* * get_user - Get a simple variable from user space. -* @x: Variable to store result. +* @x: Variable to store result. * @ptr: Source address, in user space. * * Context: User context only. This function may sleep if pagefaults are -* enabled. +* enabled. * * This macro copies a single simple variable from user space to kernel * space. It supports simple types like char and int, but not larger
@@ -107,13 +295,15 @@ extern int __get_user_bad(void);*/ #define get_user (x,ptr) ({ might_fault(); do_get_user_call(get_user,x,ptr); }) -/** + + +/* * __get_user - Get a simple variable from user space, with less checking. -* @x: Variable to store result. +* @x: Variable to store result. * @ptr: Source address, in user space. * * Context: User context only. This function may sleep if pagefaults are -* enabled. +* enabled. * * This macro copies a single simple variable from user space to kernel * space. It supports simple types like char and int, but not larger
@@ -130,7 +320,6 @@ extern int __get_user_bad(void);*/ #define __get_user (x,ptr) do_get_user_call(get_user_nocheck,x,ptr) -#ifdef CONFIG_X86_32 #define __put_user_goto_u64 (x, addr, label) \ asm goto("\n" \
@@ -190,11 +379,11 @@ extern void __put_user_nocheck_8(void);/** * put_user - Write a simple value into user space. -* @x: Value to copy to user space. +* @x: Value to copy to user space. * @ptr: Destination address, in user space. * * Context: User context only. This function may sleep if pagefaults are -* enabled. +* enabled. * * This macro copies a single simple value from kernel space to user * space. It supports simple types like char and int, but not larger
@@ -209,11 +398,11 @@ extern void __put_user_nocheck_8(void);/** * __put_user - Write a simple value into user space, with less checking. -* @x: Value to copy to user space. +* @x: Value to copy to user space. * @ptr: Destination address, in user space. * * Context: User context only. This function may sleep if pagefaults are -* enabled. +* enabled. * * This macro copies a single simple value from kernel space to user * space. It supports simple types like char and int, but not larger
arch/x86/include/asm/uaccess_64.h +54 -0
65 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC 4/17 sched: add protection to task_struct
In reply to Gatlin NewhouseAdds caching data structure for every task structure and optionally adds a statistics structure to each as well.
--- include/linux/sched.h | 11 +++++++++++ init/init_task.c | 11 +++++++++++ 2 files changed, 22 insertions(+)
include/linux/sched.h +11 -0
@@ -48,6 +48,10 @@#include <linux/tracepoint-defs.h> #include <asm/kmap_size.h> +#ifdef CONFIG_SAFEFETCH +#include <linux/safefetch.h> +#endif +/* task_struct member predeclarations (sorted alphabetically): */ struct audit_context ;struct bio_list ;
@@ -1654,6 +1658,13 @@ struct task_struct {struct user_event_mm *user_event_mm;#endif +#ifdef CONFIG_SAFEFETCH +struct df_prot_struct df_prot_struct_head; +#ifdef SAFEFETCH_DEBUG +struct df_stats_struct df_stats; +#endif +#endif +/* CPU-specific state of this task: */ struct thread_struct thread;
init/init_task.c +11 -0
@@ -17,6 +17,10 @@#include <linux/uaccess.h> +#ifdef CONFIG_SAFEFETCH +#include <linux/safefetch.h> +#endif +static struct signal_struct init_signals = { .nr_threads =1 , .thread_head =LIST_HEAD_INIT (init_task.thread_node),
@@ -220,6 +224,13 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = {#ifdef CONFIG_SECCOMP_FILTER .seccomp = { .filter_count = ATOMIC_INIT(0) },#endif +#ifdef CONFIG_SAFEFETCH +#ifndef SAFEFETCH_MEASURE_DEFENSE + .df_prot_struct_head = { .df_mem_range_allocator = { .initialized = 0 }, .df_metadata_allocator = {.first = 0, .initialized = 0, .extended = 0}, .df_storage_allocator = {.first = 0, .initialized = 0, .extended = 0}}, +#else + .df_prot_struct_head = { .df_mem_range_allocator = { .initialized = 0 }, .df_metadata_allocator = {.first = 0, .initialized = 0, .extended = 0}, .df_storage_allocator = {.first = 0, .initialized = 0, .extended = 0}, .df_measures = {.search_time = 0, .insert_time = 0, .counter = 0}}, +#endif +#endif };EXPORT_SYMBOL (init_task);
Signature · 1 line
2.25.1
RFC 5/17 uaccess: add non-caching copy_from_user functions
In reply to Gatlin Newhouse--- include/linux/uaccess.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
include/linux/uaccess.h +30 -0
@@ -186,6 +186,26 @@ _inline_copy_from_user(void *to, const void __user *from, unsigned long n)extern __must_check unsigned long _copy_from_user (void *,const void __user *,unsigned long ); +#ifdef CONFIG_SAFEFETCH +static inline __must_check unsigned long +_copy_from_user_no_dfcache (void *to,const void __user *from,unsigned long n) +{ +unsigned long res = n; + +might_fault (); +if (!should_fail_usercopy () &&likely (access_ok (from, n))) { +instrument_copy_from_user_before (to, from, n); + res =raw_copy_from_user_no_dfcache (to, from, n); +instrument_copy_from_user_after (to, from, n, res); + } +if (unlikely (res)) +memset (to + (n - res),0 , res); +return res; +} +extern __must_check unsigned long +_copy_from_user_no_dfcache (void *,const void __user *,unsigned long ); +#endif +static inline __must_check unsigned long _inline_copy_to_user (void __user *to,const void *from,unsigned long n) {
@@ -213,6 +233,16 @@ copy_from_user(void *to, const void __user *from, unsigned long n)#endif } +#ifdef CONFIG_SAFEFETCH +static __always_inline unsigned long __must_check +copy_from_user_no_dfcache (void *to,const void __user *from,unsigned long n) +{ +if (likely (check_copy_size (to, n, false))) + n =_copy_from_user_no_dfcache (to, from, n); +return n; +} +#endif +static __always_inline unsigned long __must_check copy_to_user (void __user *to,const void *from,unsigned long n) {
Signature · 1 line
2.25.1
RFC 6/17 futex: add get_user_no_dfcache() functions
In reply to Gatlin NewhouseAdd disabled cache get_user function calls to futex functions to disable using the SafeFetch cache on any fast userspace mutexes.
--- kernel/futex/core.c | 5 +++++ kernel/futex/futex.h | 4 ++++ kernel/futex/pi.c | 5 +++++ kernel/futex/requeue.c | 5 ++++- kernel/futex/waitwake.c | 4 ++++ 5 files changed, 22 insertions(+), 1 deletion(-)
kernel/futex/core.c +5 -0
@@ -1023,8 +1023,13 @@ static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr,return -1 ; retry: +#ifdef CONFIG_SAFEFETCH +if (get_user_no_dfcache (uval, uaddr)) +return -1 ; +#else if (get_user (uval, uaddr))return -1 ; +#endif /* * Special case for regular (non PI) futexes. The unlock path in
kernel/futex/futex.h +4 -0
@@ -308,7 +308,11 @@ static __always_inline int futex_get_value(u32 *dest, u32 __user *from) from =masked_user_access_begin (from); else if (!user_read_access_begin(from, sizeof(*from))) return -EFAULT; +#ifdef CONFIG_SAFEFETCH +unsafe_get_user_no_dfcache (val, from, Efault); +#else unsafe_get_user (val, from, Efault); +#endif user_read_access_end (); *dest = val;return 0 ;
kernel/futex/pi.c +5 -0
@@ -1140,8 +1140,13 @@ int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)return -ENOSYS ; retry: +#ifdef CONFIG_SAFEFETCH +if (get_user_no_dfcache (uval, uaddr)) +return -EFAULT ; +#else if (get_user (uval, uaddr))return -EFAULT ; +#endif /* * We release only a lock we actually own: */
kernel/futex/requeue.c +4 -1
@@ -468,8 +468,11 @@ int futex_requeue(u32 __user *uaddr1, unsigned int flags1,if (unlikely (ret)) {futex_hb_waiters_dec (hb2);double_unlock_hb (hb1, hb2); - +#ifdef CONFIG_SAFEFETCH + ret =get_user_no_dfcache (curval, uaddr1); +#else ret =get_user (curval, uaddr1); +#endif if (ret)return ret;
kernel/futex/waitwake.c +4 -0
@@ -629,7 +629,11 @@ int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,if (ret) {futex_q_unlock (hb); +#ifdef CONFIG_SAFEFETCH + ret =get_user_no_dfcache (uval, uaddr); +#else ret =get_user (uval, uaddr); +#endif if (ret)return ret;
Signature · 1 line
2.25.1
RFC 7/17 gup: add non-caching get_user call to fault_in_readable()
In reply to Gatlin NewhouseAdds non-caching call to fault_in_readable() for configurations with SafeFetch enabled and disabled readable pages.
--- mm/gup.c | 4 ++++ 1 file changed, 4 insertions(+)
mm/gup.c +4 -0
@@ -2224,7 +2224,11 @@ size_t fault_in_readable(const char __user *uaddr, size_t size)/* Stop once we overflow to 0. */ for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE)) +#if defined(CONFIG_SAFEFETCH ) && !defined(SAFEFETCH_PROTECT_PAGES_READABLE ) +unsafe_get_user_no_dfcache (c, (const char __user *)cur, out); +#else unsafe_get_user (c, (const char __user *)cur, out); +#endif out:user_read_access_end (); (void )c;
Signature · 1 line
2.25.1
RFC 8/17 init: add caching startup and initialization to start_kernel()
In reply to Gatlin Newhouse--- init/main.c | 7 +++++++ 1 file changed, 7 insertions(+)
init/main.c +7 -0
@@ -958,6 +958,10 @@ void start_kernel(void)trap_init ();mm_core_init ();poking_init (); +#ifdef CONFIG_SAFEFETCH +#include <linux/safefetch.h> +df_startup (); +#endif ftrace_init ();/* trace_printk can be enabled here */
@@ -1098,6 +1102,9 @@ void start_kernel(void)arch_post_acpi_subsys_init ();kcsan_init (); +#if defined(SAFEFETCH_DEBUG ) || defined(SAFEFETCH_STATIC_KEYS ) +df_sysfs_init (); +#endif /* Do the rest non-__init'ed, we're now alive */ rest_init ();
Signature · 1 line
2.25.1
RFC 9/17 exit: add destruction of SafeFetch caches and debug info to do_exit()
In reply to Gatlin Newhouse--- kernel/exit.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
kernel/exit.c +16 -0
@@ -951,6 +951,22 @@ void __noreturn do_exit(long code)exit_mm (); +#ifdef CONFIG_SAFEFETCH +#include <linux/safefetch.h> +#include <linux/region_allocator.h> +#include <linux/safefetch_static_keys.h> +// if (!(tsk->flags & PF_KTHREAD)) +// df_task_destroy(tsk); +IF_SAFEFETCH_STATIC_BRANCH_UNLIKELY_WRAPPER (safefetch_hooks_key) { +if (!(tsk->flags &PF_KTHREAD )) { +destroy_regions (); +#ifdef SAFEFETCH_DEBUG +df_debug_task_destroy (tsk); +#endif + } + } +#endif +if (group_dead)acct_process ();
Signature · 1 line
2.25.1
RFC 10/17 iov_iter: add SafeFetch pinning call to copy_from_user_iter()
In reply to Gatlin Newhouse--- lib/iov_iter.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
lib/iov_iter.c +12 -0
@@ -41,6 +41,10 @@ size_t copy_to_user_iter_nofault(void __user *iter_to, size_t progress,return res <0 ? len : res; } +#ifndef PIN_BUDDY_PAGES_WATERMARK +#define PIN_BUDDY_PAGES_WATERMARK PAGE_SIZE +#endif +static __always_inline size_t copy_from_user_iter(void __user *iter_from, size_t progress,size_t len,void *to,void *priv2)
@@ -52,7 +56,15 @@ size_t copy_from_user_iter(void __user *iter_from, size_t progress,if (access_ok (iter_from, len)) { to += progress;instrument_copy_from_user_before (to, iter_from, len); +#ifdef SAFEFETCH_PIN_BUDDY_PAGES +/* #warning "Using Page_pinning for copyin calls" */ +if (len >=PIN_BUDDY_PAGES_WATERMARK ) + res =raw_copy_from_user_pinning (to, iter_from, len); +else + res =raw_copy_from_user (to, iter_from, len); +#else res =raw_copy_from_user (to, iter_from, len); +#endif instrument_copy_from_user_after (to, iter_from, len, res); }return res;
Signature · 1 line
2.25.1
RFC 11/17 kernel: add SafeFetch cache handling to dup_task_struct()
In reply to Gatlin Newhouse--- kernel/fork.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
kernel/fork.c +17 -0
@@ -122,6 +122,12 @@#include <kunit/visibility.h> +#ifdef CONFIG_SAFEFETCH +#include <linux/safefetch.h> +#include <linux/safefetch_static_keys.h> +#include <linux/mem_range.h> +#endif +/* * Minimum number of threads to boot the kernel */
@@ -955,6 +961,17 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) tsk->last_mm_cid =-1 ; tsk->mm_cid_active =0 ; tsk->migrate_from_cpu =-1 ; +#endif + +#ifdef CONFIG_SAFEFETCH +IF_SAFEFETCH_STATIC_BRANCH_UNLIKELY_WRAPPER (safefetch_hooks_key) { +df_task_dup (tsk); + } +#ifdef SAFEFETCH_DEBUG +WARN_ON (SAFEFETCH_TASK_MEM_RANGE_INIT_FLAG (tsk)); +WARN_ON (tsk->df_prot_struct_head.df_metadata_allocator.extended); +WARN_ON (tsk->df_prot_struct_head.df_storage_allocator.extended); +#endif #endif return tsk;
Signature · 1 line
2.25.1
RFC 12/17 bug: add SafeFetch statistics tracking to __report_bug() calls
In reply to Gatlin Newhouse--- lib/bug.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
lib/bug.c +10 -0
@@ -155,6 +155,9 @@ static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *restruct bug_entry *bug;const char *file;unsigned line, warning, once, done; +#if defined(SAFEFETCH_DEBUG ) + current->df_stats.traced =1 ; +#endif if (!is_valid_bugaddr (bugaddr))return BUG_TRAP_TYPE_NONE ;
@@ -194,6 +197,9 @@ static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *re/* this is a WARN_ON rather than BUG/BUG_ON */ __warn (file, line, (void *)bugaddr,BUG_GET_TAINT (bug), regs,NULL ); +#if defined(SAFEFETCH_DEBUG ) + current->df_stats.traced =0 ; +#endif return BUG_TRAP_TYPE_WARN ; }
@@ -203,6 +209,10 @@ static enum bug_trap_type __report_bug(unsigned long bugaddr, struct pt_regs *repr_crit ("Kernel BUG at %pB [verbose debug info unavailable]\n" , (void *)bugaddr); +#if defined(SAFEFETCH_DEBUG ) + current->df_stats.traced =0 ; +#endif +return BUG_TRAP_TYPE_BUG ; }
Signature · 1 line
2.25.1
RFC 13/17 softirq: add SafeFetch statistics to irq_enter_rc() and irq_exit()
In reply to Gatlin Newhouse--- kernel/softirq.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/softirq.c +8 -0
@@ -632,6 +632,10 @@ void irq_enter_rcu(void)*/ void irq_enter (void ) { +#ifdef SAFEFETCH_DEBUG +/* #warning IRQ_DEFENSE */ + current->df_stats.in_irq =1 ; +#endif ct_irq_enter ();irq_enter_rcu (); }
@@ -708,7 +712,11 @@ void irq_exit(void)__irq_exit_rcu ();ct_irq_exit ();/* must be last! */ +#ifdef SAFEFETCH_DEBUG + current->df_stats.in_irq =0 ; +#endif lockdep_hardirq_exit (); + }/*
Signature · 1 line
2.25.1
RFC 14/17 makefile: add SafeFetch support to makefiles
In reply to Gatlin NewhouseAlso update version string for differentiating testing kernels from host kernels.
--- Makefile | 3 ++- mm/Makefile | 1 + scripts/Makefile.lib | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-)
Makefile +2 -1
@@ -2,7 +2,7 @@ VERSION = 6 PATCHLEVEL = 16 SUBLEVEL = 0 -EXTRAVERSION = -rc5 +EXTRAVERSION = -rc5-safefetch NAME = Baby Opossum Posse # *DOCUMENTATION*
@@ -1088,6 +1088,7 @@ include-$(CONFIG_KCOV) += scripts/Makefile.kcov include-$(CONFIG_RANDSTRUCT) += scripts/Makefile.randstruct include-$(CONFIG_AUTOFDO_CLANG) += scripts/Makefile.autofdo include-$(CONFIG_PROPELLER_CLANG) += scripts/Makefile.propeller +include-$(CONFIG_SAFEFETCH) += scripts/Makefile.safefetch include-$(CONFIG_GCC_PLUGINS) += scripts/Makefile.gcc-plugins include $(addprefix $(srctree)/, $(include-y))
mm/Makefile +1 -0
@@ -92,6 +92,7 @@ obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_KASAN) += kasan/ obj-$(CONFIG_KFENCE) += kfence/ obj-$(CONFIG_KMSAN) += kmsan/ +obj-$(CONFIG_SAFEFETCH) += safefetch/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_FAIL_PAGE_ALLOC) += fail_page_alloc.o obj-$(CONFIG_MEMTEST) += memtest.o
scripts/Makefile.lib +4 -0
@@ -90,6 +90,10 @@ _rust_flags += $(if $(patsubst n%,, \ $(RUSTFLAGS_KCOV)) endif +ifeq ($(CONFIG_SAFEFETCH),y) +_c_flags += $(CFLAGS_SAFEFETCH) +endif + # # Enable KCSAN flags except some files or directories we don't want to check # (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
Signature · 1 line
2.25.1
RFC 15/17 kconfig: debug: add SafeFetch to debug kconfig
In reply to Gatlin Newhouse--- lib/Kconfig.debug | 1 + 1 file changed, 1 insertion(+)
lib/Kconfig.debug +1 -0
@@ -1040,6 +1040,7 @@ config MEM_ALLOC_PROFILING_DEBUG source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan" +source "lib/Kconfig.safefetch" endmenu # "Memory Debugging"
Signature · 1 line
2.25.1
RFC 16/17 x86: enable SafeFetch on x86_64 builds
In reply to Gatlin NewhouseDisable HAVE_ARCH_AUDITSYSCALL and HAVE_ARCH_SOFT_DIRTY. Both options are untested with SafeFetch enabled as of right now.
--- arch/x86/Kconfig | 5 +++-- init/Kconfig | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
arch/x86/Kconfig +3 -2
@@ -31,7 +31,7 @@ config X86_64 select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 select ARCH_SUPPORTS_PER_VMA_LOCK select ARCH_SUPPORTS_HUGE_PFNMAP if TRANSPARENT_HUGEPAGE - select HAVE_ARCH_SOFT_DIRTY + # select HAVE_ARCH_SOFT_DIRTY select MODULES_USE_ELF_RELA select NEED_DMA_MAP_STATE select SWIOTLB
@@ -194,7 +194,7 @@ config X86 select HAVE_ACPI_APEI if ACPI select HAVE_ACPI_APEI_NMI if ACPI select HAVE_ALIGNED_STRUCT_PAGE - select HAVE_ARCH_AUDITSYSCALL + # select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_HUGE_VMAP if X86_64 || X86_PAE select HAVE_ARCH_HUGE_VMALLOC if X86_64 select HAVE_ARCH_JUMP_LABEL
@@ -203,6 +203,7 @@ config X86 select HAVE_ARCH_KASAN_VMALLOC if X86_64 select HAVE_ARCH_KFENCE select HAVE_ARCH_KMSAN if X86_64 + select HAVE_ARCH_SAFEFETCH if X86_64 select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS if MMU select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT
init/Kconfig +1 -1
@@ -494,7 +494,7 @@ config HAVE_ARCH_AUDITSYSCALL bool config AUDITSYSCALL - def_bool y + def_bool n depends on AUDIT && HAVE_ARCH_AUDITSYSCALL select FSNOTIFY
Signature · 1 line
2.25.1
RFC 17/17 vfs: ioctl: add logging to ioctl_file_dedupe_range() for testing
In reply to Gatlin NewhouseThis adds a message indicating a double-fetch bug trigger for testing the SafeFetch patchset. It add the message right before the fix for CVE-2016-6516 [1][2] introduced by Scott Bauer [3]. Which can be tested by first compiling the double-fetch program from [4], and running a shell script similar to the one provided by the SafeFetch paper authors in their artifacts repository (see: run_security_artifact.sh) [5].
In summary, you can compile the sample from [4], then clear dmesg, run the sample with ./a.out 7 65534 1000000 0. Then remove both files used in the sample /tmp/test.txt and /tmp/test2.txt. Now count the bug warning messages in dmesg before clearing dmesg again. Then enable safefetch with ./safefetch_control.sh -hooks followed by ./safefetch_control.sh -adaptive 4096 4096 0 or ./safefetch_control.sh -rbtree 4096 4096 0 where safefetch_control.sh can be found in [5]. Now run the compiled sample again and count the bug warning messages in dmesg.
This was my method of testing the patchset as I forward ported it from v5.11 after fixing any merge conflicts or compiler errors.
[1] https://nvd.nist.gov/vuln/detail/CVE-2016-6516
[2] https://www.openwall.com/lists/oss-security/2016/07/31/6
[3] 10eec60ce79187686e052092e5383c99b4420a20
[4] https://github.com/wpengfei/CVE-2016-6516-exploit/tree/master/Scott%20Bauer
[5] https://github.com/vusec/safefetch-ae/
--- fs/ioctl.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/ioctl.c +6 -0
@@ -439,6 +439,12 @@ static int ioctl_file_dedupe_range(struct file *file, goto out; } +// Add an extra check before the bug fix to check whether a double-fetch occurred +// With SafeFetch enabled this check will never get triggered because we correct +// the second fetch from the cache. +if (same->dest_count != count) +pr_warn ("[Bug-Warning] Bug triggered\n" ); + same->dest_count = count; ret =vfs_dedupe_file_range (file, same);if (ret)
Signature · 1 line
2.25.1