Skip to content

Commit 444da3f

Browse files
kuba-moodavem330
authored andcommitted
bitfield.h: don't compile-time validate _val in FIELD_FIT
When ur_load_imm_any() is inlined into jeq_imm(), it's possible for the compiler to deduce a case where _val can only have the value of -1 at compile time. Specifically, /* struct bpf_insn: _s32 imm */ u64 imm = insn->imm; /* sign extend */ if (imm >> 32) { /* non-zero only if insn->imm is negative */ /* inlined from ur_load_imm_any */ u32 __imm = imm >> 32; /* therefore, always 0xffffffff */ if (__builtin_constant_p(__imm) && __imm > 255) compiletime_assert_XXX() This can result in tripping a BUILD_BUG_ON() in __BF_FIELD_CHECK() that checks that a given value is representable in one byte (interpreted as unsigned). FIELD_FIT() should return true or false at runtime for whether a value can fit for not. Don't break the build over a value that's too large for the mask. We'd prefer to keep the inlining and compiler optimizations though we know this case will always return false. Cc: [email protected] Fixes: 1697599 ("bitfield.h: add FIELD_FIT() helper") Link: https://lore.kernel.org/kernel-hardening/CAK7LNASvb0UDJ0U5wkYYRzTAdnEs64HjXpEUL7d=V0CXiAXcNw@mail.gmail.com/ Reported-by: Masahiro Yamada <[email protected]> Debugged-by: Sami Tolvanen <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Nick Desaulniers <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f19008e commit 444da3f

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

include/linux/bitfield.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
*/
7878
#define FIELD_FIT(_mask, _val) \
7979
({ \
80-
__BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: "); \
80+
__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
8181
!((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
8282
})
8383

0 commit comments

Comments
 (0)