Skip to content

Commit 5a3984f

Browse files
author
Marc Zyngier
committed
KVM: arm64: Add build-time sanity checks for flags
Flags are great, but flags can also be dangerous: it is easy to encode a flag that is bigger than its container (unless the container is a u64), and it is easy to construct a flag value that doesn't fit in the mask that is associated with it. Add a couple of build-time sanity checks that ensure we catch these two cases. Reviewed-by: Fuad Tabba <[email protected]> Reviewed-by: Reiji Watanabe <[email protected]> Signed-off-by: Marc Zyngier <[email protected]>
1 parent e19f2c6 commit 5a3984f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,29 @@ struct kvm_vcpu_arch {
433433
#define __unpack_flag(_set, _f, _m) _f
434434
#define unpack_vcpu_flag(...) __unpack_flag(__VA_ARGS__)
435435

436+
#define __build_check_flag(v, flagset, f, m) \
437+
do { \
438+
typeof(v->arch.flagset) *_fset; \
439+
\
440+
/* Check that the flags fit in the mask */ \
441+
BUILD_BUG_ON(HWEIGHT(m) != HWEIGHT((f) | (m))); \
442+
/* Check that the flags fit in the type */ \
443+
BUILD_BUG_ON((sizeof(*_fset) * 8) <= __fls(m)); \
444+
} while (0)
445+
436446
#define __vcpu_get_flag(v, flagset, f, m) \
437447
({ \
448+
__build_check_flag(v, flagset, f, m); \
449+
\
438450
v->arch.flagset & (m); \
439451
})
440452

441453
#define __vcpu_set_flag(v, flagset, f, m) \
442454
do { \
443455
typeof(v->arch.flagset) *fset; \
444456
\
457+
__build_check_flag(v, flagset, f, m); \
458+
\
445459
fset = &v->arch.flagset; \
446460
if (HWEIGHT(m) > 1) \
447461
*fset &= ~(m); \
@@ -452,6 +466,8 @@ struct kvm_vcpu_arch {
452466
do { \
453467
typeof(v->arch.flagset) *fset; \
454468
\
469+
__build_check_flag(v, flagset, f, m); \
470+
\
455471
fset = &v->arch.flagset; \
456472
*fset &= ~(m); \
457473
} while (0)

0 commit comments

Comments
 (0)