From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70C1726F288 for ; Tue, 10 Feb 2026 14:35:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770734133; cv=none; b=uFfISG3w7SLAuXgnfuA1i44r7KxcAGN+NHcPkrtRpZbPKYC+ZM6UN/TT7sqE1fiAOoMIHYEdsPdB4zFGWnigF8ULdNWlN8q9suTXZaYJ7kw22HAiZmNkajgpdwkgJ1IObTGGGt63gPzvfdT3F/0hkbHwQmW7F3e/JEXTIn65p+8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770734133; c=relaxed/simple; bh=VnQpWo6V3EU1WRNM2jmpmp4wK5Yp/7mccmZJp/oIeXk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BmkB0RztQn5m74h/lTfiV1SjYCu+LJjnvFIXn5zssrzjTEI6sHuqpT6SRPOVkXuIXFRddNkUcLoXoOmS8Cp7pydvUCjm5278yMwIe2wT81w9l6eAwvO+SU8ZijaGUo2VvEo9ryhyDOD2gceSJhX8ww1ZaaVGIbpuECn0kDNJzwI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3Tj9Mawj; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3Tj9Mawj" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34e5a9de94bso9484390a91.0 for ; Tue, 10 Feb 2026 06:35:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770734131; x=1771338931; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1m/dqoDPG2dxYrS/cuCZ4EFUvg1uumm8Y87K346i69Y=; b=3Tj9MawjvHP4nAmEF/sOu8y2VtwYBz5x949CPHBWcVkHEJ3dgJtcQRLH9CVfEc3O9u nQcv0g9U2fxpjQPqhCN842uGYS65xF1STBFsBpC/abpDsrVfTLvj+UzXlRm6QUoREWfh tghkVYkgLk8AUDrky5BZtWjxLisa92uq31mYR997zr3iuCNHh3o0iJKX0pZpeJ82FGMR cYx6acduHmdn7sg9AHwpMG7u4v0Irer9mLsRY93YughPPgKl6HnK7Q/EZeOximzQP/9/ djRWdh3ZwGxF2BgCOGS2yt6NMUyUEUjAZuolt68SsFx4/78YMe7zwR0oS9yVypypzCm2 H4iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770734131; x=1771338931; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1m/dqoDPG2dxYrS/cuCZ4EFUvg1uumm8Y87K346i69Y=; b=QBkAWM4zXlcvCG3RLIKabfbV9rqIYRmvHKQDUBXX8f+jnRPwd8FHZ41tltJyieQjtd fUJ+7mK3sAdgqOI7cv4mQ9D08UnVKquWtkmSJqTtZrpyO+o0AJXdQafLekHfaw4f3Vwx kk3zOjRF7yEC9pQuQj6pfLRg1Y3CLWLCbtcmARjj41kYuf03A+bVuyATKa8bznOh35af 20Qj4hpPW9oQ82D6wCSTtWPj1d4g8s+Kb5Ceg2jtYR4hKW/vPNj7N6rsptCoplE1jBXA eFZkptYFnAEu+6KCvGt3hp+kw4iv2KWFJrTolnBd4aXxoykVdksX/7S5bQCDGqRoi7sL RwBA== X-Forwarded-Encrypted: i=1; AJvYcCVAuzAZtdwVXCT1GklDOZ/WzDXDFMy85/xG513l/kTP3o3hdXL5K3i1f+cHqKoytqR0RqaHNbAbr6Ns9YQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzZpovA7ccmyvrXdGdxZ/PlzgfI8Fw8xqYFlUiSikm/weCw/G/4 Svz5h8VZGm/TqQKHMDn4XZRhpJiE8BmUwMdMynwp95hGLyXdLSLJppulwHATMxaqKKdZ8MZQxek gaOj3zg== X-Received: from pjbee13.prod.google.com ([2002:a17:90a:fc4d:b0:350:fd27:2bc8]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3505:b0:354:a09a:1012 with SMTP id 98e67ed59e1d1-354b3e3473dmr14956940a91.17.1770734130796; Tue, 10 Feb 2026 06:35:30 -0800 (PST) Date: Tue, 10 Feb 2026 06:35:28 -0800 In-Reply-To: <5f3e0ca5-cf60-4f07-bbc6-663b04192c49@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <369eaaa2b3c1425c85e8477066391bc7@huawei.com> <5f3e0ca5-cf60-4f07-bbc6-663b04192c49@redhat.com> Message-ID: Subject: Re: [BUG REPORT] USE_AFTER_FREE in complete_emulated_mmio found by KASAN/Syzkaller fuzz test (v5.10.0) From: Sean Christopherson To: Paolo Bonzini Cc: Zhangjiaji , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "Wangqinxiao (Tom)" , zhangyashu , "wangyanan (Y)" Content-Type: text/plain; charset="us-ascii" On Tue, Feb 10, 2026, Paolo Bonzini wrote: > > > I've analyzed the Syzkaller output and the complete_emulated_mmio() code path. > > The buggy address is created in em_enter(), where it passes its local variable `ulong rbp` to emulate_push(), finally ends in emulator_read_write_onepage() putting the address into vcpu->mmio_fragments[].data . > > The bug happens when kvm guest executes an "enter" instruction, and top of the stack crosses the mem page. > > In that case, the em_enter() function cannot complete the instruction within itself, but leave the rest data (which is in the other page) to complete_emulated_mmio(). > > When complete_emulated_mmio() starts, em_enter() has exited, so local variable `ulong rbp` is also released. > > Now complete_emulated_mmio() trys to access vcpu->mmio_fragments[].data , and the bug happened. > > > > any idea? > > Ouch, the bug is certainly legit. The easiest way to fix it is something > like this: > > diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c > index c8e292e9a24d..1c8698139dd5 100644 > --- a/arch/x86/kvm/emulate.c > +++ b/arch/x86/kvm/emulate.c > @@ -1905,7 +1905,7 @@ static int em_enter(struct x86_emulate_ctxt *ctxt) > rbp = reg_read(ctxt, VCPU_REGS_RBP); > rc = emulate_push(ctxt, &rbp, stack_size(ctxt)); > if (rc != X86EMUL_CONTINUE) > - return rc; > + return X86EMUL_UNHANDLEABLE; This won't do anything, rc == X86EMUL_CONTINUE when MMIO is needed. The check would need to be something equivalent to this hack: rc = emulate_push(ctxt, &rbp, stack_size(ctxt)); if (((struct kvm_vcpu *)ctxt->vcpu)->mmio_needed) return X86EMUL_UNHANDLEABLE; but that's largely a moot point. > assign_masked(reg_rmw(ctxt, VCPU_REGS_RBP), reg_read(ctxt, VCPU_REGS_RSP), > stack_mask(ctxt)); > assign_masked(reg_rmw(ctxt, VCPU_REGS_RSP), > > The hard part is auditing all similar places that lead to passing a > local variable to emulator_read_write_onepage(). > > For example I found this one: > > write_segment_descriptor(ctxt, old_tss_sel, &curr_tss_desc); > > which however is caught at kvm_task_switch() and changed to an > emulation error. > > It may be a good idea to stick a defensive "vcpu->mmio_needed = false;" > in prepare_emulation_failure_exit(), as well as > "vcpu->arch.complete_userspace_io = NULL" both there and in > kvm_task_switch(). Please see my other reply[*]. There are a pile of instructions and flows that read/write using on-stack variables. IMO, anything that relies on updating callers is a non-starter, and I don't love the idea of simply disallowing MMIO for any instruction that uses an on-stack variable. [*] https://lore.kernel.org/all/aYpBz1wDdsDfl8Al@google.com