Skip to content

Commit 73ab4a3

Browse files
sean-jckees
authored andcommitted
KVM: x86: Replace memset() "optimization" with normal per-field writes
Explicitly zero select fields in the emulator's decode cache instead of zeroing the fields via a gross memset() that spans six fields. gcc and clang are both clever enough to batch the first five fields into a single quadword MOV, i.e. memset() and individually zeroing generate identical code. Removing the wart also prepares KVM for FORTIFY_SOURCE performing compile-time and run-time field bounds checking for memset(). No functional change intended. Reported-by: Kees Cook <keescook@chromium.org> Signed-off-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/lkml/YR0jIEzEcUom/7rd@google.com Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent dfd42fa commit 73ab4a3

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

arch/x86/kvm/emulate.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5380,8 +5380,13 @@ static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop)
53805380

53815381
void init_decode_cache(struct x86_emulate_ctxt *ctxt)
53825382
{
5383-
memset(&ctxt->rip_relative, 0,
5384-
(void *)&ctxt->modrm - (void *)&ctxt->rip_relative);
5383+
/* Clear fields that are set conditionally but read without a guard. */
5384+
ctxt->rip_relative = false;
5385+
ctxt->rex_prefix = 0;
5386+
ctxt->lock_prefix = 0;
5387+
ctxt->rep_prefix = 0;
5388+
ctxt->regs_valid = 0;
5389+
ctxt->regs_dirty = 0;
53855390

53865391
ctxt->io_read.pos = 0;
53875392
ctxt->io_read.end = 0;

arch/x86/kvm/kvm_emulate.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,7 @@ struct x86_emulate_ctxt {
336336
fastop_t fop;
337337
};
338338
int (*check_perm)(struct x86_emulate_ctxt *ctxt);
339-
/*
340-
* The following six fields are cleared together,
341-
* the rest are initialized unconditionally in x86_decode_insn
342-
* or elsewhere
343-
*/
339+
344340
bool rip_relative;
345341
u8 rex_prefix;
346342
u8 lock_prefix;

0 commit comments

Comments
 (0)