Skip to content

Commit 1f13c38

Browse files
committed
Merge tag 'hardening-v6.13-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull hardening fix from Kees Cook: "Silence a GCC value-range warning that is being ironically triggered by bounds checking" * tag 'hardening-v6.13-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: fortify: Hide run-time copy size from value range tracking
2 parents 59dbb9d + 239d873 commit 1f13c38

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

include/linux/fortify-string.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,21 +616,33 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
616616
return false;
617617
}
618618

619+
/*
620+
* To work around what seems to be an optimizer bug, the macro arguments
621+
* need to have const copies or the values end up changed by the time they
622+
* reach fortify_warn_once(). See commit 6f7630b1b5bc ("fortify: Capture
623+
* __bos() results in const temp vars") for more details.
624+
*/
619625
#define __fortify_memcpy_chk(p, q, size, p_size, q_size, \
620626
p_size_field, q_size_field, op) ({ \
621627
const size_t __fortify_size = (size_t)(size); \
622628
const size_t __p_size = (p_size); \
623629
const size_t __q_size = (q_size); \
624630
const size_t __p_size_field = (p_size_field); \
625631
const size_t __q_size_field = (q_size_field); \
632+
/* Keep a mutable version of the size for the final copy. */ \
633+
size_t __copy_size = __fortify_size; \
626634
fortify_warn_once(fortify_memcpy_chk(__fortify_size, __p_size, \
627635
__q_size, __p_size_field, \
628636
__q_size_field, FORTIFY_FUNC_ ##op), \
629637
#op ": detected field-spanning write (size %zu) of single %s (size %zu)\n", \
630638
__fortify_size, \
631639
"field \"" #p "\" at " FILE_LINE, \
632640
__p_size_field); \
633-
__underlying_##op(p, q, __fortify_size); \
641+
/* Hide only the run-time size from value range tracking to */ \
642+
/* silence compile-time false positive bounds warnings. */ \
643+
if (!__builtin_constant_p(__copy_size)) \
644+
OPTIMIZER_HIDE_VAR(__copy_size); \
645+
__underlying_##op(p, q, __copy_size); \
634646
})
635647

636648
/*

0 commit comments

Comments
 (0)